Home >Backend Development >PHP Tutorial >PHP study notes 2
The content of this article is about PHP learning notes 2, which has a certain reference value. Now I share it with you. Friends in need can refer to it
1 , IF...ELSE statement
is the same as C language.
<?php $t=date("H"); if ($t<"10") { echo "Have a good morning!"; } elseif ($t<"20") { echo "Have a good day!"; } else { echo "Have a good night!"; } ?>
2. SWITCH statement
is the same as C language.
<?php $favcolor="red"; switch ($favcolor) { case "red": echo "你喜欢的颜色是红色!"; break; case "blue": echo "你喜欢的颜色是蓝色!"; break; case "green": echo "你喜欢的颜色是绿色!"; break; default: echo "你喜欢的颜色不是 红, 蓝, 或绿色!"; } ?>
3. While loop
(1)while
(2)do...while will at least execute Code once and then check the condition
# same as C language.
4. For loop - know in advance the number of times the script needs to be run
(1) for
(2) foreach is used Traverse array
<?php $x=array("one","two","three"); foreach ($x as $value){ echo $value . "<br>"; } ?>
<br/>
5, array
In PHP, array () function is used to create arrays.
<?php $cars=array("Volvo","BMW","Toyota"); echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . "."; ?>
(1) Array type
The first type: numerical array, automatic allocation of ID values and manual allocation of ID values
Get the length of the array - count() function, for example: count($cars);
Traverse the values Array - for loop
<?php $cars=array("Volvo","BMW","Toyota"); $arrlength=count($cars); for($x=0;$x<$arrlength;$x++){ echo $cars[$x]; echo "<br>"; } ?>
Second type: Associative array, without ID, using the specified key assigned to the array
<?php $age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43"); echo "Peter is " . $age['Peter'] . " years old."; ?>
Traverse associative array - foreach loop
<?php $age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43"); foreach($age as $x=>$x_value){ echo "Key=" . $x . ", Value=" . $x_value; echo "<br>"; } ?>
<pre class="brush:php;toolbar:false"> <?php $cars=array("Volvo","BMW","Toyota"); sort($cars); print_r($cars); ?>
<?php $x = 75; $y = 25; function addition() { $GLOBALS['z'] = $GLOBALS['x'] + $GLOBALS['y']; } addition(); echo $z; //z是一个$GLOBALS数组中的超级全局变量,同样可以在函数外部访问 ?>
##(4)$_POST
$_POST is used to collect form data
(5)$_GET
$_GET should be used to collect form data
7. Function
(1) PHP built-in Function
(2) Function
##Format: function functionName(...){..... .}
<?php function add($x,$y) { $total=$x+$y; return $total; } echo "1 + 16 = " . add(1,16); ?>Related recommendations:
PHP learning Note 1
The above is the detailed content of PHP study notes 2. For more information, please follow other related articles on the PHP Chinese website!