php基础_变量和比较符

本人php菜鸟一枚,初读《php和mysql web开发》,把其中的比较基础的一些东西列举出来,方便和自己一样的菜鸟快速入门,并且方便自己以后复习理解。

①变量

  1.比较有特色的应该是变量不需要预定义,可以直接使用。

  2.变量放在单引号和双引号中的作用是完全不一样的

1 <?php
2         $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 {

}

 

    

  

posted @ 2014-11-13 13:42  木头爹  阅读(608)  评论(0编辑  收藏  举报