PHP study notes 2

不言
Release: 2023-03-24 12:04:01
Original
1529 people have browsed it


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!";
}
?>
Copy after login

<br/>

2. SWITCH statement

is the same as C language. <br/>

<?php
$favcolor="red";
switch ($favcolor)
{
case "red":
    echo "你喜欢的颜色是红色!";
    break;
case "blue":
    echo "你喜欢的颜色是蓝色!";
    break;
case "green":
    echo "你喜欢的颜色是绿色!";
    break;
default:
    echo "你喜欢的颜色不是 红, 蓝, 或绿色!";
}
?>
Copy after login

<br/>

3. While loop

(1)while

(2)do...while will at least execute Code once and then check the condition

<br/>

# same as C language.

<br/>

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>";
}
?>
Copy after login

<br/>

<br/>
Copy after login

<br/>

5, array

<br/>

In PHP, array () function is used to create arrays. <br/>

<?php
$cars=array("Volvo","BMW","Toyota");
echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . ".";
?>
Copy after login

<br/>

(1) Array type

The first type: numerical array, automatic allocation of ID values ​​and manual allocation of ID values<br/>

<br/>

<br/>

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>";
}
?>
Copy after login

<br/>

Second type: Associative array, without ID, using the specified key assigned to the array<br/>

<?php
$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
echo "Peter is " . $age[&#39;Peter&#39;] . " years old.";
?>
Copy after login

<br/>

Traverse associative array - foreach loop<br/>

<?php
$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
 
foreach($age as $x=>$x_value){
    echo "Key=" . $x . ", Value=" . $x_value;
    echo "<br>";
}
?>
Copy after login

<br/>

##(2) Array sorting (function)

First Type: sort(), ascending sort

<br/>

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

<br/>

Result:

Second type: rsort( ), sort in descending order

<br/>

<br/>

<br/>

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

<br/>

<br/>

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

<br/>

<br/>

6. Super global variables

are available in the entire scope of a script.

<br/>

<br/>

(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数组中的超级全局变量,同样可以在函数外部访问
?>
Copy after login

<br/>

(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.

<br/>

<br/>

<br/>

(3) $_REQUEST

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

<br/>

<br/>##(4)$_POST

$_POST is used to collect form data

<br/>

<br/>(5)$_GET

$_GET should be used to collect form data

<br/>

<br/>

<br/>7. Function

<br/>

<br/> (1) PHP built-in Function

(2) Function

<br/>

##Format: function functionName(...){..... .}<br/>

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);
?>
Copy after login
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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!