上次学习了常量constant

这次先复习一下运算符

image-20220221202109200

强相等必须是类型和内容都相等,弱相等只需要有同样的键值对

1
2
3
4
5
<?php
$x = array("a" => "red", "b" => "green");
$y = array("c" => "blue", "d" => "yellow");
var_dump($x == $y);
?>

也就是键值对相同,内容的类型可以不相同,比如一个字符串一个数字。

下面看三目运算符:

1
2
3
4
5
6
7
<?php
echo $status = (empty($user)) ? "anonymous" : "logged in";
echo ("<br>");

$user = "John";
echo $status = (empty($user)) ? "anonymous" : "logged in";
?>

image-20220221210612118

输出结果预料之中。

下面看

if else:

1
2
3
4
5
6
7
<?php
$t = date("H");#返回当前的小时数,比如上午10点,返回10

if ($t < '20') {
echo "Have a good day";
}
?>

image-20220221211118080

下面加上else:

1
2
3
4
5
6
7
8
9
<?php
$t = date("H");#返回当前的小时数,比如上午10点,返回10

if ($t < '20') {
echo "Have a good day";
} else {
echo "Have a good night";
}
?>

下面引入else if

1
2
3
4
5
6
7
8
9
10
11
<?php
$t = date("H");#返回当前的小时数,比如上午10点,返回10

if ('10'<= $t < '20') {
echo "Have a good day";
} elseif($t < '10') {
echo "Have a good morning";
} else {
echo "Have a good night";
}
?>

image-20220221211822407

下面看switch语句:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?php
$favcolor = "red";

switch ($favcolor) {
case "red":
echo "RED";
break;
case "blue":
echo "BLUE";
break;
case "yellow":
echo "YELLOW";
break;
default:
echo "NONE";
}
?>

其实和C语言的结构基本上一样!

下面看循环

PHP的循环有好多种:

image-20220221212412806

对于while循环

1
2
3
4
5
6
7
8
<?php
$x = 1;

while($x <= 5) {
echo "The number is : $x<br>";
$x++;
}
?>

image-20220221222949845

下面看do while循环

1
2
3
4
5
6
7
<?php
$x = 6;
do {
echo "The number is : $x <br>";
$x++;
} while ($x < 5);
>?

image-20220221223232054

注意:dowhile循环里面至少循环了一次do过程,因为检验放在了后面,即使检验结果是错的,也会执行一次do

下面看for语句

1
2
3
4
5
<?php
for ($x=0;$x<=10;$x++) {
echo "The number is : $x <br>";
}
?>

不过PHP比C多了一个循环:foreach

foreach只工作在数组中,用来循环每一个键值对。

1
2
3
4
5
6
7
<?php
$color = array("red", "green", "blue", "yellow");

foreach($color as $value) {
echo "$value<br>";
}
?>

image-20220221224122090

运行如图所示。

下面看遍历复杂键值对

1
2
3
4
5
6
7
<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");

foreach($age as $key=>$value) {
echo "$key=>$value<br>";
}
?>

image-20220221224343487

如图输出对应键值。

下面看BREAK/CONTINUE:

用来跳出switch语句和各种循环!

1
2
3
4
5
6
7
8
<?php
for ($x = 0;$x < 10;$x++) {
if ($x == 4){
break;
}
echo "The number is $x<br>";
}
?>

image-20220222000237432

可以看出到4就已经跳出了循环,只输出123。

而continue只跳过一次循环,并不跳出所有次数循环(只跳过当前)

1
2
3
4
5
6
7
8
<?php
for ($x = 0;$x < 10;$x ++) {
if ($x == 4) {
continue;
}
echo "The number is $x<br>";
}
?>

image-20220222000435424

如图,只跳过了4一个。

上述循环也可以用while代替:

1
2
3
4
5
6
7
8
9
10
11
<?php
$x = 0;

while($x < 10) {
if ($x == 4){
break;
}
echo "The number is $x <br>";
$x ++;
}
?>

输出结果和上述for语句中带break相同。

image-20220222000745474

PHP有超过1000个已构建函数,可以直接调用用来完成一些任务。

注意:

函数名必须以字母或者下划线开头。函数名是不区分大小写的。

我们直接看一个两个parameter的函数:

1
2
3
4
5
6
7
<?php
function _name($fname, $year) {
echo "$fname is born in $year";
}
_name("Zhang", 2002);
_name("Pang", 2002);
?>

输出结果可想而知。

注:PHP将传参变量的值自动关联到数据类型。

注:在PHP7中要声明类型。

