PHP/Variables: Difference between revisions
No edit summary |
No edit summary |
||
| Line 46: | Line 46: | ||
?> | ?> | ||
Does not explicitly define the type of variable ''a''. Of course, the PHP runtime environment recognizes during execution of this line that the programmer provided a string literal as the value for the variable. | Does not explicitly define the type of variable ''a''. Of course, the PHP runtime environment recognizes during execution of this line that the programmer provided a string literal as the value for the variable. If you want to do the same in [[Topic:C++|C++]], you would would have to explicitly declare the variable type: | ||
char[6] a; | char[6] a; | ||
a = "Stuff"; | a = "Stuff"; | ||
Additional equivalent statements combining declaration and initialization of a variable with the same value would be: | |||
char* a = "Stuff"; | char* a = "Stuff"; // using char pointer | ||
char[] b = "Stuff"; // using char array with string literal initialization | |||
char[] c = { 'S', 't', 'u', 'f', 'f', '\0' }; // using char array with array initialization | |||
Notice that C++ requires explicit variable declaration (including variable type) whereas PHP does not. However, it is good practise to declare all variables, as if they are not declared PHP defaults their value to FALSE (0, empty etc.). This is because PHP is a dynamically typed language, and as a result neither type casting nor declaration is enforced. The following is possible: | Notice that C++ requires explicit variable declaration (including variable type) whereas PHP does not. However, it is good practise to declare all variables, as if they are not declared PHP defaults their value to FALSE (0, empty etc.). This is because PHP is a dynamically typed language, and as a result neither type casting nor declaration is enforced. The following is possible: | ||