PHP基础

1.  输出 echo 或 print 

<?php

    echo "hello"; 

?>
<?php

    echo 13*123;

?>
<?php

    print "hello";

?>

 

2. 变量,$开头

<?php $myVariable=0 ; ?>

 

3 . 注释 : // 同  c, java

 

4. if else elseif: 基本同C  Java, 但是elseif连写

if ()

{}

elseif ()

{} 

else

{}

 

5. 数组 array

$items=array("apple","banana"); //创建数组



echo $items[2];// 输出数组元素



unset($items);

unset($items[2]);// 删除元素或整个数组

array_push($items,"pig"); // push

count($items);// count

sort($items); // sort

  associative array:  类似于python的字典,c++的map

$myAssocArray = array('year' => 2012, 'colour' => 'blue',  'doors' => 5);



echo $myAssocArray['colour'];//输出

using  for each in associative array

 $food=array("apple"=>"yes","banana"=>"no","pig"=>"yes");

 

foreach($food as $f =>$isYes)

{

     echo $isYes. '  '.$f.'<br >';

}

 

6. for 循环:同C Java

 

7. foreach

$items= array("JavaScript", "HTML/CSS", "PHP","Python","Ruby");

        

foreach ($items as $i) 

{

      echo "<li>$i</li>";

}

  

8. while/ do while: 同 C Java

 

9. 函数 function

function foo($pa1, $pa2)

{

return "xx";

}

 

10. 类 class

class Person

{

     public $name;

     public $age;

     public function __construct($na)//构造函数

    {

         $this->name=$na;//访问成员变量前不加"$"

    }

}

            

$student=new Person("student");//创建实例

    

 

is_a($me, "Person");// 判断是否是某个类



property_exists($me, "name");//  判断是否有某个property



method_exists($me, "dance");// 判断是否有某个method

 

class Person

{

    public static function say()// 静态函数

    {

        echo "Hello!";

    }

    const name="xxx";//常量

}





Person::say();

Person::name;//调用静态函数和常量

 11. triple equal 

double equal only compare value but ignore type

triple equal will compare both

<?php

$num1 = "1";

$num2 = 1;

if ($num1 === $num2){

    echo "type and value equal";

}

elseif($num1 == $num2){

    echo "value equal";

}

?>

 

你可能感兴趣的:(php基础)