Home >Backend Development >PHP Tutorial >PHP type conversion php7 PHP environment construction PHP from entry to proficiency
php is a weakly typed language. During the operation, the data will be converted into data that can be processed by the operator according to the operator.
1. Automatic conversion
<code><span>$a</span> = <span>"3"</span>; <span>$b</span> = <span>"5"</span>; <span>$c</span> = <span>$a</span> + <span>$b</span> ; var_dump(<span>$c</span>);<span>//</span><span>8</span> integer </code>
<code>3. 连接运算符. 转成字符串类型 </code>
<code><span>$str</span> = <span>'123'</span>.<span>true</span>.<span>'abc'</span>;<span>//</span><span>1231</span>abc string </code>
<code>4. 比较运算符,分几种情况 </code>
①Comparison of two numbers – normal situation
②If one of the data is of Boolean type, convert them all to Boolean type for comparison true>false
③If there is no Boolean type in the data, but one of the data is a number, convert it to a number and compare
④ Both sides are numeric strings, convert them into numbers for comparison
⑤Both sides of the comparison operator are string types. ASCII values are compared sequentially starting from the first character. The subsequent comparison stops if the value is larger.
Some conversion rules:
“12” –> 12
“1.2” –> 1.2
"1.2a" - –> 1.2
"a1.2" —> 0
"1.1a1" —> 1.1
"0" —> false
"1" —> true
0 —> false
"0.0" —> true
"abc" —> 0
"" —> 0
"" - >false
<code><span><span><?php </span>header(<span>"content-type:text/html;charset=utf-8"</span>); <span>$name</span> = <span>"tom"</span>; <span>if</span>(<span>$name</span>) { <span>echo</span><span>'$name为真'</span>.<span>'<hr>'</span>; }<span>else</span> { <span>echo</span><span>'$name为假'</span>.<span>'<hr>'</span>; } <span>//1.在算数运算符中的类型转换 :会转换成数字进行计算</span><span>$a</span> = <span>"1.1"</span>;<span>//"1.1a" 会转换成1.1 , "a1.1"会转换成0</span><span>$b</span> = <span>"7"</span>; <span>$c</span> = <span>$a</span> + <span>$b</span> ; <span>echo</span><span>$c</span>,<span>" "</span>,getType(<span>$c</span>),<span>'<hr>'</span>;<span>//12 double</span><span>//连接运算符.其结果是字符串类型</span><span>$a</span> = <span>5</span>; <span>$b</span> = <span>true</span>;<span>// true 转换为 1 , false 转换成 0</span><span>$c</span> = <span>$a</span>.<span>$b</span> ; <span>echo</span><span>$c</span>,<span>" "</span>,getType(<span>$c</span>),<span>'<hr>'</span>;<span>//51 string</span><span>//比较运算符,其结果一定是true或false</span><span>$a</span> = <span>false</span> ; <span>$b</span> = <span>5</span>; <span>//如果两边至少一个布尔值,则把非布尔值数据转为布尔值比较,其结果类型也是布尔</span><span>$c</span> = (<span>$a</span> $b</span>);<span>//这里$b转为true , $c = false <span>echo</span><span>$c</span>,<span>" "</span>,getType(<span>$c</span>),<span>'<hr>'</span>;<span>//1 boolean</span><span>//比较运算符中,两边不存在布尔类型但存在数字,则都转为数字类型比较</span><span>$a</span> = <span>5</span> ; <span>$b</span> = <span>"abc"</span>;<span>//$b 要转为数字比较,这里为0 </span><span>$c</span> = (<span>$a</span> > <span>$b</span>); <span>echo</span><span>$c</span>,<span>" "</span>,getType(<span>$c</span>),<span>'<hr>'</span>;<span>//1 boolean</span><span>//比较运算符中两边都是字符串类型</span><span>$a</span> = <span>"abc"</span> ; <span>$b</span> = <span>"abd"</span>; <span>$c</span> = (<span>$a</span> $b</span>);<span>//从首个字符开始依次比较ASCII值,哪个大就停止后续比较</span><span>echo</span><span>$c</span>,<span>" "</span>,getType(<span>$c</span>),<span>'<hr>'</span>;<span>//1 boolean</span></span></code>
2. Force conversion
<code><span>$a</span> = <span>11.5</span>; <span>$b</span> = (int)<span>$a</span>;<span>//强制转换成int赋值给$b</span><span>echo</span><span>$a</span>.<span>" 类型:"</span>.getType(<span>$a</span>).<span>'<hr>'</span>;<span>//11.5 类型:double</span><span>echo</span><span>$b</span>.<span>" 类型:"</span>.getType(<span>$b</span>).<span>'<hr>'</span>;<span>//11.5 类型:double</span><span>$c</span> = (string)<span>$a</span>; <span>echo</span><span>$c</span>.<span>" 类型:"</span>.getType(<span>$c</span>).<span>'<hr>'</span>;<span>//11.5 类型:11.5 类型:string</span><span>$d</span> = (boolean)<span>$a</span>; <span>echo</span><span>$d</span>.<span>" 类型:"</span>.getType(<span>$d</span>).<span>'<hr>'</span>;<span>//1 类型:boolean</span><span>$e</span> = (float)<span>$b</span>;<span>//强制转换成浮点数</span><span>echo</span><span>$e</span>.<span>" 类型:"</span>.getType(<span>$e</span>).<span>'<hr>'</span>;<span>//11 类型:double</span>setType(<span>$c</span>,<span>"float"</span>); <span>echo</span><span>$c</span>.<span>" 类型:"</span>.getType(<span>$c</span>).<span>'<hr>'</span>;<span>//11.5 类型:double</span></code>
3. Type-related functions
The above introduces the type conversion of PHP, including PHP content. I hope it will be helpful to friends who are interested in PHP tutorials.