PHP Data Types
Consider an employee in a company called Robbin Jackman with a basic monthly salary of 5000 dollars. We can represent this employee in a PHP script using following variables.
$name = 'Robbin Jackman'; $salary = 5000;
Here $name is a string and $salary is a number. You can see that these two are two different data types. In PHP we call them String and Integer respectively.
Data types of variables matter when they are used in expressions and operations. For an example, think that the company is going add a title based on the gender and increase Robin Jackman’s salary in 200 dollars. You can do the modifications as below.
$name = 'Mr '.$name; $salary = $salary+200;
For $name we could do String Concatenation using dot operator because $name is a string. We could use Addition operator on $salary because it’s an integer. You can’t do String Concatenation on two integers or Addition on two strings (without losing string values).
$salary = $salary.200; // Wrong $name = 'Mr '+$name; // $name becomes 0 since PHP converts strings into numeric values before doing addition operation
Type Juggling
In some programming languages, it’s required to define the data type of a variable before assigning it any value. After that, only that type of values can be assigned to that variable. For an example in Java, you have to use variable $salary as below.
int salary; salary = 5000;
or
int salary = 5000;
Then you can’t assign a string to this variable.
salary = 'Salary is 5000'; // Wrong
In PHP, type of a variable is not defined before using it and type is determined by the PHP interpreter based on the context.
$salary = 5000; echo "Robbin Jackman's salary is ".$salary;
Here we could do String Concatenation because $salary was used in a string context and PHP interpreter treated it as a string.
$salary = $salary + 200; echo $salary; // Would print 5200
Here we could to Addition operation because $salary was used in an integer context and therefore it was interpreted as an integer.
This behavior is called Type Juggling. Refer PHP manual for more information on Type Juggling. You can also assign a complete string to $salary and use it as a string there onwards.
$salary = 'Salary is 5000';
Scalar Data Types
Variables considered so far could contain only one value at a time. Data types associated with these variables are called Scalar. There are four Scalar data types in PHP.
String
String is used for binary data. This can be text, content of an image file etc.
$today = 'Today is a nice day';
Int
For numeric values, Int is used. Int can contain a positive or a negative number.
$population = 20000000; $temperature = -14 ;
Float
Float is used for numbers that have a fractional component. Float can also contain positive or negative values.
$length = 145.78; $growthRate = -5.216;
Sometimes name Double is used for Float values.
Boolean
Boolean is used for values that are either true or false. You would often see Boolean values in conditional statements.
if (($age > 12) && ($age < 20)) { $teenage = true; } else { $teenage = false; } //Some content goes here if ($teenage) { // Show special offers for teenagers }
Refer PHP manual on Data Types for more information on these types and their limits.
Compound Data Types
Arrays and Objects fall into this category. Both can contain more than one value at a time. Arrays are used to store set of related values in an order while objects can contain values and functionalities.
Other Data Types
NULL and Resource are two data types that occur in special occasions. NULL means the variable hasn’t been assigned any value.
if (is_null($increment)) { $increment = 1; return $increment; } else { return ++$increment; }
Resource is used for variables that hold data or reference of an external resource.
$result = mysql_query($query);
Above, provided that $query is a valid MySQL query, $result will hold a resource.
Type Casting
As mentioned, PHP interpreter determines the type of a variable based on the context it’s used in. However there may be special occasions that you want to force a certain data type.
$value1 = 10; // An int $value2 = 20.38 // A float $value3 = $value1 + $value2; // $value3 will be 30.38 which is a float.
Think that $value1 and $value2 are assigned based on some other expressions and you always want to make sure $value3 is an integer. Then you can cast the type of $value1 and $value2 as below.
$value3 = (int)$value1 + (int)$value2; // $value3 will be 30
At a given time if $value1 = 10 and $value2 = 20.38, in above expression (int)$value1 would be 10 and (int)$value2 would be 20 since forcing Integer type leads $value2 to loose its fractional part.
Refer PHP manual to see more type casting options and examples.