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

Summary analysis of PHP data types_PHP tutorial

WBOY
Release: 2016-07-21 15:07:04
Original
1051 people have browsed it

PHP共有8中数据类型:

类型名称 类型表示 取值
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

布尔型bool :
对于其他类型我们可以使用(bool)或者(boolean) 进行强制转换 eg:(bool)1=true;
以下几种情况在强制转化的时候默认为false:

转换 结果
布尔型的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)

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

Integer type integer:
The range of integer type is -2147483647--2147483647. If it 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 1/2 is executed, 0.5 of float will be generated. If you want to achieve integer division effect, you can use (int)(1/2)=0 or round(25/ 7)=4
Forcibly converted to integer type (int) or (integer) bool type true is converted to 1, false is converted to 0

Floating point type float:
The maximum value of the value range: 1.8e308. I don’t know what the minimum value is? Experts please tell me
The word length of floating point numbers is also related to the machine. It seems that there is no PHP_FLOAT_SIZE. Ah, experts please tell me 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 quotes
Single quotes define the original string, and everything inside is processed as a string. If the string contains single quotes, you can use escapes
Double quotes
The string defined by double quotes will To parse some special characters (n, b) and variables
, you can place the variable in double quotes instead of converting the variable into a string (string):
$num=10;
$str = "$num"; //$str is a string type 10
heredoc syntax structure
<<String itself
Identifier
The end mark The identifier must be at the beginning of a line, and the definition format of the identifier must also be in accordance with the rules defined by PHP. It can only contain numbers, letters, and underscores, and cannot start with a number or underscore.
ends with the identifier. Which line does not allow other characters? , you can add a semicolon after the identifier, and there must 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 it is not found before the end of the file, it will generate An error
heredoc is a double quote without double quotes, that is, it can contain double quotes without escaping, and can parse special characters and variables
nowdoc syntax structure
<<< 'Identifier'
The string itself
The start identifier of nowdoc must be enclosed in single quotes, the end identifier and other rules are the same as heredoc
nowdoc is a single quote without single quotes, nowdoc contains The string will be output as it is, and the special characters and variables contained in it will not be parsed

If the double quotes contain several situations in array variables
//We first define the following array
Copy code The code is as follows:

[php]
$arr=array(
'one'=>array(
'name'=>'jiangtong',
'sex'=>'male'
),
'two'=>'zhaohaitao',
'three'= >'fanchangfa'
);

The above is the first element in the array that is two-dimensional, and the last two are one-dimensional. When we access one dimension, there are several ways: :
Copy code The code is as follows:

[php]
echo "$arr[two]"//key There is no single quotation mark
echo "$arr['two']"//If the key has single quotation marks, an error will occur. If we change it to echo "{$arr['two']}";, the result can be output correctly
echo "{$arr[two]}"//There are double 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 with

, because there are no two constants, an error occurs
It can be seen that when accessing a one-dimensional array, either do not add keys or quotes (consider In the third case), if you add it, it will be enclosed by {}, you don’t need to add it at all.
Multidimensional array test
Copy code The code is as follows:

[php]
echo "$arr[one ][name]"; //The output result is Array[name]. It can be seen that it returns an array and only parses one dimension
echo"{$arr['one']['name']}";// The output result is jiangtong

Braces must be used when accessing multi-dimensional arrays. The key must be enclosed in double quotes

Array type
has been mentioned in the string type and is enclosed by braces If it is enclosed in key quotes, it is legal. Then PHP will first search to see if there is a constant named key. If there is a constant named key, it will be replaced. If not, a warning that the constant cannot be found will be generated before pressing the ordinary string. Processing, so it is recommended that you add single quotes
to convert it into an array and use (array) type or array (type). However, if you convert a value with only one value into an array, you will get an array of one element, and the subscript is 0. Converting NULL into an array will result in an empty array
We can change the value of the array when traversing the array. In PHP5.0 and above, we can use references
Copy code The code is as follows:

