PHP basics tutorial of php basics tutorial some code code

  • 2020-05-27 04:30:17
  • OfStack

Before this tutorial, I will stop talking about the common use of PHP. This tutorial is for people who have a programming background and are new to PHP. The article is simple. It's about the structure. Detailed also please yourself a lot of research
PHP environment installation:
PHP is usually combined as MySql+PHP+Apche or IIS+PHP+MySQL or SqlServer
Of course, we can select the composite package to install. Beginners are advised to install AppServ or phpnow.
iis supports php by running 1 with this installation. mysql requires 1 installation.
You can also install parts yourself. Then configure it yourself.
PHP each version of the download address: http: / / museum php. net/php5 /
Apche download address: http: / / prdownloads sourceforge. net appserv/appserv - win32-2.5.10. exe? download
MySQL download address: http: / / www mysql. cn /
Configuration installation tutorial: http: / / wenku baidu. com/view/c6118b1810a6f524ccbf85f9 html
Or https: / / www. ofstack. com article / 33062. htm
Writing tools: Notepad++ or dreamweaver cs4 is recommended
====================================================================
Grammar:
The syntax for PHP is simple -- just look at the code: < ? php /* code part */? > This is how the PHP code is declared. Note: < ? ? > You can write it in this way, but it's not recommended.
Marks the end of a statement: the semicolon marks the end of a statement ";" -- use "; "at the end of each statement. A semicolon is the end.
=====================================================================
Comments in PHP: -- see code in the tutorial
The comments in php have a single line comment: // this is a comment
And big module comments: /* this is a comment */
=====================================================================
Variables:
The PHP variable is loose. But it's also case sensitive, which you should be aware of. No need to declare before using it - PHP automatically converts variables to the correct data type, depending on how the variable is declared.
Declare variables in PHP using the $keyword -- all variables are identified by $
Variable naming rules:
Variable names must begin with a letter or an underscore "_".
Variable names can only contain alphanumeric characters and underscores.
Variable names cannot contain Spaces. If the variable name is made up of multiple words, it should be separated by an underscore (such as $my_string) or with a capital letter (such as $myString).
Note: variable naming rules are pretty much the same in all programming languages!

Example:

<?php
       // Declare a variable 
       $var_name = "snow";
       // Use the variable 
       echo $var_name;
      /*
         According to the results : snow
      */
?>

Constants:
Declaration of constants in PHP:
Constants are declared in PHP using the define function. Directly see code

<?php
     /*
      define Function has a 3 A parameter 
        The first 1 Parameter: specifies the name of the constant  -- Keywords must not be used and constants must not exist $ symbol 
        The first 2 Parameter: specifies the value of the constant  -- Only Boolean, integer, float, string 4 A type of 
        The first 3 Parameter: specifies whether this constant is case sensitive  --true Ignore case, false Case sensitive 
    */
     define("Name"," zhang 3",true);
     echo name;
    /* Display result: zhang 3 -- Because it is true So it's not case sensitive */
?>

There are also predefined constants in PHP - you can consult the PHP manual or related materials
=====================================================================
Array: --PHP's array is simple and easy to use.
The PHP array can be used as a collection in other languages
The PHP array can hold any type supported by PHP. You can also store class objects and so on -- see code directly

<?php
        /*===================================================================*/
        // The numerical array 
         $nums = array(1,2,3); 
        // Or equivalent to 
         $nums[0] = 1;
        $nums[1] = 2;
        $nums[2] = 4;
        echo $nums[2]."<br />";
        /* The output :4*/
        /*===================================================================*/
        // An associative array   -- One of the" => "Is PHP Is the key value pair specified. 
         $ns = array("name"=>" zhang 3","age"=>22,"sex"=>"man");  
        // Or equivalent to 
         $ns["name"] = " zhang 3";
        $ns["age"] = 22;
        $ns["sex"] = "man";
        echo " The name :".$ns["name"]."<br /> age :".$ns["age"]."<br /> gender :".$ns["sex"]."<br />";
        /* The output :
             The name : zhang 3
               age :22
             gender :man
        */
        /*===================================================================*/
        // Multidimensional array  -- You can also store arrays in an array 
         $bs = array(" zhang 3"=>array(" hobby "=>" The computer "," age "=>"23"," gender "=>" male ")," The little red "=>array(" hobby "=>" Have a meal "," gender "=>" female "));
        // adjustable 1 Next format, let you see some clear 
         $bs = array
        (
            " zhang 3"=>array
            (
                " hobby "=>" The computer ",
                " age "=>"23",
                " gender "=>" male "
            ),
            " The little red "=>array
            (
                " hobby "=>" Have a meal ",
                " gender "=>" female "
            )
        );
        // Or equivalent to 
         $bs[" The little red "][" gender "] = 2; $bs[" The little red "][" hobby "] = 2; //....
        // or 
         $bs[" zhang 3"] = array(" hobby "=>" The computer "," age "=>"23"," gender "=>" male "); $bs[" The little red "] = array(" hobby "=>" Have a meal "," gender "=>" female ");
        echo $bs[" The little red "][" gender "]."<br />";
        /* The output : female */
        /*===================================================================*/
    ?>

=====================================================================
PHP operator: -- excerpt from w3school tutorial

