4zhvml8 发表于 2024-10-4 17:00:16

[精选] 问一下,你是怎么样理解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>&nbsp;<span style="color: black;">php版本:</span>.PHP_VERSION;&nbsp;<span style="color: black;">//5.6.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>
    <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>&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>&nbsp;&nbsp;&nbsp;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>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;"><span style="color: black;"><span style="color: black;"><span style="color: black;">//存放变量的值</span></span></span></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字节</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>
    <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&amp;tp=webp&amp;wxfrom=5&amp;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>




b1gc8v 发表于 2024-10-18 17:15:59

回顾过去一年,是艰难的一年;展望未来,是辉煌的一年。

nqkk58 发表于 2024-10-19 19:05:16

论坛外链网http://www.fok120.com/

4zhvml8 发表于 3 天前

大势所趋,用于讽刺一些制作目的就是为了跟风玩梗,博取眼球的作品。
页: [1]
查看完整版本: [精选] 问一下,你是怎么样理解php是弱类型语言的?