PHP has various data types to perform a different kind of logical entry.
PHP Support following data type.
1) String: The string is the text you use in the double or single quote to specify the value of the variable. For example, the default text “Hello World” represents the string.
Example :
[php]<?php $x = "Hello world!"; $y = 'Hello world!'; echo $x; echo "<br>"; echo $y; ?>[/php]
2) Integer: The integer data type contains non-decimal number ranging between -2,147,483,648 and 2,147,483,647.
[php]<?php $x = 5985; var_dump($x); ?> [/php]
In the above code, we have used the variable $x and it has the integer value 5985. To run this code we are using the var_dump() function.
The var_dump()
is used to present the data type. In this case, the data type is an integer (int) and given the value of the variable. When operating this code you get the following result.
int(5985)
The int is the short form of word integer.
There are certain rules you need to follow to avoid errors.
– The integer must contain at least one digit to perform the execution.
– You can’t use a decimal point in the integer for e.g. 10.5 or 2.5 etc. Php script will not run if the integer has the decimal point.
– An integer can be positive or negative.
– You are only allowed to use three formats to specify the integer. Decimal (10-based), hexadecimal (16-based – prefixed with 0x) or octal (8-based – prefixed with 0)
3) Float: The float is the data type specifies the variable in the decimal number for e.g. 10.5 or 15.7. These numbers are not complete numbers. You can use the float data type when you are using decimal numbers in the variable.
Example:
[php]<?php $x = 10.365; var_dump($x); ?> [/php]
In the above code, we have used the decimal number 10.365 as float value for the variable $x
.
When executed the code, it will return the following result.
float(10.365)
4) Boolean: The boolean value shows two possible results: Yes or No. The boolean data type is used to verify certain query in a simple way by executing the outcome positive or negative. If the query runs as per the logic then the boolean value return the Yes result, else it will show the negative outcome. Boolean mostly use in conditional testing of two things.
Example :
[php]$x = true; $y = false;[/php]
5) Array: The array data type is most useful variable to store multiple information in one variable. It is frequently used for storing data.
[php]<?php $cars = array("Volvo","BMW","Toyota"); var_dump($cars); ?> [/php]
The code will generate the following result.
[php]array(3) { [0]=> string(5) "Volvo" [1]=> string(3) "BMW" [2]=> string(6) "Toyota" }[/php]
In the above code, the $cars variable is having the array data type. The array has the three value properties Volvo, BMW, and Toyota. You can use unlimited value properties in the array function.
6) Object: The object is the data type that used to store data and information on how to process the data. It uses the logic to run the code defined by users.
While using the object you must declare a class of object. You can use the class keyword.
Example:
[php]<?php class Car { function Car() { $this->model = "VW"; } } // create an object $herbie = new Car(); // show object properties echo $herbie->model; ?>[/php]
The above code generates the following result in the browser.
VW
7) NULL:
The NULL data type holds no value at all. It indicates the data type doesn’t have any information. It is completely empty.
Example:
[php]<?php $x = "Hello world!"; $x = null; var_dump($x); ?>[/php]
The code will produce below result.
[php]NULL[/php]