PHP Basics: DataTypes
In PHP, a variable’s datatype determines the values it may contain, plus the operations that may be performed upon it. for instance, It will make perfect sense to add variables whose values are numbers (2 + 3), but adding variables whose values are characters (x + y) wont make much sense.
PHP supports many different datatypes, but they are generally broken into three categories:
scalar, composite, and special.
Scalar Datatypes
A scalar value contains only one value at any given time. Several datatypes falls under this category, including integer, float, string, and boolean.
- Integer: Quite simply a whole number, or a number that doesn’t contain fractional parts, such as 0, 1, 25, -16, or 418475. The range of integers allowed is platform-dependent, but in general, this data type will most likely be large enough for the numbers your program will use. if the maximum supported size is surpassed, the number will be automatically converted to a float.
-
$a = 15; // decimal
-
$b = -12548; // decimal
-
$c = 0345; // octal
-
$d = 0xF3C // hexadecimal
An Integer can be a decimal number (base 10), an octal number (base 8), or an hexadecimal number.
- Float: A floating-point number, Also referred to as float, double, or real number, in general, a number that contains fractional parts, such as
-
$a = 15.5;
-
$b = 7.0;
-
$c = 45.3e3;
-
$d = 3.24E+12
- String: A sequence of single characters that could be text, but it could also be the contents of an image file, a spreadsheet, or even a music recording. There is no practical limit on the length of a string.
We will talk about string more in details in an upcoming post.
- Boolean: The Boolean datatype has only two possible values: true and false (case insensitive). it’s generally used for simple flags that track true/false conditions.
Alternatively, there are other values that can be used as true/false:
- A number (either integer or float) is evaluated to false if its value is zero, and true otherwise.
- A string is evaluated to false only if it is empty or if it contains the single character
0. and evaluated to true if otherwise (even multiple zeros).
- When converted to a number or a string, a Boolean becomes 1 if it is true, and
0 otherwise.
Assigning datatypes
Unlike other programming languages, such as C/C++, or Java, PHP is not a strongly-typed language, which means that variables doesn’t need to be declared before they can be used. This involves stating the variable’s type and name, as you’ve already seen in other programming language.
-
<?php $a = 1; // integer
-
$b = 1.25 // float
-
$c = $a * $b; // float
-
?>
NB: When an expression involves and integer operand and a floating number operand, the result is automatically converted to a floating number.
Type Casting
Type Casting is the process by which we force a variable to behave as a type other than the one originally intended for it. This is done to take advantage of certain features of type hierarchies. For instance, values from a more limited set, such as integers, can be stored in a more compact format and later converted to a different format (float) enabling operations not previously possible, such as division with several decimal places’ worth of accuracy.
PHP explicit conversion is a c-like casting:
-
<?php
-
$a = 45;
-
$b = (int) $a; // variable
-
$c = (string) $a; // string
-
$d = (float) $a; // floating number
-
?>
Detecting the data type of a PHP variable
To find out the data type of a variable, use PHP gettype() function as shown below:
-
<?php
-
$age = 27;
-
$mile = 15.21;
-
$flag = TRUE;
-
$foo = "hello world";
-
-
// Get datatypes
-
?>
Alternatively you can use other functions to check the variable datatype:
-
is_ binary() // Finds whether a variable is a native binary string
-
is_ bool() // Finds out whether a variable is a Boolean
-
is_ double() // Alias of is_float()
-
is_ float() // Finds whether the type of a variable is float
-
is_ int() // Find whether the type of a variable is integer
-
is_ integer() // Alias of is_int()
-
is_ lon() // Alias of is_int()
-
is_ null() // Finds whether a variable is NULL
-
is_ numeric() //
-
is_ object() // Finds whether a variable is a number or a numeric string
-
is_ real() // Alias of is_float()
-
is_ resource() // Finds whether a variable is a resource
-
is_ scalar() // Finds whether a variable is a scalar
-
is_ string() // Find whether the type of a variable is string
-
var_ dump() // Dumps information about a variable
To be Continued


