Core PHP

Variables

A variable holds an item of data that can change or vary. In many programming languages, a variable can change its value but not its type. In PHP, the value and even the type of data can change.

PHP not only does not require variables to be typed, but they need not be declared or initialized prior to being accessed. Variables that are not initialized are set to the value null prior to being assigned a different value. Also, variable names are always preceded by a $ symbol.

The program below begins with a declared variable named that is undefined, which means that it is assigned the value null. Then the variable type is outputted via a the echo. This variable is then assigned the values of a string, an integer, a double, and a boolean. Note that the value 45 is considered an intger while 45.0 is considered to be a floating point double. There is a difference in how these values are stored internally, but that generally does not matter to the program.

Variables.php

<?php

$XoaxVar;
echo "The type of \$XoaxVar = " . $XoaxVar . " is " . gettype($XoaxVar) . "<br />";
$XoaxVar = "XoaX.net";
echo "The type of \$XoaxVar = " . $XoaxVar . " is " . gettype($XoaxVar) . "<br />";
$XoaxVar = 45;
echo "The type of \$XoaxVar = " . $XoaxVar . " is " . gettype($XoaxVar) . "<br />";
$XoaxVar = 45.0;
echo "The type of \$XoaxVar = " . $XoaxVar . " is " . gettype($XoaxVar) . "<br />";
$XoaxVar = true;
echo "The type of \$XoaxVar = " . $XoaxVar . " is " . gettype($XoaxVar) . "<br />";

?>
 

Output

 
 

© 2007–2024 XoaX.net LLC. All rights reserved.