PHP学习(3):循环与超全局变量
上次学习了常量constant
这次先复习一下运算符
强相等必须是类型和内容都相等,弱相等只需要有同样的键值对
1 |
|
也就是键值对相同,内容的类型可以不相同,比如一个字符串一个数字。
下面看三目运算符:
1 |
|
输出结果预料之中。
下面看
if else:
1 |
|
下面加上else:
1 |
|
下面引入else if
1 |
|
下面看switch语句:
1 |
|
其实和C语言的结构基本上一样!
下面看循环
PHP的循环有好多种:
对于while循环
1 |
|
下面看do while循环
1 |
|
注意:dowhile循环里面至少循环了一次do过程,因为检验放在了后面,即使检验结果是错的,也会执行一次do
下面看for语句
1 |
|
不过PHP比C多了一个循环:foreach
foreach只工作在数组中,用来循环每一个键值对。
1 |
|
运行如图所示。
下面看遍历复杂键值对
1 |
|
如图输出对应键值。
下面看BREAK/CONTINUE:
用来跳出switch语句和各种循环!
1 |
|
可以看出到4就已经跳出了循环,只输出123。
而continue只跳过一次循环,并不跳出所有次数循环(只跳过当前)
1 |
|
如图,只跳过了4一个。
上述循环也可以用while代替:
1 |
|
输出结果和上述for语句中带break相同。
PHP有超过1000个已构建函数,可以直接调用用来完成一些任务。
注意:
函数名必须以字母或者下划线开头。函数名是不区分大小写的。
我们直接看一个两个parameter的函数:
1 |
|
输出结果可想而知。
注:PHP将传参变量的值自动关联到数据类型。
注:在PHP7中要声明类型。
1 |
|
这里注意,会返回10,因为会把字符串的数据类型转换成整型,提取出整数5.
PHP函数中有一个默认赋值,即不从外部传参时会把函数体内部的赋值在外面用,比如:
1 |
|
下面用RETURN来输出函数值:
1 |
|
输出就不多说了,显而易见。
下面说函数值的类型:
1 |
|
会进行类型转换。
下面看最难的引用传递:分为变量的引用传递和函数的引用传递
1 | #先看变量的引用传递 |
实际上是$str修改了地址,新地址与$string共用同一值。
突然明白了,&这就是C语言的指针,传入的参数是变量的地址!将地址的内容修改了
指针函数是一个返回指针的函数!
函数指针是一个指向函数的指针!把函数的地址赋给指针!
上面是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表达式等等