Loading

PHP: Variables

Variables

In programming, a variable is a container for some data such as a number, string, object, etc. These types of data can be represented in PHP:

  • integer (values from -2147483648 to 2147483647)
  • floating point (called float or double)
    (values from 2147483648 to 1.7E308)
  • string (values like "Hello World" or "1234")
  • boolean (true or false)
  • object (in the language since PHP version 3, but reworked in version 5)
  • array (a set containing one or any number of types of data)

In PHP, variables are declared like this:

<?php
$int_var;
?>

This is how to declare and initialize a PHP variable in one step:

<?php
$int_var = 5;
?>

The variable name always must start with a "$" and then either an alpha character or an underscore "_". After that, the name can have alpha, numeric, and/or underscore characters. Each variable name is case sensitive, so these variables are not the same:

<?php
$int_var = 5;

$int_var = 8;
?>

If you try to declare a variable and then use it without initializing it, PHP will try to set the variable to a certain type and default value depending on its context and will not report an error. Default values are:

  • array: empty array
  • integer: 0
  • double: 0
  • string: empty string
  • boolean: false (0)
<?php

echo("Uninitialized variables");

//array variable
echo($arr_var[0]);

//integer variable
echo((int)$int_var);

//double variable
echo((float)$double_var);

//string variable
echo($str_var);

//boolean variable
echo($bool_var ? "true" : "false");

?>

This prints out:

Uninitialized variables

0
0

false

In PHP, variables do not have a type. This means that they can start out holding an integer, then an object, then a double, etc. Because of this, PHP is referred to as a "loosely typed" or "untyped" language. So, you do not need to, nor can you, declare a variable to be of a certain type before you use it. This also means that you cannot constrain a variable to hold only one type of data.

Sometimes, PHP will silently cast variables from one data type to another. For instance, set an integer variable to the maximum integer value of 2147483647 and then add 1. The program will keep working fine and will print out 2147483648, but the data type has silently changed from integer to double. You can see this by using the built-in gettype() function to see what the data type is:

<?php

//Declare an int variable and initialize it to 2147483647
$my_int = 2147483647;

echo("int = " . $my_int);

echo(gettype($my_int));

$my_int = $my_int + 1;

echo("int + 1 = " . $my_int);

echo(gettype($my_int));
?>

This prints out:

int = 2147483647
integer
int + 1 = 2147483648
double

Similarly, you can manually change the data type of a variable at any time by setting it equal to different data:

<?php

//integer
$my_var = 5;
echo("variable " . $my_var . " has data type of " . gettype($my_var));

//string
$my_var = "Hello World";

echo("variable " . $my_var . " has data type of " . gettype($my_var));

//boolean
$my_var = true;

echo("variable " . $my_var . " has data type of " . gettype($my_var));

//double
$my_var = 123123.123123;

echo("variable " . $my_var . " has data type of " . gettype($my_var));

?>

This prints out:

variable 5 has data type of integer
variable Hello World has data type of string
variable 1 has data type of boolean
variable 123123.123123 has data type of double

You will notice that the boolean true value prints out as "1". In programming languages usually, false is represented as "0" and true as any non-zero integer. So, if you evaluate a number to see whether it is true or false, it will register true as long as it is greater than 0:

<?php
$my_var = 11234123;

if($my_var) {
    echo("true = " . $my_var);
} else {
    echo("false = " . $my_var);
}

$my_var = 0;

if($my_var) {
    echo("true = " . $my_var);
} else {
    echo("false = " . $my_var);
}
?>

This prints out:

true = 11234123
false = 0

Arrays are implemented in an interesting way in PHP. Arrays are a set of variables (usually of the same type) collected together using indices to refer to them, usually 0–n. In PHP, an array is more like a map: it associates keys to values, or names to values. If you do not specify indices, then it will use 0–n as the keys, such as:

<?php

//array declaration
$arr_var = array("blue", "green",
                 "orange", "purple");

print_r($arr_var);

?>

This prints out:

Array ( [0] => blue [1] => green [2] => orange [3] => purple )

Here is how to specify your own keys and values, with no particular ordering of either needed:

<?php

//array declaration
$arr_var = array(34 => "blue", 35 => "green",
                 7 => "orange", "Gosh" => "purple");

print_r($arr_var);

?>

This prints out:

Array ( [34] => blue [35] => green [7] => orange [Gosh] => purple )

Another interesting thing about PHP arrays is that you can put different data types into one array, like this:

<?php

//array declaration of a string, an int, a boolean, and a double
$arr_var = array("Hello World", 12345, true, 998182.123123);

print_r($arr_var);

?>

This prints out:

Array ( [0] => Hello World [1] => 12345 [2] => 1 [3] => 998182.123123 )

A variable can also hold an object, either a standard PHP or a user-defined object. We will go through objects in a later tutorial, but for now, this is how you can declare and initialize a standard PHP object, and then call one of its functions:

<?php

$my_date_time = new DateTime();

//Gets the Unicode Timestamp
echo($my_date_time->format('U'));

?>

This prints out:

1260417730