6

In Java when I declare a variable int A; by default it assigns 0 to the variable A. But in PHP if I declare a variable as $A the default value of it sets as null. Instead of assigning it as $A=0; are there any ways available to set a default value as 0 for variable $A, as I did in Java?

3
  • 2
    PHP does not use variable types, so you will have to do $A=0 when you declare $A Commented Oct 5, 2013 at 6:57
  • @Deep PHP has types, but they're inferred instead of declared. Commented Oct 5, 2013 at 7:03
  • 2
    @Hiru Don't try to port habits from other languages to PHP. PHP isn't Java and Java isn't PHP. Nobody uses "implicit type default values" in PHP the way you are trying to do. Commented Oct 5, 2013 at 7:05

5 Answers 5

9

You can set its type. Since you don't wana initialize to 0, setting type to integer will do that for you.

<?php

settype($foo, "integer"); 

echo $foo;

?>

Fiddle

Sign up to request clarification or add additional context in comments.

6 Comments

+1 This somewhat disproves my point, but IMO is basically casting in another syntax and too verbose to be actually useful. :)
This basically does $foo = (int)null. If $foo already existed before, who knows what you're going to get?
Thank you for replying. This explains what I need. Isn't this a precise way of doing it. I'm really sorry if Im asking a stupid question. Im totally new to PHP.... :)
@Hiru But it's exactly the same as $foo = 0; except more verbose and error-prone, so why would you want to use it?
Right, nothing wrong with the answer. It just seems like OP is treating PHP as a strongly typed language, so it's worth noting that this won't actually enforce $foo as an integer type (i.e. settype($foo, "integer"); $foo = "bar"; won't even throw a warning).
|
5

Since you do not explicitly declare variables to be of a certain type, but PHP does type inference instead, there's no way to make an integer without assigning an integer value. In Java you can make a default value by declaring the type of a variable, in PHP you declare the type of a variable by assigning a value of that type to it. There may be several ways how to get an integer value of 0, but none is as straight forward as using a literal 0.

Comments

4

you can do like this, following will help you

echo (int)$A;

2 Comments

This is basically just producing an integer value, it's not setting the type of the variable.
But it gives an error like this. Can't I avoid it? Notice: Undefined variable: count in C:\xampp\htdocs\PHIS2\CreateTheForm.php on line 5
2

PHP doesn't by default make a variable integer or string, if you want to set default value, then simply write$myvariable = 0; and this will assign and make variable an integer type.

Comments

1

You can use php settype which set the type of a variable

<?php

settype($var,'integer');
echo $var;

?>

Refer php settype

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.