The data types in PHP include strings, integers, floating point numbers, logic, arrays, objects, and NULL. Each type is explained below.
PHP String
A string is a sequence of characters, such as "Hello world!".
A string can be any text within quotes. You can use single or double quotes:
Example
<?php $x = "Hello world!"; echo $x; echo "<br>"; $x = 'Hello world!'; echo $x; ?>
PHP Integers
Integers are numbers without decimals.
Integer rules:
Integers must have at least one digit (0-9)
Integers cannot contain commas or spaces
Integers cannot have decimal points
Integers can be positive or negative
Integers can be specified in three formats: decimal, hexadecimal (prefix is 0x) or octal (prefix is 0)
In the following In the example, we will test different numbers. PHP var_dump() will return the data type and value of the variable:
Example
<?php $x = 5985; var_dump($x); echo "<br>"; $x = -345; // 负数 var_dump($x); echo "<br>"; $x = 0x8C; // 十六进制数 var_dump($x); echo "<br>"; $x = 047; // 八进制数 var_dump($x); ?>
PHP floating point number
Floating point number has decimal point or exponential form number.
In the example below we will test different numbers. PHP var_dump() will return 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); ?>
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 later in this tutorial.
PHP Array
Array stores multiple values in one variable.
In the following example, we will test different arrays. PHP var_dump() will return the data type and value of the variable:
Instance
<?php $cars=array("Volvo","BMW","SAAB"); var_dump($cars); ?>
PHP Object
The object is to store data and how to process the data The data type of the information.
In PHP, objects must be declared explicitly.
First we must declare the class of the object. For this we use the class keyword. A class is a structure containing properties and methods.
Then we define the data type in the object class and then use this data type in the instance of that class:
Instance
<?php class Car { var $color; function Car($color="green") { $this->color = $color; } function what_color() { return $this->color; } } ?>
PHP NULL value
The 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 distinguish empty strings from null value databases.
You can clear the variable by setting the value to NULL:
实例 <?php $x="Hello world!"; $x=null; var_dump($x); ?>
The above is the detailed content of What are the basic data types in php. For more information, please follow other related articles on the PHP Chinese website!