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.與類型相關的函數
').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i
').text(i)); }; $numbering.fadeIn(1700); }); }); 以上就介紹了php類型轉換,包含了php方面的內容,希望對PHP教學有興趣的朋友有幫助。