PHP Study Notes: Basics

Source: Internet
Author: User

1. Basic syntax
PHP script block<? PHPStart?>To end, we can place the PHP script block anywhere in the document.
Every PHP code must end with a semicolon

2. Variables
In PHP, you do not need to declare the variable before setting it. You do not need to declare the Data Type of the variable to PhP. According to the method in which the variable is set, PHP automatically converts the variable to the correct data type.
Eg:

$a = 10;$b = 5;$b .= $a;echo $b;

3. Operators
PHP supports all basic operators:
+-*/% + + --
= + =-= * =/=. = % =
=! ==<>=<=
& |!
Eg:

$a = 10;$b = 5;$b .= $a;echo $b;

4. Array

PHP has three array types:
Value array: An array with the digit ID key
Join array: Each ID key in the array associates with a value.
Multi-dimensional array: An array containing one or more Arrays

Number array:
Each element stored in the value array has a digital ID key. The creation method is as follows:

$type1 = array(1, 2, 3);$type2[0] = 1;$type2[1] = 2;$type2[2] = 3;echo "array1[2]:" . $type1[2] . " array2[2]: " . $type2[2];

Join array:
Join array. Each ID key is associated with a value. You can create an ECS instance in either of the following ways:

$name_age1 = array("zhou"=>20,"huang"=>30,"yan"=>40);$name_age2["zhou"]=20;$name_age2["huang"]=30;$name_age2["yan"]=40;echo "name_age1['yan']:" . $name_age1['yan'] . " name_age2['yan']: " . $name_age2['yan'];echo " <br>";  

Multi-dimensional array:
In a multi-dimensional array, each element in the main array is also an array. Each element in the sub-array can also be an array, and so on
Eg:

$student = array(        "zhou"=>array        (                1,                2,                3        ),        "huang"=>array        (                1,                2,                3        ));echo "student['zhou'][2]:" . $student['zhou'][2];echo " <br>";

5. If... elseif... else, switch
PHP's If and swithch statements are similar to those of C. The example above is as follows:

$var = 2;if ($var == 1)  echo "if_test: 1"; elseif ($var == 2)  echo "if_test: 2"; else  echo "if_test: other"; echo " <br>";switch ($var){case 1:  echo "switch_test: 1";  break;case 2:  echo "switch_test: 2";  break;default:  echo "swith_test: 3";};echo " <br>";

6. Loop
While: the code block is executed cyclically as long as the specified condition is true.
Do... while: first execute a code block, and then repeat this loop when the specified condition is set.
For: the number of times the code block is executed cyclically.
Foreach: loop the code block based on each element in the array
Eg:

$name_age2["zhou"]=20;$name_age2["huang"]=30;$name_age2["yan"]=40;//do...while$i=0;do{  $i++;  echo "The number is " . $i . "<br />";}while ($i<5);//foreachforeach ($name_age2 as $value){  echo "Value: " . $value . "<br />";}

7. Functions
PHP functions are similar to C functions and have parameters and return values.
Example of a function:

//functionfunction add($x,$y){  $total = $x + $y;  return $total;}echo "100 + 10 = " . add(100,10);

Complete code:
Test. php

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.