October 30, 2026

PHP – Types of Variables

PHP uses variables a lot in storing pieces of data used by the program.

  1. Characteristics of PHP Variables
  2. PHP Data Types
  3. Working With Booleans
  4. Working With Strings

 

1. Characteristics of PHP Variables

I have made a list of important points for you to bear in mind while working with PHP variables.

  • Variables names in PHP must begin with a leading $ sign
  • The variable takes the value of its most recent assignment. So if you have $x = 5 and $x = 10, then the value of $x would be 10
  • Values of variables are assigned using the assignment operator(=).
  • It is not mandatory to declare a variable before assignment, but it is sometimes a good practice
  • In PHP, variables do not have intrinsic types – this means that a variable can be used to store any data type. So we say that PHP is not strongly typed or PHP is dynamically typed(or weakly typed)
  • Variables that are used before being assigned assume default values
  • PHP dynamically converts from one type to another when necessary

 

2. PHP Data Types

We would take a look of the eight data types supported by PHP –

  • Integers – these are whole numbers without a decimal point
  • Doubles – floating point numbers like 4.534, 50.0
  • Booleans  – take one of two possible values: true or false
  • NULL  – as special type that has only one value: NULL
  • Strings  – sequences of characters. For example ‘Hello World’
  • Arrays – named collection of values with each value having an integer index
  • Objects  – these are programmer-defined classes. Could be made up of other data types
  • Resources – special variables whose value is a reference to resources external to PHP

 

3. Working With Booleans

As mentioned, Booleans can have only one of two values: True or False For example:

<?php
    if (TRUE)
    print("This will always show<br>");

    else
    print("This will never show<br>");
?>

When this code executes, it would display the first string. Now here a few rules to bear in mind when it comes to working with booleans:

  • if the value of a boolean is a number, then it is false if it exactly equals zero, else it is true.
  • if the value is a string, then it is false if it is an empty string, else, it is true. Also, it the string is “0”, then it is false too
  • NULL values are always false
  • It the value is an array, then it is false if it contains not values, else it is true
  • Valid resources are always true
  • Double should not be used a boolean
  • A variable assigned NULL returns false if tested with the isSet() function

 

Exercise on Booleans

Look at the code below and answer what would be the value of each expression. Leave your answer in the comment box below

<?php
    $num1 = 3 + 0.4159;
    $str1 = "The First String"
    $array1[49] = "A numbers array";
    $array2 = array();
    $null1 = NULL;
    $num2 = 223 - 223;
    $str2= "";
?>

 

4. Working With Strings

As mentioned before, strings are sequences of characters.

Some examples are given below:

$string1 = "This is string in double quotes";
$string2 = 'This is a kind of longer, singly quoted string';
$string3 = "This string is thirty-eight characters";
$string4 = ""; // a string of zero characters (empty string)

 

Single and Double Quotes

Strings in single quotes are treated literally whereas string in double quotes replaces the variables with the actual values. Take for example

<?php
   $variable1 = "myname";
   $literal1 = 'My $variable1 will not print!';
   
   print($literal1);
   print "<br>";
   
   $literal1 = "My $variable1 will print!";
   print($literal1);
?>

The output of the code would be:

My $variable1 will not print!
My myname will print!

So you see that for the first line, the variable was not replaced because the string is in single quotes. However the second string replaced the variable