Variables and Data Types

category: Basics
Created by: Dr.Ferrous

Variables and Data Types

category: Basics
Created by: Dr.Ferrous
twitter google+ facebook pinned 

Variables are used to store different kinds of data (or values). These values can be numbers, text, or much more complex data. PHP has eight basic (orprimitive) types of variables.


let’s learn how to store information in the PHP script. Storing information is necessary so that you can use the value anywhere in the program.Variable is a name given to store values that is to be used in the program. In PHP, the variable names are preceded with the ‘$’ symbol.


Variable Naming

There are certain rules that must be followed when naming the variables. They are:

  • Variables are  defined with a preceded dollar sign ($)

  • PHP variables must start with a letter or underscore “_”

  • PHP variables should contain only alpha-numeric characters and underscores

  • A variable name cannot start with a number

  • We can separate the variable names by using underscores. E.g.$employee_details

  • Variable names differentiates both lower case letters and upper case letters($y and $Y are two different variables)

  • Values can be assigned using the “=” operator

  • Another important thing in PHP is that all the statements must end with a semicolon “;”

The first step to use a variable, is to assign a value to it.

Assigning Variables

Variable assignment is simple. Just write the name and add a single equal sign (=), and then expression that we want to assign to that variable.


Reassigning Variables

After assigning a value to the variable, if it has to be changed in the later stages of the program, you can also reassign the values to the same variable name.


Unassigned Variables

Many programming languages reports an error if you try to use the variables before they are assigned any value. But the PHP deals with such unassigned variables. In PHP, the default error-reporting settings allow you to use the unassigned variables without reporting any errors. If you would like to be warned about variables that have not been assigned,  the error-reporting level to E_ALL from the default level of error reporting.

This can be done in two ways:

  • By including the statement error_reporting(E_ALL) at the top of script.

  • By changing the php.ini file to set the default level.




You can use the function isset() to check if the variable is already assigned.


Default Values

When you do not pass values to the parameters in functions, PHP assigns a value by default, which is called the default value

  • Variables in PHP do not have intrinsic types

  • A variable does not know in advance whether it will be used to store a number or string of characters

  • The type of  a variable is interpreted depending on the context in which it is used

The data to be stored and used in the PHP script can be of various types. In order to differentiate this, PHP provides various Data Types.

Data Types

The values stored and used in the PHP script are of different types. The Data Types define the type of data being used in the program. PHP has 8 Data Types. They are:

  • Integers are whole numbers, without a decimal point, like 495

  • Doubles are floating point numbers, like 3.1415 or 49

  • Booleans have only two possible values: TRUE and FALSE

  • NULL is a special data type. It has only one value: NULL

  • Strings are series of characters

  • Arrays is a collection of  values

  • Objects are instances of classes, which can access both data and functions of that specific class

  • Resources are special variables that hold references to resources external to PHP(db connection)

These data types are classified into 2 major types:

  • Simple Types – integers, doubles, Booleans, Null and strings

  • Compound Types – strings, arrays, objects, resources

Simple Data Types


Integers are of the simplest type. They correspond to simple whole numbers, both positive and negative.



Doubles and Floating Point Numbers:

Real numbers (i.e., numbers containing a decimal point)




Booleans are true – or – false values, which are used in control constructs like the testing portion of an if statement




NULL type, however, takes this to the logical extreme: The type NULL has only one possible value, which is the value NULL.


A variable that has assigned to null has the following properties:

  • It evaluates to FALSE in a Boolean context

  • It returns FALSE when tested with IsSet()

Apart from declaring numbers, PHP also supports “Strings” where sequence of characters are treated as a single unit.

Declarations of Strings

Strings are a sequence of character that are treated as one unit.Strings in PHP are declared in two ways:

  • Single quoted

  • Double quoted

Single Quoted String:

Here the statement present within the single quotes will be displayed as it is without any changes.



Double Quoted String:

Here the statement present within the double quotes will be interpreted and the output of the program will be displayed.


When we want to execute a block of statements repeatedly, functions are used.