技术文档
技术文档 > 程序开发 > 详情
经典的php基本语法,看了就懂
2020-07-23 08:15 阅读:1069

print “hello world”


 <html>

  <head>

    <title>Leap Years</title>

  </head>

  <body>

    <?php

      echo "hello world";

      echo "hello" . " " . "haifei";// . 连接符

    ?>

  </body>

</html>


注释


// 单行注释

/* 多行注释 */


定义变量

php不区分类型,直接赋值


$name = "tony";

$age = 18;


数组

取值 [] 等同{}


$language = array("php", "java", "ruby","python");

echo $language[2];

echo $language{2};

$language{2} = "go";

echo $language[2];


$myAssocArray = array('year' => 2012,

                        'colour' => 'blue',

                        'doors' => 5);

echo $myAssocArray['colour'];//blue


if else


$value = 3;

if($value == 1){

    echo "value is one";

}else if($value == 2){

    echo "value is two";

}else{

    echo "echo else";

}


switch


//写法1

$var = 2;

switch($var){

    case 1:

        break;

    case 2:

        break;

    default:

        break;

}



//写法2

$var = 2;

switch($var):

    case 1:

        break;

    case 2:

        break;

    default:

        break;

endswitch;



for


for($leap = 2000; $leap<2050; $leap+=4){

    echo " $leap ";

}


foreach


$arr = array("a","b","c");

foreach($arr as $a){

    echo $a;

}


while


$loopCount = 0;

while ($loopCount<4){

   echo "<p>Iteration number: {$loopCount}</p>";

   $loopCount ++;

}


while ($loopCount<4):

   echo "<p>Iteration number: {$loopCount}</p>";

   $loopCount ++;

endwhile;


do while


$i = 0;

do {

    echo $i;

} while ($i > 0);


功能函数


字符串操作


$name = "HaiFei";

print strlen("david");//长度

$sub = substr($name,0,3);//字符串截取

print $sub;

print strtoupper($name);//大写

print strtolower($name);//小写

print strpos($name, "a");//位置

if(strpos($name,"z") === false){//是恒等计算符   同时检查表达式的值与类型

print "sorry not found";

}


取整,四舍五入


$round = round(M_PI);

print $round;  // prints 3


// This time, round pi to 4 places

$round_decimal = round(M_PI, 4);

print $round_decimal; // prints 3.1416


随机数


// prints a number between 0 and 32767

print rand();

// prints a number between 1 and 10

print rand(1,10);


数组操作


$arr = array();

array_push($arr,"11");

array_push($arr,"22");

print count($arr);// 2


$array = array(5, 3, 7, 1);

sort($array);

print join(", ", $array);// prints "1, 3, 5, 7"

rsort($array);

print join(":", $array);// prints "7:5:3:1"


函数


function aboutMe($name, $age){

    echo "Hello! My name is $name, and I am $age years old";

}

aboutMe("haifei", 18);



 // The code below creates the class

 class Person {

     // Creating some properties (variables tied to an object)

     public $isAlive = true;

     public $firstname;

     public $lastname;

     public $age;


     // Assigning the values

     public function __construct($firstname, $lastname, $age) {

       $this->firstname = $firstname;

       $this->lastname = $lastname;

       $this->age = $age;

     }


     // Creating a method (function tied to an object)

     public function greet() {

       return "Hello, my name is " . $this->firstname . " " . $this->lastname . ". Nice to meet you! :-)";

     }

   }


 // Creating a new person called "boring 12345", who is 12345 years old ;-)

 $me = new Person('boring', '12345', 12345);


 // Printing out, what the greet method returns

 echo $me->greet(); 



class Person {

  public $isAlive = true;


  function __construct($name) {

      $this->name = $name;

  }


  public function dance() {

    return "I'm dancing!";

  }

}


$me = new Person("Shane");

if (is_a($me, "Person")) {

  echo "I'm a person, ";

}

if (property_exists($me, "name")) {

  echo "I have a name, ";

}

if (method_exists($me, "dance")) {

  echo "and I know how to dance!";

}




const , static关键字


class Ninja extends Person {

    const stealth = "MAXINUM";// 没有 $


    static public $job = "coder";

    static public function proclaim() {

        return "A kingly proclamation!";

    }


}


echo Ninja::stealth;

echo Ninja::$job;

echo Ninja::proclaim();



map


$myArray = array('name'=>'haifei',

            'age'=> 18,

            'address'=>"beijing");


foreach($myArray as $key=>$value){

    echo $key . ":" . $value . " ";

}


上一篇

首页

分类

订单

购物车

我的