PHP data types
Data type: It is a division of data classification.
Includes: String, Integer, Float, Boolean, Array, Object, NULL.
PHP String
A string is a sequence of characters, like "Hello world!".
There are three ways to declare a string in the PHP language:
1. Use single quotes to declare
2. Use double quotes to declare
3. Use Character delimiter declaration (used when a very large string needs to be entered)
Single quote declaration (use English half-foot single quotes to wrap the string)
Double quotation mark declaration
Character delimiter declaration
Write three less than signs (<<<) after the equal sign after the variable
Then write characters after (<<<) (English uppercase characters are recommended). As in the following example: ABC
Then wrap the line and write any characters you want
After writing, go to the top line. At the beginning of the line, write the characters after (<<<) and a semicolon. As in the following example: ABC
Hello Word ABC; echo $y; ?>
PHP integer type
Integer type is the integer that everyone learns in mathematics.
Integer rules:
• Integers must have at least one digit (0-9)
• Integers cannot contain commas or spaces
• Integers are not 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:
Example
"; $x = -345; // 负数 var_dump($x); echo "
"; $x = 0x8C; // 十六进制数 var_dump($x); echo "
"; $x = 047; // 八进制数 var_dump($x); ?>
Try it »
Note: Just learn how to declare a decimal integer. Everyone only needs to understand the octal and hexadecimal systems.
var_dump() is a function. Insert variables between brackets (). This function will print out the data type and also display the length and value of the variable accordingly.
PHP Floating point type
Floating point numbers are numbers with a decimal part, or in 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:
Example
"; $x = 2.4e3; var_dump($x); echo "
"; $x = 8E-5; var_dump($x); ?>
Try it»
PHP Boolean
The Boolean type is: true and false. The translation of true and false in English is:
true (true)
false (false)
Boolean types are usually used Judgment based on conditions. Do not put quotes around true and false.
PHP Array
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 data type and value of the array:
Example
Try it out»
You will learn more about arrays in the following chapters.
PHP Object
The object data type can also be used to store data.
In PHP, objects must be declared.
First, you must declare a 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:
Example
color = $color; } function what_color() { return $this->color; } } ?>
Try it »
Above The PHP keyword this in the instance is a pointer to the current object instance and does not point to any other object or class.
You will learn more about objects in the following chapters.
PHP NULL value
The NULL value indicates that 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:
Example
Try it »
Next we will explain the two and null Related functions, these two functions are very commonly used:
empty
empty() can pass a variable into the middle of the brackets. If the value of this variable is false or null, it returns true.
The above example shows that $apple is null. Place the apple in the middle of empty. The result is a true interval.
if...else is a conditional statement, we will learn in the following chapters if...else
isset
isset() can Pass one or more variables between the brackets, separated by commas. As long as there is a variable that is null, it returns false. Otherwise, returns true.