php是弱类型语言,在运算中会根据运算符将数据转换成该运算符所能处理的数据。
1.自动转换
<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>
①两个数字比较–正常情况
②其中有一个数据是布尔型的,则都转成布尔型进行比较 true>false
③数据中没有布尔型的,但其中有一个数据是数字,则转成数字 比较
④两边都是数字字符串,转成数字来比较
⑤比较运算符中两边都是字符串类型,从首个字符开始依次比较ASCII值,哪个大就停止后续比较。
一些转换规则:
“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.强制转换
<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.与类型相关的函数
以上就介绍了php之类型转换,包括了php方面的内容,希望对PHP教程有兴趣的朋友有所帮助。