Automatic conversion and forced conversion of php data types
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 '美女美女我愛(ài)你'; }else{ echo '鳳姐鳳姐愛(ài)死我,執(zhí)行假區(qū)間咯'; } ?>
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 //浮點(diǎn)類型的的0,換成其他值試試 $bool = 0.0; if($bool){ echo '美女美女我愛(ài)你'; }else{ echo '鳳姐鳳姐愛(ài)死我,執(zhí)行假區(qū)間咯'; } ?>
3, the empty string is false, as long as there is a space in it, it is considered true.
<?php //空字符串,中間沒(méi)有空格喲。實(shí)驗(yàn)完加個(gè)空格試試 $str = ''; if($str){ echo '美女美女我愛(ài)你'; }else{ echo '鳳姐鳳姐愛(ài)死我,執(zhí)行假區(qū)間咯'; } ?>
4, 0 in the string, is also considered false. Everything else is true
<?php //0這個(gè)字符串喲,試試其他值看看 $str = '0'; if($str){ echo '美女美女我愛(ài)你'; }else{ echo '鳳姐鳳姐愛(ài)死我,執(zhí)行假區(qū)間咯'; } ?>
5. An empty array is also considered false. As long as there is a value in it, it is true.
<?php //這個(gè)數(shù)組當(dāng)中啥也沒(méi)放 $arr = array(); if($arr){ echo '美女美女我愛(ài)你'; }else{ echo '鳳姐鳳姐愛(ài)死我,執(zhí)行假區(qū)間咯'; } ?>
6, empty is also false
<?php //聲明了一個(gè)空的變量$bool $bool = null; if($bool){ echo '美女美女我愛(ài)你'; }else{ echo '鳳姐鳳姐愛(ài)死我,執(zhí)行假區(qū)間咯'; } ?>
7, resources that are not successful are also false
<?php //下面這段代碼會(huì)顯示警告,可忽略。暫時(shí)只需要對(duì)著實(shí)驗(yàn)知道效果即可:未聲成功的資源也為假 //下面這一塊了解意思就行:打開(kāi)adasfasfasfdsa.txt這個(gè)不存在的文件 $res = fopen('adasfasfasfdsa.txt','r'); if($res){ echo '美女美女我愛(ài)你'; }else{ echo '鳳姐鳳姐愛(ài)死我,執(zhí)行假區(qū)間咯'; } ?>
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 //布爾變整型參與運(yùn)算 $fo = true; $result = $fo + 10; //$result 結(jié)果為整型的11,因?yàn)?fo布爾的true變?yōu)榱? //如果$fo的值為0 var_dump($result); //字符串類型 $str = '419不要愛(ài)'; $result = $str + 1; //結(jié)果為420。因?yàn)閷?str變?yōu)榱苏偷?19參與運(yùn)算 //將419放在字符串中間和結(jié)尾試試 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); //看看結(jié)果是不是變了? var_dump($result); //鴨脖子為整型的5 $yabozi = 5; $re = floatval($yabozi); var_dump($re); //定義整型的變量 $yabozi = 23; $bian = strval($yabozi); //強(qiáng)制變成字符串試試 var_dump($bian); ?>
Before the variable Add the type in (), convert it and assign it to other variables
<?php //定義一個(gè)變量,我們來(lái)變化一下試試 $transfer = 12.8; //把浮點(diǎn)變?yōu)檎? $jieguo = (int)$transfer; var_dump($jieguo); //把浮點(diǎn)變?yōu)椴紶? $jieguo = (bool) $transfer; var_dump($jieguo); //把布爾變整型 $bool = true; $jieguo = (int)$bool; var_dump($jieguo); //把浮點(diǎn)變數(shù)組 $fo = 250; $jieguo = (array)$fo; var_dump($jieguo); //其他的操作方式,按照文字總結(jié)的規(guī)律你來(lái)試試 ?>
settype(variable, type) directly change the variable itself
<?php //定義浮點(diǎn)變?yōu)檎? $fo = 250.18; //settype第二個(gè)參數(shù)是int,你實(shí)驗(yàn)的時(shí)候要記得第二個(gè)參數(shù)要為字符串類型 settype($fo,'int'); //輸出看看結(jié)果 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 //小可愛(ài),記得自己做實(shí)驗(yàn),驗(yàn)證上面的9點(diǎn)喲 $t=12.9; settype($t,'int'); var_dump($t); ?>
set Pronunciation: [s?t]
Explanation: Set
type Pronunciation: [ta?p]
Explanation: type, method