php basic syntax

1. php tags

  <?php

     echo 'Hello';

  ?>

 

2. Constant constTHE_VALUE = 100;

       define('THE_VALUE',100);

  variable $a=10;

 

3. Function

    functiontraceHello($name){

       echo 'hello '.$name.'<br>';//String connection

       //echo "hello $name <br>";

       return 1;//return value

    }

 

    transfer:

       1. traceHello('zhangsan');

       2. $ func = 'traceHello'

         $func('zhangsan');

 

4. Process Control

     1.if else

        function getLevel($score){

            if($score>=90){

                 return 'excellent';

            }elseif($score>=80){

                 return 'good';

            }else{

                  return 'not good';

            }

          }

      2.switch

         function getLevel($score){

              $result = 'bad';

              switch(intval($score/10)){//intval() round up or take out the integer

                 case 10:

                 case  9:

                      $result = 'Excellent';

                      break;

                 case  8:

                      $result = 'good';

                      break;

                 default:

                      $result = 'bad';

                      //break;

               }

              return $result;

          }

        3.for loop

          for($i=0;$i<100;$i++){

             if($i==20){ break; }

             if($i==40){ continue; }

          }

       4.while loop

           $i=0;

           while(%<10)

              ...

              $i++;

           }

       5. do while loop

          $i=0;

          do{

              ...

              $i++;

           }while($i<100);

 

5. Logical Operators

      || &&  !

6. Common methods of strings

   strpos($str,'o');

   substr($str,2);

   str_split ();

   explode(' ',$str);

 

7. Arrays

   $arr=array();

   $arr[0]='Hello';

   print_r($arr);

 

   $arr['h']='hello';

   $arr['w']='world';

 

   $arr =array('h'=>'hello','w'=>'world')

 

8.require 'lib.php';//Dependency, there is no error in the file

  require_once 'lib.php';//Only referenced once

  include 'lib.php';//Include, the file does not exist and give a warning

 

9. Declare the class

     namespace jk;//namespace

     class Man{

       private $_age,$_name;

       private static $NUM = 0;//Static variable

       

        /**

        * @param int $age age

        * @param string $name name

       public funcation _construct($age,$name){

          $this-> _age = $age;//this current object pointer, self current class pointer, parent parent class pointer

          $this-> _name = $name;

       }//Construction method

       public funcation sayHello(){}//Member method

       public static funcation say(){}//Class method

     }

  Create instance

     $a=new Man();

    $a->sayHello();//Call the method

     Man::say();

 

     $h = new\jk\Hello();//There is a namespace

 

10. Class inheritance, overriding methods

   class Child extends Man{

       publicfunction _construct($age,$name){

         parent::_contruct($age,$name);

       }

      

       publicfunciton say(){//Override method

         //parent::say(); //retain the parent class method

         echo 'child';

       }

    }

 

11. Common library functions

   time();//timestamp

  date_default_timezone_set('Asia/Shanghai');//Time zone setting

   date('Ymd H:i:s');//time

   date('Ymd H:i:s', time());//Timestamp converted to time

 

   $obj =array('h'=>'Hello','w'=>'World');

   echo json_encode($obj);//Convert to json format

   $jsonStr ='{"h":"Hello","w":"World"}';

   $obj =json_decode($jsonStr);//json string is converted to php object

 

   $f = @fopen('data','w');//Open the file, @ does not output a warning

   fwrite($f,'hello');//Write the file

   fclose($f);

   $f = @fopen('data','r');

   while(!feof($)){//Determine whether the end of the file is reached

      $content =fgets($f);//Only one line can be read

      echo$content;

   }

   echofile_get_contents('data');//Get the file directly

 

   die('end page');

  

 

12. Get get parameters and post parameters

   if(isset($_GET['name'])&& $_GET['name']){}//isset() to determine whether the variable is set empty($name) to determine whether the variable is assigned a value

 

13. Get files

   $file = $_FILES['file'];

   $fileName = $file['name'];

  move_uploaded_file($file['tmp_name'],$fileName);

 

14.cookie和session

   setcookie('name','cheng');

   header('Location:a.php');//Jump

 

    //a.php

    echo $_COOKIE['name'];//Get cookies

 

    session_start();//Enable session

    $_SESSION['name'] ='cheng';

    echo session_id();//Get sessionid

    echo$_SESSION['name'];//

    session_destroy();//销毁session

 

15. mysql database

    $conn =mysql_connect('localhost','root','password');

   mysql_select_db('mydb',$conn);

    $result =mysql_query("SELECT * FROM users");//Query, or update, or delete

    $result_arr =mysql_fetch_array($result);//Fetch one item at a time, in the form of an array, with an index

    $result_arr =mysql_fetch_assoc($result);//No index

    $data_count =mysql_num_rows($result);//Get the number of data rows

 

    if(mysql_errno()){

       echomysql_error();//Get the error

    }

 

16.PDO

    PHP data objects, which provide an abstraction layer for database access, use the same methods no matter which database is used.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324518806&siteId=291194637