PHP 学习

语法特性

  • HTMLbody 标签里的 <?php ?> 标签里写语句

  • 语句末尾要加 ;

  • 注释:// 单行注释 # 单行注释 /*多行注释*/

  • 换行符可用 PHP_EOL 替代

  • 不区分大小写:关键词、类名、函数名

  • 区分大小写:变量名

  • 0null 值相等,类型不相等

  • 变量定义:$变量名

  • 变量作用域:

    local:函数内部声明的变量,仅能在函数内部访问

    global $变量名:函数内部调用外部全局变量,也可以使用 $GLOBALS[变量名] 访问全局变量

    static $变量名:函数执行结束后变量的值不被删除

  • 常量定义:define("__大写常量名__", 常量值)

  • $x <=> $yx<y返回-1,x=y返回0,x>y返回+1

  • 打印输出:echo 输出的速度比 print 快,可以使用括号,也可以不用括号

    echo $变量名:可以输出一个或多个字符串,没有返回值

    print $变量名:只允许输出一个字符串,返回值总为 1

    print_r():可以结构化打印数组变量,可以设置返回值

    var_dump():打印数据类型和值,可以结构化打印数组变量

  • . 拼接字符串

  • 引号的区别:

    单引号:不解析字符串里的 $变量名

    双引号:解析字符串里的 $变量名

  • 数组:可以用 array()[] 两种方法定义

    索引数组["orange", "apple", "banana"]

    关联数组["a"=>"星期一", "b"=>"星期二", "c"=>"星期三"]

数据类型

自动类型转换:2+"2abc"=4

严格类型要求:<?php declare(strict_types=1); ?>

函数返回类型声明:(严格类型条件下)

变量引用

字符串函数

数组迭代器

数组函数

常用函数

require 和 include

文件操作

文件操作函数

文件打开模式

正则表达式

toki 为要比配的正则表达式,i 为不区分大小写,界定符通常使用 /,也可以用 # ~ 代替

Function
Description

preg_match($pattern, $str)

匹配到返回1,否则返回0

preg_match_all($pattern, $str)

返回匹配到的次数

preg_replace()

返回被替换的整个字符串

Modifier
Description

i

Performs a case-insensitive search

m

Performs a multiline search (patterns that search for the beginning or end of a string will match the beginning or end of each line)

u

Enables correct matching of UTF-8 encoded patterns

Metacharacter
Description

|

Find a match for any one of the patterns separated by | as in: cat|dog|fish

.

Find just one instance of any character

^

Finds a match as the beginning of a string as in: ^Hello

$

Finds a match at the end of the string as in: World$

\d

Find a digit

\s

Find a whitespace character

\b

Find a match at the beginning of a word like this: \bWORD, or at the end of a word like this: WORD\b

\uxxxx

Find the Unicode character specified by the hexadecimal number xxxx

Quantifier
Description

n+

Matches any string that contains at least one n

n*

Matches any string that contains zero or more occurrences of n

n?

Matches any string that contains zero or one occurrences of n

n{x}

Matches any string that contains a sequence of X n's

n{x,y}

Matches any string that contains a sequence of X to Y n's

n{x,}

Matches any string that contains a sequence of at least X n's

preg_match()

preg_match_all()

preg_replace()

PHP 爬虫

file_get_contents() 和 file_put_contents()

Note:如果要打开有特殊字符的 URL,就需要使用 urlencode() 进行 URL 编码。

file_get_contents() 函数超时处理

在使用 file_get_contents 函数读取文件或URL的时候,经常会出现超时的情况,我们可以通过一些方法来尽量的避免或者解决。

增加超时的时间限制

这里需要注意:set_time_limit(0) 只是设置你的PHP程序的超时时间,而不是 file_get_contents 函数读取URL的超时时间。

想通过 set_time_limit(0) 影响到 file_get_contents 来增加超时时间,经测试,是无效的。真正的修改 file_get_contents 延时可以用 resource $contexttimeout 参数,timeout 参数的单位是 s(秒)。

实例:

