PHP supports the following basic data types:
An integer is a number without decimals.
Rules for integers:
In the following example we will test different numbers.
The PHP var_dump() function returns the data type and value of variables:
<?php
$x = 5786; //positive number
var_dump($x);
echo "<br />";
$x = -321; //negative number
var_dump($x);
echo "<br />";
$x = 0xF; //hexadecimal number
var_dump($x);
echo "<br />";
$x = 011; //octal number
var_dump($x);
?>
A floating point number is a number with a decimal point or a number in exponential form.
In the following example we will test different numbers.
The PHP var_dump() function returns the data type and value of variables:
<?php
$x = 24.365;
var_dump($x);
echo "<br />";
$x = 1.5e3;
var_dump($x);
?>
A string is a sequence of characters, like "Hello world!".
A string can be any text inside quotes. You can use single '
or "
double quotes:
<?php
$name = "Hello world!"; //double quote
echo $name;
echo "<br />";
$name = 'Hello world!'; //single quote
echo $name;
?>
Booleans can be either TRUE or FALSE.
Booleans are often used in conditional testing. You will learn more about conditional testing in a later chapter of this tutorial.
<?php
$x = true;
$y = false;
?>
An array stores multiple values in one single variable.
In the following example we create an array, and then use the PHP var_dump() function to return the data type and value of the array:
You will learn a lot more about arrays in later chapters of this tutorial.
<?php
$language = array("C","C++","JAVA");
var_dump($language);
?>
An object is a data type which stores data and information on how to process that data.
In PHP, an object must be explicitly declared.
First we must declare a class of object. For this, we use the class keyword. A class is a structure that can contain properties and methods.
We then define the data type in the object class, and then we use the data type in instances of that class:
You will learn more about objects in a later chapter of this tutorial.
The special NULL value represents that a variable has no value. NULL is the only possible value of data type NULL.
The NULL value identifies whether a variable is empty or not. Also useful to differentiate between the empty string and null values of databases.
Variables can be emptied by setting the value to NULL:
<?php
$var1 = ""; //empty string
var_dump($var1);
echo "<br />";
$var2 = null; //null value
var_dump($var2);
?>
Certain built-in functions (such as database functions) return variables that have the type resource. They represent external Resources Value(such as database connections).
You will almost certainly not directly manipulate a resource variable, but frequently they are returned by functions and must be passed as parameters to other functions.
<?php
$con = mysqli_connect(host,username,password,dbname);
/*here mysqli_connect() function returns Resource Value(database connection)*/
?>
In the above example we store the connection in a variable ($con
)
Ask Question