php data types
Data type
var_dump(): The function is to determine the type and length of a variable, and output the value of the variable. If the variable has a value, the value of the variable is lost and the data type is returned.
String, Integer, Float, Boolean, Array, Object, NULL resource.
PHP String
A string is a sequence of characters, like "Hello world!".
You can put any text in single and double quotes:
Example
<?php $x = "Hello world!"; echo $x; echo "<br>"; $x = 'Hello world!'; echo $x; ?>
---------------- -------------------------------------------------- -
PHP Integer
An integer is a number without decimals.
Integer rules:
· using ’ ’ s ’ s ’ s ’ s ‐ out to be digits (0-9)
The decimal point is
· The integer can be positive or negative
· The integer 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; // 负数 var_dump($x); echo "<br>"; $x = 0x8C; // 十六进制数 var_dump($x); echo "<br>"; $x = 047; // 八进制数 var_dump($x); ?>
------------------------ -----------------------------------------------
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
<?php $x = 10.365; var_dump($x); echo "<br>"; $x = 2.4e3; var_dump($x); echo "<br>"; $x = 8E-5; var_dump($x); ?>
----------------------- --------------------------------------------------
PHP Boolean bool
For other types, we can use (bool) or (boolean) for forced conversion eg: (bool) 1=true;
Note: -1 and other non-zero values (regardless of positive or negative) are true
------------------ --------------------------------------------------
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
<?php $cars=array("Volvo","BMW","Toyota"); var_dump($cars); ?>
------ -------------------------------------------------- -----------
PHP Object
The object data type 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:
Example
<?php class Car { var $color; function Car($color="green") { $this->color = $color; } function what_color() { return $this->color; } } ?>
In the above example, the PHP keyword this points to the current A pointer to an object instance that does not point to any other object or class.
You will learn more about objects in the following chapters.
-------------------------------------------------- -----------------------
PHP NULL value
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
<?php $x="Hello world!"; $x=null; var_dump($x); ?>
PHP resource value
Resource
Resource is a special variable type that saves a reference to an external resource: such as an open file, database connection, graphics canvas area, etc.
Resources are created and used through specialized functions.
Example:
<?php if(!file_exists("test.txt")){ $fh = fopen("test.txt","w"); //打开文件 echo get_resource_type($fh); // 输出:stream fclose($fh); //关闭文件 } ?>
Convert to resource
Since the resource type variable holds special handles for opening files, database connections, graphics canvas areas, etc. , so other types of values cannot be converted to resources.
Release resources
Since the PHP4 Zend engine has introduced a resource counting system, it can automatically detect that a resource is no longer referenced (same as Java). In this case, all external resources used by this resource will be released by the garbage collection system. Therefore, it is rarely necessary to manually free memory using some free-result function.
Note: Persistent database connections are special, they will not be destroyed by the garbage collection system.