Heim >php教程 >php手册 >PHP-Kurzanleitung

PHP-Kurzanleitung

WBOY
WBOYOriginal
2016-08-04 08:53:111420Durchsuche

1. Dieser Artikel richtet sich hauptsächlich an Personen mit Grundkenntnissen in der C-Sprache (oder anderen Programmiersprachen), um PHP schnell zu erlernen. Daher wird nicht zu viel über einige Grundkenntnisse erklärt, z. B. „=“ ist eine Aufgabe gleich.
2. Dieser Artikel ist für Personen geeignet, die eine Programmiersprache gelernt haben und schnell mit PHP durchstarten möchten.
3. Grundsätzlich können Sie nach dem Lesen dieses Artikels beginnen und üben oder Fortschritte machen.


Einführung

PHP ist die rekursive Abkürzung für Hypertext Preprocessor.
Dies ist eine serverseitige Skriptsprache, die sich besonders für die Webentwicklung eignet.
Da es sich um ein Skript handelt, das auf dem Server ausgeführt wird, kann es nicht direkt mit einem Browser geöffnet werden. Das .php-Skript muss vom Server analysiert und an den Browser gesendet werden, um den Inhalt der Webseite anzuzeigen. Daher müssen Sie die Adresse in den Browser eingeben, um auf die PHP-Datei zuzugreifen. Anschließend analysiert der Server das analysierte HTML und sendet es an den Browser, um den Inhalt der Webseite anzuzeigen.
Wenn Sie .php-Dateien auf Ihrem eigenen Computer ausführen möchten, müssen Sie zunächst eine Konfigurationsserverumgebung einrichten. Anfänger können integrierte Serverkomponenten wie XAMPP verwenden, Download-Adresse: https://www.apachefriends.org/zh_cn/. index.html
Ich werde in diesem Teil nicht zu viel vorstellen, Sie können zu Baidu gehen.

Grammatik

Mark

PHP kann überall im HTML-Dokument eingebettet werden.
PHP-Skripte beginnen mit 71ef9135a3e42222be9e2e21d78ffd5e.
Beispiel:

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

Kommentare

PHP unterstützt drei Kommentarstile: C, C und 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>

Groß-/Kleinschreibung beachten

PHP verfügt nur über Variablen, bei denen die Groß-/Kleinschreibung beachtet wird.
Bei allen benutzerdefinierten Funktionen, Klassen und Schlüsselwörtern (z. B. if, else, echo usw.) wird die Groß-/Kleinschreibung nicht beachtet.

Variable

  • PHP-Variablen beginnen mit dem $-Zeichen.

  • PHP ist eine schwach typisierte Sprache und muss keine Typen deklarieren.

  • PHP-Variablennamen unterscheiden zwischen Groß- und Kleinschreibung.

作用域

  • 函数之外声明的变量拥有 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;">/*** Definieren Sie MyClass*/</span>
<span class="hljs-class"><span class="hljs-keyword" style="color: #815ba4;">Klasse</span> <span class="hljs-title" style="color: #776e71;">Meine Klasse</span>
</span>{
    <span class="hljs-comment" style="color: #776e71;">//Deklarieren Sie einen öffentlichen Konstruktor</span>
    <span class="hljs-keyword" style="color: #815ba4;">öffentlich</span> <span class="hljs-function" style="color: #06b6ef;"><span class="hljs-keyword" style="color: #815ba4;">Funktion</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;">//Eine öffentliche Methode deklarieren</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;">// Eine geschützte Methode deklarieren</span>
    <span class="hljs-keyword" style="color: #815ba4;">geschützt</span> <span class="hljs-function" style="color: #06b6ef;"><span class="hljs-keyword" style="color: #815ba4;">Funktion</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;">//Eine private Methode deklarieren</span>
    <span class="hljs-keyword" style="color: #815ba4;">privat</span> <span class="hljs-function" style="color: #06b6ef;"><span class="hljs-keyword" style="color: #815ba4;">Funktion</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;">// Diese Methode ist öffentlich</span>
    <span class="hljs-function" style="color: #06b6ef;"><span class="hljs-keyword" style="color: #815ba4;">Funktion</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;">// Diese Zeile kann normal ausgeführt werden</span>
<span class="hljs-variable" style="color: #ef6155;">$myclass</span>->MyProtected(); <span class="hljs-comment" style="color: #776e71;">// Diese Zeile erzeugt einen schwerwiegenden Fehler</span>
<span class="hljs-variable" style="color: #ef6155;">$myclass</span>->MyPrivate(); <span class="hljs-comment" style="color: #776e71;">// Diese Zeile erzeugt einen schwerwiegenden Fehler</span>
<span class="hljs-variable" style="color: #ef6155;">$myclass</span>->Foo(); <span class="hljs-comment" style="color: #776e71;">// Kann öffentlich, geschützt oder privat ausgeführt werden </span>


<span class="hljs-comment" style="color: #776e71;">/*** Definieren Sie MyClass2*/</span>
<span class="hljs-class"><span class="hljs-keyword" style="color: #815ba4;">Klasse</span> <span class="hljs-title" style="color: #776e71;">MyClass2</span> <span class="hljs-keyword" style="color: #815ba4;">erweitert</span> <span class="hljs-title" style="color: #776e71;">MyClass</span>
</span>{
    <span class="hljs-comment" style="color: #776e71;">// Diese Methode ist öffentlich</span>
    <span class="hljs-function" style="color: #06b6ef;"><span class="hljs-keyword" style="color: #815ba4;">Funktion</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;">// Diese Zeile erzeugt einen schwerwiegenden Fehler</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;">// Diese Zeile kann normal ausgeführt werden</span>
<span class="hljs-variable" style="color: #ef6155;">$myclass2</span>->Foo2(); <span class="hljs-comment" style="color: #776e71;">// Sowohl öffentliche als auch geschützte können ausgeführt werden, private jedoch nicht</span>

<span class="hljs-class"><span class="hljs-keyword" style="color: #815ba4;">Klasse</span> <span class="hljs-title" style="color: #776e71;">Bar</span>
</span>{
    <span class="hljs-keyword" style="color: #815ba4;">öffentlich</span> <span class="hljs-function" style="color: #06b6ef;"><span class="hljs-keyword" style="color: #815ba4;">Funktion</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>
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Vorheriger Artikel:PHP加密解密类Nächster Artikel:简单的站内信系统