Home  >  Article  >  Backend Development  >  PHP study notes 2

PHP study notes 2

不言
不言Original
2018-04-19 14:27:251500browse


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[&#39;Peter&#39;] . " 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>";
}
?>


##(2) Array sorting (function)

First Type: sort(), ascending sort


<pre class="brush:php;toolbar:false"> 
<?php 
$cars=array("Volvo","BMW","Toyota");  
sort($cars);  
print_r($cars); 
?> 


Result:

Second type: rsort( ), sort in descending order




The third type: asort(), sorts the array in ascending order according to the value of the array ( For associative arrays)

The fourth method: ksort(), sorts the array in ascending order according to the keys of the array



The fifth type: arsort(), sort in descending order according to the value of the array

The sixth type: krsort(), sort in descending order according to the key of the array



6. Super global variables

are available in the entire scope of a script.



(1) $GLOBALS

$GLOBALS is a global combination array containing all variables. The name is the key of the array.

<?php 
$x = 75; 
$y = 25;
 
function addition() { 
    $GLOBALS[&#39;z&#39;] = $GLOBALS[&#39;x&#39;] + $GLOBALS[&#39;y&#39;]; 
}
 
addition(); 
echo $z; //z是一个$GLOBALS数组中的超级全局变量,同样可以在函数外部访问
?>


(2)$_SERVER

$_SERVER is a server that contains information such as header, path, and script locations. ) and other information. The items in this array were created by the web server. There is no guarantee that all items will be available on every server.




(3) $_REQUEST

$_REQUEST is used to collect data submitted by HTML forms.



##(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(...){..... .}

Guidelines: functionName starts with a letter or underscore

Note that the return value type does not need to be specified

<?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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:PHP study notes oneNext article:PHP study notes one