This session, I will explain in detail about PHP Data Types. Variables contained in PHP can store data of different types, and different data types can do different things. The concept of variables in PHP is actually almost the same as other programming languages.
But to be more complete, PHP supports the following data types:
- String
- Integer
- Float (floating point numbers – also called double)
- Boolean
- Array
- Object
- NULL
- Resource
[the_ad id=”651″]
PHP String
A string is an arrangement of characters, similar to “Hello world!”.
A string can be any content inside statements. You can utilize single or twofold statements. Example is :
1 2 3 4 5 6 7 |
<?php $x = "Hello world!"; $y = 'Hello world!'; echo $x; echo "<br>"; echo $y; ?> |
PHP Integer
A whole number information type is a non-decimal number between – 2,147,483,648 and 2,147,483,647.
Rules for numbers:
- A number must have at any rate one digit
- A number must not have a decimal point
- A number can be either positive or negative
- Numbers can be determined in: decimal (base 10), hexadecimal (base 16), octal (base 8), or double (base 2) documentation
In the accompanying model $x is a number. The PHP var_dump() work restores the information type and worth. See the example below:
1 2 3 4 |
<?php $x = 5985; var_dump($x); ?> |
PHP Float
A buoy (skimming point number) is a number with a decimal point or a number in exponential structure.
In the accompanying model $x is a buoy. The PHP var_dump() work restores the information type and worth. See the example below:
1 2 3 4 |
<?php $x = 10.365; var_dump($x); ?> |
PHP Boolean
A Boolean represents two possible states: TRUE or FALSE.
1 2 |
$x = true; $y = false; |
Booleans are regularly utilized in restrictive testing. You will become familiar with restrictive testing in a later part of this instructional exercise.
PHP Array
An array stores multiple values in one single variable.
In the following example $cars is an array. The PHP var_dump() function returns the data type and value. See the example below:
1 2 3 4 |
<?php $cars = array("Volvo","BMW","Toyota"); var_dump($cars); ?> |
You will become familiar with much progressively about clusters in later parts of this instructional exercise.
PHP Object
An article is an information type which stores information and data on the most proficient method to process that information.
In PHP, an article must be expressly pronounced.
First we should proclaim a class of item. For this, we utilize the class watchword. A class is a structure that can contain properties and techniques. See the example below:
1 2 3 4 5 6 7 8 9 10 11 |
<?php class Car { function Car() { $this->model = "VW"; } } // create an object $herbie = new Car(); // show object properties echo $herbie->model; ?> |
PHP NULL Value
NULL is an exceptional information type which can have just one worth: NULL.
A variable of information type NULL is a variable that has no worth relegated to it.
Tip: If a variable is made without a worth, it is consequently allocated an estimation of NULL.
Factors can likewise be exhausted by setting the incentive to NULL. See the example below:
1 2 3 4 5 |
<?php $x = "Hello world!"; $x = null; var_dump($x); ?> |
PHP Resource
The uncommon asset type isn’t a real information type. It is the putting away of a reference to capacities and assets outer to PHP.
A typical case of utilizing the asset information type is a database call.
We won’t discuss the asset type here, since it is a propelled theme.