1
2
3
4
5
6
<?php
function _add(int $a, int $b) {
return $a +$b;
}
echo _add(5, "5 days");
?>

这里注意,会返回10,因为会把字符串的数据类型转换成整型,提取出整数5.

PHP函数中有一个默认赋值,即不从外部传参时会把函数体内部的赋值在外面用,比如:

1
2
3
4
5
6
7
<?php
function _set(int $height = 50) {
echo "The height is : $height<br>";
}
_set(500);
_set();#在这里会使用函数中的50来赋值。
?>

下面用RETURN来输出函数值:

1
2
3
4
5
6
7
8
<?php
function sum(int $x, int $y){
$z = $x + $y;
return $z;
}
echo "5 + 10 = ".sum(5, 10)."<br>";
echo "7 + 7 = ".sum(7, 7)."<br>";
?>

输出就不多说了,显而易见。

下面说函数值的类型:

1
2
3
4
5
6
<?php
function add(float $a, $float $b) : int{
return (int)($a + $b);
}
echo add(1.2,5.2);
?>

image-20220222003302099

会进行类型转换。

下面看最难的引用传递:分为变量的引用传递和函数的引用传递

1
2
3
4
5
6
7
8
<?php#先看变量的引用传递
function adduce(&$string) {
$string .= "我是引用传递";
echo $string;#输出“我是外部字符串,我是引用传递”。
}
$str = "我是外部字符串,";
adduce($str);
echo $str #输出“我是外部字符串,我是引用传递”。#即修改了变量所在的地址!就是指针函数!

实际上是$str修改了地址,新地址与$string共用同一值。

突然明白了,&这就是C语言的指针,传入的参数是变量的地址!将地址的内容修改了

指针函数是一个返回指针的函数!

函数指针是一个指向函数的指针!把函数的地址赋给指针!

image-20220222005745060

上面是PHP的一些超全局变量,在任何情况下都是可访问的。最高权限变量!。

Element/Code Description
$_SERVER[‘PHP_SELF’] Returns the filename of the currently executing script
$_SERVER[‘GATEWAY_INTERFACE’] Returns the version of the Common Gateway Interface (CGI) the server is using
$_SERVER[‘SERVER_ADDR’] Returns the IP address of the host server
$_SERVER[‘SERVER_NAME’] Returns the name of the host server (such as www.w3schools.com)
$_SERVER[‘SERVER_SOFTWARE’] Returns the server identification string (such as Apache/2.2.24)
$_SERVER[‘SERVER_PROTOCOL’] Returns the name and revision of the information protocol (such as HTTP/1.1)
$_SERVER[‘REQUEST_METHOD’] Returns the request method used to access the page (such as POST)
$_SERVER[‘REQUEST_TIME’] Returns the timestamp of the start of the request (such as 1377687496)
$_SERVER[‘QUERY_STRING’] Returns the query string if the page is accessed via a query string
$_SERVER[‘HTTP_ACCEPT’] Returns the Accept header from the current request
$_SERVER[‘HTTP_ACCEPT_CHARSET’] Returns the Accept_Charset header from the current request (such as utf-8,ISO-8859-1)
$_SERVER[‘HTTP_HOST’] Returns the Host header from the current request
$_SERVER[‘HTTP_REFERER’] Returns the complete URL of the current page (not reliable because not all user-agents support it)
$_SERVER[‘HTTPS’] Is the script queried through a secure HTTP protocol
$_SERVER[‘REMOTE_ADDR’] Returns the IP address from where the user is viewing the current page
$_SERVER[‘REMOTE_HOST’] Returns the Host name from where the user is viewing the current page
$_SERVER[‘REMOTE_PORT’] Returns the port being used on the user’s machine to communicate with the web server
$_SERVER[‘SCRIPT_FILENAME’] Returns the absolute pathname of the currently executing script
$_SERVER[‘SERVER_ADMIN’] Returns the value given to the SERVER_ADMIN directive in the web server configuration file (if your script runs on a virtual host, it will be the value defined for that virtual host) (such as [email protected])
$_SERVER[‘SERVER_PORT’] Returns the port on the server machine being used by the web server for communication (such as 80)
$_SERVER[‘SERVER_SIGNATURE’] Returns the server version and virtual host name which are added to server-generated pages
$_SERVER[‘PATH_TRANSLATED’] Returns the file system based path to the current script
$_SERVER[‘SCRIPT_NAME’] Returns the path of the current script
$_SERVER[‘SCRIPT_URI’] Returns the URI of the current page

$_SERVER用来返回一些路径信息。

下面开始复习进阶内容,RUQUEST,GET,POST,REG表达式等等