Summary of PHP Basics (iii) Process Control, functions, class objects, and databases

Source: Internet
Author: User
Tags php basics

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-&GT;A1 =$a; $this-&GT;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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.