Php笔记-基础语法

变量

php 的变量必须用$开头

$myAge

switch语法

switch有两种语法:

  • 正常的语法:

        $myAge = 25;
        switch($myAge){
            case 20:
                print "your are age is 20";
                break;
            case 24:
                print "your are age is 24";
                break;
            case 25:
                print "your are age is 25";
                break;
            default:
                print "your are age is not match";
                break;

        }
  • sugar语法:

        $myAge = 25;
        switch($myAge):
            case 20:
                print "your are age is 20";
                break;
            case 24:
                print "your are age is 24";
                break;
            case 25:
                print "your are age is 25";
                break;
            default:
                print "your are age is not match";
                break;
        endswitch;
    ?>

数组

 $array = array("cheng", "fang", "peng");

获取数组的值有两种方法

  • 通过[]
    $array[2];
  • 通过{}
 $array{2}

数组中删除元素

unset(array[2]);
 
        $languages = array("HTML/CSS",
        "JavaScript", "PHP", "Python", "Ruby");

        unset($languages[3]);

        foreach($languages as $lang) {
          print "

$lang

"
; } />

for循环

 
        // 输出前五个偶数
        for ($i = 2; $i < 11; $i = $i + 2) {
          echo $i;
        }
      ?>

foreach循环

foreach循环是迭代对象里的每一个元素。

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

          foreach ($langs as $lang) {
              echo "
  • $lang
  • "
    ; } unset($lang); ?>

    while 循环

    
        $headCount = 0;
        $flipCount = 0;
        while ($headCount < 3) {
            $flip = rand(0,1);
            $flipCount ++;
            if ($flip){
                $headCount ++;
                echo "
    H
    "
    ; } else { $headCount = 0; echo "
    T
    "
    ; } } echo "

    It took {$flipCount} flips!

    "
    ; ?>

    endwhile的使用

    
         $isLoop = true;
         $index = 0;
        while($isLoop):
            if($index > 10){
                $isLoop = false;
                }
                echo $index;
                $index++;
        endwhile;
            ?>

    do-while的使用

    先执行do中的代码,然后再执行while循环

    $flipCount = 0;
        do {
            $flip = rand(0,1);
            $flipCount ++;
            if ($flip){
                echo "
    H
    "
    ; } else { echo "
    T
    "
    ; } } while ($flip); $verb = "were"; $last = "flips"; if ($flipCount == 1) { $verb = "was"; $last = "flip"; } echo "

    There {$verb} {$flipCount} {$last}!

    "
    ; ?>

    函数

    字符串内建函数

    • strlen 返回字符串的长度
     
            $name = "chengfangpeng";
            print strlen($name);
        ?>
    • substr 截取字符串函数,返回截取到的子字符串
      
           $name = "chengfangpeng";
            print substr($name, 5, 10);
        ?>
    
    • strtoupper 将字符串转为大写
     
    
        $name = "chengfangpeng";
        print strtoupper($name);
        ?>
    • strtolower 将字符串转为小写
      
        $name = "CHENGFANGPENG";
        print strtolower($name);
        ?>
    • strpos 找出字符串中某个子字符串的位置,如果没有所给的字符串,返回false,否则返回子字符串在原字符串中的位置。
     
    
        $name = "chengfangpeng";
        print strpos($name, "fang");
    
        if(strpos($name, "Hello") === false){
            print "no match string";
        }
        ?>

    数学内建函数

    • round 将一个float类型的数,四舍五入为整形,如果再传入保留位数参数可以转化成保留小数位的float类型。
     
          print round(M_PI); //3
          print round(M_PI, 3);//3.142
        ?>
    //使用rand,strlen,substr函数随机打印你姓名中的一个字母
     $name = "chengfangpeng";
        $rIndex = rand(0, strlen($name));
        print substr($name, $rIndex, 1);
        ?>

    你可能感兴趣的:(php,php)