超过几秒就重新执行,多次重试直到成功

file_get_contents 超时有时候失败是因为网络等因素造成,没有解决办法,但是可以修改程序,失败时重试几次,仍然失败就放弃,因为 file_get_contents() 如果失败将返回 FALSE,所以可以下面这样编写代码:

注意:如果执行时间过长,还需要用函数 set_time_limit(0); 就不会超时了。

file_get_contents() 函数代理设置处理

设置代理 IP 去获取页面数据

设置需要验证的代理 IP 去采集获取页面数据

超全局变量

$_SERVER

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

$_REQUEST

$_POST

$_GET

header() 函数

作用:发送一个原始 HTTP 标头到客户端,服务器以 HTTP 协义传 HTML 到浏览器前所送出的字串,在 PHP 中送回 HTML 资料前,需先传完所有的标头。

内容类型

页面功能

数据传输

传值

  • window.location.href:返回当前页面 url

  • window.location.hostname:返回网站域名

  • window.location.pathname:放回当前 url 的相对路径

  • window.location.protocol:返回网页端口号

  • window.location.assign("新的 url"):加载新的页面

取值

表单写法

文件上传校验

Session 的工作机制是:为每个访客创建一个唯一的 id (UID),并基于这个 UID 来存储变量。UID 存储在 cookie 中,或者通过 URL 进行传导。

  1. 安全性 Session:存储服务器端,安全性高 Cookie:存储浏览器端,安全性低

  2. 数据大小 Cookie:的数量和大小都有限制 Session:数据存储不限

  3. 可用数据类型 Cookie:只能存储简单数据,数值 字符串 Session:可以存储复杂数据(自动序列化)

  4. 保存位置 Cookie:保存在浏览器上 Session:保存在服务器上

  • setcookie("字段", "值", 生命周期, "存放路径", "有效域名", 是否只允许https, 是否不允许脚本访问);

时间戳:默认为 0,代表浏览器关闭就删除 cookie 存放路径:父目录中设置过的 cookie 可以在子目录中获取到,反之不行,一般用 '/'

设置 Session

  • session_set_cookie_params(lifetime, path, domain, secure, httponly):在调用 session_start() 函数之前调用,PHP 7.3 版本以上才有效

  • session_start():启动会话,必须位于 <html> 标签之前

  • unset($_SESSION["name"])

  • $_SESSION = array()

  • session_unset() 释放当前在内存中已经创建的所有 $_SESSION 变量,但不删除 session 文件以及不释放对应的 session id

  • session_destroy():删除当前用户对应的 session 文件以及释放 session id,内存中的 $_SESSION 变量内容依然保留

常用过滤条件

$_FILES['']['error']

  1. UPLOAD_ERR_OK 其值为 0,没有错误发生,文件上传成功。

  2. UPLOAD_ERR_INI_SIZE 其值为 1,上传的文件超过了 php.ini 中 upload_max_filesize选项限制的值。

  3. UPLOAD_ERR_FORM_SIZE 其值为 2,上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值。

  4. UPLOAD_ERR_PARTIAL 其值为 3,文件只有部分被上传。

  5. UPLOAD_ERR_NO_FILE 其值为 4,没有文件被上传。

  6. UPLOAD_ERR_NO_TMP_DIR 其值为 6,找不到临时文件夹。PHP 4.3.10 和 PHP 5.0.3 引进。

  7. UPLOAD_ERR_CANT_WRITE 其值为 7,文件写入失败。PHP 5.1.0 引进。

数据库连接

面向对象

  • 类的变量用 var 声明

  • 类的常量用 const 声明,常量不需要用$符

  • 静态、常量、重写的属性与方法 调用:::

  • 对象的属性与方法 调用:->

  • 类:Class

  • 继承:extends

  • 接口:interfaceimplements

  • this关键字:$this -> name

  • 构造函数:__construct() - 实例化对象时自动调用

  • 析构函数:__destruct() - 对象被释放时自动调用

  • 访问修饰符:public(默认)privateprotected

算法

冒泡排序

Last updated