PHP Variable Types (Understanding Variable Types with Examples)

What is a static variable PHP?

Use the static keyword to declare a class’s attributes and methods as static. Static attributes and methods may be accessed without instantiating the class.

Additionally, the static keyword is used to declare variables in a function that retain their value after the function has been completed.

What are local variables in PHP?

Local variables are variables that are declared within a PHP function and have their scope limited to that function.

Because local variables have no scope outside of the function (variables cannot be referenced outside of the function), they cannot be used in the program outside of their scope.

What are the variable types in PHP?

PHP supports the following variable types:

By the way, to test the provided examples below, we have an Online PHP Compiler for you!

String in PHP

A string is a type of non-numerical data. It can contain alphabetic characters, numeric characters, and special characters.

String values must be enclosed in single or double quotation marks.

$str_a = 'Using Single Quotation Marks';
$str_b = "Using Double Quotation Marks";
$str_c = "Enter numbers such as 12345";
$str_d = "Enter symbols such as !@#$%^&*()";
$str_d = ""; //you can also put empty string

Strings with single quotes are taken almost literally, while strings with double quotes replace variables with their values and special character sequences.

   $var = "itsourcecode";
   $str_a = 'This will not print the value of $var';

   // As you may have noticed, we use a double quote to use the character sequence \n.
   echo $str_a."\n";

   $str_a = "Using double quotes this will print the value of $var";
   echo $str_a;


This will not print the value of $var
Using double quotes this will print the value of itsourcecode

There are absolutely no arbitrary limits on how long a string can be. As long as you have enough memory, you should be able to make strings of any length you want.

PHP does the following two things to strings that are separated by double quotes (""):

  1. Some sequences of characters that start with a backslash (\) are changed to special characters.
  2. Variable names that start with ($) are changed to strings that show what their values are.

The following is the list of escape-sequence replacements:

  • To use backslash inside a string use \\.
  • To use a double-quote inside a string use \".
  • To tab a character inside a string use \t.
  • To create a newline use \n.
  • To use dollar sign inside a string use \$.

Integer in PHP

An integer is a type of non-decimal number. In simple terms, integers are whole numbers, either positive or negative.

Like strings, integers can be assigned to variables, but without quotation marks. They can also be used in expressions.


$x = 1214;
$y = -72998;
$z = 4102 + 12395;

Integers can be written in three different formats: decimal (base 10), octal (base 8), and hexadecimal (base 16).

Decimal is the default format, octal integers start with a 0, and hexadecimal starts with a 0x.

Float in PHP

Floating point numbers are decimal or fractional numbers, as shown in the example below. They are also called “floats“, “doubles“, or “real numbers“.

  $x = 4.599;
  echo $x."\n";

  $y = 2.2e9;
  echo $y."\n";

  $z = 5E-41;
  echo $z."\n";

Boolean in PHP

PHP has two constants that can be used as Booleans: TRUE and FALSE. They can be used like this:

  print("This will print if it is TRUE");
  print("This will print if it is FALSE");

Interpreting other types as Booleans

The following are the rules for assessing the “truth” of any non-Boolean value:

  • If the value is a number, it is false if it equals zero exactly and true otherwise.
  • If the value is a string, it returns false if it is empty (has no characters) or the string “0,” and returns true otherwise.
  • Type NULL values are always false.
  • If the value is an array, it is false if it doesn’t contain any other values, and it is true otherwise. Having a member variable that has been given a value is what it means for an object to have a value.
  • Although valid resources are true, some functions that yield resources when successful will return FALSE when unsuccessful.
  • Avoid using doubles as Booleans.

When used in a Boolean context, each of the following variables has the truth value contained in its name.

$true_num = 33431;
$true_str = "True, if it has values";
$true_array[32] = "This is an array";
$false_array = array();
$false_null = NULL;
$false_num = 500 - 500;
$false_str = "";

Array in PHP

An array in PHP is a variable that can hold multiple values at once. It is useful to group together a group of related objects, such as a list of nation or city names.

A formal definition of an array is an indexed collection of data items. An array’s index (also known as the key) is unique and points to a related value.

Additional information from PHP official documentation:

An array in PHP is actually an ordered map. A map is a type that associates values to keys.

This type is optimized for several different uses; it can be treated as an array, list (vector), hash table (an implementation of a map), dictionary, collection, stack, queue, and probably more.

As array values can be other arrays, trees and multidimensional arrays are also possible.

PHP official documentation


$fruits = array("Apple", "Banana", "Orange", "Guava");

echo "\n";
$fruit_colors = array(
    "Apple" => "Red",
    "Banana" => "Yellow",
    "Orange" => "Orange",
    "Guava" => "Green"


array(4) {
  string(5) "Apple"
  string(6) "Banana"
  string(6) "Orange"
  string(5) "Guava"

array(4) {
  string(3) "Red"
  string(6) "Yellow"
  string(6) "Orange"
  string(5) "Green"

Object in PHP

An object is a data type that supports not only the storage of data but also processing instructions for that data. A specific instance of a class that serves as a template for objects is an object.

By using the new keyword, objects based on this template are produced.

The properties and methods of an object are the same as those of its parent class. Each instance of an object is completely independent.

It has its own properties and methods, so it can be changed without affecting other instances of the same class.

Here’s a simple example of defining a class and then making an object.

// class
class sampleClass{
    // method
    function message(){
        echo 'PHP is FUN!';
// object from class
$obj = new sampleClass;
// calling the method inside the class




Null value in PHP

A null value is a special data type that is used to represent empty variables in PHP.

A variable of type NULL contains no data. The only valid value of type null is NULL.

$var = NULL;
$var1 = "PHP is FUN!";
$var1 = NULL;

A variable to which the value NULL has been assigned has the following properties:

  • In a Boolean context, it evaluates to FALSE.
  • Using the IsSet() method, it returns FALSE.

Resource in PHP

A resource is a special type of data that can be used to hold a reference to any external resource.

A resource variable is a pointer to an outside source of data, like a stream, file, database, or other resources.

Most of the time, special handlers for opened files and database connections are stored in resource variables.

$handler = fopen("main.php","r");


In summary, we’ve explained the variable types in PHP by using various examples. We’ve also learned the eight data types of PHP: String, Integer, Float, Boolean, Array, Object, Null, and Resource.

Lastly, if you want to learn more about the variable types of PHP, please leave a comment below. We’ll be happy to hear it!

Leave a Comment