What is a php variable?

Computer stores data into a memory. In order to identify which data is stored where it uses a unique keyword which we call variable.

Variables basically store data. They are pointers to a memory location in a computer. Think about this way library is a computer and books are variables. Each book has a specific information.

How to define a variable in php?

In php language every variable is prefixed using $ symbol followed by a unique name. There are some rules to define a variable in php. If you dont follow the rule you will get syntax errors:

  • A variable must start with $ symbol followed by a unique name
  • A variable name must start with a letter or the underscore character
  • A variable name cannot start with a number
  • A variable name can only contain alpha numeric string and underscore
  • Variable names are case sensitive

Always write meaningful name to variable so that it is easily redable by other developers.

Note:  PHP automatically converts the variable to the correct data type, depending on its value.

Variable scopes in php?

In php script, you can define variables anywhere. Scope is a part of a php script where you can use the defined variables. Followings are different types of scopes:

  • local
  • global
  • static

We will learn about these types later in this tutorial.

Types of php variables

  • Scalar
    • boolean
    • integer
    • float
    • string
  • Compound
    • array
    • object
    • callable
    • iterable
  • resource
  • NULL

Scalar Variables in php

Scarar variables incude following types of variables

  • strings
  • integers
  • floats
  • booleans

What is a boolean variable in php?

Boolean variable basically determines true or false exerciseression. Let's check following example:


// declare a boolean variable
$is_allowed_to_process = true;


What is an integer variable in php?

Integer variables can be declared in decimal, hexadecimal, octal or binary notation and can be declared using + or - sign to define them positive or negative integers.

Followings are some of the ways you can declare an integer variable:


// decimal number
$number = 1234; 

// a negative number
$negative_number = -123; 

// octal number (equivalent to 83 decimal)
$octal_number = 0123; 

// hexadecimal number (equivalent to 26 decimal)
$hexa_decimal_number = 0x1A; 

// binary number (equivalent to 255 decimal)
$binary_number = 0b11111111; 


What is a float variable in php?

Floating point number variables are also known as floats, doubles or real numbers:


$a = 1.454; 
$b = 1.2e5; 
$c = 8E-10;


Note: Floating point numbers have limited precision. Do not compare floats for equality if your applications has to correctly do sum or any other math. Checkout Why don't my numbers add up?

What is a string variable in php?

A single character refers to a byte in a computer. Combination of bytes are called a string. String represents number of characters in a row.

Followings are some of the types of strings in php:

  • single quoted
  • double quoted
  • heredoc
  • nowdoc

Let's check out some examples to see how they can be declared:


$name_of_fruit = 'apple';
$name_of_city = 'Toronto';

$what_is_php = "PHP is a simple programming language";
$html_string = "<h1>This is a html title tag example</h1>";

$html_page = <<<HTML
   <p>Sample Paragraph</p>

$sample_string = <<<'EOD'
Example of string
spanning multiple lines
using heredoc syntax.

What is the difference between heredoc and nowdoc?

Followings are some of the difference between heredoc and nowdoc strings:

  • Nowdocs are to single-quoted strings while heredocs are to double-quoted strings.
  • Variables declared in nowdocs can not be escaped
  • Variables declared in heredoc will be escaped with their values

Compond variables in php

Compound variables in php can include following types of variables:

  • arrays
  • objects
  • callables
  • iterables

Arrays in php

Arrays in php are basically ordered map. A map defines association between a key and value. Followings are some of the ways you can define arrays in php:


// arrays with default index starts with 0
$name_of_fruites = ['mango', 'apple', 'grape'];

// array with keys/values i.e. associative array
$type_of_cars = [
   'mazda'  => ['mazda3', 'mazda5'],
   'toyota' => ['camry', 'raw4'],


We will learn more about other types of variable in a later tutorial. There are lot to learn about variables because you are beginners we have to consume php slowly.

Let's do some practise because practise will make us a good programmer. Let's do some excerise regarding variables in our next tutorial.


Related Posts