Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting a default integer variable in PHP

Tags:

php

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?

like image 969
Hiru Avatar asked Dec 03 '22 21:12

Hiru


1 Answers

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

like image 53
Hanky Panky Avatar answered Dec 09 '22 15:12

Hanky Panky