Summary analysis of PHP data types_PHP tutorial
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
<<
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
[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: :
[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
[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
[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
[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
[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
[php]
$obj1=(object) 'jiang';
echo $obj1->scalar;//Output result jiang
NULL 空类型
null大小写不敏感,NULL类型只有一个取值,表示一个变量没有值,下面三种情况变量被认为为NULL
1.被赋值为NULL
2.尚未被赋值
3.被unset();
PHP type comparison tables
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 |
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 |
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 |

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Validator can be created by adding the following two lines in the controller.
