Heim >Backend-Entwicklung >PHP-Tutorial >php之类型转换 php7 php环境搭建 php从入门到精通

php之类型转换 php7 php环境搭建 php从入门到精通

WBOY
WBOYOriginal
2016-07-29 08:55:171017Durchsuche

php是弱类型语言,在运算中会根据运算符将数据转换成该运算符所能处理的数据。

1.自动转换

  1. if($a | 数字) { 。。。} //if里面的变量或者数字会自动转换成布尔
  2. 算术运算符会将数据变为数字处理
<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.与类型相关的函数

  1. var_dump() :输出一个变量的完整信息
  2. getType():获取一个变量的类型名称,返回是一个类型名称字符串
  3. setType(变量,”目标类类型”); 设置一个变量的类型
  4. isset() 如果变量存在并且值不是 NULL 则返回 TRUE ,否则返回 FALSE 。
  5. empty 判断一个变量数据是否为空
  6. unset() 销毁一个变量
  7. is_int()
  8. is_numeric() —1 ,1.2 ,”1.2” ,”12” 都为真
  9. is_array() is_scalar—是否基本数据类型: string float int boolean
  10. is_null — 检测变量是否为 NULL

php要学哪些,php教程,php下载,php是什么,php文件怎么打开,php工程师,php7,php环境搭建,php从入门到精通

').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

以上就介绍了php之类型转换,包括了php方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn