String, Integer, Float, Boolean, Array, Object, NULL.
A string is a sequence of characters, like "Hello world!".
You can put any text in single and double quotes:
<?php $x = " Hello world! " ; echo $x ; echo " <br> " ; $x = ' Hello world! ' ; echo $x ; ?>
An integer is a number without decimals.
Integer rules:
The integer must have at least one digit (0-9)
Integers cannot contain commas or spaces
Integers have no decimal point
Integers can be positive or negative
Integers can be specified in three formats: decimal, hexadecimal (prefixed by 0x) or octal (prefixed by 0).
In the following examples we will test different numbers.
The PHP var_dump() function returns the data type and value of the variable:
<?php $x = 5985 ; var_dump ( $x ) ; echo " <br> " ; $x = - 345 ; // Negative number var_dump ( $x ) ; echo " <br> " ; $x = 0x8C ; // Hexadecimal number var_dump ( $x ) ; echo " <br> " ; $x = 047 ; // octal number var_dump ( $x ) ; ?>
Floating point numbers are numbers with a decimal part, or exponential form.
In the following examples we will test different numbers. The PHP var_dump() function returns the data type and value of the variable:
<?php $x = 10.365 ; var_dump ( $ x ) ; echo " <br> " ; $x = 2 .4 e3 ; var_dump ( $x ) ; echo " <br> " ; $x = 8 E - 5 ; var_dump ( $x ) ; ?>
Boolean type can be TRUE or FALSE.
$x=true;$y=false;
Boolean type is usually used for conditional judgment. You will learn more about conditional control in the following chapters.
Arrays can store multiple values in one variable.
In the following example, an array is created and then the PHP var_dump() function is used to return the array's data type and value:
<?php $cars = array ( " Volvo " , " BMW " , " Toyota " ) ; var_dump ( $cars ) ; ?>
You'll learn more about arrays in the following chapters.
Object data types can also be used to store data.
In PHP, objects must be declared.
First, you must declare the class object using the class keyword. Classes are structures that can contain properties and methods.
Then we define the data type in the class and then use the data type in the instantiated class:
<?php class Car { var $color ; function __construct ( $color = " green " ) { $this -> color = $color ; } function what_color ( ) { return $this -> color ; } } ?>
In the above example, the PHP keyword this is a pointer to the current object instance and does not point to any other object or class.
You'll learn more about objects in the following chapters.
A NULL value means the variable has no value. NULL is a value of data type NULL.
The NULL value indicates whether a variable has a null value. It can also be used to distinguish between data null values and NULL values.
You can clear variable data by setting the variable value to NULL:
<?php $x = " Hello world! " ; $x = null ; var_dump ( $x ) ; ?>