[php]
$arr=array('a','b','c','d','e' );
foreach($arr as &$value)
{
$value=strtoupper($value);
echo $value;
}//Output result ABCDE

Object object type
To instantiate an object, we use new to add a person class. We can use the following method
Copy code The code is as follows:

[php]
$objPerson=new person();

Forcing (object): If a If the object is converted to an object, then there will be no change. For any other value, an object of stdclass will be instantiated. If the value is NULL, an empty object will be instantiated. If the array is converted to an object, the key of the array will be used as Attributes of the object, value is the attribute value, and for other types of values, the member variable named scalar contains the value
Copy code The code is as follows:

[php]
$arr=array('one'=>'a','two'=>'b' );
$obj=(object)$arr;
echo $obj->one //The output result is a;

Note: This is an array of keys. If there is no character key array, I don’t know how to access it. Who knows and hopes to tell Brother, thank you.
For other values
Copy code The code is as follows:

[php]
$obj1=(object) 'jiang';
echo $obj1->scalar;//Output result jiang

NULL 空类型
null大小写不敏感,NULL类型只有一个取值,表示一个变量没有值,下面三种情况变量被认为为NULL
1.被赋值为NULL
2.尚未被赋值
3.被unset();

PHP type comparison tables
Comparisons of $x with PHP functions
Expression gettype() empty() is_null() isset() boolean :if($x)
$x = ""; string TRUE FALSE TRUE FALSE
$x = null NULL TRUE TRUE FALSE FALSE
var $x; NULL TRUE TRUE FALSE FALSE
$x is undefined NULL TRUE TRUE FALSE FALSE
$x = array(); array TRUE FALSE TRUE FALSE
$x = false; boolean TRUE FALSE TRUE FALSE
$x = true; boolean FALSE FALSE TRUE TRUE
$x = 1; integer FALSE FALSE TRUE TRUE
$x = 42; integer FALSE FALSE TRUE TRUE
$x = 0; integer TRUE FALSE TRUE FALSE
$x = -1; integer FALSE FALSE TRUE TRUE
$x = "1"; string FALSE FALSE TRUE TRUE
$x = "0"; string TRUE FALSE TRUE FALSE
$x = "-1"; string FALSE FALSE TRUE TRUE
$x = "php"; string FALSE FALSE TRUE TRUE
$x = "true"; string FALSE FALSE TRUE TRUE
$x = "false"; string FALSE FALSE TRUE TRUE
Loose comparisons with ==
TRUE FALSE 1 0 -1 "1" "0" "-1" NULL array() "php" ""
TRUE TRUE FALSE TRUE FALSE TRUE TRUE FALSE TRUE FALSE FALSE TRUE FALSE
FALSE FALSE TRUE FALSE TRUE FALSE FALSE TRUE FALSE TRUE TRUE FALSE TRUE
1 TRUE FALSE TRUE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE
0 FALSE TRUE FALSE TRUE FALSE FALSE TRUE FALSE TRUE FALSE TRUE TRUE
-1 TRUE FALSE FALSE FALSE TRUE FALSE FALSE TRUE FALSE FALSE FALSE FALSE
"1" TRUE FALSE TRUE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE
"0" FALSE TRUE FALSE TRUE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE
"-1" TRUE FALSE FALSE FALSE TRUE FALSE FALSE TRUE FALSE FALSE FALSE FALSE
NULL FALSE TRUE FALSE TRUE FALSE FALSE FALSE FALSE TRUE TRUE FALSE TRUE
array() FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE TRUE TRUE FALSE FALSE
"php" TRUE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE
"" FALSE TRUE FALSE TRUE FALSE FALSE FALSE FALSE TRUE FALSE FALSE TRUE


Strict comparisons with ===
TRUE FALSE 1 0 -1 "1" "0" "-1" NULL array() "php" ""
TRUE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
1 FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
0 FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
-1 FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
"1" FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE
"0" FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE
"-1" FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE
NULL FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE
array() FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE
"php" FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE
"" FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327569.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 memory...
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