PHP7飛船來了

來源:互聯網
上載者:User

標籤:比較   php   浮點型   



    鬧眼子?這裡說的飛船不是真的飛船,是被成為飛船符號(<=>)。


    在PHP7,一個新的功能,飛船操作符已經被引入。它是用於比較兩個運算式。當第一個運算式比第二個運算式分別小於,等於或大於它返回-1,0或1。

<?php   //整型比較   print( 1 <=> 1);print("<br/>");   print( 1 <=> 2);print("<br/>");   print( 2 <=> 1);print("<br/>");   print("<br/>");      //浮點性比較   print( 1.5 <=> 1.5);print("<br/>");   print( 1.5 <=> 2.5);print("<br/>");   print( 2.5 <=> 1.5);print("<br/>");   print("<br/>");      //字串比較   print( "a" <=> "a");print("<br/>");   print( "a" <=> "b");print("<br/>");   print( "b" <=> "a");print("<br/>");?>


     在之前的博文中提交php浮點性比較的坑,最後使用了bccmp()函數強制轉換字串來解決。


    >>詳情見:php7采坑記:浮點型資料比較



    那麼今天,利用php7這個新特性,可以完美跳出坑。


$a = 42735.04;$b = 17806.2;$c = $a / 36;$c = round($c, 2);$d = $c * 15;echo ‘$b value is: ‘ . $b . "\n";echo ‘$d value is: ‘ . $d . "\n";echo "<br/>";if ((round($b,2) <=> round($d,2)) === 0) {    echo "ok\n";} else {    echo "sorry\n";}


    <=>    起飛吧

本文出自 “我的PHP之路” 部落格,請務必保留此出處http://phpme.blog.51cto.com/663593/1956225

PHP7飛船來了

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.