PHP is a open source server side scripting language, which is easy to learn and favorite programming language among developers across the globe. Due to its easy syntax and open source nature anyone can master this scripting language with the little knowledge of HTML. However for novice(beginner) programmers PHP is not a easy cake walk. One must, first masters its basics before diving into advance concepts of PHP.
So with keeping this in mind, i dedicate this article to the php data types. However, compared to the other high level programming languages PHP considered as a loosely typed language. There is no type safety in PHP, it means you can pass a numeric data into string.
Note: In PHP functions names are not case sensitive but variables names are case sensitive.
PHP offers eight data types. Among of them four are scalar types,two are composite types and the remaining two are special types such as resource and null. So now let’s understand all these data types in detail one by one.
Data Types in PHP
Scalar types:
a) Integer: In PHP Integer(whole numbers) is one of the scalar data type. Integers are whole numbers such as 9,21,149. It typically ranges from -2,147,483,648 to +2,147,483,647. If we try to store large variable into integer then it will automatically converted into floating point number. We can also check for the variable whether it store integer value or not, for example:
<?php
$y = 10;
if(is_int($y)) {
echo "$y"." contains a integer value";
} else {
echo "$y"."is not an integer value";
}
?>
b) Floating Point Numbers: The numeric values with decimal digits in php often referred to as floating point numbers or real numbers. It ranges between 1.7E-308 and 1.7E+308 with 15 digits of accuracy. PHP reads floating point numbers in two formats such as: 4.5 , 0.39
Floating-point values are only approximate representations of numbers. For example, on many systems 7.5 is actually represented as 7.5999999999. We can also check whether a variable holds a floating value or not with is_float() function. for example:
<?php
$x = 10.9;
if(is_float($x)) {
echo "$x"." contains floating value";
} else {
echo "$x"."is not a floating value";
}
?>
c) Strings: PHP support string data type for creating and manipulating strings. Anything in single ‘ ‘ or double ” “ quotes refers to the string in PHP. Sometimes we also refer a short term for String as “Sequence of characters“. In PHP below two lines are termed as string:
‘Welcome to PHP world’ // single quote refers to string in PHP
“Welcome to PHP world” // double quote refers to string in PHP
Even variables are also expanded within double quotes but in single quote they are printed as it is. for example:
<?php
$test = "PHP World";
echo "Welcome, $test"."<br>"; // output = Welcome, PHP World
echo 'Welcome, $test'; // output = Welcome, $test
?>
In String we use is_string() function to check whether a variable contains string value or not. for example:
<?php
$x = "Welcome to PHP World";
if(is_string($x)) {
echo "$x"." contains a string value";
} else {
echo "$x"."is not a string value";
}
?>
d) Boolean: Boolean represents a value to be true or false in php. We use this data type to check whether a given variable satisfy any condition or not. By default the value of boolean is false in PHP. we use is_bool() function to check whether a variable satisfy any condition or not. for example:
<?php
$x = true;
if(is_bool($x)) {
echo "This will returns: ".$x;
} else {
echo "This will returns: ".$x;
}
?>
When a variable satisfy its condition to true then it returns ‘1’ and when it doesn’t satisfy the condition then it returns ‘0’.
Composite data type:
a) Array: In short words we can describe array as “collection of similar data items“. An array plays an important role in php when we use array to extract large records from database. An array index always starts from ‘0’(zero). We can easily learn to create an array from its syntax such as:
$z = array(“Welcome”,”in”,”PHP”,”World”); //Array syntax
We can also use is_array() function to check whether a value is an array such as:
<?php
$x = array('hello','welcome');
if(is_array($x)) {
echo "This will returns: ".$x;
} else {
echo "This will returns: ".$x;
}
?>
Note: However this program will give you a notice for Array to string conversion.
b) Objects: As the new version of PHP5 announce the support for OOPS(object oriented programming), the need of modular code is now seems complete with this new addition. It guarantees the clean and reusable code. Now developers can bind their code in structural way to simplify the maintenance part of the code. However we can check a value whether it is an object or not with is_object() function in PHP.
Special types:
a) Resources: Resources in PHP refers to the PHP core function which provides their predefined functionality to a program. For instance when we need to connect to the database we use “mysql_connect” to connect with the database or “mysql_select_db” to select the database. So when we finished with database connectivity then all these resources is reclaimed by PHP. This kind of cleaning is a part of PHP core functionality. We can check for a value whether it is a resource or not with is_resource() function.
b) Null: Null refers to a variable that have no value in PHP. We can also check whether a variable has a value or not with is_null() function.
c) Callbacks: Callbacks in php is like a functions or object methods used by some specific functions. for Example:
<?php
$x = function myfun() {
echo "Welcome to PHP World";
}
somefun($x); // Callbacks are being performed here in this specific function.
?>
Note: All the data types are the part of the core functionality of PHP.
Uploaded by: Author