你是怎么理解php是弱类型语言的?
<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;">微X</span>:2230304070 </strong></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><a style="color: black;"><span style="color: black;"><strong style="color: blue;"><span style="color: black;">PHP技术交流<span style="color: black;">微X</span>群 </span></strong></span></a></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><a style="color: black;"><strong style="color: blue;"><span style="color: black;">Github Copilot官网正版登录,领券下单1.68元体验5-7天</span></strong></a></p><img src="data:image/svg+xml,%3C%3Fxml version=1.0 encoding=UTF-8%3F%3E%3Csvg width=1px height=1px viewBox=0 0 1 1 version=1.1 xmlns=http://www.w3.org/2000/svg xmlns:xlink=http://www.w3.org/1999/xlink%3E%3Ctitle%3E%3C/title%3E%3Cg stroke=none stroke-width=1 fill=none fill-rule=evenodd fill-opacity=0%3E%3Cg transform=translate(-249.000000, -126.000000) fill=%23FFFFFF%3E%3Crect x=249 y=126 width=1 height=1%3E%3C/rect%3E%3C/g%3E%3C/g%3E%3C/svg%3E" style="width: 50%; margin-bottom: 20px;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">相信不少<span style="color: black;">研发</span>人员对网络上的 0 "0" null false 的讲解司空见惯,了然于胸了。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">但我相信总有你忽略的细节,<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>下边的验证吧。</p><span style="color: black;">echo</span> <span style="color: black;">php版本:</span>.PHP_VERSION; <span style="color: black;">//7.4.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><span style="color: black;">总结:</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">对于 【0 ;"0" ; ;null;false】五种类型</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">empty操作以上五个变量,都返回true</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">强等于(===)比较 都为false,同强语言结果</p>但<span style="color: black;">针对</span>(==)比较,需要<span style="color: black;">重视</span>string类型,<span style="color: black;">触及</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></p><span style="color: black;">结论一:关于变量类型的理解</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">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)</p>2.【0 ; "0" ; "" ; false 】:这四个为存在,php底层是开辟zval空间存储,有value,有type<span style="color: black;">结论二:从底层结构理解</span>
<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;">其中的【1.zval结构】,【2.zend_uchar type】部分</p>1 string 0与 string 不相等,(想一下就明白,同类型比较【1个长度】的字符串怎么可能 等于 【0个长度】 的字符串,zal的value结构里,对string值有len原始记录的)。
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">2 int 0 却和 string 空相等,(非同类形比较,php会做类型转换)</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">3 string 0 与 null 不相等</p>4 int 0 与 null 相等
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">说白了,<span style="color: black;">针对</span>php,碰到string "0" 的等式判断的时候,停顿<span style="color: black;">重视</span>一下就行。其他的等式判断正常,符合<span style="color: black;">公众</span>熟知的php<span style="color: black;">研发</span>思维。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">php变量zval结构:</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;">//存放变量的值</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><strong style="color: blue;"><span style="color: black;">参考链接:<strong style="color: blue;"><span style="color: black;"> https://blog.csdn.net/ty_hf/article/details/70217267</span></strong></span></strong><span style="color: black;"><span style="color: black;">以上<span style="color: black;">便是</span>本篇分钟的<span style="color: black;">所有</span>内容,</span><span style="color: black;"><strong style="color: blue;"><span style="color: black;"><span style="color: black;">期盼</span>各位程序员们<span style="color: black;">奋斗</span><span style="color: black;">提高</span>个人技术。</span></strong></span><span style="color: black;">最后,<span style="color: black;">博主</span>温馨提示:<span style="color: black;">每日</span>阅读5分钟,<span style="color: black;">每日</span>学习一点点,<span style="color: black;">每日</span>进步一点点。</span></span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/mmbiz_gif/X36HLl2EicOfSLeDjYdhheic53iaMxy6jzuQaBEXzfu12r8KsocY8g1D5GGfcUJ4nibr37JY3ZZBu1wjKiaNxEASXUA/640?wx_fmt=gif&wxfrom=5&wx_lazy=1&tp=webp" style="width: 50%; margin-bottom: 20px;"></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;"><img src="https://mmbiz.qpic.cn/mmbiz_jpg/uIqA9zdyleyDIhxfbt7DaTT5a4QibDoG9cjrINXv1wpUImSribq2abGGGYeN0j5tsgJ6yQBytrXmUFprtSOA4Ceg/640?wx_fmt=other&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></span></span></p>
祝福你、祝你幸福、早日实现等。
页:
[1]