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
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 |

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

查找方法:1、用strpos(),语法“strpos("字符串值","查找子串")+1”;2、用stripos(),语法“strpos("字符串值","查找子串")+1”。因为字符串是从0开始计数的,因此两个函数获取的位置需要进行加1处理。


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

Dreamweaver CS6
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.