和乐得资源网

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

  • 时间:2020-07-23 08:15 编辑: 来源: 阅读:919
  • 扫一扫,手机访问
摘要:经典的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学习手册基础语法之一
下一篇:已是最后一篇内容
资讯详情页最新发布上方横幅
最新发布的资讯信息
【技术文档|手机专题】DevEco Studio真机调试方法(2024-06-17 08:35)
【技术文档|程序开发】重磅消息PHP后台实现微信小程序客服消息功能(2022-03-27 17:16)
【技术文档|电脑数码】怎样判断RS232线是直连还是交叉连线?(2022-03-21 12:10)
【技术文档|程序开发】PHP如何实现高级检索功能以及动态拼接sql(2022-02-24 08:17)
【机关公文|对照检查】党史学习教育专题组织生活会个人对照检查材料(2021-07-31 20:01)
【机关公文|党的建设】学习“七一”重要讲话精神宣讲提纲(2021-07-31 20:00)
【机关公文|党的建设】学习贯彻习近平总书记 “七一”重要讲话精神宣讲报告(2021-07-31 19:56)
【机关公文|党的建设】在中央和国家机关优秀共产党员、 优秀党务工作者和先进基层党组织表彰大会上的讲话(2021-07-31 19:50)
【技术文档|程序开发】个人免签支付原理(2021-06-28 14:06)
【技术文档|程序开发】友价商城自由支付接口的设置方法及解释(2021-06-28 07:59)
本站资源全部来源于网络,仅用于学习交流,严禁从事商业或者非法活动,如存在侵权问题,请及时联系本站处理,谢谢配合!
网站首页 | 关于我们 | 广告合作 | 联系我们 | 隐私条款 | 免责声明 | 网站地图
CopyRight 2014-2025 和乐得资源网 | 桂ICP备20004151号
展开