As many other languages, PHP handles many different data types. Data types and their correct use are key for implementing a very Robust Enterprise Class PHP Application.
Prerequisites:
These are the most common data types in PHP:
null
This type represents a void or nothing stored in the variable set with this data type. Nulls are difficult to understand at first, but were designed for the developer to be able to differentiate a variable with nothing in it, from a variable with an empty string or a variable with a zero value.
$a = null; // This variable exists but doesnt has any value nor a data type
(string) $b = ''; // This variable exists and it has type casted as string with 0 length
(int) $c = 0; // This variable exists and it has type casted as an integer which value is 0
(bool) $d = false; // This variable exists and it has type casted as a boolean with value false
(bool) $e = 0; // This variable exists and it has type casted as boolean with value false
if you compare these variables each one against the others , none of them are equal, except for the last 2, a boolean false is equal as a boolean 0
Refer to PHP Types Intro for more on this.
bool
This type represents a Boolean value and can be either true
or false
Because in earlier versions of PHP and other languages, booleans were declared as 1 for true and 0 for false
nowadays these values are interchangeable, but as best practice, it is highly recommended to use true / false
This data type is used for Boolean Algebra and / or for setting flags that helps in validation routines.
Refer to PHP Booleans for more info.
int
This data type represents any positive or negative Integer Number within the permitted range. Ints can be specified in decimal (base 10), hexadecimal (base 16), octal (base 8) or binary (base 2) notation and you can do any sort of arithmetic operations with them.
Refer to PHP Int Data Type and PHP Arithmetic Operators for more on this.
float (floating-point number)
A float can be any positive or negative number and it differentiates from the int data type because this can have as many decimal numbers as allowed by the precision set in the PHP configuration. They allow the "e" nototation.
$a = 1.23456;
$b = 2.3e4;
$c= 4.56e-5;
refer to PHP Floating Numbers for more on this.
string
This data type is the most generic one and it can contain any combination of valid ASCII characters
$singleQuotedString = 'This is a string';
$doubleQuotedString = "This is a string";
$heredocString = <<<HEREDOC
This is a really long string
and can contain many paragraphs
until reaching the end.
HEREDOC;
Refer to PHP String Types for more info on this.
array
Arrays are one of the most important data types in PHP and can contain many different kinds of Data Structures like List, Vector, Map, Hash Table, Dictionary, Stack, Queue, Trees, and many others. Basically all of these are based on the concept of key - value
pairs . Arrays are declared in PHP this way:
use App\Entity\Animal;
// Array notation.
$a = array();// Empty array assigned to $a.
// Bracket notation.
$b = [];//Empty array assigned to $b.
//These two arrays are equal
$a = array('key1' => 'value1', 'key2' => 'value2', 'keyn' => 'valuen');
$b = ['key1' => 'value1', 'key2' => 'value2', 'keyn' => 'valuen'];
//Array keys can be strings or integers
$c = [0 => 'value1', 1 => 'value2', 2 =>'value3'];
//Array values can be any other data type including other arrays, objects, collections, etc...
$d = [
'k1' => 1, //value is an integer
'k2' => 'a string',//value is a string
'k3' => ['kk1' => 'vv1', 'kk2' => 'vv2'], //value is another array
'k4' => new Animal('Zebra'),//value is an object
12 => 3.4e5,//value is a float
];
//Arrays can be a simple list where the keys are not shown but the key pointer is kept nternally
$list = ['a','b','c', 'd','e'];//letter 'c' key is 2 -- THis representation only allows integer values for the keys
//Arrays can be multi dimensional
$multiDimensional['x']['y']['z'] = [1,2,3,4,5,6,7,8,9];
refer to PHP Arrays for more on this.
object
To better understand the concept of object, please refer to this post: Basics of Object Oriented Programming in PHP - Part I
In terms of PHP Data Type, and Object is a special type of Array , where each key is either a property or a method and their corresponding value stores the content of it. If you do this:
$zebra = new Animal('zebra');
$zebra->setEyes(2);
$zebra->setLegs(4);
$zebra->setTail(3.5);
$zebra->setMotionSpeed(23.45);
print_r($zebra);
Then in the Browser you'll see this:
When you ask PHP to print out an object it will only shows the properties, not the methods. however internally, these methods are stored in key value pairs so PHP can point at them when needed.
If the object's Class definition has only properties, you can convert this to an array:
$object = new \StdClass();//Creates a new Generic Object
$object->key1 = 1;
$object->key2 = 'a string';
$object->key3 = [1,2,3];
print_r((array) $object);
Then in the Browser:
Also, if you have an Array, it can be converted into an object where the array keys become object properties:
$d = [
'k1' => 1, //value is an integer
'k2' => '--- a string ---',//value is a string
'k3' => ['kk1' => 'vv1', 'kk2' => 'vv2'], //value is another array
'k4' => new Animal('Zebra'),//value is an object
'k5' => 3.4e5,//value is a float
];
$dd = (object) $d;
print_r($dd);
echo "\n\n print out the content of property k2: ";
echo "{$dd->k2}";
In the Browser:
Refer to PHP Objects for more on this.