[精选] 问一下,你是怎么样理解php是弱类型语言的?
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">举荐</span>关注</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">以下请在本公众号里操作</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;"><span style="color: black;">回复「<span style="color: black;">微X</span>群」</span></strong></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">加入PHP技术交流群,<span style="color: black;">一块</span>学习交流</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;"><span style="color: black;">回复「资料」</span></strong></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">免费获取 PHP 等语言学习视频课程。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;"><span style="color: black;">回复「文档」</span></strong></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">免费获取 PHP 等语言电子文档</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;"><span style="color: black;">回复「激活码」</span></strong></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">免费获取 phpStorm2021版 专属激活码。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;"><span style="color: black;">回复「商务合作」</span></strong></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">就可</span>获取合作方式与客服联系方式。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">相信不少<span style="color: black;">研发</span>人员对网络上的 0 "0" null false 的讲解司空见惯,了然于胸了。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">但我相信总有你忽略的细节,<span style="color: black;">或</span>从变量结构<span style="color: black;">方向</span>出发,该<span style="color: black;">怎样</span>解释。<span style="color: black;">那样</span><span style="color: black;">瞧瞧</span>下边的验证吧</span>。</p><span style="color: black;">echo</span> <span style="color: black;">php版本:</span>.PHP_VERSION; <span style="color: black;">//5.6.28</span>$a = <span style="color: black;">0</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">; </p>$b=<span style="color: black;">"0"</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">; </p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">$c= ; </p>$d= <span style="color: black;">null</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">; </p>$e = <span style="color: black;">false</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p><span style="color: black;">echo</span> <span style="color: black;">"5个变量-原始测试类型"</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p>var_dump($a);<span style="color: black;">//int 0</span>var_dump($b);<span style="color: black;">//string 0</span>var_dump($c);<span style="color: black;">//string </span>var_dump($d);<span style="color: black;">//null</span>var_dump($e);<span style="color: black;">//boolean false</span><span style="color: black;">echo</span> <span style="color: black;">"<h4>empty测试</h4>"</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p>var_dump(<span style="color: black;">empty</span>($a));<span style="color: black;">//true</span>var_dump(<span style="color: black;">empty</span>($b));<span style="color: black;">//true</span>var_dump(<span style="color: black;">empty</span>($c));<span style="color: black;">//true</span>var_dump(<span style="color: black;">empty</span>($d));<span style="color: black;">//true</span>var_dump(<span style="color: black;">empty</span>($e));<span style="color: black;">//true</span><span style="color: black;">echo</span> <span style="color: black;">"<hr>"</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p>var_dump(<span style="color: black;">isset</span>($a));<span style="color: black;">//true</span>var_dump(<span style="color: black;">isset</span>($b));<span style="color: black;">//true</span>var_dump(<span style="color: black;">isset</span>($c));<span style="color: black;">//true</span>var_dump(<span style="color: black;">isset</span>($d));<span style="color: black;">//【false】 见结论一</span>var_dump(<span style="color: black;">isset</span>($e));<span style="color: black;">//true</span><span style="color: black;">echo</span> <span style="color: black;">"<h4>(==)双等式测试</h4>"</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p>var_dump($a == $b);<span style="color: black;">//true</span>var_dump($a == $c);<span style="color: black;">//true</span>var_dump($a == $d);<span style="color: black;">//true</span>var_dump($a == $e);<span style="color: black;">//true !!</span>var_dump($b == $c);<span style="color: black;">//【false】见结论二</span>var_dump($b == $d);<span style="color: black;">//【false】见结论二</span>var_dump($b == $e);<span style="color: black;">//true</span>var_dump($c == $d);<span style="color: black;">//true</span>var_dump($c == $e);<span style="color: black;">//true</span><span style="color: black;">echo</span> <span style="color: black;">"<h4>(===)三等式测试</h4>"</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p>var_dump($a === $b);<span style="color: black;">//false</span>var_dump($a === $c);<span style="color: black;">//false</span>var_dump($a === $d);<span style="color: black;">//false</span>var_dump($a === $e);<span style="color: black;">//false</span>var_dump($b === $c);<span style="color: black;">//false</span>var_dump($b === $d);<span style="color: black;">//false</span>var_dump($b === $e);<span style="color: black;">//false</span>var_dump($c === $d);<span style="color: black;">//false</span>var_dump($c === $e);<span style="color: black;">//false</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;">总结:</strong></span><span style="color: black;"><span style="color: black;"><span style="color: black;"><span style="color: black;">针对</span> 【0 ;"0" ; ;null;false】五种类型</span><span style="color: black;">empty操作以上五个变量,都返回true</span><span style="color: black;">强等于(===)比较 都为false,同强语言结果</span><span style="color: black;">但<span style="color: black;">针对</span>(==)比较,需要<span style="color: black;">重视</span>string类型,<span style="color: black;">触及</span>到底层结构与类型转换</span></span></span><span style="color: black;"><strong style="color: blue;">结论一:关于变量类型的理解</strong></span><span style="color: black;"><span style="color: black;"><span style="color: black;">1.null为不存在之意:php底层的zval空间里(结构见下方)<span style="color: black;">无</span>存其value值,只存储了一个type标志其 IS_NULL(<span style="color: black;">因此</span>解释了 empty(null)=true,isset(null)=false ,isset()=true)</span><span style="color: black;">2.【0 ; "0" ; "" ; false 】:这四个为存在,php底层是开辟zval空间存储,有value,有type</span></span></span><span style="color: black;"><strong style="color: blue;">结论二:从底层结构理解</strong></span><span style="color: black;"><span style="color: black;">理解了上边结论一,<span style="color: black;">那样</span>下边的结论就容易理解了,还不太清楚的<span style="color: black;">能够</span><span style="color: black;">仔细</span>看下【<a style="color: black;">学习PHP底层就该学习PHP内核的存储机制</a></span><span style="color: black;"><span style="color: black;">】。</span></span></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;"><span style="color: black;">其中的【1.zval结构】,【2.zend_uchar type】部分</span><span style="color: black;">1 string 0与 string 不相等,(想一下就明白,同类型比较【1个长度】的字符串怎么可能 等于 【0个长度】 的字符串,zal的value结构里,对string值有len原始记录的)。</span></span></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;"><span style="color: black;">2 int 0 却和 string 空相等,(非同类形比较,php会做类型转换)</span><span style="color: black;">3 string 0 与 null 不相等</span><span style="color: black;">4 int 0 与 null 相等</span><span style="color: black;">说白了,<span style="color: black;">针对</span>php,碰到string "0" 的等式判断的时候,停顿<span style="color: black;">重视</span>一下就行。其他的等式判断正常,符合<span style="color: black;">公众</span>熟知的php<span style="color: black;">研发</span>思维。</span></span></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;"><strong style="color: blue;">php变量zval结构:</strong></span></span><span style="color: black;"><span style="color: black;">//存放变量的基本信息</span></span></p><span style="color: black;"><span style="color: black;">struct</span> _<span style="color: black;">zval_struct</span> {</span> <span style="color: black;">/* Variable information */</span> zvalue_value value; <span style="color: black;">/* 变量值<span style="color: black;">保留</span>在<span style="color: black;">这儿</span> 12字节*/</span> zend_uint refcount;<span style="color: black;">//4字节,变量引用计数器 </span> zend_uchar type; <span style="color: black;">/* active type变量类型 1字节*/</span>zend_uchar is_ref;<span style="color: black;">//<span style="color: black;">是不是</span>变量被&引用,0<span style="color: black;">暗示</span>非引用,1<span style="color: black;">暗示</span>引用,1字节 </span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}; </p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;"><span style="color: black;">//存放变量的值</span></span></span></p><span style="color: black;">typedef</span> <span style="color: black;">union</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> _zvalue_value { </p> <span style="color: black;">long</span> lval; <span style="color: black;">/* long value */</span> <span style="color: black;">double</span> dval; <span style="color: black;">/* double value */</span> <span style="color: black;"><span style="color: black;">struct</span> {</span> <span style="color: black;">char</span> *val; <span style="color: black;">//4字节</span> <span style="color: black;">int</span> len; <span style="color: black;">//4字节 </span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> } str; </p> HashTable *ht; <span style="color: black;">/* hash table value */</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> zend_object_value obj; </p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">} zvalue_value;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">原文链接:https://blog.csdn.net/ty_hf/article/details/70217267</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">点个赞</span></strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/mmbiz_gif/Ljib4So7yuWge7Mibiad1tV0iaF8zSD5gzicbYiaOtl6tmz4onkADe25mdeWcVhZlQ8Q0bH73nZAwICyJ4pPgJU3VrCg/640?wx_fmt=gif&tp=webp&wxfrom=5&wx_lazy=1" style="width: 50%; margin-bottom: 20px;"></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">再走吧</span></strong></p>
回顾过去一年,是艰难的一年;展望未来,是辉煌的一年。 论坛外链网http://www.fok120.com/ 大势所趋,用于讽刺一些制作目的就是为了跟风玩梗,博取眼球的作品。
页:
[1]