Home > Backend Development > PHP Tutorial > Summary of PHP data types_PHP tutorial

Summary of PHP data types_PHP tutorial

WBOY
Release: 2016-07-13 17:50:38
Original
831 people have browsed it

PHP has a total of 8 data types:

 类型名称  类型表示  取值
 bool  布尔型   true,false
 integer  整型 -2147483647-2147483648
 string  字符串型  字符串长度取决于机器内存
 float  浮点型  最大值1.8e308
 object  对象 通过new实例化 $obj=new person(); 
 array  数组类型  $arr=array(1,2,3,4,5,6);//一维数组
 resourse    
 null  空值  null
Type name

Type representation

Value

bool Boolean true,false
integer Integer type -2147483647-2147483648
string String type The string length depends on the machine memory
float Floating point type Maximum value 1.8e308
object Object Instantiated by new $obj=new person();
array Array type $arr=array(1,2,3,4,5,6);//One-dimensional array
resource
null Null value null

Boolean bool:
 转换  结果
 布尔型的false var_dump((bool) false)  bool(false)
 整型0  var_dump((bool) 0);  bool(false)
 浮点型0.0  var_dump((bool) 0.0);  bool(false)
 字符串‘0’ var_dump((bool) '0');  bool(false)
 空数组$arr=array(); var_dump((bool) $arr)  bool(false)
 不包含任何成员变量的空对象只在PHP4使用,PHP5中为true  bool(false)
 NULL或者尚未赋值的变量var_dump((bool) NULL)  bool(false)
 从没有任何标记(tags)的XML文档生成的SimpleXML 对象  bool(false)
For other types we can use (bool) or (boolean) for forced conversion eg: (bool)1=true; The following situations default to false during forced conversion: ​
Convert Results
Boolean false var_dump((bool) false) bool(false)
Integer type 0 var_dump((bool) 0); bool(false)
Floating point type 0.0 var_dump((bool) 0.0); bool(false)
String ‘0’ var_dump((bool) ‘0’); bool(false)
Empty array $arr=array(); var_dump((bool) $arr) bool(false)
Empty objects that do not contain any member variables are only used in PHP4 and are true in PHP5 bool(false)
NULL or a variable that has not yet been assigned a value var_dump((bool) NULL) bool(false)
SimpleXML object generated from an XML document without any tags bool(false)


The conversion result of string '0.0' is bool(true)
Note: -1 and other non-zero values ​​(whether positive or negative) are true

Integer type integer:
The range of integer type is -2147483647--2147483647. If the value exceeds this value, it will be automatically converted to float type
We can use echo PHP_INT_SZIE to output the word length of integer, which depends on the machine. echo PHP_INT_MAX outputs the maximum value of integer
There is no integer division operation in PHP. If you execute 1/2, it will produce float 0.5. If you want to achieve the integer division effect, you can use (int)(1/2)=0 or round(25/7)=4
Force conversion to integer type (int) or (integer) bool type true is converted to 1, false is converted to 0

Floating point type float:
Value range Maximum value: 1.8e308 I don’t know what the minimum value is? Please let the experts know
The word length of floating point numbers is also related to the machine. It seems that there is no PHP_FLOAT_SIZE. Please let me know how to get the length of floating point numbers

String type string:
4 ways to define strings:
1. Single quotes
2.Double quotes
3.heredoc syntax structure
4.nowdoc syntax structure (after PHP5.3.0)
Single quote
Single quotes define the original string, and everything inside is processed as a string. If the string contains single quotes, you can use escape
Double quotes
Strings defined by double quotes will parse some special characters (n, b) and variables
Instead of converting a variable to a string, you can place it in double quotes:
$num=10;
$str = "$num"; //$str is a string type 10
heredoc syntax structure
<< The string itself
Identifier
The identifier at the end must be at the beginning of the line, and the definition format of the identifier must also follow the rules defined by PHP. It can only contain numbers, letters, and underscores, and cannot start with a number or underscore
No other characters are allowed on which line of the end identifier. You can add a semicolon after the identifier. There can be no tabs or spaces before and after the semicolon. Otherwise, PHP will not be able to parse the identifier and will continue to search for the identifier. If If it is not found before the end of the file, an error will be generated
A heredoc is a double quote without double quotes, that is, it can contain double quotes without escaping, and it can parse special characters and variables
nowdoc syntax structure
<<<'Identifier'
The string itself
Identifier www.2cto.com
The start identifier of nowdoc must be enclosed in single quotes, and the end identifier and other rules are the same as heredoc
nowdoc means single quotes without single quotes. The string contained in nowdoc will be output as is, and the special characters and variables contained in it will not be parsed

If double quotes contain several situations in array variables
//We first define the following array
[php]
1. $arr=array(
2. 'one'=>array(
3. 'name'=>'jiangtong',
4. 'sex'=>'male'
5. ),
6. 'two'=>'zhaohaitao',
7. 'three'=>'fanchangfa'
8. );

The first element in the array above is two-dimensional, and the last two are one-dimensional. When we access one dimension, there are several ways:
[php]
1. echo "$arr[two]"//key has no single quotes
2. echo "$arr['two']"//key has single quotes, which will cause an error. If we change it to echo "{$arr['two']}"; the result can be correctly output
3. echo "{$arr[two]}"//There are double curly braces, but the key does not have single quotes. In this case, PHP will first look for the constant banana, and if so, replace it. Since there is no two constant, an error will occur.
It can be seen that when accessing a one-dimensional array, either the key is not added or the quotation marks are added (considering the third situation). If it is added, it will be enclosed by {}, and it can be omitted at all.
Multidimensional array test
[php]
1. echo "$arr[one][name]"; //The output result is Array[name]. It can be seen that it returns an array and only parses one dimension
2. echo "{$arr['one']['name']}";//The output result is jiangtong
Braces must be used when accessing multi-dimensional arrays, and the key must be enclosed in double quotes

Array type
As mentioned in the string type, it is legal if it is enclosed in curly brackets without adding key quotes. Then PHP will first look for whether there is a constant named key. If there is a constant named key, it will be replaced. If not, it will be generated. A warning that a constant cannot be found is treated as an ordinary string, so it is recommended that you always add single quotes
Convert to an array using (array)type or array(type), but if you convert a value with only one value to an array, you will get an array of one element, and the subscript is 0. Converting NULL to an array will get an empty array
We can change the value of the array when traversing the array. In PHP5.0 and above, we can use references
[php]
1. $arr=array('a','b','c','d','e' );
2. foreach($arr as &$value)
3. {
4. $value=strtoupper($value);
5. echo $value;
6. }//Output result ABCDE

Object object type
To instantiate an object, we use new to add a person class. We can use the following methods

[php]
1. $objPerson=new person();

Coercion (object): If an object is converted into an object, it will not have any changes. For any other value, an object of stdclass will be instantiated. If the value is NULL, an empty object will be instantiated. If When the array is converted into an object, the key of the array will be used as the attribute of the object, and the value will be the attribute value. For other types of values, the member variable named scalar contains the value
[php]
1. $arr=array('one'=>'a','two'=>'b' );
2. $obj=(object)$arr;
3. echo $obj->one //The output result is a;
Note: This is an array of keys. If there is no array of character keys, I don’t know how to access it. If anyone knows, I hope you can tell me, thank you.
For other values ​​
[php]
1. $obj1=(object)'jiang';
2. echo $obj1->scalar;//output result jiang

NULL empty type
null is not case-sensitive. The NULL type has only one value, indicating that a variable has no value. The variable in the following three situations is considered NULL
1. Is assigned a value of NULL
2. Has not been assigned a value yet
3. Being unset();


Excerpted from jt521xlg’s column

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/478264.htmlTechArticlePHP has a total of 8 data types: Type name Type represents value bool Boolean true, false integer Integer -2147483647 -2147483648 string The length of the string string depends on the machine...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template