首頁  >  文章  >  php教程  >  PHP快速教學

PHP快速教學

WBOY
WBOY原創
2016-08-04 08:53:111388瀏覽

1.本文主要針對有C語言(或其他程式語言)基礎的人快速學習PHP的,所以不會對一些基礎知識作過多解釋,例如「=」是賦值不是等於。
2.本文適合已學過程式語言,想要快速入門PHP的人。
3.基本上看完本文即可入門,可以動手實作或進階。


簡介

PHP是Hypertext Preprocessor(超文本預處理器)的遞歸縮寫。
這是一種特別適合網頁開發的伺服器端腳本語言。
它是運行在伺服器上的腳本,因此不能直接用瀏覽器開啟.php腳本,需要伺服器解析後傳送給瀏覽器才能查看網頁內容。因此要在瀏覽器裡輸入地址訪問.php文件,然後伺服器解析後發送給瀏覽器解析後的Html,才能查看網頁內容。
如果要在自己電腦上執行.php文件,需要先建立設定伺服器環境,初學者可以使用整合式伺服器元件,例如XAMPP,下載位址:https://www.apachefriends.org/zh_cn/index.html
這部分不作過多介紹,大家可以去百度。

文法

標記

PHP可以內嵌在Html文件中的任何位置。
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;"><!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支援C、C++、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只有變數大小寫敏感。
所有使用者定義的函數、類別和關鍵字(例如 if、else、echo 等等)都對大小寫不敏感。

變數

  • PHP變數以$符號開頭。

  • PHP是弱型別語言,無需聲明型別。

  • PHP變數名稱大小寫敏感。

作用域

  • 函数之外声明的变量拥有 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>&mdash; 引用全局作用域中可用的全部变量
<span class="hljs-variable" style="color: #ef6155;">$_SERVER</span>&mdash; 服务器和执行环境信息
<span class="hljs-variable" style="color: #ef6155;">$_REQUEST</span>&mdash; HTTP Request 变量
<span class="hljs-variable" style="color: #ef6155;">$_POST</span>&mdash; HTTP POST 变量
<span class="hljs-variable" style="color: #ef6155;">$_GET</span>&mdash; HTTP GET 变量
<span class="hljs-variable" style="color: #ef6155;">$_FILES</span>&mdash; HTTP 文件上传变量
<span class="hljs-variable" style="color: #ef6155;">$_ENV</span>&mdash; 环境变量
<span class="hljs-variable" style="color: #ef6155;">$_COOKIE</span>&mdash; HTTP Cookies
<span class="hljs-variable" style="color: #ef6155;">$_SESSION</span>&mdash; 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 & 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() 函数设置常量:

  1. 首个参数定义常量的名称

  2. 第二个参数定义常量的值

  3. 可选的第三个参数规定常量名是否对大小写敏感。默认是 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,为真。

  2. ===是全等,不仅值相等,类型也要相同,比如'1'===1,为假。

  3. !=和a8093152e673feb7aba1828c43532094都是不等于。

  4. !==不全等,类型不同就是不全等。

  5. $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语言一样,不同之处:

  1. 多了xor异或。
    $x xor $y,如果 $x 和 $y 有且仅有一个为 true,则返回 true。

流程控制

  1. for循环

  2. while循环

  3. do while循环

  4. switch开关

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

文件包含

requireinclude可以将 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_once()和include_once()

和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;">/*** 定義我的類別*/</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;">// 宣告一個公有的建構子</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;">// 宣告一個公有的方法</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;">// 聲明一個受保護的方法</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;">// 宣告一個私有的方法</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;">// 此方法為公有</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;">// 這行能被正常執行</span>
<span class="hljs-variable" style="color: #ef6155;">$myclass</span>->MyProtected(); <span class="hljs-comment" style="color: #776e71;">// 這行會產生一個致命錯誤</span>
<span class="hljs-variable" style="color: #ef6155;">$myclass</span>->MyPrivate(); <span class="hljs-comment" style="color: #776e71;">// 這行會產生一個致命錯誤</span>
<span class="hljs-variable" style="color: #ef6155;">$myclass</span>->Foo(); <span class="hljs-comment" style="color: #776e71;">// 公有,受保護,私有都可以執行</span>


<span class="hljs-comment" style="color: #776e71;">/*** 定義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;">// 此方法為公有</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;">// 這行會產生一個致命錯誤</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;">// 這行能被正常執行</span>
<span class="hljs-variable" style="color: #ef6155;">$myclass2</span>->Foo2(); <span class="hljs-comment" style="color: #776e71;">// 公有的和受保護的都可執行,但私有的不行</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>
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn