和乐得资源网

经典的php基本语法,看了就懂

  • 时间:2020-07-23 08:15 编辑: 来源: 阅读:70
  • 扫一扫,手机访问
摘要:经典的php基本语法,看了就懂

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 . " ";

}


  • 全部评论(0)
上一篇:疯狂的php学习手册基础语法之一
下一篇:已是最后一篇内容
资讯详情页最新发布上方横幅
最新发布的资讯信息
【机关公文|党的建设】中央列出违反八项规定清单80条(2020-09-18 14:36)
【机关公文|领导讲话】70后市长致辞,才华横溢,读后唇齿留香!(2020-09-17 13:06)
【技术文档|网络科技】小心!WordPress插件有漏洞,数百万网站遭探测和攻击(2020-09-09 08:15)
【机关公文|综合汇报】6名县委书记同场向省委汇报!(2020-09-07 22:08)
【机关公文|领导讲话】清华毕业的市委书记: 在市七届人大五次会议闭幕会上的讲话(2020-09-06 16:15)
【心情驿站|婚姻】用四个技巧快速追到相亲女生(2020-09-02 15:16)
【心情驿站|情感】最新520甜蜜情话(2020-09-02 15:06)
【心情驿站|情感】暖心情话 撩妹金句(2020-09-02 15:01)
【技术文档|网络科技】光刻机大败局!!(2020-08-31 18:37)
【机关公文|主题教育】“厉行节约、反对浪费”倡议书参考(2篇)(2020-08-28 15:52)
本站资源全部来源于网络,仅用于学习交流,造成一切损失与本站无关,如存在侵权问题,请及时联系本站处理,谢谢配合!
网站首页 | 关于我们 | 广告合作 | 联系我们 | 隐私条款 | 免责声明 | 网站地图
CopyRight 2014-2020 和乐得资源网 | 桂ICP备20004151号-2
展开