String, Integer, floating-point number, logical, array, object, NULL.
PHP string
The string is a sequence of characters, such as "Hello world!".
The string can be any text within quotation marks. You can use single or double quotation marks:
<?php $x = "Hello world!"; echo $x; echo "<br>"; $x = ' Hello world! '; echo $x;? >
PHP integer
Integers are numbers that have no decimals.
Integer rule:
- Integers must have at least one number (0-9)
- Integers cannot contain commas or spaces
- Integers cannot have decimal points
- Integers can be both positive and negative
- Integers can be specified in three formats: decimal, hexadecimal (prefix 0x), or octal (prefix 0)
In the example below, we will test for different numbers. PHP Var_dump () returns the data type and value of the variable:
Instance
<?php $x = 5985; var_dump (///Negative var_dump//16 binary number var_dump (//Eight binary var_dump($x);? >
Post-run output
Int (5985)
Int (-345)
Int (140)
Int (39)
PHP floating-point number
Floating-point numbers are digits that have decimal or exponential form.
In the example below, we will test for different numbers. PHP Var_dump () returns the data type and value of the variable:
Instance
<?php $x = 10.365;var_dump ($x); echo "<br>"; $x = 2.4e3;var_dump ($x); echo "<br>"; $x = 8e-5;var_dump ($x);? >
Format of the output
Float (10.365)
Float (2400)
Float (8.0E-5)
PHP logic
The logic is true or false.
$x =true; $y =false;
Logic is often used for conditional testing. You'll learn more about conditional testing in the chapters later in this tutorial.
PHP arrays
An array stores multiple values in a variable.
In the following example, we will test different arrays. PHP Var_dump () returns the data type and value of the variable:
Instance
<?php $cars =array ("Volvo", "BMW", "SAAB"); Var_dump ($cars);? >
Results of the output
Array (3) {[0]=> string (5) "Volvo" [1]=> string (3) "BMW" [2]=> string (4) "SAAB"}
PHP Object
Objects are data types that store data and information about how to work with the data.
In PHP, you must explicitly declare an object.
First we must declare the class of the object. For this, we use the class keyword. A class is a structure that contains properties and methods.
We then define the data type in the object class, and then use this data type in an instance of the class:
Instance
<?phpclass car{ var $color; function Car ($color = "green") { $this->color = $color; } function What_color () { return $this->color; }}? >
PHP NULL Value
A special NULL value indicates that the variable has no value. Null is the only possible value for the data type NULL.
The null value indicates whether the variable is empty. Also used to differentiate between empty strings and null-valued databases.
You can empty the variable by setting the value to NULL:
<?php$x= "Hello world!"; $x =null;var_dump ($x);? >
The output is NULL
PHP data type