> php教程 > php手册 > PHP 빠른 튜토리얼

PHP 빠른 튜토리얼

WBOY
풀어 주다: 2016-08-04 08:53:11
원래의
1407명이 탐색했습니다.

1. 이 글은 주로 C 언어(또는 다른 프로그래밍 언어) 기초를 갖춘 사람들이 PHP를 빠르게 배울 수 있도록 작성되었으므로 "="는 할당이 아니라 동일과 같은 몇 가지 기본 지식에 대해서는 너무 많이 설명하지 않습니다.
2. 이 글은 프로그래밍 언어를 배웠고 PHP를 빨리 시작하고 싶은 사람들에게 적합합니다.
3. 기본적으로는 이 글을 읽은 후 시작하시면 되고, 연습하거나 발전하셔도 됩니다.


소개

PHP는 Hypertext Preprocessor의 재귀적 약어입니다.
이는 웹 개발에 특히 적합한 서버측 스크립팅 언어입니다.
서버에서 실행되는 스크립트이므로 브라우저로 직접 열 수 없습니다. 웹 페이지 콘텐츠를 보려면 .php 스크립트를 서버에서 구문 분석하고 브라우저로 보내야 합니다. 따라서 .php 파일에 접근하기 위해서는 브라우저에 주소를 입력해야 하며, 그러면 서버는 웹 페이지의 내용을 보기 위해 파싱된 HTML을 브라우저에 파싱하여 전송합니다.
자신의 컴퓨터에서 .php 파일을 실행하려면 먼저 구성 서버 환경을 설정해야 합니다. 초보자는 XAMPP와 같은 통합 서버 구성 요소를 사용할 수 있습니다. 다운로드 주소: https://www.apachefriends.org/zh_cn/ index.html
이 부분에서는 너무 많이 소개하지 않겠습니다. Baidu에 가실 수 있습니다.

문법

마크

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. !=和<>都是不等于。

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

  5. $a <=> $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;">/*** MyClass 정의*/</span>
<span class="hljs-class"><span class="hljs-keyword" style="color: #815ba4;">수업</span> <span class="hljs-title" style="color: #776e71;">내수업</span>
</span>{
    <span class="hljs-comment" style="color: #776e71;">//공개 생성자 선언</span>
    <span class="hljs-keyword" style="color: #815ba4;">공개</span> <span class="hljs-function" style="color: #06b6ef;"><span class="hljs-keyword" style="color: #815ba4;">함수</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;">공개</span> <span class="hljs-function" style="color: #06b6ef;"><span class="hljs-keyword" style="color: #815ba4;">함수</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;">보호됨</span> <span class="hljs-function" style="color: #06b6ef;"><span class="hljs-keyword" style="color: #815ba4;">함수</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;">비공개</span> <span class="hljs-function" style="color: #06b6ef;"><span class="hljs-keyword" style="color: #815ba4;">함수</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;">함수</span> <span class="hljs-title" style="color: #776e71;">푸</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;">새</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;">수업</span> <span class="hljs-title" style="color: #776e71;">MyClass2</span> <span class="hljs-keyword" style="color: #815ba4;">연장</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;">기능</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;">새</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;">클래스</span> <span class="hljs-title" style="color: #776e71;">바</span>
</span>{
    <span class="hljs-keyword" style="color: #815ba4;">공개</span> <span class="hljs-function" style="color: #06b6ef;"><span class="hljs-keyword" style="color: #815ba4;">함수</span> <span class="hljs-title" style="color: #776e71;">테스트</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;">공개</span> <span class="hljs-function" style="color: #06b6ef;"><span class="hljs-keyword" style="color: #815ba4;">함수</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;">에코</span> <span class="hljs-str"></span></code>
로그인 후 복사
관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 추천
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