Curriculum
This tutorial will teach you about PHP data types such as scalar types, compound types, and special types.
A type specifies the amount of memory that it allocates to a value. A type also governs the operations that can be performed on it.
PHP has ten primitive types, four of which are scalar types, four of which are compound types, and two of which are special types:
When a variable holds a single value of the type integer, float, string, or boolean, it is referred to as a scalar.
Whole numbers in the range…-3,-2-,-1,0,1,2,3… are known as integers. The integer’s size is determined by the operating system on which PHP is installed.
The PHP INT SIZE constant determines the integer size on a particular platform. The integer type is represented by PHP’s int keyword.
In this example, some integers are shown:
<?php $count = 0; $max = 1000; $page_size = 10;
Floats, sometimes known as floats, doubles, or real numbers, are floating-point numbers.
The IEEE 754 double format is used by PHP to represent floats. Floats, like other programming languages, have a finite amount of precision.
The float keyword in PHP is used to represent floating-point numbers. The following PHP example shows how to use floating-point numbers:
<?php $price = 10.25; $tax = 0.08;
The truth value of a Boolean expression can be either true or false. The Boolean type is represented in PHP through the bool keyword.
True and false are the two values of the bool type. Because keywords are case-insensitive, you can denote boolean values with true, True, TRUE, false, False, and False.
The example below demonstrates how to assign Boolean values to variables:
<?php $is_admin = true; $is_user_logged_in = false;
PHP converts values of various kinds to boolean values when used in boolean contexts, such as if-else and switch-case statements.
The following values are treated as false by PHP:
The keyword false.
0 and -0 are integers (zero).
0.0 and -0.0 are two floats (zero).
The string “0” and the empty string (“”, “).
(array() or []) The empty array.
The null value.
SimpleXML objects are made up of empty elements with no attributes.
The values that aren’t one of the above-mentioned false values are true.
A string is a collection of characters encircled by single (‘) or double (‘) quotes. Consider the following scenario:
<?php $str = 'PHP scalar type'; $message = "PHP data types";
Compound data is made up of values that have several values. Array and object are two compound types in PHP.
An array is a type of ordered map that links keys to values. For example, with a shopping cart, you can create a list of things like this:
<?php $carts = [ 'laptop', 'mouse', 'keyboard' ];
There are three string values in the $carts array. It assigns the values ‘laptop, mouse, and keyboard’ to the indexes 0, 1, and 2. Because it employs numeric indexes as keys, $carts is referred to as an indexed array.
The square brackets are used to access a value in an array:
<?php echo $carts[0]; // 'laptop' echo $carts[1]; // 'mouse' echo $carts[2]; // 'keyboard'
Strings can be used as keys for array elements in addition to numeric indices. Associative arrays are the name for these types of arrays. Consider the following scenario:
<?php $prices = [ 'laptop' => 1000, 'mouse' => 50, 'keyboard' => 120 ];
The key in square brackets is used to access an element in an associative array. Consider the following scenario:
<?php echo $prices['laptop']; // 1000 echo $prices['mouse']; // 50 echo $prices['keyboard']; // 120
An object is a class instance. It’s a fundamental idea in object-oriented programming.
An object possesses properties. A person object, for example, may have the properties first name, last name, and age.
An object can also have behaviours, which are referred to as methods. A person object, for example, can have a method called getFullName() that returns the full name.
There are two special types in PHP: null and resource.
The null type has only one value, null, which represents a variable with no value.
The resource type represents a pointer to an external resource, such as a filehandle or a database connection.