PHP script block to <? PHP starts with At the end, we can place the script block of PHP anywhere in the document.
Every code in PHP must end with a semicolon
2. variable
In PHP, you don't need to declare the variable before setting it, and you don't need to declare the data type of the variable to PHP. According to the way the variable is set, PHP automatically converts the variable to the correct data type.
eg:
$a = 10; $b = 5; $b .= $a; echo $b;
3. operator
PHP supports all basic operators:
+ - * / % ++ --
= += -= *= /= .= %=
== != > < >= <=
&& || !
eg:
4. array$a = 10; $b = 5; $b .= $a; echo $b;
PHP has three types of arrays:
Numeric arrays: arrays with numeric ID keys
Association array: Each ID key in the array associates a value
Multidimensional arrays: arrays containing one or more arrays
Each element stored in a numeric array has a numeric ID key, which can be created in two ways:
Association array:$type1 = array(1, 2, 3); $type2[0] = 1; $type2[1] = 2; $type2[2] = 3; echo "array1[2]:" . $type1[2] . " array2[2]: " . $type2[2];
Association array, where each ID key is associated with a value. There are two ways to create it:
Multidimensional arrays:$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>";
In multidimensional arrays, each element in the main array is also an array. Each element in a subarray can also be an array, and so on.
eg:
5. if..elseif..else ,switch$student = array ( "zhou"=>array ( 1, 2, 3 ), "huang"=>array ( 1, 2, 3 ) ); echo "student['zhou'][2]:" . $student['zhou'][2]; echo " <br>";
The if and switch statements of PHP are basically similar to those of version c.
6. cycle$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>";
while: As long as the specified condition holds, the code block is executed iteratively
do...while: First execute a block of code, and then repeat the loop when the specified condition holds
for: Number of times specified by a code block is executed in a loop
foreach: Loop code blocks based on each element in the array
eg:
7. function$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); //foreach foreach ($name_age2 as $value) { echo "Value: " . $value . "<br />"; }
The php function is similar to the c function, with parameters and return values
An example of using a function:
//function function add($x,$y) { $total = $x + $y; return $total; } echo "100 + 10 = " . add(100,10);
Complete code:
test.php
Test run:<html> <body> <?php /* * php note */ // php note //variable $txt = "hello php"; echo $txt; echo " <br>"; //operator $a = 10; $b = 5; $b .= $a; echo $b; echo " <br>"; //array $type1 = array(1, 2, 3); $type2[0] = 1; $type2[1] = 2; $type2[2] = 3; echo "array1[2]:" . $type1[2] . " array2[2]: " . $type2[2]; echo " <br>"; //array $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>"; //array $student = array ( "zhou"=>array ( 1, 2, 3 ), "huang"=>array ( 1, 2, 3 ) ); echo "student['zhou'][2]:" . $student['zhou'][2]; echo " <br>"; //if $var = 2; if ($var == 1) echo "if_test: 1"; elseif ($var == 2) echo "if_test: 2"; else echo "if_test: other"; echo " <br>"; //switch switch ($var) { case 1: echo "switch_test: 1"; break; case 2: echo "switch_test: 2"; break; default: echo "swith_test: 3"; }; echo " <br>"; //do...while $i=0; do { $i++; echo "The number is " . $i . "<br />"; }while ($i<5); //foreach foreach ($name_age2 as $value) { echo "Value: " . $value . "<br />"; } //function function add($x,$y) { $total = $x + $y; return $total; } echo "100 + 10 = " . add(100,10); ?> </body> </html>
Browser input: http://192.168.21.133/test.php
Output:
hello php
510
array1[2]:3 array2[2]: 3
name_age1['yan']:40 name_age2['yan']: 40
student['zhou'][2]:3
if_test: 2
switch_test: 2
The number is 1
The number is 2
The number is 3
The number is 4
The number is 5
Value: 20
Value: 30
Value: 40
100 + 10 = 110
Reproduced in: https://my.oschina.net/zipu888/blog/549651