login

PHP Variable

A variable stores a value such as an integer or text string. A variable can be reused throughout the code. A variable is actually a name of a memory location. In PHP, you define a variable with the following form:

<?php

   $variable_name = value;

?>

Be noted that a variable always start with $ sign and followed by either a letter or an underscore. There are some rules you must follow when you define a variable:

  • PHP variables consists of only alphanumeric characters and underscores; for example, a-z, A-Z, 0-9, and _
  • Variables in PHP are case-sensitive. This means that $abc and
    $AbC are completely different.
  • You can use underscore to seperate a PHP Variables with more than one word to make them easier to read; for example, $html_header
  • PHP Variables can be assigned values using the equals sign (=).
  • Always end with a semicolon (;) to complete the assignment of the variable.

For example, to define integer variables you can do as follows:

<?php
   $x = 10;
   $y = 20;
?>

Variable types

PHP is not strongly typed. When you define a variable you don't have to specify type in the code. Instead PHP automatically pick the data types based on the value you assign to variable. These data types include integer, strings, numbers and even more complex elements such as arrays and objects.

If you define a variable and use it for specific purpuse such as calculation. PHP attempts to convert data automatically. For example if you have a string variable with the value '2', PHP will convert it to an integer 2 when doing calculation.

<?php
      $x = '2'; // a string $x
      $y =  3;  // an integer $y
      $y = $y + $x; // $y = 5, php Converts '2' to 2
?>

Finding variable's type

PHP provides you a built-in function gettype() to get the type of a variable. Here is example of using gettype() function:

<?php
    $int = 10;
    $str = 'this is a string';
    $bool = true;
    $d = 3.14;
    echo gettype($int), '<br />';
    echo gettype($str) , '<br />';
    echo gettype($bool), '<br />';
    echo gettype($d), '<br />';
?>

Checking variable's type

PHP provides a list of useful function to allow you to check a variable type. These functions are very useful in case you want to make sure the type of varible you are working with is suitable.

Function Name Meaning
 is_int($var); Return true if $var is an integer, otherwise return false.
 is_string($var); Return true if $var is a string, otherwise return false.
 is_bool($var); Return true if $var is a boolean, otherwise return false.
 is_float($var); Return true if $var is a float, otherwise return false.
 is_long($var); Return true if $var is a long type, otherwise return false.
is_numeric($var); Return true if $var is a numeric, otherwise return false.
 is_double($var); Return true if $var is a double, otherwise return false.
is_object($var) Return true if $var is an object, otherwise return false.
 is_array($var); Return true if $var is an array, otherwise return false.

Setting and unsetting variable

There is a pair of useful functions: isset() and unset() which accept a variable as a parmater to check a variable is set and unset it. A variable is set when it is assigned a value or other variable. We use isset() function to check whether a variable is set. If we don't want that variable available anymore, we use unset() function. Here is an example of using isset() and unset():

 <?php
$x; // $x is not set
if (isset($x)) {
    echo '$x is set. <br/>';
} else {
    echo '$x is not set. <br/>';
}
$x = 10; // $x is set
if (isset($x)) {
    echo '$x is set. <br/>';
} else {
    echo '$x is not set. <br/>';
}
unset($x); // $x is unset

if (isset($x)) {
    echo '$x is set. <br/>';
} else {
    echo '$x is not set. <br/>';
}
?>

You'll learn more about the PHP if control flow in later tutorial. Just follow the status of variable $x;

is_null() and empty() functions

To find a variable is NULL, we use function is_null(). To check whether a string is empty or blank we use function empty(). Here is an example of using is_null() and empty() function.

<?php
    $obj = NULL;
    if (is_null($obj)) {
	 echo '$obj is null <br/>';
    }
    $str = '';
    if (empty($str)){
	 echo '$str is empty <br/>';
    }
?>

In this tutorial, you've learnt how to define a variable, understand PHP variable data types and different built-in function to work with a variable. In the next tutorial, you will learn different kind of variables in PHP.