9q13nh 发表于 2024-10-4 17:03:22

你是怎么理解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&nbsp;</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>群&nbsp;</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>&nbsp;<span style="color: black;">php版本:</span>.PHP_VERSION;&nbsp;<span style="color: black;">//7.4.28</span>$a&nbsp;=&nbsp;&nbsp;<span style="color: black;">0</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;&nbsp;</p>$b=<span style="color: black;">"0"</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;&nbsp;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">$c=&nbsp;;&nbsp;</p>$d=&nbsp;<span style="color: black;">null</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;&nbsp;</p>$e&nbsp;=&nbsp;<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>&nbsp;<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&nbsp;0</span>var_dump($b);<span style="color: black;">//string&nbsp;0</span>var_dump($c);<span style="color: black;">//string&nbsp;</span>var_dump($d);<span style="color: black;">//null</span>var_dump($e);<span style="color: black;">//boolean&nbsp;false</span><span style="color: black;">echo</span>&nbsp;<span style="color: black;">"&lt;h4&gt;empty测试&lt;/h4&gt;"</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>&nbsp;<span style="color: black;">"&lt;hr&gt;"</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】&nbsp;见结论一</span>var_dump(<span style="color: black;">isset</span>($e));<span style="color: black;">//true</span><span style="color: black;">echo</span>&nbsp;<span style="color: black;">"&lt;h4&gt;(==)双等式测试&lt;/h4&gt;"</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p>var_dump($a&nbsp;==&nbsp;$b);<span style="color: black;">//true</span>var_dump($a&nbsp;==&nbsp;$c);<span style="color: black;">//true</span>var_dump($a&nbsp;==&nbsp;$d);<span style="color: black;">//true</span>var_dump($a&nbsp;==&nbsp;$e);<span style="color: black;">//true !!</span>var_dump($b&nbsp;==&nbsp;$c);<span style="color: black;">//【false】见结论二</span>var_dump($b&nbsp;==&nbsp;$d);<span style="color: black;">//【false】见结论二</span>var_dump($b&nbsp;==&nbsp;$e);<span style="color: black;">//true</span>var_dump($c&nbsp;==&nbsp;$d);<span style="color: black;">//true</span>var_dump($c&nbsp;==&nbsp;$e);<span style="color: black;">//true</span><span style="color: black;">echo</span>&nbsp;<span style="color: black;">"&lt;h4&gt;(===)三等式测试&lt;/h4&gt;"</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p>var_dump($a&nbsp;===&nbsp;$b);<span style="color: black;">//false</span>var_dump($a&nbsp;===&nbsp;$c);<span style="color: black;">//false</span>var_dump($a&nbsp;===&nbsp;$d);<span style="color: black;">//false</span>var_dump($a&nbsp;===&nbsp;$e);<span style="color: black;">//false</span>var_dump($b&nbsp;===&nbsp;$c);<span style="color: black;">//false</span>var_dump($b&nbsp;===&nbsp;$d);<span style="color: black;">//false</span>var_dump($b&nbsp;===&nbsp;$e);<span style="color: black;">//false</span>var_dump($c&nbsp;===&nbsp;$d);<span style="color: black;">//false</span>var_dump($c&nbsp;===&nbsp;$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>&nbsp;_<span style="color: black;">zval_struct</span>&nbsp;{</span>&nbsp;&nbsp;&nbsp;<span style="color: black;">/*&nbsp;Variable&nbsp;information&nbsp;*/</span>&nbsp;&nbsp;&nbsp;zvalue_value&nbsp;value;&nbsp;&nbsp;<span style="color: black;">/*&nbsp;变量值<span style="color: black;">保留</span>在<span style="color: black;">这儿</span>&nbsp;12字节*/</span>zend_uint&nbsp;refcount;<span style="color: black;">//4字节,变量引用计数器&nbsp;&nbsp;</span>&nbsp;&nbsp;&nbsp;zend_uchar&nbsp;type;&nbsp;&nbsp;&nbsp;<span style="color: black;">/*&nbsp;active&nbsp;type变量类型&nbsp;1字节*/</span>&nbsp;&nbsp;&nbsp;zend_uchar&nbsp;is_ref;<span style="color: black;">//<span style="color: black;">是不是</span>变量被&amp;引用,0<span style="color: black;">暗示</span>非引用,1<span style="color: black;">暗示</span>引用,1字节&nbsp;&nbsp;</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">};&nbsp;&nbsp;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">//存放变量的值</p><span style="color: black;">typedef</span>&nbsp;<span style="color: black;">union</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;_zvalue_value&nbsp;{&nbsp;&nbsp;</p>&nbsp;&nbsp;&nbsp;<span style="color: black;">long</span>&nbsp;lval;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">/*&nbsp;long&nbsp;value&nbsp;*/</span>&nbsp;&nbsp;&nbsp;<span style="color: black;">double</span>&nbsp;dval;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">/*&nbsp;double&nbsp;value&nbsp;*/</span>&nbsp;&nbsp;&nbsp;<span style="color: black;"><span style="color: black;">struct</span>&nbsp;{</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">char</span>&nbsp;*val;&nbsp;<span style="color: black;">//4字节&nbsp;&nbsp;</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">int</span>&nbsp;len;&nbsp;&nbsp;&nbsp;<span style="color: black;">//4字节&nbsp;&nbsp;</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;}&nbsp;str;&nbsp;&nbsp;</p>&nbsp;&nbsp;&nbsp;HashTable&nbsp;*ht;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">/*&nbsp;hash&nbsp;table&nbsp;value&nbsp;*/</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;zend_object_value&nbsp;obj;&nbsp;&nbsp;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}&nbsp;zvalue_value;</p><strong style="color: blue;"><span style="color: black;">参考链接:<strong style="color: blue;"><span style="color: black;">&nbsp;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&amp;wxfrom=5&amp;wx_lazy=1&amp;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&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" style="width: 50%; margin-bottom: 20px;">‍</span></span></p>




wrjc1hod 发表于 2024-10-14 14:20:47

祝福你、祝你幸福、早日实现等。
页: [1]
查看完整版本: 你是怎么理解php是弱类型语言的?