PHP Basic Syntax
1. Process Control
Conditional statement: If ElseIf else/switch
if ($a = =1elseif ($a = = 2) {...} Else {...}
Looping statements: for foreach
for($i= 1;$i<= 10;$i++) { Echo $i;}foreach($arr as $key=$value){//one-dimensional array looping keys and values...}foreach($arr as $key=$value){//two-dimensional array loop key and value foreach($value as $value 2){//you can take a value directly in the loop...} }
2. Function:
Once written, repeated calls.
Define with Function:
function foo ($arg _1$arg _2/**/$arg _n) { echo "Example function.\n"; return $retval ;}
Foo ($a 1, $a 2,......, $an); In PHP, the name () ==> function
There are many built-in functions, such as var_dump ()
Local variables: variables defined within a function
Global variables: Variables defined outside the function
3. Classes and objects
The class is a drawing, and the object is the house which is covered by the drawing.
Object-oriented approach: it is for larger-scale collaboration.
<?PHPclassname{ Public $a 1= 0; Public $a 2= 1; Public function__construct () {//Magic method, constructor without parameters,you can do it, not write it. Initial value } Public functionS1 () {$thisS2 ();//call the brothers and sisters with $this. } Public functionS2 () {Echo $this-A1; }}$name=NewName ();//class instantiationEcho $nameA2. "<br/>";//Call the Class property, "-" is the pointer to the operator, followed by the property without the $ symbol$nameS2 ();//Calling class methodsclassname2{ Public $a 1= 0; Public $a 2= 1; Public function__construct ($a,$b){//Constructors with parameters $this->A1 =$a; $this->A2 =$b; } Public functionS1 () {$this-S2 (); } Public functionS2 () {Echo $this-A1; }}/**/$name 2=NewName2 (' A ', ' B ');Echo"<br/>" ;$name 2-S1 ();?>
Summary of PHP Basics (iii) Process Control, functions, class objects, and databases