This section lists the various operators used in PHP:
Arithmetic operator
The operator instructions example The results of + Addition x=2
x+2 4 - Subtraction x=2
5-x 3 * Multiplication x=4
x*5 20 / Division 15/5
5/2 3
2.5 % Modulus (division remainder) 5%2
10%8
10%2 1
2
0 ++ Increment x=5
x++ x=6 -- Decrement x=5
x-- x=4 assignment operator The operator instructions example = x=y x=y += x+=y x=x+y -= x-=y x=x-y *= x*=y x=x*y /= x/=y x=x/y .= x.=y x=x.y %= x%=y x=x%y

Comparison operator

The operator instructions example == is equal to 5==8 returns false != is not equal 5!=8 returns true > is greater than 5 > 8 returns false < is less than 5 < 8 returns true > = is greater than or equal to 5 > =8 returns false < = is less than or equal to 5 < =8 returns true

Logical operator

The operator instructions example & & and x=6
y=3

(x < 10 & & y > 1) returns true

|| or x=6
y=3

(x==5 || y==5) returns false

! not x=6
y=3

!(x==y) returns true


Program judgment statement:

And C#, java, C, etc. There are if.. else/else.. if, switch statements -- look directly at Code


<?php
         $name = " zhang 3"; // Declare a variable 
           /*if..else Only statements are executed 1 A, 1 All of these conditions are true. And even if that were true, it would be ignored */
         // Determine if the name is zhang 3
          if($name == " zhang 3")
         {
               echo " zhang 3";
         }
         else if($name == " li 4") // Then judge 
          {
               echo " li 4";
         }
         else // None of the above just walk in else
         {
              echo " other ";
         }
         print('<br />'); // A printout 
          $num = 1;
         /*
          switch Choose structure   Can be if The principle is similar. Only in the case To add break -- Or you can leave it out. 
             So the words execute play case 1 After will not jump out, but continue to execute 1 a case Branch. Until I met break Just jump out,. You can try it yourself 
          */
         switch($num)
         {
            case 1:
                echo "1";
                break;
            case 2:
                echo "2";
                break;
            default:  // Default branch. Execute when none of the conditions hold. 
                echo " other ";
         }

         /*
          The final result of implementation is:  
                          zhang 3
                         1
           */
    ?>

PHP cycle:

Like other strongly typed programming languages. php also has while, do while, for, foreach -- see code directly


<?php
        $index = 1;
        while($index <=10)
        {
            echo " The first ".$index." time "."<br />";
            $index++; // cumulative 
         }
        /* Output of the above results 10 time */

        echo '<br />';
        $index = 1;
        do
        {
            echo " The first ".$index." time "."<br />";
            $index++;
        }
        while($index <=1);

        /* Output of the above results 1 time */
        echo '<br />';
        for($index = 1;$index <=3;$index++)
        {
                echo " The first ".$index." time "."<br />";
        }

        /* Output of the above results 3 time */
        echo '<br />';
        $index = array("1","2","3");
        foreach($index as $temp) // Through the array 
         {
            echo " value :".$temp."<br />";
        }
        /* Output of the above results 3 time */
    ?>

PHP function:

The declaration of the php function is simple, as long as the keyword function is preceded by the function name. -- see code for the format


<?php
        /*PHP function */
        // No arguments function 
         function MyEcho()
        {
            echo " No arguments function <br />";
        }

        // Have reference function  -- The parameters passed in can also be class objects 
         function MyEcho2($str)
        {
            echo $str;
        }

        MyEcho(); // Output: no argument function 
         MyEcho2(" Hee hee ha ha! "); // The output : Hee hee ha ha! 
    ?>

PHP class:

Like other high-level languages 1, php also supports object-oriented programming. Here I refer to the base section of the php class declaration. There are about object-oriented programming, we study

The way php declares classes is also with the keyword class -- see code -(which includes static functions. Function calls, etc.)


<?php 
        class MyClass // Declaration of a class 
         {
            private $jum1; // Define private variables 
              private $jum2;
            static public $test = " Test static method "; // Define public variables 
              function Calc() // The class function 
              {
                return $this->jum1+$this->jum2; // "->"  Notation is the meaning of a class call 
              }

            function SetNum($Num1,$Num2) // Functions with arguments 
              {
                $this->jum1 = $Num1;
                $this->jum2 = $Num2;
                return $this; // We're going to return the class object itself 
              }

            static function Tt()
            {
                echo "<br />".MyClass::$test."<br />";    
            }
        }

        /* Realize computing function */
        $temp = new MyClass;
        echo $temp->SetNum(2,8)->Calc(); // The output :10
        MyClass::Tt(); //"::" Static call  // The output : Test static method 
    ?>

PHP form processing:

When the page user submits a value, the submitted value is read using the variables defined by the system $_GET and $_POST or $_REQUEST (which includes $_GET, $_POST, and $_COOKIE) -- see code


<body>
    <?php 
        echo $_POST["xx"]."<br />";  // read post value 
         echo $_REQUEST["xx"]; 
        // with get Read the values. Try yourself 
    ?>
    <form action="#" method="post">
        <input type="text" name="xx" />
        <input type="submit" value=" submit " />
    </form>
</body>

That's all for now... If I have time, I will write down the common applications of PHP. The advanced part. (including sessions, cookie, object-oriented, common functions, and so on)


Related articles: