1. This article is mainly for people with C language (or other programming languages) foundation to learn PHP quickly, so it will not explain too much about some basic knowledge, such as "=" is assignment, not equal.
2. This article is suitable for people who have learned a programming language and want to get started with PHP quickly.
3. Basically, you can get started after reading this article, and you can practice or advance.
PHP is the recursive abbreviation of Hypertext Preprocessor.
This is a server-side scripting language particularly suitable for web development.
It is a script running on the server, so the .php script cannot be opened directly with a browser. It needs to be parsed by the server and sent to the browser to view the web page content. Therefore, you need to enter the address in the browser to access the .php file, and then the server parses and sends the parsed Html to the browser in order to view the web page content.
If you want to run .php files on your own computer, you need to set up a configuration server environment first. Beginners can use integrated server components, such as XAMPP, download address: https://www.apachefriends.org/zh_cn/index.html
I won’t introduce too much in this part, you can go to Baidu.
PHP can be embedded anywhere in the Html document.
PHP scripts start with 583e18b95aeb7b912adb39467ad5cd81
.
Example:
<code class="language-php hljs" style="border: 0; border-radius: 4px; font-size: 90%; background-color: #d6dbdf; color: #4f424c; display: block; overflow-x: auto; background: #e7e9db; -webkit-text-size-adjust: none; padding: 0.5em;"><!DOCTYPE html> <html> <body> <h1>我的第一张 PHP 页面</h1> <span class="hljs-preprocessor" style="color: #f99b15;"><?php</span> <span class="hljs-keyword" style="color: #815ba4;">echo</span> <span class="hljs-string" style="color: #48b685;">'Hello World!'</span>; <span class="hljs-comment" style="color: #776e71;">// 输出 Hello World!</span> <span class="hljs-preprocessor" style="color: #f99b15;">?></span> </body> </html> </code>
PHP supports three styles of comments: C, C++, and Perl:
<code class="language-php hljs" style="border: 0; border-radius: 4px; font-size: 90%; background-color: #d6dbdf; color: #4f424c; display: block; overflow-x: auto; background: #e7e9db; -webkit-text-size-adjust: none; padding: 0.5em;"><span class="hljs-preprocessor" style="color: #f99b15;"><?php</span> <span class="hljs-keyword" style="color: #815ba4;">echo</span> <span class="hljs-string" style="color: #48b685;">'This is a test'</span>; <span class="hljs-comment" style="color: #776e71;">// This is a one-line c++ style comment</span> <span class="hljs-comment" style="color: #776e71;">/* This is a multi line comment yet another line of comment */</span> <span class="hljs-keyword" style="color: #815ba4;">echo</span> <span class="hljs-string" style="color: #48b685;">'This is yet another test'</span>; <span class="hljs-keyword" style="color: #815ba4;">echo</span> <span class="hljs-string" style="color: #48b685;">'One Final Test'</span>; <span class="hljs-comment" style="color: #776e71;"># This is a one-line shell-style comment</span> <span class="hljs-preprocessor" style="color: #f99b15;">?></span> </code>
PHP only variables are case sensitive.
All user-defined functions, classes, and keywords (such as if, else, echo, etc.) are case-insensitive.
PHP variables start with $ sign.
PHP is a weakly typed language, no need to declare types.
PHP variable names are case sensitive.
函数之外声明的变量拥有 Global 作用域,只能在函数以外进行访问。
函数内部声明的变量拥有 LOCAL 作用域,只能在函数内部进行访问。
例:
<code class="language-php hljs" style="border: 0; border-radius: 4px; font-size: 90%; background-color: #d6dbdf; color: #4f424c; display: block; overflow-x: auto; background: #e7e9db; -webkit-text-size-adjust: none; padding: 0.5em;"><span class="hljs-preprocessor" style="color: #f99b15;"><?php</span> <span class="hljs-variable" style="color: #ef6155;">$x</span>=<span class="hljs-number" style="color: #f99b15;">5</span>; <span class="hljs-comment" style="color: #776e71;">// 全局作用域</span> <span class="hljs-function" style="color: #06b6ef;"><span class="hljs-keyword" style="color: #815ba4;">function</span> <span class="hljs-title" style="color: #776e71;">myTest</span><span class="hljs-params" style="color: #f99b15;">()</span> </span>{ <span class="hljs-variable" style="color: #ef6155;">$y</span>=<span class="hljs-number" style="color: #f99b15;">10</span>; <span class="hljs-comment" style="color: #776e71;">// 局部作用域</span> <span class="hljs-keyword" style="color: #815ba4;">echo</span> <span class="hljs-string" style="color: #48b685;">"<p>测试函数内部的变量:</p>"</span>; <span class="hljs-keyword" style="color: #815ba4;">echo</span> <span class="hljs-string" style="color: #48b685;">"变量 x 是:$x"</span>; <span class="hljs-comment" style="color: #776e71;">// 输出 变量x是:</span> <span class="hljs-keyword" style="color: #815ba4;">echo</span> <span class="hljs-string" style="color: #48b685;">"<br>"</span>; <span class="hljs-keyword" style="color: #815ba4;">echo</span> <span class="hljs-string" style="color: #48b685;">"变量 y 是:$y"</span>; <span class="hljs-comment" style="color: #776e71;">// 输出 变量y是:10</span> } myTest(); <span class="hljs-keyword" style="color: #815ba4;">echo</span> <span class="hljs-string" style="color: #48b685;">"<p>测试函数之外的变量:</p>"</span>; <span class="hljs-keyword" style="color: #815ba4;">echo</span> <span class="hljs-string" style="color: #48b685;">"变量 x 是:$x"</span>; <span class="hljs-comment" style="color: #776e71;">// 输出 变量x是:5</span> <span class="hljs-keyword" style="color: #815ba4;">echo</span> <span class="hljs-string" style="color: #48b685;">"<br>"</span>; <span class="hljs-keyword" style="color: #815ba4;">echo</span> <span class="hljs-string" style="color: #48b685;">"变量 y 是:$y"</span>; <span class="hljs-comment" style="color: #776e71;">// 输出 变量y是:</span> <span class="hljs-preprocessor" style="color: #f99b15;">?></span> </code>
在函数内访问Global变量使用global
关键字。
例:
<code class="language-php hljs" style="border: 0; border-radius: 4px; font-size: 90%; background-color: #d6dbdf; color: #4f424c; display: block; overflow-x: auto; background: #e7e9db; -webkit-text-size-adjust: none; padding: 0.5em;"><span class="hljs-preprocessor" style="color: #f99b15;"><?php</span> <span class="hljs-variable" style="color: #ef6155;">$x</span>=<span class="hljs-number" style="color: #f99b15;">5</span>; <span class="hljs-variable" style="color: #ef6155;">$y</span>=<span class="hljs-number" style="color: #f99b15;">10</span>; <span class="hljs-function" style="color: #06b6ef;"><span class="hljs-keyword" style="color: #815ba4;">function</span> <span class="hljs-title" style="color: #776e71;">myTest</span><span class="hljs-params" style="color: #f99b15;">()</span> </span>{ <span class="hljs-keyword" style="color: #815ba4;">global</span> <span class="hljs-variable" style="color: #ef6155;">$x</span>,<span class="hljs-variable" style="color: #ef6155;">$y</span>; <span class="hljs-variable" style="color: #ef6155;">$y</span>=<span class="hljs-variable" style="color: #ef6155;">$x</span>+<span class="hljs-variable" style="color: #ef6155;">$y</span>; } myTest(); <span class="hljs-keyword" style="color: #815ba4;">echo</span> <span class="hljs-variable" style="color: #ef6155;">$y</span>; <span class="hljs-comment" style="color: #776e71;">// 输出 15</span> <span class="hljs-preprocessor" style="color: #f99b15;">?></span> </code>
也可以使用$GLOBALS超全局变量访问全局变量:
例:
<code class="language-php hljs" style="border: 0; border-radius: 4px; font-size: 90%; background-color: #d6dbdf; color: #4f424c; display: block; overflow-x: auto; background: #e7e9db; -webkit-text-size-adjust: none; padding: 0.5em;"><span class="hljs-preprocessor" style="color: #f99b15;"><?php</span> <span class="hljs-variable" style="color: #ef6155;">$x</span>=<span class="hljs-number" style="color: #f99b15;">5</span>; <span class="hljs-variable" style="color: #ef6155;">$y</span>=<span class="hljs-number" style="color: #f99b15;">10</span>; <span class="hljs-function" style="color: #06b6ef;"><span class="hljs-keyword" style="color: #815ba4;">function</span> <span class="hljs-title" style="color: #776e71;">myTest</span><span class="hljs-params" style="color: #f99b15;">()</span> </span>{ <span class="hljs-variable" style="color: #ef6155;">$GLOBALS</span>[<span class="hljs-string" style="color: #48b685;">'y'</span>]=<span class="hljs-variable" style="color: #ef6155;">$GLOBALS</span>[<span class="hljs-string" style="color: #48b685;">'x'</span>]+<span class="hljs-variable" style="color: #ef6155;">$GLOBALS</span>[<span class="hljs-string" style="color: #48b685;">'y'</span>]; } myTest(); <span class="hljs-keyword" style="color: #815ba4;">echo</span> <span class="hljs-variable" style="color: #ef6155;">$y</span>; <span class="hljs-comment" style="color: #776e71;">// 输出 15</span> <span class="hljs-preprocessor" style="color: #f99b15;">?></span> </code>
static
关键字声明静态变量。
当函数完成/执行后,不会删除静态变量。
超全局变量 在 PHP 4.1.0 中引入,是在全部作用域中始终可用的内置变量。在函数或方法中无需执行 global $variable; 就可以访问它们。
<code class="language-php hljs" style="border: 0; border-radius: 4px; font-size: 90%; background-color: #d6dbdf; color: #4f424c; display: block; overflow-x: auto; background: #e7e9db; -webkit-text-size-adjust: none; padding: 0.5em;"><span class="hljs-variable" style="color: #ef6155;">$GLOBALS</span>— 引用全局作用域中可用的全部变量 <span class="hljs-variable" style="color: #ef6155;">$_SERVER</span>— 服务器和执行环境信息 <span class="hljs-variable" style="color: #ef6155;">$_REQUEST</span>— HTTP Request 变量 <span class="hljs-variable" style="color: #ef6155;">$_POST</span>— HTTP POST 变量 <span class="hljs-variable" style="color: #ef6155;">$_GET</span>— HTTP GET 变量 <span class="hljs-variable" style="color: #ef6155;">$_FILES</span>— HTTP 文件上传变量 <span class="hljs-variable" style="color: #ef6155;">$_ENV</span>— 环境变量 <span class="hljs-variable" style="color: #ef6155;">$_COOKIE</span>— HTTP Cookies <span class="hljs-variable" style="color: #ef6155;">$_SESSION</span>— Session 变量 </code>
官方文档
可变变量是一种独特的变量,它允许动态改变一个变量名称。其工作原理是,该变量的名称由另外一个变量的值来确定。
例:
<code class="language-php hljs" style="border: 0; border-radius: 4px; font-size: 90%; background-color: #d6dbdf; color: #4f424c; display: block; overflow-x: auto; background: #e7e9db; -webkit-text-size-adjust: none; padding: 0.5em;"><span class="hljs-preprocessor" style="color: #f99b15;"><?php</span> <span class="hljs-variable" style="color: #ef6155;">$a</span> = <span class="hljs-string" style="color: #48b685;">'hello'</span>; <span class="hljs-variable" style="color: #ef6155;">$$a</span> = <span class="hljs-string" style="color: #48b685;">'world'</span>; <span class="hljs-preprocessor" style="color: #f99b15;">?></span> </code>
以上代码定义了两个变量,$a='hello',$hello='world'。
官方文档
在 PHP 中,有两种基本的输出方法:echo 和 print。
echo - 能够输出一个以上的字符串
print - 只能输出一个字符串,并始终返回 1
echo 比 print 稍快,因为它不返回任何值
echo 是一个语言结构,有无括号均可使用:echo 或 echo()。
print 也是语言结构,有无括号均可使用:print 或 print()。
字符串、整数、浮点数、布尔、数组、对象、NULL。
PHP字符串可以用单引号也可以用双引号。
单引号和双引号的区别是:双引号会解析里面的变量和转义字符,而单引号不会,单引号里的字符仅仅只有\(反斜杠)和'(单引号本身)需要转义:
<code class="language-php hljs" style="border: 0; border-radius: 4px; font-size: 90%; background-color: #d6dbdf; color: #4f424c; display: block; overflow-x: auto; background: #e7e9db; -webkit-text-size-adjust: none; padding: 0.5em;"> <span class="hljs-variable" style="color: #ef6155;">$str</span>=<span class="hljs-string" style="color: #48b685;">'Hello'</span>; <span class="hljs-keyword" style="color: #815ba4;">echo</span> <span class="hljs-string" style="color: #48b685;">'I\'ll say $str\n'</span>; <span class="hljs-comment" style="color: #776e71;">// 输出 I'll say $str\n</span> <span class="hljs-keyword" style="color: #815ba4;">echo</span> <span class="hljs-string" style="color: #48b685;">"I\'ll say $str\n"</span>; <span class="hljs-comment" style="color: #776e71;">// 输出 I'll say Hello </span> <span class="hljs-preprocessor" style="color: #f99b15;">?></span> </code>
建议使用单引号表示字符串,除非需要解析里面的变量。
heredoc & nowdoc
需要表示特别长的字符串的时候,可以使用heredoc和nowdoc语法,heredoc和nowdoc语法的区别相当于双引号和单引号的区别。
<code class="language-php hljs" style="border: 0; border-radius: 4px; font-size: 90%; background-color: #d6dbdf; color: #4f424c; display: block; overflow-x: auto; background: #e7e9db; -webkit-text-size-adjust: none; padding: 0.5em;"> <span class="hljs-comment" style="color: #776e71;">/*heredoc 语法 1. 由<<<标记名......标记名包围组成 2. 开始标记和结束标记名称要一致 3. 结束标记必须顶格写 4. 主体部分,会自动解析变量和转义字符 5. 但是函数、操作符和引号则不会被解析 */</span> <span class="hljs-variable" style="color: #ef6155;">$str</span> = <span class="hljs-string" style="color: #48b685;">'hello world'</span>; <span class="hljs-keyword" style="color: #815ba4;">echo</span> << Hello World <span class="hljs-variable" style="color: #ef6155;">$str</span> HTML; <span class="hljs-preprocessor" style="color: #f99b15;">?></span> </code>
<code class="language-php hljs" style="border: 0; border-radius: 4px; font-size: 90%; background-color: #d6dbdf; color: #4f424c; display: block; overflow-x: auto; background: #e7e9db; -webkit-text-size-adjust: none; padding: 0.5em;"><span class="hljs-preprocessor" style="color: #f99b15;"><?php</span> <span class="hljs-comment" style="color: #776e71;">/*nowdoc 语法 1. 区别就是开始标记名要加单引号,但结束标记名不要加单引号 2. 主体部分的变量和转义字符不会被解析 */</span> <span class="hljs-keyword" style="color: #815ba4;">echo</span> <span class="hljs-string" style="color: #48b685;"><<<'HTML' <html> <head> <title>Hello World</title> </head> <body> <p>hello world</p> </body> </html> HTML;</span> <span class="hljs-preprocessor" style="color: #f99b15;">?></span> </code>
heredoc和nowdoc特别适合输出很长的HTML文档,比直接以字符串的形式输出要容易阅读得多。
PHP数组其实一组键值对(key/value)。
创建数组:
<code class="language-php hljs" style="border: 0; border-radius: 4px; font-size: 90%; background-color: #d6dbdf; color: #4f424c; display: block; overflow-x: auto; background: #e7e9db; -webkit-text-size-adjust: none; padding: 0.5em;"><span class="hljs-variable" style="color: #ef6155;">$age</span>=<span class="hljs-keyword" style="color: #815ba4;">array</span>(<span class="hljs-string" style="color: #48b685;">"Peter"</span>=><span class="hljs-string" style="color: #48b685;">"35"</span>,<span class="hljs-string" style="color: #48b685;">"Ben"</span>=><span class="hljs-string" style="color: #48b685;">"37"</span>,<span class="hljs-string" style="color: #48b685;">"Joe"</span>=><span class="hljs-string" style="color: #48b685;">"43"</span>); </code>
或
<code class="language-php hljs" style="border: 0; border-radius: 4px; font-size: 90%; background-color: #d6dbdf; color: #4f424c; display: block; overflow-x: auto; background: #e7e9db; -webkit-text-size-adjust: none; padding: 0.5em;"><span class="hljs-variable" style="color: #ef6155;">$age</span>[<span class="hljs-string" style="color: #48b685;">'Peter'</span>]=<span class="hljs-string" style="color: #48b685;">"35"</span>; <span class="hljs-variable" style="color: #ef6155;">$age</span>[<span class="hljs-string" style="color: #48b685;">'Ben'</span>]=<span class="hljs-string" style="color: #48b685;">"37"</span>; <span class="hljs-variable" style="color: #ef6155;">$age</span>[<span class="hljs-string" style="color: #48b685;">'Joe'</span>]=<span class="hljs-string" style="color: #48b685;">"43"</span>; </code>
也可以不指定键值(key),那么默认的索引就是从0开始的有序数字:
<code class="language-php hljs" style="border: 0; border-radius: 4px; font-size: 90%; background-color: #d6dbdf; color: #4f424c; display: block; overflow-x: auto; background: #e7e9db; -webkit-text-size-adjust: none; padding: 0.5em;"><span class="hljs-variable" style="color: #ef6155;">$cars</span>=<span class="hljs-keyword" style="color: #815ba4;">array</span>(<span class="hljs-string" style="color: #48b685;">"Volvo"</span>,<span class="hljs-string" style="color: #48b685;">"BMW"</span>,<span class="hljs-string" style="color: #48b685;">"SAAB"</span>,<span class="hljs-number" style="color: #f99b15;">6</span>=><span class="hljs-string" style="color: #48b685;">"Audi"</span>,<span class="hljs-string" style="color: #48b685;">"Daewoo"</span>); </code>
相当于:
<code class="language-php hljs" style="border: 0; border-radius: 4px; font-size: 90%; background-color: #d6dbdf; color: #4f424c; display: block; overflow-x: auto; background: #e7e9db; -webkit-text-size-adjust: none; padding: 0.5em;"><span class="hljs-variable" style="color: #ef6155;">$cars</span>[<span class="hljs-number" style="color: #f99b15;">0</span>]=<span class="hljs-string" style="color: #48b685;">"Volvo"</span>; <span class="hljs-variable" style="color: #ef6155;">$cars</span>[<span class="hljs-number" style="color: #f99b15;">1</span>]=<span class="hljs-string" style="color: #48b685;">"BMW"</span>; <span class="hljs-variable" style="color: #ef6155;">$cars</span>[<span class="hljs-number" style="color: #f99b15;">2</span>]=<span class="hljs-string" style="color: #48b685;">"SAAB"</span>; <span class="hljs-variable" style="color: #ef6155;">$cars</span>[<span class="hljs-number" style="color: #f99b15;">6</span>]=<span class="hljs-string" style="color: #48b685;">"Audi"</span>; <span class="hljs-variable" style="color: #ef6155;">$cars</span>[<span class="hljs-number" style="color: #f99b15;">7</span>]=<span class="hljs-string" style="color: #48b685;">"Daewoo"</span>; </code>
遍历数组:
<code class="language-php hljs" style="border: 0; border-radius: 4px; font-size: 90%; background-color: #d6dbdf; color: #4f424c; display: block; overflow-x: auto; background: #e7e9db; -webkit-text-size-adjust: none; padding: 0.5em;"><span class="hljs-preprocessor" style="color: #f99b15;"><?php</span> <span class="hljs-variable" style="color: #ef6155;">$age</span>=<span class="hljs-keyword" style="color: #815ba4;">array</span>(<span class="hljs-string" style="color: #48b685;">"Bill"</span>=><span class="hljs-string" style="color: #48b685;">"35"</span>,<span class="hljs-string" style="color: #48b685;">"Steve"</span>=><span class="hljs-string" style="color: #48b685;">"37"</span>,<span class="hljs-string" style="color: #48b685;">"Peter"</span>=><span class="hljs-string" style="color: #48b685;">"43"</span>); <span class="hljs-keyword" style="color: #815ba4;">foreach</span>(<span class="hljs-variable" style="color: #ef6155;">$age</span> <span class="hljs-keyword" style="color: #815ba4;">as</span> <span class="hljs-variable" style="color: #ef6155;">$x</span>=><span class="hljs-variable" style="color: #ef6155;">$x_value</span>) { <span class="hljs-keyword" style="color: #815ba4;">echo</span> <span class="hljs-string" style="color: #48b685;">"Key="</span> . <span class="hljs-variable" style="color: #ef6155;">$x</span> . <span class="hljs-string" style="color: #48b685;">", Value="</span> . <span class="hljs-variable" style="color: #ef6155;">$x_value</span>; <span class="hljs-keyword" style="color: #815ba4;">echo</span> <span class="hljs-string" style="color: #48b685;">"<br>"</span>; } <span class="hljs-preprocessor" style="color: #f99b15;">?></span> </code>
当然也可以用for循环,count()返回数组元素个数:
<code class="language-php hljs" style="border: 0; border-radius: 4px; font-size: 90%; background-color: #d6dbdf; color: #4f424c; display: block; overflow-x: auto; background: #e7e9db; -webkit-text-size-adjust: none; padding: 0.5em;"><span class="hljs-preprocessor" style="color: #f99b15;"><?php</span> <span class="hljs-variable" style="color: #ef6155;">$cars</span> = <span class="hljs-keyword" style="color: #815ba4;">array</span>(<span class="hljs-string" style="color: #48b685;">"Volvo"</span>, <span class="hljs-string" style="color: #48b685;">"BMW"</span>, <span class="hljs-string" style="color: #48b685;">"Toyota"</span>); <span class="hljs-variable" style="color: #ef6155;">$arrlength</span> = count(<span class="hljs-variable" style="color: #ef6155;">$cars</span>); <span class="hljs-keyword" style="color: #815ba4;">for</span>(<span class="hljs-variable" style="color: #ef6155;">$x</span> = <span class="hljs-number" style="color: #f99b15;">0</span>; <span class="hljs-variable" style="color: #ef6155;">$x</span> < <span class="hljs-variable" style="color: #ef6155;">$arrlength</span>; <span class="hljs-variable" style="color: #ef6155;">$x</span>++) { <span class="hljs-keyword" style="color: #815ba4;">echo</span> <span class="hljs-variable" style="color: #ef6155;">$cars</span>[<span class="hljs-variable" style="color: #ef6155;">$x</span>]; <span class="hljs-keyword" style="color: #815ba4;">echo</span> <span class="hljs-string" style="color: #48b685;">"<br>"</span>; } <span class="hljs-preprocessor" style="color: #f99b15;">?></span> </code>
常量是一个固定值的标识符。
有效的常量名以字符或下划线开头(常量名称前面没有 $ 符号)。
常量默认是大小写敏感的。
通常,常量全部使用大写字母。
与变量不同,常量贯穿整个脚本是自动全局的。
使用 define() 函数设置常量:
首个参数定义常量的名称
第二个参数定义常量的值
可选的第三个参数规定常量名是否对大小写敏感。默认是 false,大小写敏感。
例:
<code class="language-php hljs" style="border: 0; border-radius: 4px; font-size: 90%; background-color: #d6dbdf; color: #4f424c; display: block; overflow-x: auto; background: #e7e9db; -webkit-text-size-adjust: none; padding: 0.5em;"><span class="hljs-preprocessor" style="color: #f99b15;"><?php</span> define(<span class="hljs-string" style="color: #48b685;">"FOO"</span>,<span class="hljs-string" style="color: #48b685;">"something"</span>); <span class="hljs-keyword" style="color: #815ba4;">echo</span> FOO; <span class="hljs-preprocessor" style="color: #f99b15;">?></span> </code>
加减乘除取余,自加自减和C语言一样。
连接两个字符串用“.”。
<code class="language-php hljs" style="border: 0; border-radius: 4px; font-size: 90%; background-color: #d6dbdf; color: #4f424c; display: block; overflow-x: auto; background: #e7e9db; -webkit-text-size-adjust: none; padding: 0.5em;"><span class="hljs-variable" style="color: #ef6155;">$str</span>=<span class="hljs-string" style="color: #48b685;">'and'</span>; <span class="hljs-variable" style="color: #ef6155;">$str</span>=<span class="hljs-string" style="color: #48b685;">'something'</span>.<span class="hljs-variable" style="color: #ef6155;">$str</span>; <span class="hljs-comment" style="color: #776e71;">// somethingand</span> </code>
和C语言基本相同,不同之处:
== 是相等,值相等类型可以不同,比如'1'==1,为真。
===是全等,不仅值相等,类型也要相同,比如'1'===1,为假。
!=和a8093152e673feb7aba1828c43532094都是不等于。
!==不全等,类型不同就是不全等。
$a 96b4fef55684b9312718d5de63fb7121 $b,$a小于$b时,等于-1,等于$b时,等于0,大于$b时,大于0. 这是PHP7加入的运算符。
<code class="language-php hljs" style="border: 0; border-radius: 4px; font-size: 90%; background-color: #d6dbdf; color: #4f424c; display: block; overflow-x: auto; background: #e7e9db; -webkit-text-size-adjust: none; padding: 0.5em;"> <span class="hljs-comment" style="color: #776e71;">// Integers</span> <span class="hljs-keyword" style="color: #815ba4;">echo</span> <span class="hljs-number" style="color: #f99b15;">1</span> <=> <span class="hljs-number" style="color: #f99b15;">1</span>; <span class="hljs-comment" style="color: #776e71;">// 0</span> <span class="hljs-keyword" style="color: #815ba4;">echo</span> <span class="hljs-number" style="color: #f99b15;">1</span> <=> <span class="hljs-number" style="color: #f99b15;">2</span>; <span class="hljs-comment" style="color: #776e71;">// -1</span> <span class="hljs-keyword" style="color: #815ba4;">echo</span> <span class="hljs-number" style="color: #f99b15;">2</span> <=> <span class="hljs-number" style="color: #f99b15;">1</span>; <span class="hljs-comment" style="color: #776e71;">// 1</span> <span class="hljs-comment" style="color: #776e71;">// Floats</span> <span class="hljs-keyword" style="color: #815ba4;">echo</span> <span class="hljs-number" style="color: #f99b15;">1.5</span> <=> <span class="hljs-number" style="color: #f99b15;">1.5</span>; <span class="hljs-comment" style="color: #776e71;">// 0</span> <span class="hljs-keyword" style="color: #815ba4;">echo</span> <span class="hljs-number" style="color: #f99b15;">1.5</span> <=> <span class="hljs-number" style="color: #f99b15;">2.5</span>; <span class="hljs-comment" style="color: #776e71;">// -1</span> <span class="hljs-keyword" style="color: #815ba4;">echo</span> <span class="hljs-number" style="color: #f99b15;">2.5</span> <=> <span class="hljs-number" style="color: #f99b15;">1.5</span>; <span class="hljs-comment" style="color: #776e71;">// 1</span> <span class="hljs-comment" style="color: #776e71;">// Strings</span> <span class="hljs-keyword" style="color: #815ba4;">echo</span> <span class="hljs-string" style="color: #48b685;">"a"</span> <=> <span class="hljs-string" style="color: #48b685;">"a"</span>; <span class="hljs-comment" style="color: #776e71;">// 0</span> <span class="hljs-keyword" style="color: #815ba4;">echo</span> <span class="hljs-string" style="color: #48b685;">"a"</span> <=> <span class="hljs-string" style="color: #48b685;">"b"</span>; <span class="hljs-comment" style="color: #776e71;">// -1</span> <span class="hljs-keyword" style="color: #815ba4;">echo</span> <span class="hljs-string" style="color: #48b685;">"b"</span> <=> <span class="hljs-string" style="color: #48b685;">"a"</span>; <span class="hljs-comment" style="color: #776e71;">// 1</span> <span class="hljs-keyword" style="color: #815ba4;">echo</span> <span class="hljs-string" style="color: #48b685;">"a"</span> <=> <span class="hljs-string" style="color: #48b685;">"aa"</span>; <span class="hljs-comment" style="color: #776e71;">// -1</span> <span class="hljs-keyword" style="color: #815ba4;">echo</span> <span class="hljs-string" style="color: #48b685;">"zz"</span> <=> <span class="hljs-string" style="color: #48b685;">"aa"</span>; <span class="hljs-comment" style="color: #776e71;">// 1</span> <span class="hljs-comment" style="color: #776e71;">// Arrays</span> <span class="hljs-keyword" style="color: #815ba4;">echo</span> [] <=> []; <span class="hljs-comment" style="color: #776e71;">// 0</span> <span class="hljs-keyword" style="color: #815ba4;">echo</span> [<span class="hljs-number" style="color: #f99b15;">1</span>, <span class="hljs-number" style="color: #f99b15;">2</span>, <span class="hljs-number" style="color: #f99b15;">3</span>] <=> [<span class="hljs-number" style="color: #f99b15;">1</span>, <span class="hljs-number" style="color: #f99b15;">2</span>, <span class="hljs-number" style="color: #f99b15;">3</span>]; <span class="hljs-comment" style="color: #776e71;">// 0</span> <span class="hljs-keyword" style="color: #815ba4;">echo</span> [<span class="hljs-number" style="color: #f99b15;">1</span>, <span class="hljs-number" style="color: #f99b15;">2</span>, <span class="hljs-number" style="color: #f99b15;">3</span>] <=> []; <span class="hljs-comment" style="color: #776e71;">// 1</span> <span class="hljs-keyword" style="color: #815ba4;">echo</span> [<span class="hljs-number" style="color: #f99b15;">1</span>, <span class="hljs-number" style="color: #f99b15;">2</span>, <span class="hljs-number" style="color: #f99b15;">3</span>] <=> [<span class="hljs-number" style="color: #f99b15;">1</span>, <span class="hljs-number" style="color: #f99b15;">2</span>, <span class="hljs-number" style="color: #f99b15;">1</span>]; <span class="hljs-comment" style="color: #776e71;">// 1</span> <span class="hljs-keyword" style="color: #815ba4;">echo</span> [<span class="hljs-number" style="color: #f99b15;">1</span>, <span class="hljs-number" style="color: #f99b15;">2</span>, <span class="hljs-number" style="color: #f99b15;">3</span>] <=> [<span class="hljs-number" style="color: #f99b15;">1</span>, <span class="hljs-number" style="color: #f99b15;">2</span>, <span class="hljs-number" style="color: #f99b15;">4</span>]; <span class="hljs-comment" style="color: #776e71;">// -1</span> <span class="hljs-comment" style="color: #776e71;">// Objects</span> <span class="hljs-variable" style="color: #ef6155;">$a</span> = (object) [<span class="hljs-string" style="color: #48b685;">"a"</span> => <span class="hljs-string" style="color: #48b685;">"b"</span>]; <span class="hljs-variable" style="color: #ef6155;">$b</span> = (object) [<span class="hljs-string" style="color: #48b685;">"a"</span> => <span class="hljs-string" style="color: #48b685;">"b"</span>]; <span class="hljs-keyword" style="color: #815ba4;">echo</span> <span class="hljs-variable" style="color: #ef6155;">$a</span> <=> <span class="hljs-variable" style="color: #ef6155;">$b</span>; <span class="hljs-comment" style="color: #776e71;">// 0</span> <span class="hljs-variable" style="color: #ef6155;">$a</span> = (object) [<span class="hljs-string" style="color: #48b685;">"a"</span> => <span class="hljs-string" style="color: #48b685;">"b"</span>]; <span class="hljs-variable" style="color: #ef6155;">$b</span> = (object) [<span class="hljs-string" style="color: #48b685;">"a"</span> => <span class="hljs-string" style="color: #48b685;">"c"</span>]; <span class="hljs-keyword" style="color: #815ba4;">echo</span> <span class="hljs-variable" style="color: #ef6155;">$a</span> <=> <span class="hljs-variable" style="color: #ef6155;">$b</span>; <span class="hljs-comment" style="color: #776e71;">// -1</span> <span class="hljs-variable" style="color: #ef6155;">$a</span> = (object) [<span class="hljs-string" style="color: #48b685;">"a"</span> => <span class="hljs-string" style="color: #48b685;">"c"</span>]; <span class="hljs-variable" style="color: #ef6155;">$b</span> = (object) [<span class="hljs-string" style="color: #48b685;">"a"</span> => <span class="hljs-string" style="color: #48b685;">"b"</span>]; <span class="hljs-keyword" style="color: #815ba4;">echo</span> <span class="hljs-variable" style="color: #ef6155;">$a</span> <=> <span class="hljs-variable" style="color: #ef6155;">$b</span>; <span class="hljs-comment" style="color: #776e71;">// 1</span> <span class="hljs-comment" style="color: #776e71;">// only values are compared</span> <span class="hljs-variable" style="color: #ef6155;">$a</span> = (object) [<span class="hljs-string" style="color: #48b685;">"a"</span> => <span class="hljs-string" style="color: #48b685;">"b"</span>]; <span class="hljs-variable" style="color: #ef6155;">$b</span> = (object) [<span class="hljs-string" style="color: #48b685;">"b"</span> => <span class="hljs-string" style="color: #48b685;">"b"</span>]; <span class="hljs-keyword" style="color: #815ba4;">echo</span> <span class="hljs-variable" style="color: #ef6155;">$a</span> <=> <span class="hljs-variable" style="color: #ef6155;">$b</span>; <span class="hljs-comment" style="color: #776e71;">// 1</span> <span class="hljs-preprocessor" style="color: #f99b15;">?></span> </code>
基本和C语言一样,不同之处:
多了xor异或。
$x xor $y
,如果 $x 和 $y 有且仅有一个为 true,则返回 true。
for循环
while循环
do while循环
switch开关
if else条件语句
和C语言一样,不同的是elseif
连起来写而不是写作else if
和弱类型语言JavaScript语法差不多,以function关键字开头,执行可以在定义的前面:
<code class="language-php hljs" style="border: 0; border-radius: 4px; font-size: 90%; background-color: #d6dbdf; color: #4f424c; display: block; overflow-x: auto; background: #e7e9db; -webkit-text-size-adjust: none; padding: 0.5em;"><span class="hljs-function" style="color: #06b6ef;"><span class="hljs-keyword" style="color: #815ba4;">function</span> <span class="hljs-title" style="color: #776e71;">function_name</span><span class="hljs-params" style="color: #f99b15;">()</span></span>{ <span class="hljs-comment" style="color: #776e71;">// TODO:</span> } </code>
参数可以有默认值
<code class="language-php hljs" style="border: 0; border-radius: 4px; font-size: 90%; background-color: #d6dbdf; color: #4f424c; display: block; overflow-x: auto; background: #e7e9db; -webkit-text-size-adjust: none; padding: 0.5em;"> <span class="hljs-function" style="color: #06b6ef;"><span class="hljs-keyword" style="color: #815ba4;">function</span> <span class="hljs-title" style="color: #776e71;">setHeight</span><span class="hljs-params" style="color: #f99b15;">(<span class="hljs-variable" style="color: #ef6155;">$minheight</span>=<span class="hljs-number" style="color: #f99b15;">50</span>)</span> </span>{ <span class="hljs-keyword" style="color: #815ba4;">echo</span> <span class="hljs-string" style="color: #48b685;">"The height is : $minheight "</span>; } setHeight(<span class="hljs-number" style="color: #f99b15;">350</span>); setHeight(); <span class="hljs-comment" style="color: #776e71;">// 将使用默认值 50</span> setHeight(<span class="hljs-number" style="color: #f99b15;">135</span>); setHeight(<span class="hljs-number" style="color: #f99b15;">80</span>); <span class="hljs-preprocessor" style="color: #f99b15;">?></span> </code>
参数可以使用引用传递,从而形参和实参指向同一块内存:
<code class="language-php hljs" style="border: 0; border-radius: 4px; font-size: 90%; background-color: #d6dbdf; color: #4f424c; display: block; overflow-x: auto; background: #e7e9db; -webkit-text-size-adjust: none; padding: 0.5em;"> <span class="hljs-variable" style="color: #ef6155;">$x</span>=<span class="hljs-number" style="color: #f99b15;">1</span>; <span class="hljs-variable" style="color: #ef6155;">$y</span>=<span class="hljs-number" style="color: #f99b15;">2</span>; <span class="hljs-function" style="color: #06b6ef;"><span class="hljs-keyword" style="color: #815ba4;">function</span> <span class="hljs-title" style="color: #776e71;">exchange</span><span class="hljs-params" style="color: #f99b15;">(&<span class="hljs-variable" style="color: #ef6155;">$x</span>,&<span class="hljs-variable" style="color: #ef6155;">$y</span>)</span></span>{ <span class="hljs-variable" style="color: #ef6155;">$temp</span>=<span class="hljs-variable" style="color: #ef6155;">$x</span>; <span class="hljs-variable" style="color: #ef6155;">$x</span>=<span class="hljs-variable" style="color: #ef6155;">$y</span>; <span class="hljs-variable" style="color: #ef6155;">$y</span>=<span class="hljs-variable" style="color: #ef6155;">$temp</span>; } exchange(<span class="hljs-variable" style="color: #ef6155;">$x</span>,<span class="hljs-variable" style="color: #ef6155;">$y</span>); <span class="hljs-comment" style="color: #776e71;">// $x,$y的值被交换了</span> <span class="hljs-comment" style="color: #776e71;">// 调用函数的时候参数前面不要加&</span> <span class="hljs-preprocessor" style="color: #f99b15;">?></span> </code>
函数可以返回引用,如果要返回引用,函数声明时要加&,将返回的引用赋值给一个变量时也要加&:
<code class="nohighlight hljs" style="border: 0; border-radius: 4px; font-size: 90%; background-color: #d6dbdf; color: #4f424c; display: block; overflow-x: auto; background: #e7e9db; -webkit-text-size-adjust: none; padding: 0.5em;"> function &test() { static $b=0;//申明一个静态变量 $b=$b+1; echo $b; return $b; } $a=test();//这条语句会输出 $b的值 为1 $a=5; $a=test();//这条语句会输出 $b的值 为2 $a=&test();//这条语句会输出 $b的值 为3 $a=5; $a=test();//这条语句会输出 $b的值 为6 ?> </code>
require和include可以将 PHP 文件的内容插入另一个 PHP 文件(在服务器执行它之前)。
包含可用于创建可在多个页面重复使用的函数、页眉、页脚或元素。
语法,加上文件名即可,或者加上括号:
<code class="language-php hljs" style="border: 0; border-radius: 4px; font-size: 90%; background-color: #d6dbdf; color: #4f424c; display: block; overflow-x: auto; background: #e7e9db; -webkit-text-size-adjust: none; padding: 0.5em;"><span class="hljs-keyword" style="color: #815ba4;">require</span> <span class="hljs-string" style="color: #48b685;">'file.php'</span>; <span class="hljs-keyword" style="color: #815ba4;">require</span> (<span class="hljs-string" style="color: #48b685;">'file.txt'</span>); <span class="hljs-keyword" style="color: #815ba4;">include</span> <span class="hljs-string" style="color: #48b685;">'file.txt'</span>; <span class="hljs-keyword" style="color: #815ba4;">include</span> (<span class="hljs-string" style="color: #48b685;">'file.php'</span>); </code>
区别:
错误处理不同,require 会生成致命错误(E_COMPILE_ERROR)并停止脚本,include 只生成警告(E_WARNING),并且脚本会继续
使用弹性不同,require通常放在PHP程序的最前面,PHP程序在执行前会先读入require所指定引入的档案,使它变成程序网页的一部分;include通常放在流程控制处理中,PHP程序读到include的档案时,才将它读进来。
和require、include的区别就是:如果该文件中的代码已经被包括了,则不会再次包括。
<code class="language-php hljs" style="border: 0; border-radius: 4px; font-size: 90%; background-color: #d6dbdf; color: #4f424c; display: block; overflow-x: auto; background: #e7e9db; -webkit-text-size-adjust: none; padding: 0.5em;"> <span class="hljs-class"><span class="hljs-keyword" style="color: #815ba4;">class</span> <span class="hljs-title" style="color: #776e71;">phpClass</span> </span>{ <span class="hljs-keyword" style="color: #815ba4;">var</span> <span class="hljs-variable" style="color: #ef6155;">$var1</span>; <span class="hljs-keyword" style="color: #815ba4;">var</span> <span class="hljs-variable" style="color: #ef6155;">$var2</span> = <span class="hljs-string" style="color: #48b685;">"constant string"</span>; <span class="hljs-function" style="color: #06b6ef;"><span class="hljs-keyword" style="color: #815ba4;">function</span> <span class="hljs-title" style="color: #776e71;">myfunc</span> <span class="hljs-params" style="color: #f99b15;">(<span class="hljs-variable" style="color: #ef6155;">$arg1</span>, <span class="hljs-variable" style="color: #ef6155;">$arg2</span>)</span> </span>{ [..] } [..] } <span class="hljs-preprocessor" style="color: #f99b15;">?></span> </code>
类使用 class 关键字后加上类名定义。
类名后的一对大括号({})内可以定义变量和方法。
类的变量使用 var 来声明, 变量也可以初始化值。
函数定义类似 PHP 函数的定义,但函数只能通过该类及其实例化的对象访问。
栗子:
<code class="language-php hljs" style="border: 0; border-radius: 4px; font-size: 90%; background-color: #d6dbdf; color: #4f424c; display: block; overflow-x: auto; background: #e7e9db; -webkit-text-size-adjust: none; padding: 0.5em;"> <span class="hljs-comment" style="color: #776e71;">/*** Define MyClass*/</span> <span class="hljs-class"><span class="hljs-keyword" style="color: #815ba4;">class</span> <span class="hljs-title" style="color: #776e71;">MyClass</span> </span>{ <span class="hljs-comment" style="color: #776e71;">//Declare a public constructor</span> <span class="hljs-keyword" style="color: #815ba4;">public</span> <span class="hljs-function" style="color: #06b6ef;"><span class="hljs-keyword" style="color: #815ba4;">function</span> <span class="hljs-title" style="color: #776e71;">__construct</span><span class="hljs-params" style="color: #f99b15;">()</span> </span>{ } <span class="hljs-comment" style="color: #776e71;">//Declare a public method</span> <span class="hljs-keyword" style="color: #815ba4;">public</span> <span class="hljs-function" style="color: #06b6ef;"><span class="hljs-keyword" style="color: #815ba4;">function</span> <span class="hljs-title" style="color: #776e71;">MyPublic</span><span class="hljs-params" style="color: #f99b15;">()</span> </span>{ } <span class="hljs-comment" style="color: #776e71;"> // Declare a protected method </span> <span class="hljs-keyword" style="color: #815ba4;">protected</span> <span class="hljs-function" style="color: #06b6ef;"><span class="hljs-keyword" style="color: #815ba4;">function</span> <span class="hljs-title" style="color: #776e71;">MyProtected</span><span class="hljs-params" style="color: #f99b15;">()</span> </span>{ } <span class="hljs-comment" style="color: #776e71;">//Declare a private method</span> <span class="hljs-keyword" style="color: #815ba4;">private</span> <span class="hljs-function" style="color: #06b6ef;"><span class="hljs-keyword" style="color: #815ba4;">function</span> <span class="hljs-title" style="color: #776e71;">MyPrivate</span><span class="hljs-params" style="color: #f99b15;">()</span> </span>{ } <span class="hljs-comment" style="color: #776e71;">//This method is public</span> <span class="hljs-function" style="color: #06b6ef;"><span class="hljs-keyword" style="color: #815ba4;">function</span> <span class="hljs-title" style="color: #776e71;">Foo</span><span class="hljs-params" style="color: #f99b15;">()</span> </span>{ <span class="hljs-variable" style="color: #ef6155;">$this</span>->MyPublic(); <span class="hljs-variable" style="color: #ef6155;">$this</span>->MyProtected(); <span class="hljs-variable" style="color: #ef6155;">$this</span>->MyPrivate(); } } <span class="hljs-variable" style="color: #ef6155;">$myclass</span> = <span class="hljs-keyword" style="color: #815ba4;">new</span> MyClass; <span class="hljs-variable" style="color: #ef6155;">$myclass</span>->MyPublic(); <span class="hljs-comment" style="color: #776e71;">// This line can be executed normally</span> <span class="hljs-variable" style="color: #ef6155;">$myclass</span>->MyProtected(); <span class="hljs-comment" style="color: #776e71;">// This line will generate a fatal error</span> <span class="hljs-variable" style="color: #ef6155;">$myclass</span>->MyPrivate(); <span class="hljs-comment" style="color: #776e71;">// This line will generate a fatal error</span> <span class="hljs-variable" style="color: #ef6155;">$myclass</span>->Foo(); <span class="hljs-comment" style="color: #776e71;">// Can be executed by public, protected or private</span> <span class="hljs-comment" style="color: #776e71;">/*** Define MyClass2*/</span> <span class="hljs-class"><span class="hljs-keyword" style="color: #815ba4;">class</span> <span class="hljs-title" style="color: #776e71;">MyClass2</span> <span class="hljs-keyword" style="color: #815ba4;">extends</span> <span class="hljs-title" style="color: #776e71;">MyClass</span> </span>{ <span class="hljs-comment" style="color: #776e71;">//This method is public</span> <span class="hljs-function" style="color: #06b6ef;"><span class="hljs-keyword" style="color: #815ba4;">function</span> <span class="hljs-title" style="color: #776e71;">Foo2</span><span class="hljs-params" style="color: #f99b15;">()</span> </span>{ <span class="hljs-variable" style="color: #ef6155;">$this</span>->MyPublic(); <span class="hljs-variable" style="color: #ef6155;">$this</span>->MyProtected(); <span class="hljs-variable" style="color: #ef6155;">$this</span>->MyPrivate(); <span class="hljs-comment" style="color: #776e71;">// This line will generate a fatal error</span> } }<span class="hljs-variable" style="color: #ef6155;">$myclass2</span> = <span class="hljs-keyword" style="color: #815ba4;">new</span> MyClass2; <span class="hljs-variable" style="color: #ef6155;">$myclass2</span>->MyPublic(); <span class="hljs-comment" style="color: #776e71;">// This line can be executed normally</span> <span class="hljs-variable" style="color: #ef6155;">$myclass2</span>->Foo2(); <span class="hljs-comment" style="color: #776e71;">// Both public and protected ones can be executed, but private ones cannot</span> <span class="hljs-class"><span class="hljs-keyword" style="color: #815ba4;">class</span> <span class="hljs-title" style="color: #776e71;">Bar</span> </span>{ <span class="hljs-keyword" style="color: #815ba4;">public</span> <span class="hljs-function" style="color: #06b6ef;"><span class="hljs-keyword" style="color: #815ba4;">function</span> <span class="hljs-title" style="color: #776e71;">test</span><span class="hljs-params" style="color: #f99b15;">()</span> </span>{ <span class="hljs-variable" style="color: #ef6155;">$this</span>->testPrivate(); <span class="hljs-variable" style="color: #ef6155;">$this</span>->testPublic(); } <span class="hljs-keyword" style="color: #815ba4;">public</span> <span class="hljs-function" style="color: #06b6ef;"><span class="hljs-keyword" style="color: #815ba4;">function</span> <span class="hljs-title" style="color: #776e71;">testPublic</span><span class="hljs-params" style="color: #f99b15;">()</span> </span>{ <span class="hljs-keyword" style="color: #815ba4;">echo</span> <span class="hljs-str"></span></code>