php基礎_變數和比較符

來源:互聯網
上載者:User

標籤:style   blog   http   io   color   ar   使用   sp   資料   

本人php菜鳥一枚,初讀《php和mysql web開發》,把其中的比較基礎的一些東西列舉出來,方便和自己一樣的菜鳥快速入門,並且方便自己以後複習理解。

①變數

  1.比較有特色的應該是變數不需要預定義,可以直接使用。

  2.變數放在單引號和雙引號中的作用是完全不一樣的

1 <?php2         $test = ‘myString‘;3         echo ‘$test.------‘;4         echo "$test.------";5 ?>

  顯示結果如下

  

  結論:變數在雙引號內才能被識別,在單引號內只能被當做字串

  3.字串串連用【.】($aaa = $bbb.$ccc)

  4.常量(屬於變數的一部分吧)

    常量和變數在定義時候有一個明顯的不同就是【$】符號的使用

    例如

      define(‘aaa‘,‘aString‘) // 常量

           $aaa = ‘aString‘ //變數

 ②比較符號

  1.==(等價)

    說明:

    變數:數值相等即可。

    數組:包含相同元素即可。 

 1 <?php 2         $test1 = ‘111‘; 3         $test2 = 111; 4         $test3 = array(111,222,333); 5         $test4 = array (‘222‘,‘111‘,‘333‘); 6         $test5 = array(‘test1‘=>‘111‘,‘test2‘=>‘222‘,‘test3‘=>‘333‘); 7         $test6 = array(‘test1‘=>111,‘test2‘=>222,‘test3‘=>333); 8         if ($test1==$test2) { echo ‘success1‘;} 9         if ($test3==$test4) { echo ‘success2‘;}10         if ($test5==$test6) { echo ‘success3‘;}11 ?>

    輸出結果:success1success3

  2.===(恒等)

    變數:數值和資料類型都相等

    數組:包含相同的順序和類型

<?php        $test1 = ‘111‘;        $test2 = 111;        $test3 = array(111,222,333);        $test4 = array (‘111‘,‘222‘,‘333‘);        $test5 = array(‘test1‘=>‘111‘,‘test2‘=>‘222‘,‘test3‘=>‘333‘);        $test6 = array(‘test1‘=>111,‘test2‘=>222,‘test3‘=>333);        $test7 = array(‘test3‘=>‘333‘,‘test2‘=>‘222‘,‘test1‘=>‘111‘);        $test8 = array(‘test1‘=>‘111‘,‘test2‘=>‘222‘,‘test3‘=>‘333‘);        if ($test1===$test2) { echo ‘success1‘;}        if ($test3===$test4) { echo ‘success2‘;}        if ($test5===$test6) { echo ‘success3‘;}        if ($test5===$test7) { echo ‘success4‘;}        if ($test5===$test8) { echo ‘success5‘;}?>

    輸出結果:success5

  3.0和非0

    非0為true,0為false

<?php        $test1 = ‘0‘;        $test2 = 1;        if ($test1) { echo ‘success1‘;}        if ($test2) { echo ‘success2‘;}?>

    輸出結果:success2(在用==做判斷時候切記別少寫了等號,寫成$test1 = ‘0‘)

③結構

  1.分支 (elseif之間沒有空格)

if (condition1) {} elseif (condition2) {} else {}

 

    

  

php基礎_變數和比較符

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.