PHP is a completely weakly typed programming language in the PHP 5.x stage. The so-called weak type means that when declaring a variable, there is no need to specify the type of the variable. I want to declare an integer variable. I don’t have to write the type in front and then write the variable.
The performance of PHP 7 has been greatly improved. According to actual test results, the performance of PHP 7 has been improved by nearly 200% compared with PHP5.6. In some places in PHP 7, we can force the type to be specified, or declare variables without forcing the type to be specified.
Let’s talk about forced type conversion and automatic type conversion next.
[Default level] English words for automatic type conversion of Boolean values and forced type conversion
Automatic type conversion of Boolean values
Automatic type conversion , that is, the data type will automatically change to other types to participate in operations under certain circumstances. Automatic type conversion occurs when certain values are automatically converted during operations and judgments.
The following situation is automatic type conversion when judging Boolean value:
1, 0 of the integer type is false, and all other integer values are true
2, 0.0 for floating point, false for Boolean value. It is true as long as there is a non-zero value after the decimal point.
3, an empty string is false, as long as there is a space in it, it is considered true.
4, 0 in a string, is also treated as false. Everything else is true
5, and an empty array is also considered false. As long as there is a value in it, it is true.
6, empty is also false
7, resources that have not been declared successful are also false
Let’s experiment with the above principles one by one. result.
1, 0 for integer type is false, all other integer values are true
<?php //整型的0,换成整型的其他值试试 $bool = 0; if($bool){ echo '美女美女我爱你'; }else{ echo '凤姐凤姐爱死我,执行假区间咯'; } ?>
2, 0.0 for floating point, boolean value is false. It is true as long as there is a non-zero value after the decimal point.
<?php //浮点类型的的0,换成其他值试试 $bool = 0.0; if($bool){ echo '美女美女我爱你'; }else{ echo '凤姐凤姐爱死我,执行假区间咯'; } ?>
3, the empty string is false, as long as there is a space in it, it is considered true.
<?php //空字符串,中间没有空格哟。实验完加个空格试试 $str = ''; if($str){ echo '美女美女我爱你'; }else{ echo '凤姐凤姐爱死我,执行假区间咯'; } ?>
4, 0 in the string, is also considered false. Everything else is true
<?php //0这个字符串哟,试试其他值看看 $str = '0'; if($str){ echo '美女美女我爱你'; }else{ echo '凤姐凤姐爱死我,执行假区间咯'; } ?>
5. An empty array is also considered false. As long as there is a value in it, it is true.
<?php //这个数组当中啥也没放 $arr = array(); if($arr){ echo '美女美女我爱你'; }else{ echo '凤姐凤姐爱死我,执行假区间咯'; } ?>
6, empty is also false
<?php //声明了一个空的变量$bool $bool = null; if($bool){ echo '美女美女我爱你'; }else{ echo '凤姐凤姐爱死我,执行假区间咯'; } ?>
7, resources that are not successful are also false
<?php //下面这段代码会显示警告,可忽略。暂时只需要对着实验知道效果即可:未声成功的资源也为假 //下面这一块了解意思就行:打开adasfasfasfdsa.txt这个不存在的文件 $res = fopen('adasfasfasfdsa.txt','r'); if($res){ echo '美女美女我爱你'; }else{ echo '凤姐凤姐爱死我,执行假区间咯'; } ?>
Automatic type conversion of other types
Automatic type conversion can also occur during operation. It is the same as all the rules and opinions we summarize: summarize first, then experiment.
Only scalar operations will produce the following automatic type conversions:
<?php //布尔变整型参与运算 $fo = true; $result = $fo + 10; //$result 结果为整型的11,因为$fo布尔的true变为了1 //如果$fo的值为0 var_dump($result); //字符串类型 $str = '419不要爱'; $result = $str + 1; //结果为420。因为将$str变为了整型的419参与运算 //将419放在字符串中间和结尾试试 var_dump($result); ?>
Summary:
The true value of the Boolean value will become an integer or floating point 1 when participating in the operation. The false value of the Boolean value will become an integer or floating point 0 when participating in the operation. The beginning of the string is a character of the integer or floating point type. , will be converted into the corresponding type to participate in the operation
Forced type conversion
There are three methods of forced type conversion:
1. Use the following three functions to complete the type conversion, intval(), floatval(), strval()
2. Add () before the variable and write the type in it, convert it and assign it to other variables
3.settype(variable, Type) Directly change the quantity itself
Let’s experiment:
intval(), floatval(), strval()conversion
<?php $float = 1.23; $result = intval($float); //看看结果是不是变了? var_dump($result); //鸭脖子为整型的5 $yabozi = 5; $re = floatval($yabozi); var_dump($re); //定义整型的变量 $yabozi = 23; $bian = strval($yabozi); //强制变成字符串试试 var_dump($bian); ?>
Before the variable Add the type in (), convert it and assign it to other variables
<?php //定义一个变量,我们来变化一下试试 $transfer = 12.8; //把浮点变为整型 $jieguo = (int)$transfer; var_dump($jieguo); //把浮点变为布尔 $jieguo = (bool) $transfer; var_dump($jieguo); //把布尔变整型 $bool = true; $jieguo = (int)$bool; var_dump($jieguo); //把浮点变数组 $fo = 250; $jieguo = (array)$fo; var_dump($jieguo); //其他的操作方式,按照文字总结的规律你来试试 ?>
settype(variable, type) directly change the variable itself
<?php //定义浮点变为整型 $fo = 250.18; //settype第二个参数是int,你实验的时候要记得第二个参数要为字符串类型 settype($fo,'int'); //输出看看结果 var_dump($fo); ?>
[Try it] The following are the characteristics of forced type conversion. You can experiment with each item to see if it is correct:
1 .If converted to an integer, it will be an integer value of 0
2. If it is converted to a floating point, it will be a floating point value of 0
3. If it is converted to a string, it will be an empty string ''
4. If the floating point value 123.0 is converted to a string, it will be a string of 123
5. If the floating point value of 123.2 is converted to a string, it will be a string of 123.2
6. Floating point value No matter how big the decimal point is, it will be removed and the value after the decimal point will be discarded
7. If the string is converted to an integer, if the value is in front, the previous value will be taken out as Conversion value of integer type.
8.settype(variable,'null'); Equivalent to unset() a variable
9.$Target variable = (type)$ Operating variables will only change the type of the target variable , does not change the type of the original variable, Settype changes the original value
<?php //小可爱,记得自己做实验,验证上面的9点哟 $t=12.9; settype($t,'int'); var_dump($t); ?>
set Pronunciation: [sɛt]
Explanation: Set
type Pronunciation: [taɪp]
Explanation: type, method