標籤: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基礎_變數和比較符