Heim > Artikel > Backend-Entwicklung > PHP学习(4)——数据类型
PHP 支持 8 种原始数据类型。
四种标量类型:(标量类型即为基本类型)
两种复合类型:
最后是两种特殊类型:
变量的类型通常不是由程序员设定的,确切地说,是由 PHP 根据该变量使用的上下文在运行时决定的。
如果想查看某个表达式的值和类型,用 var_dump() 函数。
如果只是想得到一个易读懂的类型的表达方式用于调试,用 gettype() 函数。要查看某个类型,不要用 gettype(),而用 is_type 函数。
例子:
<code><span><span><?php </span><span>$a_bool</span> = <span>TRUE</span>; <span>// a boolean</span><span>$a_str</span> = <span>"foo"</span>; <span>// a string</span><span>$a_str2</span> = <span>'foo'</span>; <span>// a string</span><span>$an_int</span> = <span>12</span>; <span>// an integer</span><span>$a_float</span> = <span>3.14</span>; <span>// a float(double)</span><span>echo</span> gettype(<span>$a_bool</span>).<span>"<br>"</span>; <span>// prints out: boolean</span><span>echo</span> gettype(<span>$a_str</span>).<span>"<br>"</span>; <span>// prints out: string</span><span>echo</span> gettype(<span>$an_int</span>).<span>"<br>"</span>; <span>// prints out: integer</span><span>echo</span> gettype(<span>$a_float</span>).<span>"<br>"</span>; <span>// prints out: double</span><span>// If this is an integer, increment it by four</span><span>if</span> (is_int(<span>$an_int</span>)) { <span>echo</span><span>"an_int = "</span>.<span>$an_int</span>.<span>"<br>"</span>; <span>$an_int</span> += <span>4</span>; <span>echo</span><span>"an_int = "</span>.<span>$an_int</span>.<span>"<br>"</span>; } <span>// If $bool is a string, print it out</span><span>// (does not print out anything)</span><span>if</span> (is_string(<span>$a_str</span>)) { <span>echo</span><span>"String: $a_str"</span>.<span>"<br>"</span>; } <span>echo</span> var_dump(<span>$a_float</span>, <span>$a_bool</span>, <span>$a_str</span>, <span>$an_int</span>); <span>?></span></span></span></code>
输出:
<code>boolean <span>string</span> integer <span>double</span> an_int = <span>12</span> an_int = <span>16</span> String: foo <span>float</span>(<span>3.14</span>) <span>bool</span>(<span>true</span>) <span>string</span>(<span>3</span>) <span>"foo"</span><span>int</span>(<span>16</span>)</code>
php手册中对gettype()的解释(请放大查看?):
每种类型的具体使用,请参考PHP的官方手册,我这里也只是抛砖引玉。
').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });以上就介绍了PHP学习(4)——数据类型,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。