Php 简明教程
PHP - Introduction
PHP 最初是一个小型开源项目,随着越来越多的人发现其用处,它不断发展。Rasmus Lerdorf 早在 1994 年就发布了 PHP 的第一个版本。最初,PHP 被认为是“个人主页”的缩写,但现在它代表递归首字母缩略词“PHP:超文本预处理器”。
PHP started out as a small open source project that evolved as more and more people found out how useful it was. Rasmus Lerdorf released the first version of PHP way back in 1994. Initially, PHP was supposed to be an abbreviation for "Personal Home Page", but it now stands for the recursive initialism "PHP: Hypertext Preprocessor".
Lerdorf 于 1993 年开始使用 C 编写几个通用网关接口 (CGI) 程序,他用这些程序来维护他的个人主页,由此开始了 PHP 的开发。后来,他将它们扩展到与 Web 表单配合使用并与数据库进行通信。PHP 的这种实现是“个人主页/表单解释器”或 PHP/FI。
Lerdorf began PHP development in 1993 by writing several Common Gateway Interface (CGI) programs in C, which he used to maintain in his personal homepage. Later on, He extended them to work with web forms and to communicate with databases. This implementation of PHP was "Personal Home Page/Forms Interpreter" or PHP/FI.
当代,PHP 是世界上最流行的服务器端编程语言,用于构建 Web 应用程序。多年来,它经历了多次修订和版本更新。
Today, PHP is the world’s most popular server-side programming language for building web applications. Over the years, it has gone through successive revisions and versions.
PHP Versions
PHP 是由拉斯姆斯·勒多夫于 1994 年开发的,是一套用 C 语言编写的简单的 CGI 二进制文件。他将这套脚本称为“个人主页工具”。它可以看作是 PHP 1.0 版本。
PHP was developed by Rasmus Lerdorf in 1994 as a simple set of CGI binaries written in C. He called this suite of scripts "Personal Home Page Tools". It can be regarded as PHP version 1.0.
-
In April 1996, Rasmus introduced PHP/FI. Included built-in support for DBM, mSQL, and Postgres95 databases, cookies, user-defined function support. PHP/FI was given the version 2.0 status.
-
PHP: Hypertext Preprocessor – PHP 3.0 version came about when Zeev Suraski and Andi Gutmans rewrote the PHP parser and acquired the present-day acronym. It provided a mature interface for multiple databases, protocols and APIs, object-oriented programming support, and consistent language syntax.
-
PHP 4.0 was released in May 2000 powered by Zend Engine. It had support for many web servers, HTTP sessions, output buffering, secure ways of handling user input and several new language constructs.
-
PHP 5.0 was released in July 2004. It is mainly driven by its core, the Zend Engine 2.0 with a new object model and dozens of other new features. PHP’s development team includes dozens of developers and others working on PHP-related and supporting projects such as PEAR, PECL, and documentation.
-
PHP 7.0 was released in Dec 2015. This was originally dubbed PHP next generation (phpng). Developers reworked Zend Engine is called Zend Engine 3. Some of the important features of PHP 7 include its improved performance, reduced memory usage, Return and Scalar Type Declarations and Anonymous Classes.
-
PHP 8.0 was released on 26 November 2020. This is a major version having many significant improvements from its previous versions. One standout feature is Just-in-time compilation (JIT) that can provide substantial performance improvements. The latest version of PHP is 8.2.8, released on July 4th, 2023.
PHP Application Areas
PHP 是网络上使用最为广泛的语言之一。以下是 PHP 的一些应用领域:
PHP is one of the most widely used language over the web. Here are some of the application areas of PHP −
-
PHP is a server-side scripting language that is embedded in HTML. It is used to manage dynamic content, databases, session tracking, even build entire e-commerce sites. Although it is especially suited to web development, you can also build desktop standalone applications as PHP also has a command-line interface. You can use PHP-GTK extension to build GUI applications in PHP.
-
PHP is widely used for building web applications, but you are not limited to output only HTML. PHP’s ouput abilities include rich file types, such as images or PDF files, encrypting data, and sending emails. You can also output easily any text, such as JSON or XML.
-
PHP is a cross-platform language, capable of running on all major operating system platforms and with most of the web server programs such as Apache, IIS, lighttpd and nginx. PHP also supports other services using protocols such as LDAP, IMAP, SNMP, NNTP, POP3, HTTP, COM, etc.
以下是 PHP 的更多一些重要特性:
Here are some more important features of PHP −
-
PHP performs system functions. It can create, open, read, write, and close the files.
-
PHP can handle forms. It can gather data from files, save data to a file, through email you can send data, return data to the user.
-
You add, delete, modify elements within your database through PHP.
-
Access cookies variables and set cookies.
-
Using PHP, you can restrict users to access some pages of your website.
-
It can encrypt data.
PHP 提供大量的可重复使用的类,并且可以在“PEAR”和“Composer”中找到库。“PEAR”(PHP 扩展和应用程序存储库) 是可重复使用的 PHP 库或类的仓库。“Composer”是 PHP 中的依赖管理工具。
PHP provides a large number of reusable classes and libraries are available on "PEAR" and "Composer". PEAR (PHP Extension and Application Repository) is a distribution system for reusable PHP libraries or classes. "Composer" is a dependency management tool in PHP.
PHP - Installation
你可以在互联网上免费获取任何一个 PHP 在线编译器来学习 PHP 编程的基本知识。这有助于熟悉 PHP 的特性,而无需在电脑上安装 PHP。之后,在本地计算机上安装一个成熟的 PHP 环境。
You can start learning the basics of programming in PHP with the help of any of the online PHP compilers freely available on the Internet. This will help in getting acquainted with the features of PHP without installing it on your computer. Later on, install a full-fledged PHP environment on your local machine.
Tutorialpoint 的 “Coding Ground for Developers” 提供了一个这样的在线 PHP 编译器。访问 https://www.tutorialspoint.com/codingground.htm ,输入 PHP 脚本并执行。
One such online PHP compiler is provided by Tutorialpoint’s "Coding Ground for Developers". Visit https://www.tutorialspoint.com/codingground.htm, enter PHP script and execute it.
不过,要能够学习 PHP 的高级特性,特别是与服务器变量、使用后端数据库等网络概念相关的特性,你需要在本地计算机上安装 PHP 环境。
However, to be able to learn the advanced features of PHP, particularly related to the web concepts such as server variables, using backend databases, etc., you need to install the PHP environment on your local machine.
为了开发并运行 PHP 网页,你需要在你的计算机系统上安装三个至关重要的组成部分。
In order to develop and run PHP Web pages, you neeed to install three vital components on your computer system.
-
Web Server − PHP will work with virtually all Web Server software, including Microsoft’s Internet Information Server (IIS), NGNIX, or Lighttpd etc. The most often used web server software is the freely available Apache Server. Download Apache for free here − https://httpd.apache.org/download.cgi
-
Database − PHP will work with virtually all database software, including Oracle and Sybase but most commonly used is freely available MySQL database. Download MySQL for free here − [role="bare"]https://www.mysql.com/downloads/
-
PHP Parser − In order to process PHP script instructions a parser must be installed to generate HTML output that can be sent to the Web Browser.
虽然可以单独安装这三个组成部分并正确配置安装,但这是一个有点复杂的过程,特别是对于初学者来说。相反,使用包含预编译的 Apache、MySQL 和 PHP 二进制文件的所有一体化打包分发版更为方便。
Although it is possible to install these three components separately, and configure the installation correctly, it is a little complex process, particularly for the beginners. Instead, using any all-in-one packaged distribution that contains precompiled Apache, MySQL and PHP binaries is convenient.
XAMPP Installation
有许多已预先编译的包在开源和专有发行版中可用。来自 Apache Friends ( https://www.apachefriends.org/ ) 的 XAMPP 是最流行的 PHP 启用 Web 服务器包之一。我们将在本教程中使用 XAMPP。
There are many precompiled bundles available both in open-source as well as proprietary distributions. XAMPP, from Apache Friends (https://www.apachefriends.org/) is one of the most popular PHP enabled web server packages. We shall be using XAMPP in this tutorial.
XAMPP 是一个易于安装的 Apache 发行版,它包含 A*pache, *M*ariaDB, *P*HP and *P*erl. The letter *X 缩写表示它是一项跨平台软件,可在 Windows、Linux 和 OS X 上使用。请注意,XAMPP 包含 MariaDB,它是 MySQL 的一个派生,但功能没有区别。
XAMPP is an easy to install Apache distribution that contains A*pache, *M*ariaDB, *P*HP and *P*erl. The letter *X in the acronym indicates that it is a cross-platform software, available for use on Windows, Linux and OS X. Note that XAMPP includes MariaDB, which is a fork of MySQL, with no difference in its functionality.
要下载适用于你操作系统的安装程序,请访问 https://www.apachefriends.org/download.html ,并下载以下之一:
To download the respective installer for your operating system, visit https://www.apachefriends.org/download.html, and download one of the following −
-
Windows − link:https://sourceforge.net/projects/xampp/files/XAMPP%20Windows/8.0.28/xampp-windows-x64-8.0.28-0-VS16-installer.exe [[role="bare"]https://sourceforge.net/projects/]
-
OS X − link:https://sourceforge.net/projects/xampp/files/XAMPP%20Mac%20OS%20X/8.0.28/xampp-osx-8.0.28-0-installer.dmg [[role="bare"]https://sourceforge.net/projects/]
在 Windows 上使用安装程序是一个完全基于向导的安装。你需要提供的是管理员访问权限和安装目录的位置,默认为“c:\xampp”。
Using the installer on Windows is a completely wizard based installation. All you need to provide is an administrator access and the location of the installation directory which is "c:\xampp" by default.
要在 Linux 上安装 XAMPP,请使用以下步骤:
To install XAMPP on Linux, use the following steps −
Step 1 - 更改安装程序的权限:
Step 1 − Change the permissions to the installer −
chmod 755 xampp-linux-*-installer.run
运行安装程序:
Run the installer −
sudo ./xampp-linux-*-installer.run
XAMPP 现在安装在“/opt/lamp”目录下面。
XAMPP is now installed below the "/opt/lamp" directory.
Step 2 - 要启动 XAMPP,只需调用此命令:
Step 2 − To start XAMPP simply call this command −
sudo /opt/lampp/lampp start
你现在应该在屏幕上看到类似此内容的内容:
You should now see something like this on your screen −
Starting XAMPP ...
LAMPP: Starting Apache...
LAMPP: Starting MySQL...
LAMPP started.
Ready. Apache and MySQL are running.
你还可以使用图形工具轻松管理服务器。你可以使用以下命令来启动此工具:
You can also use a graphical tool to manage your servers easily. You can start this tool with the following commands −
cd /opt/lampp
sudo ./manager-linux.run (or manager-linux-x64.run)
Step 3 - 要停止 XAMPP,只需调用此命令:
Step 3 − To stop XAMPP simply call this command −
sudo /opt/lampp/lampp stop
你现在应该在屏幕上看到类似此内容的内容:
You should now see something like this on your screen −
Stopping XAMPP ...
LAMPP: Stopping Apache...
LAMPP: Stopping MySQL...
LAMPP stopped.
此外,请注意,有一个可以轻松启动/停止服务器的图形工具。你可以使用以下命令来启动此工具:
Also, note that there is a graphical tool that you can use to start/stop your servers easily. You can start this tool with the following commands −
cd /opt/lampp
sudo ./manager-linux.run (or manager-linux-x64.run)
如果你使用的是 OS X,请按照以下步骤操作:
If you are using OS X, follow these steps −
-
To start the installation, Open the DMG-Image, and double-click the image to start the installation process.
-
To start XAMPP simply open XAMPP Control and start Apache, MySQL and ProFTPD. The name of the XAMPP Control is "manager-osx".
-
To stop XAMPP simply open XAMPP Control and stop the servers. The name of the XAMPP Control is "manager-osx".
-
The XAMPP control panel is a GUI tool from which the Apache server, and MySQL can be easily started and stopped.
启动 Apache 模块后按下 Admin 按钮。XAMPP 主页会像下面这样显示−
Press the Admin button after starting the Apache module. The XAMPP homepage appears like the one shown below −
PHP Parser Installation
在你继续之前,务必确保在你的机器上设置了适当的环境,以便使用 PHP 开发你的网络程序。
Before you proceed it is important to make sure that you have proper environment setup on your machine to develop your web programs using PHP.
在您的浏览器的地址框中输入以下地址。
Type the following address into your browser’s address box.
http://127.0.0.1/info.php
如果显示一个显示你的 PHP 安装相关的信息的页面,则意味着你的 PHP 和网络服务安装正确。否则,你必须遵循以下步骤在你的电脑上安装 PHP。
If this displays a page showing your PHP installation related information then it means you have PHP and Webserver installed properly. Otherwise you have to follow given procedure to install PHP on your computer.
本部分将指导您在以下四个平台上安装和配置 PHP −
This section will guide you to install and configure PHP over the following four platforms −
Apache Configuration
如果你使用 Apache 作为网络服务器,那么本节将指导你编辑 Apache 配置文件。
If you are using Apache as a Web Server then this section will guide you to edit Apache Configuration Files.
只需在此处检查− @ {s4}
Just Check it here − PHP Configuration in Apache Server
PHP.INI File Configuration
PHP 配置文件 php.ini 是影响 PHP 功能的最直接的最终方式。
The PHP configuration file, php.ini, is the final and most immediate way to affect PHP’s functionality.
只需在此处检查− @ {s5}
Just Check it here − PHP.INI File Configuration
PHP - History
PHP 最初是一个小型开源项目,随着越来越多人发现它的用处,逐渐演变。Rasmus Lerdorf 在 1994 年发布了 PHP 的第一个版本。当时,PHP 的意思是个人主页,因为他用它来维护自己的个人主页。后来,他添加了数据库支持,并称之为“个人主页/Forms 解译器”或 PHP/FI,可以用来构建简单的动态 Web 应用程序。
PHP started out as a small open-source project that evolved gradually as more and more people found out how useful it was. Rasmus Lerdorf released the first version of PHP way back in 1994. At that time, PHP stood for Personal Home Page, as he used it to maintain his personal homepage. Later on, he added database support and called it as "Personal Home Page/Forms Interpreter" or PHP/FI, which could be used to build simple, dynamic web applications.
-
Zeev Suraski and Andi Gutmans rewrote the parser in 1997 and formed the base of PHP 3. The name of the language was also changed to the recursive acronym PHP: Hypertext Preprocessor. They are also the authors of Zend Engine, a compiler and runtime environment for the PHP. Zend Engine powered PHP 4 was released in May 2000.
-
PHP 5 was released in 2004, which included many new features such as OOP support, the PHP Data Objects (PDO), and numerous performance enhancements.
-
PHP 7, is a new major PHP version which was developed in 2015. It included new language features, most notable being, the introduction of return type declarations for functions which complement the existing parameter type declarations, and support for the scalar types (integer, float, string, and boolean) in parameter and return type declarations.
New Features in PHP 8
PHP 8 是最新的主要版本,于 2020 年 11 月发布。一些新功能和值得注意的更改包括:
PHP 8 is the latest major version, released in November 2020. Some of the new features and notable changes include:
Just-in-time (JIT) Compilation
PHP 8 的 JIT 编译器为数学类型操作提供了相比普通 Web 开发用例而言显著的性能提升。JIT 编译器提供了将来将某些代码从 C 迁移至 PHP 的潜力。
PHP 8’s JIT compiler provides substantial performance improvements mathematical-type operations than for common web-development use cases. The JIT compiler provides the future potential to move some code from C to PHP.
PHP 8 – Type Changes and Additions
PHP 8 引入了联合类型、一个新的静态返回类型和一个新的混合类型。PHP 8 还提供了属性(类似于其他编程语言中的“注释”),用于向 PHP 类添加元数据。
PHP 8 introduced union types, a new static return type, and a new mixed type. PHP 8 also provided Attributes, (similar to "annotations" in other programming languages) that help in adding metadata to PHP classes.
此外,对 PHP 标准库做出了多项更改和补充。PHP 8.2.9 是可用的最新稳定版本。
In addition, there have been many changes and additions to the PHP standard library. PHP 8.2.9 is the latest stable version available.
PHP 发布历史中的重要里程碑总结在以下表格中 −
Important milestones in PHP’s release history are summarized in the following table −
Version |
Description |
Version 1.0 (8 June 1995) |
Officially called "Personal Home Page Tools (PHP Tools)". This is the first use of the name "PHP". |
Version 2.0 (1 November 1997) |
Officially called "PHP/FI 2.0". This is the first release that could actually be characterised as PHP, being a standalone language with many features that have endured to the present day. |
Version 3.0 (6 June 1998) |
Development moves from one person to multiple developers. Zeev Suraski and Andi Gutmans rewritten the base for this version. |
Version 4.0 (22 May 2000) |
Added more advanced two-stage parse/execute tag-parsing system called the Zend engine. |
Version 5.0 (13 July 2004) |
Zend Engine II with a new object model. |
Version 5.1 (24 November 2005) |
Performance improvements with the introduction of compiler variables in re-engineered PHP Engine. Added PHP Data Objects (PDO) as a consistent interface for accessing databases. |
Version 6.x Not released |
Abandoned version of PHP that planned to include native Unicode support. |
Version 7.0 (3 December 2015) |
Zend Engine 3 , Uniform variable syntax, Added Closure:call(), ?? (null coalesce) operator, Return type declarations, Scalar type declarations, <⇒ "spaceship" three-way comparison operator, Anonymous classes |
Version 7.3 (6 December 2018) |
Flexible Heredoc and Nowdoc syntax |
Version 8.0 (26 November 2020) |
Just-In-Time (JIT) compilation, Arrays starting with a negative index, TypeError on invalid arithmetic/bitwise operators, Variable syntax tweaks, Attributes, Named arguments, Match expression, Union types, Mixed type, Static return type |
PHP - Features
PHP(超文本预处理程序)是一种开源服务端脚本语言,主要用于 Web 开发。PHP 可以嵌入 HTML 代码中。
PHP (Hypertext Preprocessor) is an open-source server-side scripting language primarily used for web development. PHP can be embedded into HTML code.
PHP 主要用于服务端脚本编写,它在 Web 服务器上运行脚本,然后将处理后的 HTML 转发给客户端上的 Web 浏览器。这使得程序员可以设计可管理会话、处理表单、与数据库通信并执行各种其他必要的在线应用程序任务的动态网页。
PHP is mainly used for server-side scripting, which runs scripts on the web server and then forwards the HTML they process to the web browser on the client. This makes it possible for programmers to design dynamic webpages that can manage sessions, handle forms, communicate with databases, and carry out a variety of other duties necessary for online applications.
Features of PHP
多年来,PHP 结合了许多特性。它不断通过新特性和代码修订进行升级。在本章中,让我们重点介绍 PHP 的一些关键特性:
Over the years, PHP has incorporated numerous features. It is being consistently upgraded with new features and code revisions. In this chapter, let’s highlight some of the key features of PHP:
PHP is Simple and Easy to Learn
与 C、Java 和 Perl 相比,PHP 的语法较为简单,开发人员很容易理解,特别是那些已经熟悉其他编程语言的开发人员。由于有丰富的预定义函数,可以快速开发 Web 应用程序。
The syntax of PHP compared to that of C, Java, and Perl, which makes it rather simple for developers to comprehend, particularly for those who are already familiar with other programming languages. Web apps can be developed quickly because of its generous pre-defined functions.
PHP is Open Source
PHP 是免费且开源的,意味着我们可以免费下载它,任何人都可以使用、修改和分发它。这鼓励了一个庞大且充满活力的开发人员社区,他们使用论坛、教程和文档来支持和促进其开发。
PHP is free and open-source, meaning we can download it for free, and anyone can use it, modify it, and distribute. This encourages a sizable and vibrant developer community that uses forums, tutorials, and documentation to support and contribute to its development.
PHP is Cross-Platform Compatible
PHP 与多种操作系统兼容,包括 Windows、Linux、macOS 和 UNIX;以及不同的数据库,如 MongoDB、PostgreSQL、MySQL。
Numerous operating systems including Windows, Linux, macOS, and UNIX; and different databases like MongoDB, PostgreSQL, MySQL are compatible with PHP.
由于具有跨平台互操作性,基于 PHP 的应用程序可以在多种环境中运行,无需进行任何修改。
PHP-based apps can operate on several environments without requiring any modifications due to this cross-platform inter-operability.
Server-Side Scripting in PHP
PHP 主要用于服务端脚本编写,它在 Web 服务器上运行脚本,然后将处理后的 HTML 转发给客户端上的 Web 浏览器。它帮助开发人员在多个请求中处理表单提交和用户会话管理。
PHP is mainly used for server-side scripting, which runs scripts on the web server and then forwards the HTML they process to the web browser on the client. It helps the developers in Form Submission and Session Management with users across multiple requests.
PHP Supports Easy Integration with Databases
PHP 为各种 DBMS 提供强大的数据库交互支持。它提供多种内置函数来实现数据库连接。
PHP offers strong database interaction support for various DBMS. It offers numerous built-in functions to achieve the database connection.
PHP 还包括数据库抽象层,它集成了应用程序与数据库之间的通信。这使得开发人员可以轻松设计基于数据库的 Web 应用程序。
PHP also includes database abstraction layer which integrates the communication between the application and the database. This makes it simple for developers to design database-driven web applications.
PHP Provides Extensive Library Support
PHP 为多种功能提供了丰富的库,例如图像处理、加密、PDF 生成、解析 XML 和 JSON、处理会话和 Cookie 等。
PHP provides extensive libraries for various functionalities like image processing, encryption, PDF generation, parsing XML and JSON, handling sessions and cookies, and much more.
Security Features in PHP
PHP 为数据加密提供了大量内置函数。开发人员还可以利用第三方应用程序来保障安全性。
PHP provides a plethora of built-in functions for data encryption. Developers can also leverage third-party applications for security.
PHP 采用 Sha1 和 MD5 等安全算法来加密字符串。此外,诸如 filter_var 和 strip_tags 等函数有助于为用户维护一个安全的环境。PHP 还支持 HTTPS 等安全通信协议。
PHP employs security algorithms like Sha1 and MD5 to encrypt strings. Additionally, functions like filter_var and strip_tags contribute in maintaining a secure environment for the users. PHP also supports secure communication protocols like HTTPS.
Efficient Memory and Session Management in PHP
PHP 是一款可靠的语言,因为它具有高效的内存管理和会话管理。它避免了不必要的内存分配。
PHP is a reliable language due to its efficient memory management and session management. It avoids unnecessary memory allocation.
PHP 代码在其自己的内存空间中运行,与其他脚本语言相比速度更快,使其更高效。在 PHP 中,数据库连接也很快。
PHP code runs in its own memory space which makes it faster compared to other scripting languages making it more efficient. In PHP, the database connections are also fast.
PHP Has Active Community and Support
由于 PHP 是一个开源平台,它拥有一个充满活力的开发人员社区,他们积极为其发展做出贡献、分享知识、提供支持并创建第三方工具和框架。
Since PHP is an open-source platform, it has a vibrant community of developers who actively contribute to its development, share knowledge, provide support, and create third-party tools and frameworks.
由于这个活跃的社区支持,PHP 保持最新状态,开发人员可以轻松地在编写 PHP 代码时遇到任何错误或异常时从其他社区成员那里寻求帮助。
Due to this active community support, PHP remains up-to-date and developers can easily seek help from other community members in case they get any errors or exceptions while writing PHP codes.
PHP - Syntax
PHP 的语法规则与 C 语言非常相似。PHP 是一种服务器端脚本语言。PHP 代码存储为扩展名为“php”的文本文件。PHP 文件本质上是一个网页,内容是穿插在 HTML 脚本中的一个或多个 PHP 代码块。但是,它必须在浏览器中使用 HTTP 协议 URL 打开。也就是说,如果您双击 PHP 文件图标,它将使用文件协议在本地打开。例如,如果您在 Apache 服务器的文档根目录中打开“index.php”文件,它可能只显示 PHP 代码文本。但是,如果您启动 Apache 服务器并打开 URL http://localhost/index.php ,它将显示 Apache 主页。
The syntax rules of PHP are very similar to C Language. PHP is a server side scripting language. A PHP code is stored as a text file with ".php" extension. A ".php" file is essentially a web page with one or more blocks of PHP code interspersed in the HTML script. However, it must be opened in a browser with a HTTP protocol URL. In other words, if you double-click on the PHP file icon, it will be opened locally with the file protocol. For example, if you open the "index.php" file in the document root folder of your Apache server, it may just show the text of the PHP code. However, if you launch the Apache server and open the URL http://localhost/index.php, it displays the Apache home page.
“php”文件可能包含 HTML、CSS 和 JavaScript 代码块以及 PHP 代码。因此,PHP 解析器必须区分出 PHP 代码和其他元素。当在网络浏览器中打开“php”文件时,HTML 引擎会呈现 HTML/CSS/JavaScript 部分,并在遇到包含在 PHP 标记内的语句时即退出 HTML 块。PHP 解析器解释器处理此块并向浏览器返回响应。
A ".php" file may contain HTML, CSS and JavaScript code blocks along with the PHP code. Hence, the PHP parser must differentiate between the PHP code from the other elements. When a ".php" file is opened in the web browser, the HTML engine renders the HTML/CSS/JavaScript part and escapes out of the HTML block as soon as the statements included in PHP tags are encountered. The PHP parser interpreter processes this block and returns the response to the browser.
PHP 定义了使用标记来让 PHP 代码脱离 HTML 的两种方法。标准 PHP 标记和短标记(SGML 样式)。
PHP defines two methods of using tags for escaping the PHP code from HTML. Canonical PHP tags and Short-open (SGML-style) tags.
Canonical PHP Tags
通用最有效的 PHP 标记样式为 −
The most universally effective PHP tag style is −
<?php
One or more PHP statements
?>
如果您使用此样式,则可以确信您的标记将始终被正确解释。
If you use this style, you can be positive that your tags will always be correctly interpreted.
Short-open (SGML-style) Tags
短标记或短开标记看起来像这样 −
Short or short-open tags look like this −
<?php
One or more PHP statements
?>
顾名思义,短标记是最短的选项。您必须执行两件事之一,以使 PHP 能够识别标记 −
Short tags are, as one might expect, the shortest option. You must do one of two things to enable PHP to recognize the tags −
-
Choose the "--enable-short-tags" configuration option when you’re building PHP.
-
Set the "short_open_tag" setting in your php.ini file to on.
short_open_tag=on
必须禁用此选项才能使用 PHP 解析 XML,因为 XML 标记使用相同的语法。
This option must be disabled to parse XML with PHP because the same syntax is used for XML tags.
ASP-style tags −
The use of ASP-style tags −
<%...%>
和 HTML script tags −
and HTML script tags −
<script language = "PHP">...</script>
的使用已被停止。
has been discontinued.
Escaping from HTML
PHP 解析器忽略开闭标记对之外的所有内容。因此,PHP 文件可以具有混合内容。这允许在 HTML 文档中嵌入 PHP −
The PHP parser ignores everything outside of a pair of opening and closing tags. Thus, a PHP file can have mixed content. This allows PHP to be embedded in HTML documents −
<p>This is a HTML statement</p>
<?php echo This is a PHP statement.'; ?>
<p>This is another HTML statement.</p>
下面展示了使用条件脱离的稍高阶示例 −
A little advanced example of escaping using conditions is shown below −
<?php if ($expression == true): ?>
This HTML statement will be rendered.
<?php else: ?>
Otherwise this HTML statement will be rendered.
<?php endif; ?>
PHP 跳过不满足条件的块,即使它们在 PHP 打开/关闭标记之外。
PHP skips the blocks where the condition is not met, even though they are outside of the PHP open/close tags.
对于输出大块文本,退出 PHP 解析模式通常比通过 echo 或 print 发送所有文本更有效率。
For outputting large blocks of text, dropping out of PHP parsing mode is generally more efficient than sending all of the text through echo or print.
Basic Syntax of PHP
PHP 的基本语法与 C 和 C++ 的语法非常相似。
The basic syntax of PHP is very similar to that of C and C++.
Statements are Expressions Terminated by Semicolons
PHP 中的语句是后面紧跟一个分号 (;) 的任何表达式。任何被 PHP 标记括起来的有效 PHP 语句序列都是有效的 PHP 程序。
A statement in PHP is any expression that is followed by a semicolon (;). Any sequence of valid PHP statements that is enclosed by the PHP tags is a valid PHP program.
以下是在 PHP 中一个典型的语句,在这个案例中它给一个叫做 “$greeting” 的变量分配一个字符串 −
Here is a typical statement in PHP, which in this case assigns a string of characters to a variable called "$greeting" −
$greeting = "Welcome to PHP!";
文本编辑器中的一个物理行在 PHP 代码中没有任何意义。一行中可能会包含多个以分号结尾的语句。另一方面,如果需要的话,一个 PHP 语句可以溢出到多行。
A physical line in the text editor doesn’t have any significance in a PHP code. There may be multiple semicolon-terminated statements in a single line. On the other hand, a PHP statement may spill over more than one line if required.
Expressions are Combinations of Tokens
PHP 最小的构建块是不可分割的标记,诸如数字 (3.14159)、字符串 (“two”)、变量 ($two)、常量 (TRUE) 以及构成 PHP 自身语法的特殊单词,诸如 “if”、“else”、“while”、“for” 等。
The smallest building blocks of PHP are the indivisible tokens such as numbers (3.14159), strings ("two"), variables ($two), constants (TRUE), and the special words that make up the syntax of PHP itself like "if", "else", "while", "for", and so forth.
Braces Make Blocks
尽管不能将语句像表达式那样组合,但你总可以通过将它们括在一对花括号中,将语句序列放在任何可以放置语句的位置。
Although statements cannot be combined like expressions, you can always put a sequence of statements anywhere a statement can go, by enclosing them in a set of curly braces.
这里,以下两个语句是等价的 −
Here, both the following statements are equivalent −
if (3 == 2 + 1)
print("Good - I haven't totally lost my mind.");
if (3 == 2 + 1) {
print("Good - I haven't totally");
print("lost my mind.");
}
PHP is Case Sensitive
PHP 是一种大小写敏感的语言。各种 PHP 标识符的名称(诸如变量、函数、类等)都是大小写敏感的。结果是变量 “$age” 不同于 “$Age”。同样,一个名为 “myfunction()” 的函数不同于另一个名为 “MyFunction()” 的函数。
PHP is a case sensitive language. The names of various PHP identifiers such as variable, function, class, etc., are case sensitive. As a result, the variable "$age" is not the same as "$Age". Similarly, a function called "myfunction()" is different from another function called "MyFunction()".
PHP - Hello World
通常,学习者在学习一门新语言或框架时,会作为其第一个程序编写一个“Hello World”程序。其目的是验证要使用的软件是否已正确安装并且按预期工作。要在 PHP 中运行一个 Hello World 程序,你应该在使用的操作系统上安装了 Apache 服务器以及 PHP 模块。
Conventionally, learners write a "Hello World" program as their first program when learning a new language or a framework. The objective is to verify if the software to be used has been installed correctly and is working as expected. To run a Hello World program in PHP, you should have installed the Apache server along with PHP module on the operating system you are using.
PHP 是一种服务端编程语言。PHP 代码必须位于 Web 服务器的文档根目录中。Web 服务器文档根目录是你系统上运行的 Web 服务器的根目录。此根目录下的文档可供连接到 Web 服务器的任何系统(提供用户有权限)访问。如果一个文件不在此根目录下,则无法通过 Web 服务器访问它。
PHP is a server-side programming language. The PHP code must be available in the document root of the web server. The web server document root is the root directory of the web server running on your system. The documents under this root are accessible to any system connected to the web server (provided the user has permissions). If a file is not under this root directory, then it cannot be accessed through the web server.
在此教程中,我们使用 XAMPP 服务器软件编写 PHP 代码。默认文档根目录通常在 Windows 上为“C:\xampp\htdocs\”,在 Linux 上为“/opt/lamp/htdocs/”。但是,你可以通过修改 Apache 服务器的配置文件“httpd.conf”中的 DocumentRoot 设置来更改默认文档根目录。
In this tutorial, we are using XAMPP server software for writing PHP code. The default document root directory is typically "C:\xampp\htdocs\" on Windows and "/opt/lamp/htdocs/" on Linux. However, you can change the default document root by modifying the DocumentRoot setting in Apache server’s configuration file "httpd.conf".
在 Windows 操作系统上,从 XAMPP 控制面板启动 Apache 服务器。
While on a Windows operating system, start the Apache server from the XAMPP control panel.
浏览到“htdocs”目录。将以下脚本另存为其中的“hello.php”。
Browse to the "htdocs" directory. Save the following script as "hello.php" in it.
<?php
echo "Hello World!";
?>
在浏览器中打开一个新选项卡,并输入 http://localhost/hello.php 作为 URL。你应该在浏览器窗口中看到“Hello World”消息。
Open a new tab in your browser and enter http://localhost/hello.php as the URL. You should see the "Hello World" message in the browser window.
PHP 脚本可能包含 HTML 和 PHP 代码的混合。
A PHP script may contain a mix of HTML and PHP code.
<!DOCTYPE html>
<html>
<body>
<h1>My PHP Website</h1>
<?php
echo "Hello World!";
?>
</body>
</html>
“Hello World”消息将呈现为纯文本。但是,你可以将 HTML 标签放在“Hello World”字符串中。浏览器将相应地解释这些标签。
The "Hello World" message will be rendered as a plain text. However, you can put HTML tags inside the "Hello World" string. The browser will interpret the tags accordingly.
在以下代码中,“echo”语句渲染了“Hello World”,使其处于页面中心对齐的 <h1> 标题中。
In the following code, the "echo" statement renders "Hello World" so that it is in <h1> heading with the text aligned at the center of the page.
<?php
echo "<h1 align='center'>Hello World!</h1>";
?>
PHP Script from Command Prompt
你可以从命令提示符运行你的 PHP 脚本。假设你的“hello.php”文件中有以下内容。
You can run your PHP script from the command prompt. Let’s assume you have the following content in your "hello.php" file.
<?php
echo "Hello PHP!!!!!";
?>
将 PHP 可执行文件的路径添加到你的操作系统的路径环境变量中。例如,在 Windows 上的典型 XAMPP 安装中,PHP 可执行文件“php.exe”位于“c:\xampp\php”目录中。在此路径环境变量字符串中添加此目录。
Add the path of the PHP executable to your operating system’s path environment variable. For example, in a typical XAMPP installation on Windows, the PHP executable "php.exe" is present in "c:\xampp\php" directory. Add this directory in the PATH environment variable string.
现在以命令提示符运行此脚本 −
Now run this script as command prompt −
C:\xampp\htdocs>php hello.php
你将获得以下输出 -
You will get the following output −
Hello PHP!!!!!
PHP - Comments
任何计算机程序(如 PHP 程序)中的注释都是一种特定的解释性文本,语言编译器/解释器会忽略它。其目的是帮助用户理解程序算法中使用的逻辑。
A comment in any computer program (such as a PHP program) is a certain explanatory text that is ignored by the language compiler/interpreter. Its purpose is to help the user understand the logic used in the program algorithm.
尽管在代码中放置注释并不是必须的,但这是强烈推荐的做法。注释也可以作为程序文档。在需要调试和修改代码时,注释也非常有用。
Although placing comments in the code is not essential, it is a highly recommended practice. The comments also serve as program documentation. Comments are also useful when the code needs to be debugged and modified.
PHP 有两种注释格式 -
There are two commenting formats in PHP −
-
Single-line Comments
-
Multi-line Comments
Single-line Comments
它们通常用于短说明或与本地代码相关的注释。PHP 使用两种符号在程序中插入单行注释。
They are generally used for short explanations or notes relevant to the local code. PHP uses two notations for inserting a single-line comment in a program.
Single-line Comments Using "
以“#”符号开头的 PHP 代码中的行被视为单行注释。
A line in PHP code starting with the "#" symbol is treated as a single-line comment.
<?php
# Single line comment starting with # symbol
echo 'Hello World';
?>
Single-line Comments Using "//"
PHP 还支持以“//”符号为单行注释的 C 样式。以双斜线符号开头的行被视为注释。
PHP also supports C style of single-line comments with "//" symbol. A line starting with double oblique symbol is treated as a comment.
<?php
// Single line comment starting with // symbol
echo 'Hello World';
?>
以符号 “#”或 “//”开头的注释不需要关闭。这些符号的作用持续到物理行的末尾。
A comment that starts with the symbol "#" or "//" need not be closed. The effect of these symbols last till the end of the physical line.
换句话说,PHP 解析器会将下一行作为 PHP 语句对待,而不是注释,即使没有注释结束标记。
In other words, the PHP parser will treat the next line as a PHP statement and not as a comment even if there is no closing comment marker.
Multi-line Comments
多行注释通常用于在必要时提供伪代码算法和更详细的说明。
Multi-line comments are generally used to provide pseudocode algorithms and more detailed explanations when necessary.
多行注释的样式与 C 中相同。嵌入在符号 “/ " and " /”内的行或多行被视为注释。
The multiline style of commenting is the same as in C. One or more lines embedded inside the "/" and "/" symbols are treated as a comment.
Example of Multi-line Comment in PHP
这是一个多行注释的示例。
Here is the example of a multi-line comment.
<?php
/* This is a multiline comment example
program to add two numbers
Variables used - $x for first number,
$y for second number */
$x=10;
$y=20;
print "Total = ". $x+$y;
?>
请注意,您甚至可以将单行放在程序中的 “/* .. /" symbols. However, if there is a "/ " 符号内,它必须有一个注释结束标记 “*/”。如果没有,将显示以下错误 -
Note that you can put even a single line inside the "/* .. /" symbols. However, if there is a "/" symbol in the program, it must have a closing end-of comment marker "*/". If not, an error will be displayed as follows −
PHP Parse error: Unterminated comment starting line 3 in /home/cg/root/65ded9eeb52fc/main.php on line 3
PHP - Variables
PHP 中的变量是一个命名内存位置,它保存属于某种数据类型的数据。
A variable in PHP is a named memory location that holds data belonging to one of the data types.
-
PHP uses the convention of prefixing a dollar sign ($) to the name of a variable.
-
Variable names in PHP are case-sensitive.
-
Variable names follow the same rules as other labels in PHP. A valid variable name starts with a letter or underscore, followed by any number of letters, numbers, or underscores.
-
As per the naming convention, "$name", "$rate_of_int", "$Age", "$mark1" are examples of valid variable names in PHP.
-
Invalid variable names: "name" (not having $ prefix), "$rate of int" (whitespace not allowed), "$Age#1" (invalid character #), "$11" (name not starting with alphabet).
变量使用“=”运算符赋值,左侧是变量,右侧是要计算的表达式。
Variables are assigned with the "=" operator, with the variable on the left hand side and the expression to be evaluated on the right.
No Need to Specify the Type of a Variable
PHP 是一种动态类型语言。不需要指定变量的类型。相反,变量的类型由分配给它的值决定。变量的值是其最近赋值的值。
PHP is a dynamically typed language. There is no need to specify the type of a variable. On the contrary, the type of a variable is decided by the value assigned to it. The value of a variable is the value of its most recent assignment.
看看下面的 example −
Take a look at this following example −
<?php
$x = 10;
echo "Data type of x: " . gettype($x) . "\n";
$x = 10.55;
echo "Data type of x now: " . gettype($x) . "";
?>
它将生成以下 output −
It will produce the following output −
Data type of x: integer
Data type of x now: double
Automatic Type Conversion of Variables
当有必要时,PHP 会很好地自动将类型从一种转换为另一种类型。在下面的代码中,PHP 将字符串变量“y”转换为“int”,对另一个整数变量执行加法,并将结果打印为 30。
PHP does a good job of automatically converting types from one to another when necessary. In the following code, PHP converts a string variable "y" to "int" to perform addition with another integer variable and print 30 as the result.
看看下面的 example −
Take a look at this following example −
<?php
$x = 10;
$y = "20";
echo "x + y is: ", $x+$y;
?>
它将生成以下 output −
It will produce the following output −
x + y is: 30
Variables are Assigned by Value
在 PHP 中,变量始终按值分配。如果将表达式分配给变量,则原始表达式的值将被复制到其中。如果分配后表达式中任何变量的值发生变化,它不会对分配的值产生任何影响。
In PHP, variables are always assigned by value. If an expression is assigned to a variable, the value of the original expression is copied into it. If the value of any of the variables in the expression changes after the assignment, it doesn’t have any effect on the assigned value.
<?php
$x = 10;
$y = 20;
$z = $x+$y;
echo "(before) z = ". $z . "\n";
$y=5;
echo "(after) z = ". $z . "";
?>
它将生成以下 output −
It will produce the following output −
(before) z = 30
(after) z = 30
Assigning Values to PHP Variables by Reference
您还可以通过引用方式将值分配给 PHP 变量。在这种情况下,新变量仅引用或成为原始变量的别名或指向原始变量。新变量的更改会影响原始变量,反之亦然。
You can also use the way to assign values to PHP variables by reference. In this case, the new variable simply references or becomes an alias for or points to the original variable. Changes to the new variable affect the original and vice versa.
要按引用分配,只需在正在分配的变量(源变量)的开头添加一个和号 (&)。
To assign by reference, simply prepend an ampersand (&) to the beginning of the variable which is being assigned (the source variable).
看看下面的 example −
Take a look at this following example −
<?php
$x = 10;
$y = &$x;
$z = $x+$y;
echo "x=". $x . " y=" . $y . " z = ". $z . "\n";
$y=20;
$z = $x+$y;
echo "x=". $x . " y=" . $y . " z = ". $z . "";
?>
它将生成以下 output −
It will produce the following output −
x=10 y=10 z = 20
x=20 y=20 z = 40
PHP - Echo/Print
在 PHP 中, echo 和 print 语句都用于在浏览器或 PHP 控制台上呈现输出。它们两个都不是函数,而是语言结构。因此,不应在其中任何一个中使用括号。
In PHP, both echo and print statements are used to render the output either on the browser or the PHP console. Both of them are not functions but they are language constructs. Hence, parentheses should not be used with either of them.
The "echo" Statement in PHP
echo 语句与以下 syntax 搭配使用 −
The echo statement is used with following syntax −
echo(string ...$expressions): void
echo 语句输出一个或多个表达式,没有额外的换行符或空格。
The echo statement outputs one or more expressions, with no additional newlines or spaces.
Example
以下是 echo 语句在 PHP 中工作方式的一个示例 −
Here is an example of how the echo statement works in PHP −
<?php
$name = "Rajesh";
echo "Hello " . $name . " How are you?"
?>
它将生成以下 output −
It will produce the following output −
Hello Rajesh How are you?
由于双引号字符串在 PHP 中类似于单引号字符串,因此以下语句会产生相同输出。
Since a double quoted string is similar to a single quoted string in PHP, the following statement produces the same output.
echo 'Hello ' . $name . ' How are you?';
Example
双引号字符串输出变量的值。因此,以下语句在打印输出之前插入“$name”变量的值。
A double quoted string outputs the value of the variable. Hence, the following statement inserts the value of "$name" variable before printing the output.
<?php
$name = "Rajesh";
echo "Hello $name How are you?";
?>
它将生成以下 output −
It will produce the following output −
Hello Rajesh How are you?
Example
但是,单引号字符串将按原样输出“$name”。
But, a single-quoted string will output "$name" as it is.
<?php
$name = "Rajesh";
echo 'Hello $name How are you?';
?>
它将生成以下 output −
It will produce the following output −
Hello $name How are you?
传递给 echo 语句的字符串可以逐个作为多个参数传递,也可以连接在一起作为单个参数传递。因此,以下两个语句都是有效的 −
A string passed to an echo statement can either be passed individually as multiple arguments or concatenated together and passed as a single argument. So, both the following statements are valid −
echo 'Hello ', 'how ', 'are ', 'you?', "\n";
echo 'Hello ' . 'how ' . 'are ' . 'you?' . "\n";
Example
请注意,如果不使用换行字符,两个连续 echo 语句的输出将在同一行中呈现。请看以下示例 −
Note that output of the two successive echo statements will be rendered in the same line if the newline character is not used. Take a look at the following example −
<?php
echo "hello";
echo "world";
?>
它将生成以下 output −
It will produce the following output −
helloworld
The "print" Statement in PHP
print 语句类似于 echo,但它输出表达式。
The print statement is similar to echo, but it outputs an expression.
print(string $expression): int
与 echo 一样,print 也是一种语言结构。它的参数是一个表达式,但它不会放在括号中。
Like echo, print is also a language construct. Its argument is an expression but it is not put in parentheses.
主要区别在于 PHP 中的 print 语句只接受一个参数,并且总是返回 1。
The major difference is that the print statement in PHP accepts a single argument only and always returns 1.
Example
看看下面的 example −
Take a look at this following example −
<?php
$name = "Rajesh";
print "Hello " . $name . " How are you?\n";
print "Hello $name How are you?";
?>
它将生成以下 output −
It will produce the following output −
Hello Rajesh How are you?
Hello Rajesh How are you?
Output Multiline Strings Using Print/Echo
echo 和 print 语句都可以输出跨越编辑器中多行内容的多行字符串。请看以下示例 −
Both echo and print statements can output multiline strings spanning over more than one lines in the editor. Take a look at the following example −
<?php
print "
Multi-line
string can be output
by echo as well as
print statement in PHP
";
?>
它将生成以下 output −
It will produce the following output −
Multi-line
string can be output
by echo as well as
print statement in PHP
如果把 print 替换成 echo ,输出会保持不变。
The output will remain the same if we replace print with echo.
PHP var_dump() Function
PHP 中的内置函数之一是 var_dump() 函数。此函数显示结构化信息,例如类型和作为此函数的参数给出的一个或多个表达式的值。
One of the built-in functions in PHP is the var_dump() function. This function displays structured information such as type and the value of one or more expressions given as arguments to this function.
var_dump(mixed $value, mixed ...$values): void
此函数返回输出中对象的全部 public、private 和 protected 属性。数组和对象的转储信息缩进得当,显示递归结构。
This function returns all the public, private and protected properties of the objects in the output. The dump information about arrays and objects is properly indented to show the recursive structure.
对于内置整数、浮点数和布尔变量,var_dump() 函数显示参数变量的类型和值。
For the built-in integer, float and Boolean varibles, the var_dump() function shows the type and value of the argument variable.
Example 1
例如,这是一个整数变量 −
For example, here is an integer variable −
<?php
$x = 10;
var_dump ($x);
?>
转储信息如下 −
The dump information is as follows −
int(10)
Example 2
让我们看看它对浮点数变量的行为 −
Let’s see how it behaves for a float variable −
<?php
$x = 10.25;
var_dump ($x);
?>
var_dump() 函数返回以下 output −
The var_dump() function returns the following output −
float(10.25)
Example 3
如果表达式是布尔值 −
If the expression is a Boolean value −
<?php
$x = true;
var_dump ($x);
?>
它将生成以下 output −
It will produce the following output −
bool(true)
Example 4
对于字符串变量,var_dump() 函数还返回字符串的长度。
For a string variable, the var_dump() function returns the length of the string also.
<?php
$x = "Hello World";
var_dump ($x);
?>
它将生成以下 output −
It will produce the following output −
string(11) "Hello World"
这里我们可以使用 <pre> HTML 标记,它显示预格式化文本。<pre> 元素中的文本以固定宽度的字体显示,并且文本保留空格和换行符。
Here we can use the <pre> HTML tag that dislays preformatted text. Text in a <pre> element is displayed in a fixed-width font, and the text preserves both the spaces and the line breaks.
<?php
echo "<pre>";
$x = "Hello World";
var_dump ($x);
echo "</pre>"
?>
它将生成以下 output −
It will produce the following output −
string(11) "Hello World"
Example 5 - Studying the Array Structure Using var_dump()
var_dump() 函数对于研究数组结构非常有用。在以下示例中,我们有一个数组,其中数组的一个元素是另一个数组。换句话说,我们有嵌套数组的情况。
The var_dump() function is useful to study the array structure. In the following example, we have an array with one of the elements of the array being another array. In other words, we have a nested array situation.
<?php
$x = array("Hello", false, 99.99, array(10, 20,30));
var_dump ($x);
?>
它将生成以下 output −
It will produce the following output −
array(4) {
[0]=>
string(5) "Hello"
[1]=>
bool(false)
[2]=>
float(99.99)
[3]=>
array(3) {
[0]=>
int(10)
[1]=>
int(20)
[2]=>
int(30)
}
}
Example 6
由于在前面的示例中“$x”是一个 indexed array ,所以从“0”开始的索引及其值将被转储。如果数组是 associate array ,则会转储键值对信息。
Since "$x" is an indexed array in the previous example, the index starting with "0" along with its value is dumped. In case the array is an associate array, the key-value pair information is dumped.
<?php
$x = array(
"Hello", false, 99.99,
array(1=>10, 2=>20,3=>30)
);
var_dump($x);
?>
在这里,你会得到以下 output −
Here, you will get the following output −
array(4) {
[0]=>
string(5) "Hello"
[1]=>
bool(false)
[2]=>
float(99.99)
[3]=>
array(3) {
[1]=>
int(10)
[2]=>
int(20)
[3]=>
int(30)
}
}
当你使用 var_dump() 来显示数组值时,不需要使用结束标记“ </pre> ”。
When you use var_dump() to show array value, there is no need of using the end tag " </pre> ".
Example 7
var_dump() 函数还可以显示表示类的对象的特性。在下面的示例中,我们声明了一个 Point 类,其中包含两个私有特性“x”和“y”。该类的构造函数使用传递给它的参数初始化对象“p”。
The var_dump() function can als reveal the properties of an object representing a class. In the following example, we have declared a Point class with two private properties "x" and "y". The class constructor initializes the object "p" with the parameters passed to it.
var_dump() 函数提供有关对象特性及其对应值的信息。
The var_dump() function provides the information about the object properties and their corrcponding values.
<?php
class Point {
private int $x;
private int $y;
public function __construct(int $x, int $y = 0) {
$this->x = $x;
$this->y = $y;
}
}
$p = new Point(4, 5);
var_dump($p)
?>
它将生成以下 output −
It will produce the following output −
object(Point)#1 (2) {
["x":"Point":private]=>
int(4)
["y":"Point":private]=>
int(5)
}
PHP 中有一个类似的内置函数,用于生成转储,名为 get_defined_vars() 。
There is a similar built-in function for producing dump in PHP, named as get_defined_vars().
var_dump(get_defined_vars());
它会将所有已定义的变量转储到浏览器中。
It will dump all the defined variables to the browser.
PHP - $ and $$ Variables
我们知道 PHP 使用“$”符号作为变量名前缀的惯例。PHP 还可以通过在名称之前添加两个美元符号 ($$) 来声明动态变量。变量变量(或动态变量)可以动态设置和使用。
We know that PHP uses the convention of prefixing the variable names by the "$" symbol. PHP also has the provision of declaring dynamic variables by prefixing two dollar symbols ($$) to the name. A variable variable (or a dynamic variable) can be set and used dynamically.
普通变量的声明如下 −
The declaration of a normal variable is like this −
$a = 'good';
动态变量取正常变量的值并将其作为变量的名称。在上面的示例中,“good”可以使用两个美元符号“$$”作为变量名 −
A dynamic variable takes the value of a normal variable and treats that as the name of the variable. In the above example, "good" can be used as the name of a variable by using two dollar signs "$$" −
$$a = 'morning';
我们现在有两个变量:“$a”内容为“good”和“$$a”内容为“morning”。因此,以下 echo 语句将产生相同输出 −
We now have two variables: "$a" with contents "good" and "$$a" with contents "morning". As a result, the following echo statements will produce the same output −
echo "$a {$$a}";
echo "$a $good";
两者都产生相同的输出 −
Both produce the same output −
good morning
Example 1
看看下面的 example −
Take a look at this following example −
<?php
$a = 'good';
$$a = 'morning';
echo "$a {$$a}\n";
echo "$a $good";
?>
它将生成以下 output −
It will produce the following output −
good morning
good morning
Example 2
我们来看另一个示例 −
Let’s take a look at another example −
<?php
$x = "foo";
$$x = "bar";
echo "Value of x = " .$x . "\n";
echo 'Value of $$x = ' . $$x . "\n";
echo 'Value of foo = ' . $foo;
?>
在这里,你会得到以下 output −
Here, you will get the following output −
Value of x = foo
Value of $$x = bar
Value of foo = bar
Using Multiple "$" Symbols
请注意,美元符号“$”的使用并不限于两个。可以添加任意数量的美元符号作为前缀。
Note that the use of "$" symbol is not restricted to two. Any number of dollar symbols can be prefixed.
假设有一个变量“$x”其值为“a”。接下来,我们定义 x='as',那么“x”和“$a”将具有相同的值。同样,语句 $$$x='and' 实际上声明了一个“$as”变量,其值为’and'。
Suppose there is a variable "$x" with "a" as its value. Next, we define x='as', then "x" as well as "$a" will have the same value. Similarly, the statement $$$x='and' effectively declares a "$as" variable whose value is 'and'.
Example
这是一个完整的示例,展示了多个“$”符号的使用。
Here is a complete example that shows the use of multiple "$" symbols.
<?php
$php = "a";
$lang = "php";
$World = "lang";
$Hello = "World";
$a = "Hello";
echo '$a= ' . $a;
echo "\n";
echo '$$a= ' . $$a;
echo "\n";
echo '$$$a= ' . $$$a;
echo "\n";
echo '$$$$a= ' . $$$$a;
echo "\n";
echo '$$$$$a= ' . $$$$$a;
?>
当您运行此代码时,它将生成以下 output −
When you run this code, it will produce the following output −
$a= Hello
$$a= World
$$$a= lang
$$$$a= php
$$$$$a= a
Using Dynamic Variables with Arrays
将动态变量与数组一起使用可能会导致某些歧义的情况。对于数组“a”,如果你写 a[1],那么解析器需要知道你是否正在引用“$a[1]”作为变量,或者你希望“a”作为变量,然后从该变量中获取 [1] 索引。
Using dynamic variables with arrays may lead to certain ambiguous situations. With an array "a", if you write a[1], then the parser needs to know if you are refering to "$a[1]" as a variable or if you want "a" as the variable and then the [1] index from that variable.
若要解决此歧义,请对第一种情况使用 ${$a[1]},对第二种情况使用 ${$a}[1]。
To resolve this ambiguity, use ${$a[1]} for the first case and ${$a}[1] for the second.
Example
请看以下示例:
Take a look at the following example −
<?php
$vars = array("hw", "os", "lang");
$var_hw="Intel";
$var_lang="PHP";
$var_os="Linux";
foreach ($vars as $var)
echo ${"var_$var"} . "\n";
print "$var_hw\n$var_os\n$var_lang";
?>
它将生成以下 output −
It will produce the following output −
Intel
Linux
PHP
Intel
Linux
PHP
需要注意的是,这个技术不能在函数或类方法中与 PHP 的超全局数组(PHP 中的几个预定义变量是“超全局”,这意味着它们在整个脚本中的所有作用域中都可用)一起使用。变量“$this”是 PHP 中的一个特殊变量,不能动态引用。
It may be noted that this technique cannot be used with PHP’s Superglobal arrays (Several predefined variables in PHP are "superglobals", which means they are available in all scopes throughout a script) within functions or class methods. The variable "$this" is a special variable in PHP and it cannot be referenced dynamically.
PHP - Constants
PHP 中的常量是简单值的一个名称或标识符。常量值在 PHP 脚本执行期间不能更改。
A constant in PHP is a name or an identifier for a simple value. A constant value cannot change during the execution of the PHP script.
-
By default, a PHP constant is case-sensitive.
-
By convention, constant identifiers are always uppercase.
-
A constant name starts with a letter or underscore, followed by any number of letters, numbers, or underscore.
-
There is no need to write a dollar sign ($) before a constant, however one has to use a dollar sign before a variable.
Examples of Valid and Invalid Constant Names in PHP
以下是 PHP 中有效和无效常量名称的一些示例 -
Here are some examples of valid and invalid constant names in PHP −
// Valid constant names
define("ONE", "first thing");
define("TWO2", "second thing");
define("THREE_3", "third thing");
define("__THREE__", "third value");
// Invalid constant names
define("2TWO", "second thing");
Difference between Constants and Variables in PHP
-
Constants cannot be defined by simple assignment; they can only be defined using the define() function.
-
Constants may be defined and accessed anywhere without regard to variable scoping rules.
-
Once the Constants have been set, they may not be redefined or undefined.
Defining a Named Constant
PHP 函数库中的 define() 函数用于在运行时定义一个命名常量。
The define() function in PHP library is used to define a named constant at runtime.
define(string $const_name, mixed $value, bool $case = false): bool
Parameters
-
const_name − The name of the constant.
-
value − The value of the constant. It can be a scalar value (int, float, string, bool, or null) or array values are also accepted.
-
case − If set to true, the constant will be defined case-insensitive. The default behavior is case-sensitive, i.e., CONSTANT and Constant represent different values.
define() 函数在成功时返回 "true",失败时返回 "false"。
The define() function returns "true" on success and "false" on failure.
Example 1
以下示例演示了 define() 函数的工作原理:
The following example demonstrates how the define() function works −
<?php
define("CONSTANT", "Hello world.");
echo CONSTANT;
// echo Constant;
?>
第一条 echo 语句输出 CONSTANT 的值。您将获得以下 output −
The first echo statement outputs the value of CONSTANT. You will get the following output −
Hello world.
但是,当您取消对第二条 echo 语句的注释时,它将显示以下错误:
But, when you uncomment the second echo statement, it will display the following error −
Fatal error: Uncaught Error: Undefined constant "Constant" in hello.php: on line 5
如果将 case 参数设置为 False,PHP 不区分大小写常量。
If you set the case parameter to False, PHP doesn’t differentiate upper and lowercase constants.
Example 2
您还可以将数组用作常量的值。查看以下示例:
You can also use an array as the value of a constant. Take a look at the following example −
<?php
define(
$name="LANGS",
$value=array('PHP', 'Java', 'Python')
);
var_dump(LANGS);
?>
它将生成以下 output −
It will produce the following output −
array(3) {
[0]=>
string(3) "PHP"
[1]=>
string(4) "Java"
[2]=>
string(6) "Python"
}
Using the constant() Function
echo 语句输出已定义常量的值。您还可以使用 constant() 函数。它返回 name 指示的常量的值。
The echo statement outputs the value of the defined constant. You can also use the constant() function. It returns the value of the constant indicated by name.
constant(string $name): mixed
如果您需要检索常量的值,但不知道其名称,constant() 函数非常有用。即,它存储在变量中或由函数返回。
The constant() function is useful if you need to retrieve the value of a constant, but do not know its name. I.e. it is stored in a variable or returned by a function.
<?php
define("MINSIZE", 50);
echo MINSIZE;
echo PHP_EOL;
echo constant("MINSIZE"); // same thing as the previous line
?>
它将生成以下 output −
It will produce the following output −
50
50
Using the defined() Function
PHP 函数库提供了一个 defined() 函数,用于检查是否给定一个已命名的常量存在。查看以下示例:
The PHP library provides a defined() function that checks whether a given named constant exists. Take a look at the following example −
<?php
define('MAX', 100);
if (defined('MAX')) {
echo MAX;
}
?>
它将生成以下 output −
It will produce the following output −
100
PHP 还有一个名为 "get_defined_constants()" 的函数,它返回一个包含所有已定义常量及其值在内的关联数组。
PHP also has a function called "get_defined_constants()" that returns an associative array of all the defined constants and their values.
PHP - Magic Constants
PHP 中的魔术常量是预定义常量。它们可以供在上面运行的任何脚本使用,并且它们会根据使用位置而改变。与在运行时解决的常规常量不同,所有这些“魔术”常量均在编译时解决。
The magical constants in PHP are predefined constants. They are available to any script on which they run, and they change depending on where they are used. All these "magical" constants are resolved at compile time, unlike regular constants, which are resolved at runtime.
PHP 中有九个魔术常量。这些特殊常量不区分大小写。
There are nine magical constants in PHP. These special constants are case insensitive.
LINE
它返回文件中的当前行号。以下 example 展示了如何使用此魔术常量。
It returns the current line number of the file. The following example shows how you can use this magic constant.
<?php
$x="Hello World";
echo "$x. The current Line number is " . __LINE__ . ".";
?>
它将生成以下 output −
It will produce the following output −
Hello World. The current Line number is 5.
FILE
此魔术常量返回文件的完整路径和文件名。如果在 include 内使用,则返回已包括文件的名字。请看以下 example −
This magic constant returns the full path and filename of the file. If used inside an include, the name of the included file is returned. Take a look at the following example −
<?php
$x="Hello World";
echo "$x. Current PHP script name is " . __FILE__ . ".";
?>
它将生成以下 output −
It will produce the following output −
Hello World. Current PHP script name is C:\xampp\htdocs\hello.php.
DIR
此魔术常量返回文件的目录。如果在 include 内使用,则返回已包括文件的目录。这相当于“dirname( FILE )”。此目录名称没有尾随斜杠,除非它是根目录。见以下 example −
This magical constant returns the directory of the file. If used inside an include, the directory of the included file is returned. This is equivalent to "dirname(FILE)". This directory name does not have a trailing slash unless it is the root directory. See the following example −
<?php
$x="Hello World";
echo "$x. Directory of the Current PHP script name is " . __DIR__ . ".";
?>
它将在浏览器上显示以下 output −
It will show the following output on the browser −
Hello World. Directory of the Current PHP script name is C:\xampp\htdocs.
FUNCTION
此魔术常量返回使用常量的功能名称,或匿名功能的 {closure}。以下 example 展示了它是如何工作的 −
This magical constant returns the function name in which the constant is used, or {closure} for anonymous functions. The following example shows how it works −
<?php
function hello(){
$x="Hello World";
echo "$x. The function name is ". __FUNCTION__ . "";
}
hello();
?>
它将生成以下 output −
It will produce the following output −
Hello World. The function name is hello
如果此魔术常量在功能之外使用,那么它将给空白输出。
If this magic constant is used outside the function, then it will give a blank output.
CLASS
此常量返回类名称。类名称包含它声明的命名空间。见以下 example −
This constant returns the name of a class. The class name includes the namespace it was declared in. See the following example −
<?php
class myclass {
public function __construct() {
echo "Inside the constructor of ". __CLASS__ . PHP_EOL;
}
function getClassName(){
echo "from an instance method of " . __CLASS__ . "";
}
}
$obj = new myclass;
$obj->getClassName();
?>
它将生成以下 output −
It will produce the following output −
Inside the constructor of myclass
from an instance method of myclass
METHOD
METHOD 常量返回类方法名称。以下 example 展示了它是如何工作的 −
The METHOD constant returns the class method name. The following example shows how it works −
<?php
class myclass {
public function __construct() {
echo "Calling " . __METHOD__ . " of " . __CLASS__ ."<br>";
}
function mymethod(){
echo "Calling " . __METHOD__ . " of " . __CLASS__ ."";
}
}
$obj = new myclass;
$obj->mymethod();
?>
它将生成以下 output −
It will produce the following output −
Calling myclass::__construct of myclass
Calling myclass::mymethod of myclass
TRAIT
返回性状名称。性状名称包含它声明的命名空间。在 PHP 中,性状是重新使用代码的机制。性状类似于类,但只准备以精细且一致的方式对功能分组。无法在它自己上实例化这种性状。
It returns the trait name. The trait name includes the namespace it was declared in. In PHP, traits are a mechanism for code reuse. A trait is similar to a class, but only intended to group functionality in a fine-grained and consistent way. It is not possible to instantiate a trait on its own.
看看以下 example −
Take a look at the following example −
<?php
trait mytrait {
public function hello() {
echo "Hello World from " . __TRAIT__ ."";
}
}
class myclass {
use mytrait;
}
$obj = new myclass();
$obj->hello();
?>
它将生成以下 output −
It will produce the following output −
Hello World from mytrait
NAMESPACE
此常量返回当前命名空间的名称。在 PHP 中,通过命名空间,我们能够使用名称相同的类 / 功能 / 常量在不同的环境中,无需任何冲突,从而封装这些项目。命名空间是类 / 功能的逻辑组合,根据它们的相关性。
This constant returns the name of the current namespace. In PHP, namespaces allow us to use classes / functions / constants of same name in different contexts without any conflict, thereby encapsulating these items. A namespace is a logical grouping of classes/functions depending on their relevance.
以下 example 展示了如何使用此魔术常量 −
The following example shows how you can use this magic constant −
<?php
namespace myspace;
class myclass {
public function __construct() {
echo "Name of the class: " . __CLASS__ . " in " . __NAMESPACE__ . "";
}
}
$class_name = __NAMESPACE__ . '\myclass';
$a = new $class_name;
?>
它将生成以下 output −
It will produce the following output −
Name of the class: myspace\myclass in myspace
ClassName::class
与其他魔术常量不同,这个魔术常量不以双下划线 (__ ) 开头或结尾。它返回完全限定的类名。
Unlike the other magic constants, this magic constant does not start and end with the double underscore (__). It returns the fully qualified class name.
以下 example 展示了如何使用此魔术常量 −
The following example shows how you can use this magic constant −
<?php
namespace myspace;
class myclass {
public function __construct() {
echo "Name of the class: " . myclass::class ;
}
}
use myspace;
$a = new myclass;
?>
它将生成以下 output −
It will produce the following output −
Name of the class: myspace\myclass
PHP – Data Types
“数据类型”一词是指将数据分类到不同类别中。PHP 共有八种数据类型,我们使用这些数据类型构造变量−
The term "data types" refers to the classification of data in distinct categories. PHP has a total of eight data types that we use to construct our variables −
-
Integers − Whole numbers, without a decimal point, like 4195.
-
Doubles − Floating-point numbers like 3.14159 or 49.1.
-
Booleans − Have only two possible values, either true or false.
-
NULL − Special type that only has one value: NULL.
-
Strings − Sequences of characters, like 'PHP supports string operations.'
-
Arrays − Named and indexed collections of other values.
-
Objects − Instances of programmer-defined classes, which can package up both other kinds of values and functions that are specific to the class.
-
Resources − Special variables that hold references to resources external to PHP (such as database connections).
前五个是简单类型,后两个(数组和对象)是复合类型。复合类型可以打包任意类型的其他任意值,而简单类型则不能。
The first five are simple types, and the next two (arrays and objects) are compound types. The compound types can package up other arbitrary values of arbitrary type, whereas the simple types cannot.
在本章中,让我们详细讨论 PHP 的这些内置数据类型。
In this chapter, let’s discuss in detail about these built-in data types of PHP.
Integer Data Type in PHP
没有小数点(例如 4195)的整数在 PHP 中为 int 类型。整型数据类型是最简单的类型。它们对应于简单的整数,包括正数和负数。
A whole number without a decimal point (like 4195) is of int type in PHP. Integer data types are the simplest type. They correspond to simple whole numbers, both positive and negative.
-
An int is a number of the set Z = {…, -2, -1, 0, 1, 2, …}.
-
An int can be represented in a decimal (base 10), hexadecimal (base 16), octal (base 8) or binary (base 2) notation.
要使用八进制记法,数字应加上前缀“0o”或“0O”。要使用十六进制记法,数字应加上前缀“0x”。要使用二进制记法,数字应加上前缀“0b”。
To use octal notation, a number is preceded with "0o" or "0O". To use hexadecimal notation, precede the number with "0x". To use binary notation, precede the number with "0b".
下面给出一些 examples -
Given below are some examples −
-
Decimal Integer − 201, 4195, -15
-
Octal Integer − 0010, 0O12, -0O21
-
Hexadecimal Integer − 0x10, -0x100
-
Binary Integer − 0b10101, -0b100
可以将整数分配给变量,也可以在表达式中使用它们,如下所示 -
Integers can be assigned to variables, or they can be used in expressions, like so −
$int_var = 12345;
$another_int = -12345 + 12345;
Double Data Type in PHP
双变量表示浮点数(也称为“浮点数”、“双精度数”或“实数”),即带分数部分的数字。分数部分跟在整数部分后面,中间用小数符号 (.) 分隔。
Double variables represent floating point numbers (also known as "floats", "doubles", or "real numbers") that are the numbers with a fractional component. The fractional component follows after the integer component separated by the decimal symbol (.)
Note − 双精度变量可以为正、负或零。
Note − A double variable can be positive, negative, or zero.
$var1 = 1.55
$var2 =-123.0
Scientific Float Notation
PHP还允许使用科学计数法表示小数点后具有更多位数的浮点数。符号“E”或“e”用于分隔整数部分和小数部分。
PHP also allows the use of scientific notation to represent a floating point number with more digits after the decimal point. The symbol "E" or "e" is used to separate the integer and fractional part.
− 1.2e3, 2.33e-4, 7E-10, 1.0E5
默认情况下,双精度打印时使用所需的小数位数。查看以下 example −
By default, doubles print with the minimum number of decimal places needed. Take a look at the following example −
<?php
$many = 2.2888800;
$many_2 = 2.2111200;
$few = $many + $many_2;
print("$many + $many_2 = $few");
?>
它生成下列内容 output −
It produces the following output −
2.28888 + 2.21112 = 4.5
Boolean Data Type in PHP
bool 类型只有两个值;它可以为真或假。 bool 类型用于表示真值。
The bool type only has only two values; it can either be True or False. The bool type is used to express a truth value.
$bool1 = true;
$bool2 = false;
你也可以使用整数值“1”和“0”来表示真和假布尔值 −
You can also use the integer values "1" and "0" to represent True and False Boolean values −
$bool3 = 1;
$bool4 = 0;
通常,返回布尔值的运算符的结果将传递给诸如 if, while 或 do-while 之类的控制结构。例如,
Typically, the result of an operator which returns a bool value is passed on to a control structure such as if, while or do-while. For example,
if (TRUE)
print("This will always print.");
else
print("This will never print.");
Interpreting Other Data Types as Booleans
这里有一组规则,你可以使用它来将其他数据类型解释为布尔值 −
Here is a set of rules that you can use to interpret other data types as Booleans −
-
If the value is a number, then it is False only if the value is equal to zero, otherwise the value is True.
-
If the value is a string, it is False if the string is empty (has zero characters) or is the string "0", and is True otherwise.
-
The values of type NULL are always False.
-
If the value is an array, it is False if it contains no other values; True otherwise. For an object, containing a value means having a member variable that has been assigned a value.
-
Valid resources are true (although some functions that return resources when they are successful will return FALSE when unsuccessful).
Note − 不要将双精度用于布尔值。
Note − Don’t use double as Booleans.
当在布尔上下文中使用这些变量中的每一个时,这些变量中每一个都有其名称中嵌入的真值。
Each of the following variables has the truth value embedded in its name when it is used in a Boolean context.
$true_num = 3 + 0.14159;
$true_str = "Tried and true"
$true_array[49] = "An array element";
$false_array = array();
$false_null = NULL;
$false_num = 999 - 999;
$false_str = "";
String Data Type in PHP
字符串是一系列字符,例如’PHP支持字符串操作'。
A string is a sequence of characters, for example, 'PHP supports string operations.'
在PHP中,一个字符和一个字节相同。它意味着PHP仅支持256个字符集,因此不提供本机Unicode支持。
In PHP, a character is the same as a byte. It means PHP only supports a 256 character set, and hence does not offer native Unicode support.
PHP支持单引号和双引号字符串的构建。以下两种表示在PHP中均有效 −
PHP supports single-quoted as well as double-quoted string formation. Both the following representations are valid in PHP −
$string_1 = "This is a string in double quotes";
$string_2 = 'This is a somewhat longer, singly quoted string';
以下是字符串类型的其他一些示例 −
Here are some more examples of string type −
$string_39 = "This string has thirty-nine characters";
$string_0 = ""; // a string with zero characters
单引号字符串几乎按原样对待,而双引号字符串用其值替换变量,并特别解释某些字符序列。
Single-quoted strings are treated almost literally, whereas double-quoted strings replace variables with their values as well as specially interpreting certain character sequences.
<?php
$variable = "name";
$literally = 'My $variable will not print!';
print($literally);
print "\n";
$literally = "My $variable will print!";
print($literally);
?>
当您运行此代码时,它将生成以下 output −
When you run this code, it will produce the following output −
My $variable will not print!
My name will print
字符串长度没有限制。在可用内存范围内,您可以创建任意长的字符串。
There are no artificial limits on string length. Within the bounds of available memory, you ought to be able to make arbitrarily long strings.
由双引号分隔的字符串(如“this”)将在 PHP 中通过以下两种方式进行预处理:
Strings that are delimited by double quotes (as in "this") are preprocessed in both the following two ways by PHP −
-
Certain character sequences beginning with backslash (\) are replaced with special characters.
-
Variable names (starting with $) are replaced with string representations of their values.
转义序列替换如下:
The escape-sequence replacements are −
-
\n is replaced by the newline character
-
\r is replaced by the carriage-return character
-
\t is replaced by the tab character
-
\$ is replaced by the dollar sign itself ($)
-
\" is replaced by a single double-quote (")
-
\\ is replaced by a single backslash (\)
PHP 也有字符串数据类型的 Heredoc 和 Nowdoc 表示法。
PHP also has Heredoc and Nowdoc representations of string data type.
Heredoc Representation of String Data Type
您可以使用 heredoc 将多行分配给单个字符串变量:
You can assign multiple lines to a single string variable using heredoc −
<?php
$channel =<<<_XML_
<channel>
<title>What's For Dinner</title>
<link>http://menu.example.com/ </link>
<description>Choose what to eat tonight.</description>
</channel>
_XML_;
echo <<< END
This uses the "here document" syntax to output multiple lines with
variable interpolation. Note that the here document terminator must
appear on a line with just a semicolon. no extra whitespace!
END;
print $channel;
?>
当你运行这段代码时,它将产生以下输出:
When you run this code, it will produce the following output −
This uses the "here document" syntax to output
multiple lines with variable interpolation. Note
that the here document terminator must appear on a
line with just a semicolon. no extra whitespace!
<channel>
<title>What's For Dinner</title>
<link>http://menu.example.com/ </link>
<description>Choose what to eat tonight.</description>
</channel>
Nowdoc Representation of String Data Type
heredoc 标识符的所有规则也适用于 nowdoc 标识符。 nowdoc 的指定方式与 heredoc 相同,但 nowdoc 中不进行解析。您可以使用 nowdoc 构造来嵌入大块文本,而无需使用任何转义字符。
All the rules for heredoc identifiers also apply to nowdoc identifiers. A nowdoc is specified just like a heredoc, but there is no parsing inside a nowdoc. You can use the nowdoc construct for embedding large blocks of text without having to use any escape characters.
nowdoc 使用与 heredoc 相同的 <<< 序列进行标识,但标识符用单引号括起来,例如 <<<'EOT'。Nowdoc 用于单引号字符串,就像 heredoc 用于双引号字符串一样。
A nowdoc is identified with the same <<< sequence used for heredocs, but the identifier is enclosed in single quotes, e.g. <<<'EOT'. Nowdocs apply to single-quoted strings just the way heredocs apply to double-quoted strings.
请看以下示例:
Take a look at the following example −
<?php
echo <<<'IDENTIFIER'
As the cat cleared its throat with a refined "Meow",
the squirrel chirped excitedly about its latest
discovery of a hidden stash of peanut treasure!
IDENTIFIER;
?>
运行代码并检查其输出:
Run the code and check its output −
As the cat cleared its throat with a refined "Meow",
the squirrel chirped excitedly about its latest
discovery of a hidden stash of peanut treasure!
Null Data Type in PHP
在 PHP 中,null 表示一个特殊类型,它只有一个值:NULL。未定义和 unset() 变量将变为值“null”。
In PHP, null represents a special type that only has one value: NULL. Undefined and unset() variables will resolve to the value "null".
程序员通常在 PHP 中使用 Null 数据类型来初始化变量或指示某个值缺失。
Programmers normally use the Null data type in PHP to initialize variables or to indicate that a value is missing.
若要给变量赋值为 NULL,只需像这样赋值:
To give a variable the NULL value, simply assign it like this −
$my_var = NULL;
根据惯例,特殊常量 NULL 为大写,但实际上它就是 case insensitive; 你也可以直接输入 −
The special constant NULL is capitalized by convention, but actually it is case insensitive; you could just as well have typed −
$my_var = null;
已分配了 NULL 的变量具有以下属性 −
A variable that has been assigned NULL has the following properties −
-
It evaluates to FALSE in a Boolean context.
-
It returns FALSE when tested with IsSet() function.
Note − PHP 中变量的数据类型在运行时根据赋值给它们的 值来确定。
Note − The data types of variables in PHP are determined at runtime based on the values that are assigned to them.
Array Data Type in PHP
PHP 中的一个数组是一个有序映射,一个键与一个或多个值相关联。PHP 数组使用 array() 函数定义,或使用将数据放入方括号中的简短表示法进行定义。
An array in PHP is an ordered map, a key is associated with one or more values. A PHP array is defined using the array() function, or with the use of a short notation where the data is put in square brackets.
请看以下 associative arrays 示例 −
Take a look at the following examples of associative arrays −
Using the Short Notation
$arr = [
"foo" => "bar",
"bar" => "foo",
];
PHP 中的数组也可以使用“键 - 值对”语法定义。它被称为 indexed array 。
An array in PHP can also be defined with the "key-value pair" syntax. It is called an indexed array.
$arr = array("foo", "bar", "hello", "world");
在 multi-dimensional array 中,主数组中的每个元素也可以是一个数组。而且,子数组中的每个元素也可以是一个数组,依此类推。使用多重索引访问多维数组中的值。
In a multi-dimensional array, each element in the main array can also be an array. And, each element in the sub-array can be an array, and so on. Values in the multi-dimensional array are accessed using multiple index.
Note − 在 PHP 中,复合数据类型用于存储数据集合,包括数组和对象。
Note − In PHP, compound data types are used to store collections of data, including arrays and objects.
Object Data Type in PHP
对象类型是一个由程序员定义的类的实例,它可以将其他类型的值和特定于该类的函数打包在一起。
An object type is an instance of a programmer-defined class, which can package up both other kinds of values and functions that are specific to the class.
要创建一个新对象,请使用 new statement 实例化一个类 −
To create a new object, use the new statement to instantiate a class −
class foo {
function bar() {
echo "Hello World.";
}
}
$obj = new foo;
$obj->bar();
Resource Data Type in PHP
资源是保存对 PHP 外部资源(如文件流或数据库连接)的引用的特殊变量。
Resources are special variables that hold references to resources external to PHP (such as a file stream or database connections).
以下是一个 file resource 示例 −
Here is an example of file resource −
$fp = fopen("foo.txt", "w");
属于以上任何类型的的数据都存储在一个变量中。但是,由于 PHP 是一种动态类型语言,因此无需指定变量的类型,因为这将在运行时确定。
Data belonging to any of the above types is stored in a variable. However, since PHP is a dynamically typed language, there is no need to specify the type of a variable, as this will be determined at runtime.
Example: The gettype() Function
gettype() 函数有助于找出存储在变量中的数据类型 −
The gettype() function is helpful to find out the type of data stored in a variable −
<?php
$x = 10;
echo gettype($x) . "\n";
$y = 10.55;
echo gettype($y) . "\n";
$z = [1,2,3,4,5];
echo gettype($z);
?>
当您运行此代码时,它将生成以下 output −
When you run this code, it will produce the following output −
integer
double
array
PHP - Type Casting
术语 "类型转换" 指的是将一种类型的数据转换为另一种类型。由于 PHP 是弱类型语言,解析器在执行某些操作时会将某些数据类型强制转换为其他类型。例如,如果一个带有数字的字符串是加法运算中涉及的运算符之一,则它将转换为整数。
The term "Type Casting" refers to conversion of one type of data to another. Since PHP is a weakly typed language, the parser coerces certain data types into others while performing certain operations. For example, a string having digits is converted to integer if it is one of the operands involved in the addition operation.
Implicit Type Casting
下面是一个强制或隐式类型转换的示例:
Here is an example of coercive or implicit type casting −
<?php
$a = 10;
$b = '20';
$c = $a+$b;
echo "c = " . $c;
?>
在这种情况下,$b 是一个字符串变量,被转换为整数以启用加法。它将生成以下 output −
In this case, $b is a string variable, cast into an integer to enable addition. It will produce the following output −
c = 30
我们再来看另一个示例。这里,整数变量 $a 被转换为一个字符串,以便与一个字符串变量连接。
Let’s take another example. Here, an integer variable $a is converted to a string so that it is concatenated with a string variable.
<?php
$a = 10;
$b = '20';
$c = $a.$b;
echo "c = " . $c;
?>
它将生成以下 output −
It will produce the following output −
c = 1020
除了此类强制类型转换之外,还有其他方法可将一种类型的数据显式转换为另一种类型。为此,您可以使用 PHP 的类型转换运算符或类型转换函数。
In addition to such coercive type conversion, there are other ways to explicitly cast one type of data to other. You can use PHP’s type casting operators or type casting functions for this purpose.
Type Casting Operators
若要将一个类型的表达式转换为另一个类型,您需要在表达式前面的括号中放入后一个类型的数据类型。
To convert an expression of one type to another, you need to put the data type of the latter in parenthesis before the expression.
$var = (type)expr;
PHP 中的某些类型转换运算符为 −
Some of the type casting operators in PHP are −
-
(int) or (integer) casts to an integer
-
(bool) or (boolean) casts to a boolean
-
(float) or (double) or (real) casts to a float
-
(string) casts to a string
-
(array) casts to an array
-
(object) casts to an object
Casting to Integer
您可以轻松地将浮点值转换为整数。看看以下 example −
You can easily convert a float value to an integer. Take a look at the following example −
<?php
$a = 9.99;
$b = (int)$a;
var_dump($b);
?>
它将生成以下 output −
It will produce the following output −
int(9)
请注意,浮点值不会四舍五入到最接近的整数;它只会返回整数部分。
Note that the float value is not rounded to the nearest integer; instead it just returns the integer part.
String to Integer Conversion
(int) 运算符还会将字符串转换为整数。如果字符串仅由数字组成,则转换非常简单。
The (int) operator also coverts a string to integer. The conversion is straightforward if the string consists of just digits.
<?php
$a = "99";
$b = (int)$a;
var_dump($b);
?>
在这里,你会得到以下 output −
Here, you will get the following output −
int(99)
即使字符串包含浮点数,(int) 运算符也会仅返回整数部分。
Even if the string contains a floating point number, the (int) operator returns just the integer part.
现在我们再举一个例子来理解一个特殊情况。 If the string is alphanumeric, casting with (int) works differently 。
Now let’s take another example to understand a special case. If the string is alphanumeric, casting with (int) works differently.
-
If the string starts with digits followed by non-numeric characters, only the initial digits are considered.
-
If the string starts with non-numeric characters and the digits are in the middle, the csting operator returns "0".
看看以下 example −
Take a look at the following example −
<?php
$a = "10 Rs.";
$b = (int)$a;
var_dump($b);
$a = "$100";
$b = (int)$a;
var_dump($b);
?>
它将生成以下 output −
It will produce the following output −
int(10)
int(0)
Casting to Float Type
您可以使用 (float) 或 (double) 强转运算符将变量或表达式显式转换为浮点数。
You can use either the (float) or (double) casting operator to explicitly convert a variable or expression to a float.
<?php
$a = 100;
$b = (double)$a;
var_dump($b);
?>
它将生成以下 output −
It will produce the following output −
float(100)
包含任何有效数字表示的字符串都可以使用强转运算符转换为浮点类型。
A string containing any valid numeric representation may be cast to a float type by using a casting operator.
<?php
$a = "100";
$b = (double)$a;
var_dump($b);
$a = "9.99";
$b = (float)$a;
var_dump($b);
?>
在这里,你会得到以下 output −
Here, you will get the following output −
float(100)
float(9.99)
即使字符串嵌入了浮点的科学计数法,该字符串也会转换为浮点。看看下面的例子 −
The string gets converted to float even when it embeds a scientific notation of float. Take a look at the following example −
<?php
$a = "1.23E01";
$b = (double)$a;
var_dump($b);
$a = "5.5E-5";
$b = (float)$a;
var_dump($b);
?>
它将生成以下 output −
It will produce the following output −
float(12.3)
float(5.5E-5)
所有浮点数后边的非数字字符均被忽略。类似的,如果字符串以任何一个非数字字符开头,则会转换为“0”。见以下的 example −
All the non-numeric characters after the floating point numbers are ignored. Similarly, the string converts to "0" if it starts with any non-numeric character. See the following example −
<?php
$a = "295.95 only";
$b = (double)$a;
var_dump($b);
$a = "$2.50";
$b = (float)$a;
var_dump($b);
?>
它将生成以下 output −
It will produce the following output −
float(295.95)
float(0)
Casting to String Type
通过使用一个强制类型转换操作符,任何求值为一个浮点数或整数的表达式都可以强制转换为一个字符串类型。下面给出几个示例−
By using a casting operator, any expression evaluating to a floating-point or integer may be cast to a string type. Few examples are given below −
<?php
$a = 100;
$b = (string)$a;
var_dump($b);
$x = 55.50;
$y = (string)$x;
var_dump($y);
?>
您将获得以下内容 output −
You will get the following output −
string(3) "100"
string(4) "55.5"
Casting to Bool Type
任何非零数字(可以是整数或浮点数)使用 (bool) 操作符强制转换为 true。求值为“0”的表达式返回 false。字符串始终强制转换为 true。
Any non-zero number, either integer or float, is cast to true with (bool) operator. An expression evaluating to "0" returns false. A string is always cast to true.
看看以下 example −
Take a look at the following example −
<?php
$a = 100;
$b = (bool)$a;
$x = 0;
$y = (bool)$x;
$m = "Hello";
$n = (bool)$m;
var_dump($b);
var_dump($y);
var_dump($n);
?>
它将生成以下 output −
It will produce the following output −
bool(true)
bool(false)
bool(true)
Type Casting Functions
PHP 包含用于执行类型强制转换的以下内置函数−
PHP includes the following built-in functions for performing type casting −
-
intval()
-
floatval()
-
strval()
让我们详细讨论这些内置函数。
Let’s discuss these built-in functions in detail.
The intval() Function
这个函数获取一个变量的整数值。
This function gets the integer value of a variable.
intval(mixed $value, int $base = 10): int
$base 参数默认为 10,这意味着该值转换为十进制数。
The $base parameter is 10 by default, which means the value is converted to decimal number.
-
If the value is a float, the intval() function returns an integer, discarding the fractional part.
-
A string representation of a number returns a corresponding integer, discarding the fractional part, if any.
-
If the value is a string with a valid octal number and the base is 8, the intval() function returns a corresponding octal number.
When the base is "0" ,值的转换基于字符前缀。
When the base is "0", the conversion of value takes place on the basis of character prefix.
-
If the value starts with 0X or 0x, a hexadecimal number is returned.
-
If the value starts with 0B or 0b, a binary number is returned
-
If the value starts with 0, the function returns an octal number.
intval() 函数对于 true,返回 1,对于 false 布尔值,返回 0。
The intval() function returns 1 for true, 0 for false Boolean values.
Example
下面的示例展示了 intval() 函数的工作方式−
The following example shows how the intval() function works −
<?php
echo intval(42). PHP_EOL;
echo intval(4.2). PHP_EOL;
echo intval('42') . PHP_EOL;
echo intval(042) . PHP_EOL; # 0ctal number
echo intval('042', 0) . PHP_EOL; # 0ctal number
echo intval('42', 8) . PHP_EOL; # octal
echo intval(0x1A) . PHP_EOL; # Hexadecimal
echo intval('0x1A', 16) . PHP_EOL; # Hexadecimal
echo intval('0x1A', 0) . PHP_EOL; # Hexadecimal
echo intval(false) . PHP_EOL;
echo intval(true) . PHP_EOL;
?>
它将生成以下 output −
It will produce the following output −
42
4
42
34
34
34
26
26
26
0
1
The floatval() Function
floatval() 函数获取一个表达式的浮点值。
The floatval() function gets the float value of an expression.
floatval(mixed $value): float
该值可能是一个任意的标量变量。带有非数字字符的字符串返回“0”。带有数字表示或带有一个数字表示起始子串的字符串返回对应的数字。下面的 example 展示了 floatval() 函数的工作方式 −
The value may be any scalar variable. String with non-numeric characters returns "0". A string with a numeric representation or with the starting substring with a numeric representation returns the corresponding number. The following example shows how the floatval() function works −
<?php
echo floatval(42). PHP_EOL;
echo floatval(4.2). PHP_EOL;
echo floatval('42') . PHP_EOL;
echo floatval('99.90 Rs') . PHP_EOL;
echo floatval('$100.50') . PHP_EOL;
echo floatval('ABC123!@#') . PHP_EOL;
echo (true) . PHP_EOL; ;
echo (false) . PHP_EOL;
?>
它将生成以下 output −
It will produce the following output −
42
4.2
42
99.9
0
0
1
doubleval() 函数是 floatval() 函数的一个别名,因此返回类似的结果。
The doubleval() function is an alias of floatval() function, and hence returns similar results.
The strval() Function
strval() 函数获取变量的字符串值。此函数不对返回的值执行任何格式化。
The strval() function gets the string value of a variable. This function performs no formatting on the returned value.
strval(mixed $value): string
正在转换为字符串的值可以是任何标量类型、null 值或实现了 __toString() 方法的对象。查看以下内容:
The value that is being converted to a string may be any scalar type, null, or an object that implements the __toString() method. Take a look at the following example −
<?php
echo strval(42). PHP_EOL;
echo strval(4.2). PHP_EOL;
echo strval(4.2E5) . PHP_EOL;
echo strval(NULL) . PHP_EOL;
echo (true) . PHP_EOL;
echo (false) . PHP_EOL;
?>
它将生成以下 output −
It will produce the following output −
42
4.2
420000
1
以下内容定义了一个实现了 __toString() 方法的类:
The following example defines a class that implements the _toString() method.
<?php
class myclass {
public function __toString() {
return __CLASS__;
}
}
echo strval(new myclass);
?>
在这里,你会得到以下 output −
Here, you will get the following output −
myclass
PHP - Type Juggling
PHP 被称为动态类型语言。PHP 中变量的类型会动态更改。此特性在 PHP 中称为“类型转换”。
PHP is known as a dynamically typed language. The type of a variable in PHP changes dynamically. This feature is called "type juggling" in PHP.
在 C、C++ 和 Java 中,您需要在随后代码中使用变量之前声明该变量及其类型。该变量只能取与声明的类型相匹配的值。
In C, C++ and Java, you need to declare the variable and its type before using it in the subsequent code. The variable can take a value that matches with the declared type only.
在 PHP 中既不需要显式类型声明变量,也不支持显式类型声明。因此,PHP 变量的类型由分配给它的值决定,反之亦然。此外,当变量被分配了不同类型的值时,其类型也会更改。
Explicit type declaration of a variable is neither needed nor supported in PHP. Hence the type of PHP variable is decided by the value assigned to it, and not the other way around. Further, when a variable is assigned a value of different type, its type too changes.
Example 1
了解 PHP 中的以下变量赋值。
Look at the following variable assignment in PHP.
<?php
$var = "Hello";
echo "The variable \$var is of " . gettype($var) . " type" .PHP_EOL;
$var = 10;
echo "The variable \$var is of " . gettype($var) . " type" .PHP_EOL;
$var = true;
echo "The variable \$var is of " . gettype($var) . " type" .PHP_EOL;
$var = [1,2,3,4];
echo "The variable \$var is of " . gettype($var) . " type" .PHP_EOL;
?>
它将生成以下 output −
It will produce the following output −
The variable $var is of string type
The variable $var is of integer type
The variable $var is of boolean type
The variable $var is of array type
你会看到,“$var” 的类型会根据分配给它的值动态改变。PHP 的这一特性被称为“类型混用”。
You can see the type of "$var" changes dynamically as per the value assigned to it. This feature of PHP is called "type juggling".
Example 2
在计算表达式时也会进行类型混用。在这个示例中,包含数字的字符串变量会自动转换为整数以评估加法表达式。
Type juggling also takes place during calculation of expression. In this example, a string variable containing digits is automatically converted to integer for evaluation of addition expression.
<?php
$var1=100;
$var2="100";
$var3=$var1+$var2;
var_dump($var3);
?>
以下是它的 @{s0}
−
Here is its output −
int(200)
Example 3
如果一个字符串以数字开头,在执行计算时会忽略后面的非数字字符(如果有)。然而,PHP 解析器会发出如下所示的通知 −
If a string starts with digits, trailing non-numeric characters if any, are ignored while performing the calculation. However, PHP parser issues a notice as shown below −
<?php
$var1=100;
$var2="100 days";
$var3=$var1+$var2;
var_dump($var3);
?>
您将获得以下内容 output −
You will get the following output −
int(200)
PHP Warning: A non-numeric value encountered in /home/cg/root/53040/main.php on line 4
Type Casting vs Type Juggling
注意,PHP 中的“类型转换”与“类型混用”略有不同。
Note that "type casting" in PHP is a little different from "type juggling".
-
In type juggling, PHP automatically converts types from one to another when necessary. For example, if an integer value is assigned to a variable, it becomes an integer.
-
On the other hand, type casting takes place when the user explicitly defines the data type in which they want to cast.
Example
类型转换强制将变量用作某种类型。以下脚本展示了不同的类型转换运算符示例 −
Type casting forces a variable to be used as a certain type. The following script shows an example of different type cast operators −
<?php
$var1=100;
$var2=(boolean)$var1;
$var3=(string)$var1;
$var4=(array)$var1;
$var5=(object)$var1;
var_dump($var2, $var3, $var4, $var5);
?>
它将生成以下 output −
It will produce the following output −
bool(true)
string(3) "100"
array(1) {
[0]=>
int(100)
}
object(stdClass)#1 (1) {
["scalar"]=>
int(100)
}
Example
还可以通过用双引号字符串括起来的方式将变量转换为字符串 −
Casting a variable to a string can also be done by enclosing in double quoted string −
<?php
$var1=100.50;
$var2=(string)$var1;
$var3="$var1";
var_dump($var2, $var3);
?>
在这里,你会得到以下 output −
Here, you will get the following output −
string(5) "100.5"
string(5) "100.5"
PHP - Strings
字符串是一系列字符,如“PHP 支持字符串操作”。PHP 中的字符串是一个字节数组和一个指示缓冲区长度的整数。在 PHP 中,字符与字节相同。这意味着 PHP 仅支持 256 个字符集,因此不提供本机的 Unicode 支持。
A string is a sequence of characters, like 'PHP supports string operations.' A string in PHP as an array of bytes and an integer indicating the length of the buffer. In PHP, a character is the same as a byte. This means that PHP only supports a 256-character set, and hence does not offer native Unicode support.
PHP 支持单引号和双引号的字符串格式。两种格式“this is a simple string”和“this is a simple string”均有效。PHP 还有 Heredoc 和 Nowdoc 形式的字符串数据类型。
PHP supports single quoted as well as double quoted string formation. Both the representations 'this is a simple string' as well as "this is a simple string" are valid. PHP also has Heredoc and Newdoc representations of string data type.
Single-Quoted String
用单引号(字符“'”)引起来的一系列字符是一个字符串。
A sequence of characters enclosed in single quotes (the character ') is a string.
$str = 'this is a simple string';
Example
如果要包含一个文字单引号,请用反斜杠(\)对其进行转义。
If you want to include a literal single quote, escape it with a backslash (\).
<?php
$str = 'This is a \'simple\' string';
echo $str;
?>
它将给出以下内容:
It will give you the following output −
This is a 'simple' string
Example
要指定一个文字反斜杠,请将其加倍(\\)。
To specify a literal backslash, double it (\\).
<?php
$str = 'The command C:\\*.* will delete all files.';
echo $str;
?>
以下是它的 @{s0}
−
Here is its output −
The command C:\*.* will delete all files.
Example
诸如“\r”或“\n”之类的转义序列将被视为文本,并且不会解释它们的特殊含义。如果变量出现在单引号字符串中,它们也不会扩展。
The escape sequences such as "\r" or "\n" will be treated literally and their special meaning will not be interpreted. The variables too will not be expanded if they appear in a single quoted string.
<?php
$str = 'This will not expand: \n a newline';
echo $str . PHP_EOL;
$x=100;
$str = 'Value of x = $x';
echo $str;
?>
它将生成以下 output −
It will produce the following output −
This will not expand: \n a newline
Value of x = $x
Double-Quoted String
用双引号(“"”)引起来的一系列字符是另一种字符串表示形式。
A sequence of characters enclosed in double-quotes (" ") is another string representation.
$str = "this is a simple string";
单引号和双引号字符串是等效的,除了它们对转义序列的处理不同。PHP 会解释特殊字符的某些转义序列。例如,“\r”和“\n”。
Single-quoted and double-quoted strings are equivalent except for their treatment of escape sequences. PHP will interpret certain escape sequences for special characters. For example, "\r" and "\n".
Sequence |
Meaning |
\n |
linefeed (LF or 0x0A (10) in ASCII) |
\r |
carriage return (CR or 0x0D (13) in ASCII) |
\t |
horizontal tab (HT or 0x09 (9) in ASCII) |
\v |
vertical tab (VT or 0x0B (11) in ASCII) |
\e |
escape (ESC or 0x1B (27) in ASCII) |
\f |
form feed (FF or 0x0C (12) in ASCII) |
|backslash |
\$ |
dollar sign |
\" |
How to Escape Octal and Hexademical Characters in PHP?
PHP 支持将八进制和十六进制数字转义为其 ASCII 字符。例如,字符 P 的 ASCII 字符为十进制中的 80。十进制中的 80 对应八进制中的 120。同样,十进制中的 80 对应十六进制中的 50。
PHP supports escaping an Octal and a hexadecimal number to its ASCII character. For example, the ASCII character for P is 80 in decimal. 80 in decimal to Octal is 120. Similarly, 80 in decimal to hexadecimal is 50.
要转义八进制字符,使用“\”前缀;要转义十六进制字符,使用“\x”前缀。
To escape an octal character, prefix it with "\"; and to escape a hexadecimal character, prefix it with "\x".
<?php
$str = "\120\110\120";
echo "PHP with Octal: ". $str;
echo PHP_EOL;
$str = "\x50\x48\x50";
echo "PHP with Hexadecimal: ". $str;
?>
检查 output −
Check the output −
PHP with Octal: PHP
PHP with Hexadecimal: PHP
与单引号串一样,转义任何其他字符也将导致反斜杠被打印。双引号串最重要的特征是变量名将被展开。
As in single quoted strings, escaping any other character will result in the backslash being printed too. The most important feature of double-quoted strings is the fact that variable names will be expanded.
Example
PHP 中的双引号串展开变量名(PHP 变量用 $ 符号作为前缀)。如要在 PHP 串中实际表示 “$” 符号,用 “\” 字符进行转义。
A double-quoted string in PHP expands the variable names (PHP variables are prefixed with $ symbol). To actually represent a "$" symbol in a PHP string, escape it by prefixing with the "\" character.
<?php
$price = 200;
echo "Price = \$ $price";
?>
您将获得以下内容 output −
You will get the following output −
Price = $ 200
String Concatenation Operator
要将两个字符串变量连接在一起,PHP 使用点 (.) 运算符 −
To concatenate two string variables together, PHP uses the dot (.) operator −
<?php
$string1="Hello World";
$string2="1234";
echo $string1 . " " . $string2;
?>
在这里,你会得到以下 output −
Here, you will get the following output −
Hello World 1234
在上述示例中,我们使用了连接运算符两次。这是因为我们必须插入第三个串。在两个字符串变量之间,我们添加了一个包含单个字符(一个空格)的串,以分隔这两个变量。
In the above example, we used the concatenation operator twice. This is because we had to insert a third string. Between the two string variables, we added a string with a single character, an empty space, to separate the two variables.
PHP 的标准库包括很多用于字符串处理的函数。它们可以在 PHP 的官方文档中找到 ( https://www.php.net/manual/en/ref.strings.php )。
The standard library of PHP includes many functions for string processing. They can be found at PHP’s official documentation (https://www.php.net/manual/en/ref.strings.php).
The strlen() Function
strlen() 函数用于找到一个字符串的长度。
The strlen() function is used to find the length of a string.
Example
让我们找到字符串 “Hello world!” 的长度 −
Let’s find the length of our string "Hello world!" −
<?php
echo strlen("Hello world!");
?>
它将生成以下 output −
It will produce the following output −
12
字符串的长度通常用于循环或其他函数中,这些时候了解字符串何时结束非常重要(也就是说,在循环中,我们希望在字符串中的最后一个字符后面停止循环)。
The length of a string is often used in loops or other functions, when it is important to know when the string ends (that is, in a loop, we would want to stop the loop after the last character in the string).
The strpos() Function
strpos() 函数用于在字符串内搜索字符串或字符。
The strpos() function is used to search for a string or character within a string.
-
If a match is found in the string, this function will return the position of the first match.
-
If no match is found, it will return FALSE.
Example
让我们看看能否在我们的字符串中找到字符串 “world” −
Let’s see if we can find the string "world" in our string −
<?php
echo strpos("Hello world!","world");
?>
它将生成以下 output −
It will produce the following output −
6
如你所见,字符串 “world” 在我们字符串中的位置是 “6”。这个位置是 “6” 而不是 “7” 的原因是字符串中的第一个位置是 “0”,而不是 “1”。
As you can see, the position of the string "world" in our string is "6". The reason that it is "6", and not "7", is that the first position in the string is "0", and not "1".
PHP - Boolean
在 PHP 中,“bool” 是内置标量数据类型之一。它用于表示真值,可以是 True 或 False。布尔值常量使用 PHP 常量 True 或 False。这些常量不区分大小写,即 true、TRUE 或 True 是同义词。
In PHP, "bool" is one of the built-in scalar data types. It is used to express the truth value, and it can be either True or False. A Boolean literal uses the PHP constants True or False. These constants are case-insensitive, in the sense, true, TRUE or True are synonymous.
你可以如下声明 bool 类型的变量 −
You can declare a variable of bool type as follows −
$a = true;
Example
逻辑运算符 (<, >, ==, != 等)返回布尔值。
Logical operators (<, >, ==, !=, etc.) return Boolean values.
<?php
$gender="Male";
var_dump ($gender=="Male");
?>
它将生成以下 output −
It will produce the following output −
bool(true)
Boolean Values in Control Statements
布尔值用于构造控制语句,如 if, while, for 和 foreach 。这些语句的行为取决于布尔运算符返回的 true/false 值。
Boolean values are used in the construction of control statements such as if, while, for and foreach. The behaviour of these statements depends on the true/false value returned by the Boolean operators.
以下条件语句使用在 if 关键字之前的括号中表达式返回的布尔值 −
The following conditional statement uses the Bool value returned by the expression in the parenthesis in front of the if keyword −
$mark=60;
if ($mark>50)
echo "pass";
else
echo "fail";
Converting a Value to Boolean
使用 (bool) 转换运算符将一个值转换为 bool。当一个值在逻辑上下文中使用时,它将自动解释为类型为 bool 的值。
Use the (bool) casting operator to convert a value to bool. When a value is used in a logical context it will be automatically interpreted as a value of type bool.
非零数字被认为是真,只有 0(+0.0 或 -0.0)是假。非空字符串表示真,空字符串 “” 等同于假。同样,一个空数组返回假。
A non-zero number is considered as true, only 0 (+0.0 or -0.0) is false. Non-empty string represents true, empty string "" is equivalent to false. Similarly, an empty array returns false.
Example
请查看以下示例:
Take a look at this following example −
<?php
$a = 10;
echo "$a: ";
var_dump((bool)$a);
$a = 0;
echo "$a: ";
var_dump((bool)$a);
$a = "Hello";
echo "$a: ";
var_dump((bool)$a);
$a = "";
echo "$a: ";
var_dump((bool)$a);
$a = array();
echo "$a: ";
var_dump((bool)$a);
?>
它将生成以下 output −
It will produce the following output −
10: bool(true)
0: bool(false)
Hello: bool(true)
: bool(false)
Array: bool(false)
Integer 是 PHP 中内置标量类型之一。在字面量中没有小数点的整数在 PHP 中为“int”类型。整数可以用十进制(基数 10)、十六进制(基数 16)、八进制(基数 8)或二进制(基数 2)记法表示。
Integer is one of the built-in scalar types in PHP. A whole number, without a decimal point in the literal, is of the type "int" in PHP. An integer can be represented in decimal (base 10), hexadecimal (base 16), octal (base 8) or binary (base 2) notation.
要使用八进制记法,数字之前加上“0o”或“0O”(PHP 8.1.0 及更早版本)。从 PHP 8.1.0 开始,以“0”为前缀且没有小数点的数字为八进制数字。
To use octal notation, a number is preceded with "0o" or "0O" (PHP 8.1.0 and earlier). From PHP 8.1.0 onwards, a number prefixed with "0" and without a decimal point is an octal number.
要使用十六进制记法,数字之前加上“0x”。要使用二进制记法,数字之前加上“0b”。
To use hexadecimal notation, precede the number with "0x". To use binary notation, precede the number with "0b".
Example
请看以下示例:
Take a look at the following example −
<?php
$a = 1234;
echo "1234 is an Integer in decimal notation: $a\n";
$b = 0123;
echo "0o123 is an integer in Octal notation: $b\n";
$c = 0x1A;
echo "0xaA is an integer in Hexadecimal notation: $c\n";
$d = 0b1111;
echo "0b1111 is an integer in binary notation: $d\n";
?>
它将生成以下 output −
It will produce the following output −
1234 is an Integer in decimal notation: 1234
0o123 is an integer in Octal notation: 83
0xaA is an integer in Hexadecimal notation: 26
0b1111 is an integer in binary notation: 15
从 PHP 7.4.0 开始,整数字面量可能包含下划线 (_) 作为数字之间的分隔符,以提高字面量的可读性。这些下划线由 PHP 的扫描器移除。
PHP 7.4.0 onwards, integer literals may contain underscores (_) as separators between digits, for better readability of literals. These underscores are removed by PHP’s scanner.
Example
请看以下示例:
Take a look at the following example −
<?php
$a = 1_234_567;
echo "1_234_567 is an Integer with _ as separator: $a\n";
?>
它将生成以下 output −
It will produce the following output −
1_234_567 is an Integer with _ as separator: 1234567
PHP does not support unsigned ints 。 int 的大小依赖于平台。在 32 位系统上,最大值约为二十亿。64 位平台的最大值通常约为 9E18。
PHP does not support unsigned ints. The size of an int is platform dependent. On 32 bit systems, the maximum value is about two billion. 64-bit platforms usually have a maximum value of about 9E18.
int 大小可以用常量 PHP_INT_SIZE 确定,最大值可以用常量 PHP_INT_MAX 确定,最小值可以用常量 PHP_INT_MIN 确定。
int size can be determined using the constant PHP_INT_SIZE, maximum value using the constant PHP_INT_MAX, and minimum value using the constant PHP_INT_MIN.
如果整数恰好超出了 int 类型的边界,或任何操作导致数字超出了 int 类型的边界,它将被解释为浮点数。
If an integer number happens to be beyond the bounds of the int type, or any operation results in a number beyond the bounds of the int type, it will be interpreted as a float instead.
Example
请看以下示例:
Take a look at the following example −
<?php
$x = 1000000;
$y = 50000000000000 * $x;
var_dump($y);
?>
它将生成以下 output −
It will produce the following output −
float(5.0E+19)
PHP 没有用于整数除法的运算符。因此,整数和浮点数之间的除法运算总是导致浮点数。要获得整数除法,可以使用 intval() 内置函数。
PHP doesn’t have any operator for integer division. Hence, a division operation between an integer and a float always results in float. To obtain integral division, you may use the intval() built-in function.
PHP - Integers
Integer 是 PHP 中内置标量类型之一。在字面量中没有小数点的整数在 PHP 中为“int”类型。整数可以用十进制(基数 10)、十六进制(基数 16)、八进制(基数 8)或二进制(基数 2)记法表示。
Integer is one of the built-in scalar types in PHP. A whole number, without a decimal point in the literal, is of the type "int" in PHP. An integer can be represented in decimal (base 10), hexadecimal (base 16), octal (base 8) or binary (base 2) notation.
要使用八进制记法,数字之前加上“0o”或“0O”(PHP 8.1.0 及更早版本)。从 PHP 8.1.0 开始,以“0”为前缀且没有小数点的数字为八进制数字。
To use octal notation, a number is preceded with "0o" or "0O" (PHP 8.1.0 and earlier). From PHP 8.1.0 onwards, a number prefixed with "0" and without a decimal point is an octal number.
要使用十六进制记法,数字之前加上“0x”。要使用二进制记法,数字之前加上“0b”。
To use hexadecimal notation, precede the number with "0x". To use binary notation, precede the number with "0b".
Example
请查看以下示例:
Take a look at this following example −
<?php
$a = 1234;
echo "1234 is an Integer in decimal notation: $a\n";
$b = 0123;
echo "0o123 is an integer in Octal notation: $b\n";
$c = 0x1A;
echo "0xaA is an integer in Hexadecimal notation: $c\n";
$d = 0b1111;
echo "0b1111 is an integer in binary notation: $d";
?>
它将生成以下 output −
It will produce the following output −
1234 is an Integer in decimal notation: 1234
0o123 is an integer in Octal notation: 83
0xaA is an integer in Hexadecimal notation: 26
0b1111 is an integer in binary notation: 15
从 PHP 7.4.0 开始,整数字面量可能包含下划线 (_) 作为数字之间的分隔符,以提高字面量的可读性。这些下划线由 PHP 的扫描器移除。
PHP 7.4.0 onwards, integer literals may contain underscores (_) as separators between digits, for better readability of literals. These underscores are removed by PHP’s scanner.
Example
请查看以下示例:
Take a look at this following example −
<?php
$a = 1_234_567;
echo "1_234_567 is an Integer with _ as separator: $a";
?>
它将生成以下 output −
It will produce the following output −
1_234_567 is an Integer with _ as separator: 1234567
PHP does not support unsigned ints 。 int 的大小依赖于平台。在 32 位系统上,最大值约为二十亿。64 位平台的最大值通常约为 9E18。
PHP does not support unsigned ints. The size of an int is platform dependent. On 32 bit systems, the maximum value is about two billion. 64-bit platforms usually have a maximum value of about 9E18.
int 大小可以用常量 PHP_INT_SIZE 确定,最大值可以用常量 PHP_INT_MAX 确定,最小值可以用常量 PHP_INT_MIN 确定。
int size can be determined using the constant PHP_INT_SIZE, maximum value using the constant PHP_INT_MAX, and minimum value using the constant PHP_INT_MIN.
如果整数恰好超出了 int 类型的边界,或任何操作导致数字超出了 int 类型的边界,它将被解释为浮点数。
If an integer number happens to be beyond the bounds of the int type, or any operation results in a number beyond the bounds of the int type, it will be interpreted as a float instead.
Example
请查看以下示例:
Take a look at this following example −
<?php
$x = 1000000;
$y = 50000000000000 * $x;
var_dump($y);
?>
它将生成以下 output −
It will produce the following output −
float(5.0E+19)
PHP 没有用于整数除法的运算符。因此,整数和浮点数之间的除法运算总是导致浮点数。要获得整数除法,可以使用 intval() 内置函数。
PHP doesn’t have any operator for integer division. Hence, a division operation between an integer and a float always results in float. To obtain integral division, you may use the intval() built-in function.
PHP - Files & I/O
本章将说明与文件相关的以下函数 −
This chapter will explain following functions related to files −
-
Opening a File
-
Reading a File
-
Writing a File
-
Closing a File
Opening and Closing Files
PHP fopen() 函数用于打开一个文件。它需要两个参数,首先是文件名,然后是操作模式。
The PHP fopen() function is used to open a file. It requires two arguments stating first the file name and then mode in which to operate.
文件模式可以指定为此表中的六个选项之一。
Files modes can be specified as one of the six options in this table.
Sr.No |
Mode & Purpose |
1 |
r Opens the file for reading only. Places the file pointer at the beginning of the file. |
2 |
r+ Opens the file for reading and writing. Places the file pointer at the beginning of the file. |
3 |
w Opens the file for writing only. Places the file pointer at the beginning of the file. and truncates the file to zero length. If files does not exist then it attempts to create a file. |
4 |
w+ Opens the file for reading and writing only. Places the file pointer at the beginning of the file. and truncates the file to zero length. If files does not exist then it attempts to create a file. |
5 |
a Opens the file for writing only. Places the file pointer at the end of the file. If files does not exist then it attempts to create a file. |
6 |
a+ Opens the file for reading and writing only. Places the file pointer at the end of the file. If files does not exist then it attempts to create a file. |
如果打开文件失败,则 fopen 返回 false 的值,否则它返回一个 file pointer ,用于进一步读取或写入该文件。
If an attempt to open a file fails then fopen returns a value of false otherwise it returns a file pointer which is used for further reading or writing to that file.
在对打开的文件进行更改后,使用 fclose() 函数将其关闭非常重要。 fclose() 函数需要一个文件指针作为其参数,然后在关闭成功时返回 true ,或者在关闭失败时返回 false 。
After making a changes to the opened file it is important to close it with the fclose() function. The fclose() function requires a file pointer as its argument and then returns true when the closure succeeds or false if it fails.
Reading a File
一旦使用 fopen() 函数打开一个文件,就可以用称为 fread() 的函数读取它。此函数需要两个参数。它们必须是文件指针和文件长度(以字节表示)。
Once a file is opened using fopen() function it can be read with a function called fread(). This function requires two arguments. These must be the file pointer and the length of the file expressed in bytes.
可以使用 filesize() 函数找到文件长度,它以文件名作为其参数并返回文件的大小(以字节表示)。
The files length can be found using the filesize() function which takes the file name as its argument and returns the size of the file expressed in bytes.
所以,以下是使用 PHP 读取文件所需的步骤。
So here are the steps required to read a file with PHP.
-
Open a file using fopen() function.
-
Get the file’s length using filesize() function.
-
Read the file’s content using fread() function.
-
Close the file with fclose() function.
Example
以下示例将文本文件的内容分配给一个变量,然后在网页上显示该内容。
The following example assigns the content of a text file to a variable then displays those contents on the web page.
<html>
<head>
<title>Reading a file using PHP</title>
</head>
<body>
<?php
$filename = "tmp.txt";
$file = fopen( $filename, "r" );
if( $file == false ) {
echo ( "Error in opening file" );
exit();
}
$filesize = filesize( $filename );
$filetext = fread( $file, $filesize );
fclose( $file );
echo ( "File size : $filesize bytes" );
echo ( "<pre>$filetext</pre>" );
?>
</body>
</html>
它将产生以下结果 −
It will produce the following result −
Writing a File
可以使用 PHP fwrite() 函数编写新文件或在现有文件中附加文本。该函数需要两个指定 file pointer 和要写入的数据字符串的参数。还可以选择包含第三个整数参数以指定要写入的数据的长度。如果包含第三个参数,则在达到指定长度后写入将停止。
A new file can be written or text can be appended to an existing file using the PHP fwrite() function. This function requires two arguments specifying a file pointer and the string of data that is to be written. Optionally a third integer argument can be included to specify the length of the data to write. If the third argument is included, writing would will stop after the specified length has been reached.
Example
以下示例创建一个新文本文件,然后在其中写一个简短的文本标题。关闭此文件后,使用 file_exist() 函数确认其存在,该函数将文件名作为参数
The following example creates a new text file then writes a short text heading inside it. After closing this file its existence is confirmed using file_exist() function which takes file name as an argument
<?php
$filename = "/home/user/guest/newfile.txt";
$file = fopen( $filename, "w" );
if( $file == false ) {
echo ( "Error in opening new file" );
exit();
}
fwrite( $file, "This is a simple test\n" );
fclose( $file );
?>
<html>
<head>
<title>Writing a file using PHP</title>
</head>
<body>
<?php
$filename = "newfile.txt";
$file = fopen( $filename, "r" );
if( $file == false ) {
echo ( "Error in opening file" );
exit();
}
$filesize = filesize( $filename );
$filetext = fread( $file, $filesize );
fclose( $file );
echo ( "File size : $filesize bytes" );
echo ( "$filetext" );
echo("file name: $filename");
?>
</body>
</html>
它将产生以下结果 −
It will produce the following result −
我们已介绍了有关 PHP File System Function 章节中文件输入和输出的所有函数。
We have covered all the function related to file input and out in PHP File System Function chapter.
PHP - Maths Functions
为启用数学运算,PHP 具有数学(算术)运算符和许多数学函数。在本章中,将通过示例说明以下数学函数。
To enable mathematical operations, PHP has mathematical (arithmetic) operators and a number of mathematical functions. In this chapter, the following mathematical functions are explained with examples.
PHP abs() Function
abs() 函数是 PHP 解释器的内置函数。此函数接受任意数字作为参数并返回一个正值,而不考虑其符号。任何数字的绝对值始终为正。
The abs() function is an in-built function in PHP iterpreter. This function accepts any number as argument and returns a positive value, disregarding its sign. Absolute value of any number is always positive.
abs( mixed $num)
PHP abs() 函数返回 num 的绝对值。如果 num 的数据类型是浮点数,它的返回类型也将是浮点数。对于整数参数,返回类型为整数。
PHP abs() function returns the absolute value of num. If the data type of num is float, its return type will also be float. For integer parameter, the return type is integer.
Example
请查看以下示例:
Take a look at this following example −
<?php
$num=-9.99;
echo "negative float number: " . $num . "\n";
echo "absolute value : " . abs($num) . "\n";
$num=25.55;
echo "positive float number: " . $num . "\n";
echo "absolute value : " . abs($num). "\n";
$num=-45;
echo "negative integer number: " . $num . "\n";
echo "absolute value : " . abs($num) . "\n";
$num=25;
echo "positive integer number: " . $num . "\n";
echo "absolute value : " . abs($num);
?>
它将生成以下 output −
It will produce the following output −
negative float number: -9.99
absolute value : 9.99
positive float number: 25.55
absolute value : 25.55
negative integer number: -45
absolute value : 45
positive integer number: 25
absolute value : 25
PHP ceil() Function
ceil() 函数是 PHP 解释器的内置函数。此函数接受任意浮点数作为参数并将其四舍五入到下一个最高整数。此函数始终返回一个浮点数,因为浮点数的范围大于整数范围。
The ceil() function is an in-built function in PHP iterpreter. This function accepts any float number as argument and rounds it up to the next highest integer. This function always returns a float number as the range of float is bigger than that of integer.
ceil ( float $num ) : float
PHP ceil() 函数返回大于或等于给定参数的最小整数值。
PHP ceil() function returns the smallest integer value that is bigger than or equal to given parameter.
Example 1
以下代码将 5.78 四舍五入到其下一个最高整数 6
The following code rounds 5.78 to its next highest integer which is 6
<?php
$arg=5.78;
$val=ceil($arg);
echo "ceil(" . $arg . ") = " . $val;
?>
它将生成以下 output −
It will produce the following output −
ceil(5.78) = 6
PHP exp() Function
exp() 函数计算欧拉数 e 的指数。PHP 有一个预定义常量 M_E,表示欧拉数,等于 2.7182818284590452354。因此,exp(x) 返回 2.7182818284590452354x
The exp() function calculates exponent of e that is Euler Number. PHP has a predefined constant M_E that represents Euler Number and is equal to 2.7182818284590452354. Hence, exp(x) returns 2.7182818284590452354x
此函数始终返回一个浮点数。
This function always returns a float.
exp ( float $arg ) : float
PHP exp() 函数返回提升到给定 arg. 的欧拉数 e。请注意, e 是自然算法的底数。exp() 函数是自然对数的逆函数。
PHP exp() function returns the Euler Number e raised to given arg. Note that e is the base of natural algorithm. The exp() function is the inverse of natural logarithm.
Example 1
PHP 中的一个预定义常量是 M_LN2 ,它表示 loge2,等于 0.69314718055994530942。因此,此值的 exp() 将返回 2。
One of the predefined constants in PHP is M_LN2 which stands for loge2 and is equal to 0.69314718055994530942. So, the exp() of this value will return 2.
<?php
echo "exp(" . M_LN2 . ") = " . exp(M_LN2);
?>
它将生成以下 output −
It will produce the following output −
exp(0.69314718055995) = 2
Example 2
M_LN10 是表示 loge10 的另一个预定义常量。此程序计算 exp(M_LN10) 并返回 10。
M_LN10 is another predefined constant representing loge10. This program calculates exp(M_LN10) and returns 10.
<?php
echo "exp(" . M_LN10 . ") = " . exp(M_LN10);
?>
它将生成以下 output −
It will produce the following output −
exp(2.302585092994) = 10
PHP floor() Function
floor() 函数是 PHP 解释器中的另一个内置函数。此函数接受任意浮点数字作为参数,并根据其向下取整到最接近的整数。此函数始终返回浮点数,因为浮点值的范围大于整数值的范围。
The floor() function is another in-built function in PHP interpreter. This function accepts any float number as argument and rounds it down to the next lowest integer. This function always returns a float number as the range of float is bigger than that of integer.
floor ( float $num ) : float
PHP floor() 函数返回不小于给定参数的最大整数。
PHP floor() function returns the largest integer less than or equal to the given parameter.
Example 1
以下示例展示了如何将 15.05 舍入至下一个最大整数,即 15
The following example shows how to round 15.05 to its next highest integer which is 15
<?php
$arg=15.05;
$val=floor($arg);
echo "floor(" . $arg . ") = " . $val;
?>
它将生成以下 output −
It will produce the following output −
floor(15.05) = 15
PHP intdiv() Function
intdiv() 函数返回两个整型参数的整数商。如果 x/y 除法结果为“i”,余数为“r”,则为 −
The intdiv() function returns the integer quotient of two integer parameters. If x/y results in "i" as division and "r" as remainder, then −
x = y*i+r
在这种情况下,intdiv(x,y) 返回“i”
In this case, intdiv(x,y) returns "i"
intdiv ( int $x , int $y ) : int
“x”参数是除法表达式的分子部分,而“y”参数是除法表达式的分母部分。
The "x" parameter forms numerator part of the division expression, while the "y" parameter forms the denominator part of the division expression.
PHP intdiv() 函数返回“x”除以“y”的整数商。如果两个参数都是正数或都是负数,则返回值为正数。
PHP intdiv() function returns the integer quotient of division of "x" by "y". The return value is positive if both the parameters are positive or both the parameters are negative.
Example 1
以下示例表明,如果分子小于分母,则 intdiv() 函数返回 0。
The following example shows that if the numerator is less than the denominator, then intdiv() function returns 0.
<?php
$x=10;
$y=3;
$r=intdiv($x, $y);
echo "intdiv(" . $x . "," . $y . ") = " . $r . "\n";
$r=intdiv($y, $x);
echo "intdiv(" . $y . "," . $x . ") = " . $r;
?>
它将生成以下 output −
It will produce the following output −
intdiv(10,3) = 3
intdiv(3,10) = 0
Example 2
在以下示例中,intdiv() 函数返回负数,因为分子或分母是负数。
In the following example, intdiv() function returns a negative integer because either the numerator or the denominator is negative.
<?php
$x=10;
$y=3;
$r=intdiv($x, $y);
echo "intdiv(" . $x . "," . $y . ") = " . $r . "\n";
$x=10;
$y=-3;
$r=intdiv($x, $y);
echo "intdiv(" . $x . "," . $y . ") = " . $r . "\n";
$x=-10;
$y=3;
$r=intdiv($x, $y);
echo "intdiv(" . $x . "," . $y . ") = " . $r . "\n";
$x=-10;
$y=-3;
$r=intdiv($x, $y);
echo "intdiv(" . $x . "," . $y . ") = " . $r ;
?>
它将生成以下 output −
It will produce the following output −
intdiv(10,3) = 3
intdiv(10,-3) = -3
intdiv(-10,3) = -3
intdiv(-10,-3) = 3
Example 3
在以下示例中,分母为 0。它导致 DivisionByZeroError 异常。
Denominator is 0 in the following example. It results in DivisionByZeroError exception.
<?php
$x=10;
$y=0;
$r=intdiv($x, $y);
echo "intdiv(" . $x . "," . $y . ") = " . $r . "\n";
?>
它将生成以下 output −
It will produce the following output −
PHP Fatal error: Uncaught DivisionByZeroError: Division by zero
Example 4
两个参数中的小数部分都会被忽略。PHP intdiv() 函数仅适用于整数部分。
The fractional parts in both the parameters are ignored. PHP intdiv() function is applied only to the integer parts.
<?php
$x=2.90;
$y=1.90;
$r=intdiv($x, $y);
echo "intdiv(" . $x . "," . $y . ") = " . $r . "";
?>
它将生成以下 output −
It will produce the following output −
intdiv(2.9,1.9) = 2
PHP log10() Function
log10 () 函数计算某个数字的以 10 为底的对数。以 10 为底的对数也称为 common 或 standard algorithm 。log10(x) 函数计算 log10x。它与自然算法之间的关系由以下等式表示 −
The log10 () function calculates the base-10 logarithm of a number. Base-10 logarithm is also called common or standard algorithm. The log10(x) function calculates log10x. It is related to natural algorithm by the following equation −
log10x=logex/loge10 ; So that
log10100=loge100/loge10 = 2
在 PHP 中, log10 由 log10() 函数表示
In PHP, log10 is represented by log10() function
log10 ( float $arg ) : float
PHP log10() 函数返回 arg 的以 10 为底的对数。
PHP log10() function returns the base-10 logarithm of arg.
Example 1
以下代码计算 100 的以 10 为底的对数
The following code calculates the base-10 logarithm of 100
<?php
$arg=100;
echo "log10(" . $arg. ")=" . log10($arg) . "";
?>
它将生成以下 output −
It will produce the following output −
log10(100)=2
Example 2
以下代码计算欧拉数 M_E 的以 10 为底的对数。结果等于预定义的常数 M_LOG10E
The following code calculates the base-10 logarithm of Euler Number M_E. The result is equal to a predefined constant M_LOG10E
<?php
$arg=M_E;
echo "log10(" . $arg. ")=" . log10($arg) . "\n";
echo "predefined constant M_LOG10E=" . M_LOG10E;
?>
它将生成以下 output −
It will produce the following output −
log10(2.718281828459)=0.43429448190325
predefined constant M_LOG10E=0.43429448190325
PHP max() Function
max() 函数返回数组中最大的元素,或者返回两个或两个以上逗号分隔参数中最大的参数。
The max () function returns the highest element in an array, or the highest amongst two or more comma separated parameters.
max ( array $values ) : mixed
或者,
Or,
max ( mixed $value1 [, mixed $... ] ) : mixed
-
If only one parameter is given, it should be an array of values which may be of same or different types.
-
If two or more parameters are given, they should be any comparable values of same or different types.
PHP max() 函数返回数组参数或值序列中的最大值。标准比较运算符适用。如果不同类型的值有多个被评估为相等(例如 0 和’PHP'),则将返回函数的第一个参数。
PHP max() function returns the highest value from the array parameter or sequence of values. Standard comparison operators are applicable. If multiple values of different types evaluate as equal (e.g. 0 and 'PHP'), the first parameter to the function will be returned.
Example 1
下面的代码返回数字数组中的最大值。
The following code returns the highest value from a numeric array.
<?php
$arg=array(23, 5.55, 142, 56, 99);
echo "array=";
foreach ($arg as $i) echo $i . ",";
echo "\n";
echo "max = " . max($arg);
?>
它将生成以下 output −
It will produce the following output −
array=23,5.55,142,56,99,
max = 142
Example 2
下面的代码返回字符串数组中的 max()。
The following code returns max() from an array of strings.
<?php
$arg=array("Java", "Angular", "PHP", "C", "Kotlin");
echo "array=";
foreach ($arg as $i) echo $i . ",";
echo "\n";
echo "max = " . max($arg);
?>
它将生成以下 output −
It will produce the following output −
array=Java,Angular,PHP,C,Kotlin,
max = PHP
Example 3
在下面的示例中,向 max() 函数提供了一系列字符串值。让我们看看它如何表现 −
In the following example, a series of string values is provided to the max() function. Let’s see how it behaves −
<?php
$val1="Java";
$val2="Angular";
$val3="PHP";
$val4="C";
$val5="Kotlin";
echo "values=" . $val1 . "," . $val2 . "," . $val3 . "," . $val4 . "," . $val5 . "\n";
echo "max = " . max($val1, $val2, $val3,$val4,$val5);
?>
它将生成以下 output −
It will produce the following output −
values=Java,Angular,PHP,C,Kotlin
max = PHP
Example 4
在此示例中,给定的数组是混合数据类型的集合。
In this example, the given array is a collection of mixed data types.
<?php
$arg=array(23, "Java", 142, 1e2, 99);
echo "array=";
foreach ($arg as $i) echo $i . ",";
echo "\n";
echo "max = " . max($arg);
?>
它将生成以下 output −
It will produce the following output −
array=23,Java,142,100,99,
max = 142
PHP min() Function
min() 函数返回数组中最小的元素,或者返回两个或两个以上逗号分隔参数中最小的参数。
The min () function returns the lowest element in an array, or the lowest amongst two or more comma separated parameters.
min ( array $values ) : mixed
或者,
Or,
min ( mixed $value1 [, mixed $... ] ) : mixed
-
If only one parameter is given, it should be an array of values which may be of same or different types
-
If two or more parameters are given, they should be any comparable values of same or different types
PHP min() 函数返回数组参数或值序列中的最小值。标准比较运算符适用。如果不同类型的值有多个被评估为相等(例如 0 和’PHP'),则将返回函数的第一个参数
PHP min() function returns the lowest value from the array parameter or sequence of values. Standard comparison operators are applicable. If multiple values of different types evaluate as equal (e.g. 0 and 'PHP'), the first parameter to the function will be returned
Example 1
下面的代码返回数字数组中的最小值。
The following code returns the smallest value from numeric array.
<?php
$arg=array(23, 5.55, 142, 56, 99);
echo "array=";
foreach ($arg as $i) echo $i . ",";
echo "\n";
echo "min = " . min($arg);
?>
它将生成以下 output −
It will produce the following output −
array=23,5.55,142,56,99,
min = 5.55
Example 2
下面的代码返回字符串数组中的 min()。
The following code returns min() from an array of strings.
<?php
$arg=array("Java", "Angular", "PHP", "C", "Kotlin");
echo "array=";
foreach ($arg as $i) echo $i . ",";
echo "\n";
echo "min = " . min($arg);
?>
它将生成以下 output −
It will produce the following output −
array=Java,Angular,PHP,C,Kotlin,
min = Angular
Example 3
此示例中,向 min() 函数提供了一系列字符串值。
In this example, a series of string values is provided to the min() function.
<?php
$val1="Java";
$val2="Angular";
$val3="PHP";
$val4="C";
$val5="Kotlin";
echo "values=" . $val1 . "," . $val2 . "," . $val3 . "," . $val4 . "," . $val5 . "\n";
echo "min = " . min($val1, $val2, $val3,$val4,$val5);
?>
它将生成以下 output −
It will produce the following output −
values=Java,Angular,PHP,C,Kotlin
min = Angular
Example 4
在此示例中,给定的数组是混合数据类型的集合。
In this example, the given array is a collection of mixed data types.
<?php
$arg=array(23, "Java", 142, 1e2, 99);
echo "array=";
foreach ($arg as $i) echo $i . ",";
echo "\n";
echo "min = " . min($arg);
?>
它将生成以下 output −
It will produce the following output −
array=23,Java,142,100,99,
min = 23
PHP pow() Function
pow () 函数用于计算某个数的幂。它返回 xy 计算,也称为将 x 提升到 y。PHP 还提供了“**”作为指数运算符。
The pow () function is used to compute the power of a certain number. It returns xy calculation, also termed as x raised to y. PHP also provides "**" as exponentiation operator.
因此,pow(x,y) 返回 xy,与 x**y 相同。
So, pow(x,y) returns xy which is same as x**y.
pow ( number $base , number $exp ) : number
第一个参数是要提升的底数。第二个参数是需要将底数提升到的幂。
The first parameter is the base to be raised. The second parameter is the power to which base needs to be raised.
PHP pow() 函数返回以 exp 的幂次升高的基数。如果两个参数都是非负整数,则结果将以整数形式返回,否则以浮点数形式返回。
PHP pow() function returns the base raised to the power of exp. If both arguments are non-negative integers, the result is returned as integer, otherwise it is returned as a float.
Example 1
以下示例使用 pow() 函数计算 102:
The following example calculates 102 using pow() function −
<?php
echo "pow(10,2) = " . pow(10,2);
echo " using ** operator " . 10**2;
?>
它将生成以下 output −
It will produce the following output −
pow(10,2) = 100 using ** operator 100
Example 2
将任何数字升至 0 次方得到的结果都是 1。以下示例对此进行了验证:
Any number raised to 0 results in 1. This is verified in the following example −
<?php
$x=10;
$y=0;
echo "pow(" . $x, "," . $y . ")=". pow($x,$y);
?>
它将生成以下 output −
It will produce the following output −
pow(10,0)=1
Example 3
以下示例展示了如何使用 pow() 函数计算 100 的平方根:
The following example shows how you can compute the square root of 100 using the pow() function −
<?php
$x=100;
$y=0.5;
echo "pow(" . $x, "," . $y . ")=". pow($x,$y) . "\n";
echo "using sqrt() function : ". sqrt(100);
?>
它将生成以下 output −
It will produce the following output −
pow(100,0.5)=10
using sqrt() function : 10
PHP round() Function
round() 函数可用于将任意浮点数四舍五入到所需精度级别。正精度参数使数字在小数点后四舍五入;而负精度导致数字在小数点前四舍五入。精度默认为“0”。
The round() function proves useful in rounding any floating point number upto a desired precision level. Positive precision parameter causes the number to be rounded after the decimal point; whereas with negative precision, rounding occurs before the decimal point. Precision is "0" by default.
例如,round(10.6) 返回 11,round(10.2) 返回 10。此函数始终返回浮点数。
For example, round(10.6) returns 11, round(10.2) returns 10. The function always returns a floating point number.
该函数还有另一个可选参数,称为 mode ,它采用稍后描述的重新定义的常量之一。
This function also has another optional parameter called mode that takes one of the redefined constants described later.
round ( float $value , int $precision , int $mode ) : float
Parameters
-
Value − A float number to be rounded.
-
Precision − Number of decimal digits to round to. Default is 0. Positive precision rounds given number after decimal point. Negative precision rounds the given number before decimal point.
-
Mode − One of the following predefined constants.
Sr.No |
Constant & Description |
1 |
PHP_ROUND_HALF_UP Rounds number away from 0 when it is half way there. Hence, 1.5 becomes 2 and -1.5 to -2 |
2 |
PHP_ROUND_HALF_DOWN Rounds number towards 0 when it is half way there. Hence 1.5 becomes 1 and -1.5 to -1 |
3 |
PHP_ROUND_HALF_EVEN Rounds the number to nearest even value |
4 |
PHP_ROUND_HALF_ODD Rounds the number to nearest odd value |
PHP round() 函数返回一个浮点数,方法是将该值四舍五入到所需的精度。
PHP round() function returns a float number that by rounding the value to a desired precision.
Example 1
以下代码将给定数字舍入到正精度值:
The following code rounds the given number to positive precision values −
<?php
$arg=1234.567;
echo "round(" . $arg . ") = " . round($arg) . "\n";
echo "round(" . $arg . ",1) = " . round($arg,1) . "\n";
echo "round(" . $arg . ",2) = " . round($arg,2) . "";
?>
它将生成以下 output −
It will produce the following output −
round(1234.567) = 1235
round(1234.567,1) = 1234.6
round(1234.567,2) = 1234.57
Example 2
以下代码将数字舍入到负精度值:
The following code rounds the number to negative precision values −
<?php
$arg=1234.567;
echo "round(" . $arg . ") = " . round($arg) . "\n";
echo "round(" . $arg . ",-1) = " . round($arg,-1) . "\n";
echo "round(" . $arg . ",-2) = " . round($arg,-2) . "";
?>
它将生成以下 output −
It will produce the following output −
round(1234.567) = 1235
round(1234.567,-1) = 1230
round(1234.567,-2) = 1200
Example 3
以下代码针对舍入使用 UP 和 DOWN 模式常量:
The following code uses UP and DOWN mode constants for rounding −
<?php
echo "round(3.45,HALF_UP) = " . round(3.45,0, PHP_ROUND_HALF_UP) . "\n";
echo "round(3.75 HALF_UP) = " . round(3.75, 1, PHP_ROUND_HALF_DOWN) . "";
?>
它将生成以下 output −
It will produce the following output −
round(3.45,HALF_UP) = 3
round(3.75 HALF_UP) = 3.7
Example 4
以下代码使用 ODD 和 EVEN 模式进行舍入 −
The following code uses ODD and EVEN modes for rounding −
<?php
echo "round( 3.45,HALF_ODD) = " . round(3.45,0, PHP_ROUND_HALF_ODD) . "\n";
echo "round(3.78 HALF_EVEN) = " . round(3.78, 0, PHP_ROUND_HALF_EVEN) . "";
?>
它将生成以下 output −
It will produce the following output −
round(3.45,HALF_ODD) = 3
round(3.78, HALF_EVEN) = 4
PHP sqrt() Function
sqrt() 函数返回正浮点数的平方根。由于负数的平方根未定义,因此它返回 NAN。这是最常用的函数之一。此函数始终返回浮点数。
The sqrt() function returns the square root of a positive float number. Since square root for a negative number is not defined, it returns NAN. This is one of the most commonly used functions. This function always returns a floating point number.
sqrt (float $arg) : float
PHP sqrt() 函数返回给定 arg 数字的平方根。对于负数,该函数返回 NAN。
PHP sqrt() function returns the square root of the given arg number. For negative numbers, the function returns NAN.
Example 1
以下代码计算 100 的平方根 −
The following code calculates the square root of 100 −
<?php
$arg = 100;
echo "Square root of " . $arg . "=" . sqrt($arg) . "";
?>
它将生成以下 output −
It will produce the following output −
Square root of 100=10
Example 2
对于 sqrt(2)、1/sqrt(2) 和 sqrt(3),PHP 分别有特殊预定义常量 M_SQRT2、M_SQRT1_2 和 M_SQRT3。
For sqrt(2), 1/sqrt(2) and sqrt(3), PHP has special predefined constants M_SQRT2, M_SQRT1_2 and M_SQRT3, respectively.
<?php
echo "sqrt(2) = " . sqrt(2) . "\n";
echo "M_SQRT2 = " . M_SQRT2. "\n";
echo "sqrt(3) = " . sqrt(3) . "\n";
echo "M_SQRT3 = " . M_SQRT3 . "\n";
echo "1/sqrt(2)) = " . 1/sqrt(2) . "\n";
echo "M_SQRT1_2 = " . M_SQRT1_2 . "";
?>
它将生成以下 output −
It will produce the following output −
sqrt(2) = 1.4142135623731
M_SQRT2 = 1.4142135623731
sqrt(3) = 1.7320508075689
M_SQRT3 = 1.7320508075689
1/sqrt(2)) = 0.70710678118655
M_SQRT1_2 = 0.70710678118655
Example 3
数学常量 M_SQRTPI 和 M_2_SQRTPI 分别表示 sqrt(Π) 和 2/sqrt(Π) 的值。
The mathematical constants M_SQRTPI and M_2_SQRTPI represent values of sqrt(Π) and 2/sqrt(Π).
<?php
echo "sqrt(pi) = " . sqrt(M_PI) . "\n";
echo "M_SQRTPI = " . M_SQRTPI. "\n";
echo "2/sqrt(pi) = " . 2/sqrt(M_PI) . "\n";
echo "M_2_SQRTPI = " . M_2_SQRTPI . "";
?>
它将生成以下 output −
It will produce the following output −
sqrt(pi) = 1.7724538509055
M_SQRTPI = 1.7724538509055
2/sqrt(pi) = 1.1283791670955
M_2_SQRTPI = 1.1283791670955
Predefined Mathematical Constants
除了上述数学函数外,PHP 还有以下预定义数学常量 −
In addition to the above mathematical functions, PHP also has the following list of predefined mathematical constants −
Constant |
Value |
Description |
M_PI |
3.14159265358979323846 |
Pi |
M_E |
2.7182818284590452354 |
Euler Number e |
M_LOG2E |
1.4426950408889634074 |
log2 e |
M_LOG10E |
0.43429448190325182765 |
log10 e |
M_LN2 |
0.69314718055994530942 |
loge 2 |
M_LN10 |
M_LN10 2.30258509299404568402 loge 10 |
loge 10 |
M_PI_2 |
1.57079632679489661923 |
pi/2 |
M_PI_4 |
0.78539816339744830962 |
pi/4 |
M_1_PI |
0.31830988618379067154 |
1/pi |
M_2_PI |
0.63661977236758134308 |
2/pi |
M_SQRTPI |
1.77245385090551602729 |
sqrt(pi) |
M_2_SQRTPI |
1.12837916709551257390 |
2/sqrt(pi) |
M_SQRT2 |
1.41421356237309504880 |
sqrt(2) |
M_SQRT3 |
1.73205080756887729352 |
sqrt(3) |
M_SQRT1_2 |
0.70710678118654752440 |
1/sqrt(2) |
M_LNPI |
1.14472988584940017414 |
loge(pi) |
M_EULER |
0.57721566490153286061 |
Euler constant |
PHP_ROUND_HALF_UP |
1 |
Round halves up |
PHP_ROUND_HALF_DOWN |
2 |
Round halves down |
PHP_ROUND_HALF_EVEN |
3 |
Round halves to even numbers |
PHP_ROUND_HALF_ODD |
4 |
Round halves to odd numbers |
NAN |
NAN |
Not A Number |
INF |
INF |
Infinity |
PHP - Heredoc & Nowdoc
PHP 提供了两种替代方法,可以使用 heredoc 和 newdoc 语法来声明单引号或双引号字符串。
PHP provides two alternatives for declaring single or double quoted strings in the form of heredoc and newdoc syntax.
-
The single quoted string doesn’t interpret the escape characters and doesn’t expand the variables.
-
On the other hand, if you declare a double quoted string that contains a double quote character itself, you need to escape it by the "\" symbol. The heredoc syntax provides a convenient method.
Heredoc Strings in PHP
PHP 中的 heredoc 字符串非常像双引号字符串,但没有双引号。这意味着它们不需要转义引号和展开变量。
The heredoc strings in PHP are much like double-quoted strings, without the double-quotes. It means that they don’t need to escape quotes and expand variables.
Heredoc Syntax
$str = <<<IDENTIFIER
place a string here
it can span multiple lines
and include single quote ' and double quotes "
IDENTIFIER;
首先,从 "<<<" 运算符开始。在此运算符之后,提供一个标识符,然后换行。字符串本身紧随其后,然后再次使用相同的标识符来结束引号。该字符串可以跨越多行,并包括单引号 (') 或双引号 (")。
First, start with the "<<<" operator. After this operator, an identifier is provided, then a newline. The string itself follows, and then the same identifier again to close the quotation. The string can span multiple lines and includes single quotes (‘) or double quotes (").
结尾标识符可以通过空格或制表符缩进,在这种情况下,缩进将从文档字符串中的所有行中删除。
The closing identifier may be indented by space or tab, in which case the indentation will be stripped from all lines in the doc string.
Example
该标识符只能包含字母数字字符和下划线,并且以下划线或非数字字符开头。结尾标识符不应包含分号 (;) 以外的任何其他字符。此外,结尾标识符前后必须只能是换行符。
The identifier must contain only alphanumeric characters and underscores and start with an underscore or a non-digit character. The closing identifier should not contain any other characters except a semicolon (;). Furthermore, the character before and after the closing identifier must be a newline character only.
请看以下示例:
Take a look at the following example −
<?php
$str1 = <<<STRING
Hello World
PHP Tutorial
by TutorialsPoint
STRING;
echo $str1;
?>
它将生成以下 output −
It will produce the following output −
Hello World
PHP Tutorial
by TutorialsPoint
Example
结尾标识符在编辑器的第一列之后可能包含缩进,也可能不包含缩进。如果有缩进,它将被删除。但是,结尾标识符的缩进不能比正文中的任何行更深。否则,将引发 ParseError。请看以下示例及其输出 −
The closing identifier may or may not contain indentation after the first column in the editor. Indentation, if any, will be stripped off. However, the closing identifier must not be indented further than any lines of the body. Otherwise, a ParseError will be raised. Take a look at the following example and its output −
<?php
$str1 = <<<STRING
Hello World
PHP Tutorial
by TutorialsPoint
STRING;
echo $str1;
?>
它将生成以下 output −
It will produce the following output −
PHP Parse error: Invalid body indentation level
(expecting an indentation level of at least 16) in hello.php on line 3
Example
heredoc 中的引号不需要转义,但仍可以使用 PHP 转义序列。Heredoc 语法还会展开变量。
The quotes in a heredoc do not need to be escaped, but the PHP escape sequences can still be used. Heredoc syntax also expands the variables.
<?php
$lang="PHP";
echo <<<EOS
Heredoc strings in $lang expand vriables.
The escape sequences are also interpreted.
Here, the hexdecimal ASCII characters produce \x50\x48\x50
EOS;
?>
它将生成以下 output −
It will produce the following output −
Heredoc strings in PHP expand vriables.
The escape sequences are also interpreted.
Here, the hexdecimal ASCII characters produce PHP
Nowdoc Strings in PHP
PHP 中的 Nowdoc 字符串与 Hereodoc 字符串类似,不同之处在于它不扩展变量,也不解释转义序列。
A nowdoc string in PHP is similar to a heredoc string except that it doesn’t expand the variables, neither does it interpret the escape sequences.
<?php
$lang="PHP";
$str = <<<'IDENTIFIER'
This is an example of Nowdoc string.
it can span multiple lines
and include single quote ' and double quotes "
IT doesn't expand the value of $lang variable
IDENTIFIER;
echo $str;
?>
它将生成以下 output −
It will produce the following output −
This is an example of Nowdoc string.
it can span multiple lines
and include single quote ' and double quotes "
IT doesn't expand the value of $lang variable
Nowdoc 的语法类似于 Hereodoc 的语法,不同之处在于,遵循 "<<<" 运算符的标识符需要用单引号引起来。Nowdoc 的标识符也遵循 Hereodoc 标识符的规则。
The nowdoc’s syntax is similar to the heredoc’s syntax except that the identifier which follows the "<<<" operator needs to be enclosed in single quotes. The nowdoc’s identifier also follows the rules for the heredoc identifier.
Heredoc 字符串类似于不带转义的双引号字符串。Nowdoc 字符串类似于不带转义的单引号字符串。
Heredoc strings are like double-quoted strings without escaping. Nowdoc strings are like single-quoted strings without escaping.
PHP - Compound Types
PHP 中的数据类型可以是“标量类型”或“复合类型”。整数、浮点数、布尔值和字符串类型为标量类型,而数组和对象类型归类为复合类型。复合类型中的单个变量可以存储多种类型的值。
Data types in PHP can be of "scalar type" or "compound type". Integer, float, Boolean and string types are scalar types, whereas array and object types are classified as compound types. Values of more than one types can be stored together in a single variable of a compound type.
在 PHP 中,对象和数组是两种复合数据类型。
In PHP, objects and arrays are the two compound data types.
-
An array is an ordered collection of elements of other data types, not necessarily of the same type.
-
An object is an instance of either a built-in or a user defined class, consisting of properties and methods.
Arrays in PHP
数组是一种数据结构,其中一个变量存储了一个或多个数据值。PHP 中的数组是有序映射,它将值与键关联。
An array is a data structure that stores one or more data values in a single variable. An array in PHP is an ordered map that associates the values to their keys.
-
There are two ways to declare an array in PHP. One is to use the built-in array() function, and the other is to put the array elements inside square brackets.
-
An array which is a collection of only values is called an indexed array. Each value is identified by a positional index staring from 0.
-
If the array is a collection of key-value pairs, it is called as an associative array. The key component of the pair can be a number or a string, whereas the value part can be of any type.
The array() Function in PHP
内置 array() 函数使用提供给它的参数并返回数组类型对象。一个或多个以逗号分隔的参数是数组中的元素。
The built-in array() function uses the parameters given to it and returns an object of array type. One or more comma-separated parameters are the elements in the array.
array(mixed ...$values): array
括号中的每个值可以是单个值(可能是数字、字符串、任何对象,甚至另一个数组),或键值对。键与其值之间的关联由“⇒”符号表示。
Each value in the parenthesis may be either a singular value (it may be a number, string, any object or even another array), or a key-value pair. The association between the key and its value is denoted by the "⇒" symbol.
Using Square Brackets [ ]
除了 array() 函数之外,还可以将以逗号分隔的数组元素置于方括号内以声明数组对象。在这种情况下,元素也可以是单个值、字符串或另一个数组。
Instead of the array() function, the comma-separated array elements may also be put inside the square brackets to declare an array object. In this case too, the elements may be singular values or a string or another array.
$arr1 = [10, "asd", 1.55, true];
$arr2 = ["one"=>1, "two"=>2, "three"=>3];
$arr3 = [
[10, 20, 30],
["Ten", "Twenty", "Thirty"],
["physics"=>70, "chemistry"=>80, "maths"=>90]
];
Accessing Array Elements
要从给定数组访问任何元素,可以使用 array[key] 语法。对于索引数组,将索引置于方括号内,因为索引本身就是键。
To access any element from a given array, you can use the array[key] syntax. For an indexed array, put the index inside the square bracket, as the index itself is anyway the key.
<?php
$arr1 = [10, 20, 30];
$arr2 = array("one"=>1, "two"=>2, "three"=>3);
var_dump($arr1[1]);
var_dump($arr2["two"]);
?>
它将生成以下 output −
It will produce the following output −
int(20)
int(2)
Array Traversal in PHP
还可以使用 foreach 循环来迭代索引数组。
You can also use the foreach loop to iterate through an indexed array.
<?php
$arr1 = [10, 20, 30, 40, 50];
foreach ($arr1 as $val){
echo "$val\n";
}
?>
它将生成以下 output −
It will produce the following output −
10
20
30
40
50
请注意,PHP 在内部将索引数组视为关联数组,其中索引被视为键。此事实可以通过数组的 var_dump() 输出得到验证。
Note that PHP internally treats the indexed array as an associative array, with the index being treated as the key. This fact can be verified by the var_dump() output of the array.
我们可以使用 foreach 语法将索引数组的每个元素解包为键和值变量 −
We can unpack each element of the indexed array in the key and value variables with the foreach syntax −
<?php
$arr1 = [10, 20, 30, 40, 50];
foreach ($arr1 as $key => $val){
echo "arr1[$key] = $val" . "\n";
}
?>
它将生成以下 output −
It will produce the following output −
arr1[0] = 10
arr1[1] = 20
arr1[2] = 30
arr1[3] = 40
arr1[4] = 50
foreach 循环还用于遍历关联数组,尽管任何其他类型的循环也可以通过某种操作使用。
The foreach loop is also used for iterating through an associative array, although any other type of loop can also be used with some maneuver.
让我们看一下 foreach 循环实现,每个 k-v 对在两个变量中解包。
Let us look at the foreach loop implementation, with each k-v pair unpacked in two variables.
<?php
$capitals = array(
"Maharashtra"=>"Mumbai",
"Telangana"=>"Hyderabad",
"UP"=>"Lucknow",
"Tamilnadu"=>"Chennai"
);
foreach ($capitals as $k=>$v) {
echo "Capital of $k is $v" . "\n";
}
?>
它将生成以下 output −
It will produce the following output −
Capital of Maharashtra is Mumbai
Capital of Telangana is Hyderabad
Capital of UP is Lucknow
Capital of Tamilnadu is Chennai
Objects in PHP
在 PHP 中,对象是一种复合数据类型。它是内置或用户定义类的实例。下面是一个简单的 PHP 类 −
In PHP, an object is a compound data type. It is an instance of either a built in or user defined class. Given below is a simple PHP class −
class SayHello {
function hello() {
echo "Hello World";
}
}
要声明类的对象,我们需要使用 new 操作符。
To declare an object of a class, we need to use the new operator.
$obj=new SayHello;
我们现在可以调用它的方法 −
We can now call its method −
<?php
class SayHello {
function hello() {
echo "Hello World". PHP_EOL;
}
}
$obj=new SayHello;
var_dump(gettype($obj));
$obj->hello();
?>
它将生成以下 output −
It will produce the following output −
string(6) "object"
Hello World
stdClass
PHP 提供 stdClass 作为一个通用的空类,它可用于动态添加属性和转换。stdClass 的对象开始时为 null。我们可以动态地向其中添加属性。
PHP provides stdClass as a generic empty class which is useful for adding properties dynamically and casting. An object of stdClass is null to begin with. We can add properties to it dynamically.
<?php
$obj=new stdClass;
$obj->name="Deepak";
$obj->age=21;
$obj->marks=75;
print_r($obj);
?>
它将生成以下 output −
It will produce the following output −
stdClass Object (
[name] => Deepak
[age] => 21
[marks] => 75
)
Array to Object Conversion in PHP
PHP 中的数组可以按如下方式强制转换成对象 −
An array in PHP can be typecast to an object as follows −
<?php
$arr=array("name"=>"Deepak", "age"=>21, "marks"=>75);
$obj=(object)$arr;
print_r($obj);
?>
它将生成以下 output −
It will produce the following output −
stdClass Object (
[name] => Deepak
[age] => 21
[marks] => 75
)
Object to Array Conversion in PHP
相反,对象可以强制转换成数组。请看以下示例 −
Conversely, an object can be cast to an array. Take a look at the following example −
<?php
$obj=new stdClass;
$obj->name="Deepak";
$obj->age=21;
$obj->marks=75;
$arr=(array)$obj;
print_r($arr);
?>
它将生成以下 output −
It will produce the following output −
Array
(
[name] => Deepak
[age] => 21
[marks] => 75
)
Scalar Type to Object Type Conversion in PHP
任何标量类型的变量也可以通过类型转换转换成对象。标量变量的值将成为对象标量属性的值。
A variable of any scalar type can also be converted to an object by type casting. The value of the scalar variable becomes the value of the object’s scalar property.
<?php
$name="Deepak";
$age=21;
$percent=75.50;
$obj1=(object)$name;
print_r($obj1);
$obj2=(object)$age;
print_r($obj2);
$obj3=(object)$percent;
print_r($obj3);
?>
它将生成以下 output −
It will produce the following output −
stdClass Object
(
[scalar] => Deepak
)
stdClass Object
(
[scalar] => 21
)
stdClass Object
(
[scalar] => 75.5
)
PHP - File Include
PHP 中的 include statement 类似于 Java 或 Python 中的 import statement 、C/C++ 中的 #include directive 。但是,在 PHP 中包含语句工作方式上略有不同。
The include statement in PHP is similar to the import statement in Java or Python, and #include directive in C/C++. However, there is a slight difference in the way the include statement works in PHP.
Java/Python 中的导入或 C/C++ 中的 #include 只将一个或多个语言结构(例如函数或类)从一个文件中加载到当前文件中。相比之下,PHP 中的 include 语句将另一个文件中的一切引入现有 PHP 脚本。它可能是 PHP 代码、文本文件、HTML 标记等。
The Java/Python import or #include in C/C++ only loads one or more language constructs such as the functions or classes defined in one file into the current file. In contrast, the include statement in PHP brings in everything in another file into the existing PHP script. It may be a PHP code, a text file, HTML markup, etc.
The "include" Statement in PHP
以下是 PHP 中 include 语句如何工作的一个典型示例:
Here is a typical example of how the include statement works in PHP −
test.php
<?php
include 'myfile.php';
# PHP script in test.php
?>
PHP 中的 include 关键字非常方便,尤其当你需要在项目中跨多个 PHP 脚本使用相同的 PHP 代码(函数或类)或 HTML 标记时。一个典型的例子是创建应出现在 Web 应用程序所有页面中的菜单。
The include keyword in PHP is very handy, especially when you need to use the same PHP code (function or class) or HTML markup across multiple PHP scripts in a project. A case in point is the creation of a menu that should appear across all pages of a web application.
假设你想为网站创建一个通用菜单。然后,创建一个名为“menu.php”的文件,其内容如下。
Suppose you want to create a common menu for your website. Then, create a file "menu.php" with the following content.
<a href="http://www.tutorialspoint.com/index.htm">Home</a> -
<a href="http://www.tutorialspoint.com/ebxml">ebXML</a> -
<a href="http://www.tutorialspoint.com/ajax">AJAX</a> -
<a href="http://www.tutorialspoint.com/perl">PERL</a> <br />
现在创建任意多页面并包含此文件以创建头部。例如,现在你的“test.php”文件可以具有以下内容:
Now create as many pages as you like and include this file to create the header. For example, now your "test.php" file can have the following content −
<html>
<body>
<?php include("menu.php"); ?>
<p>This is an example to show how to include PHP file!</p>
</body>
</html>
这两个文件假定存在于 XAMPP 服务器的文档根目录中。访问 http://localhost/test.php URL。它将生成以下内容 output :
Both the files are assumed to be present in the document root folder of the XAMPP server. Visit http://localhost/test.php URL. It will produce the following output −
当 PHP 解析器遇到 include 关键字时,它会尝试在当前脚本正在执行的同一目录中找到指定文件。如果未找到,则搜索“php.ini”的“include_path”设置中的目录。
When PHP parser encounters the include keyword, it tries to find the specified file in the same directory from which the current script is being executed. If not found, the directories in the "include_path" setting of "php.ini" are searched.
当包含一个文件时,它包含的代码会继承包含所在行可变的范围。呼叫文件中该行可用的任何变量都将在从该点开始的呼叫文件中可用。但是,在包含文件中定义的所有函数和类都具有全局范围。
When a file is included, the code it contains inherits the variable scope of the line on which the include occurs. Any variables available at that line in the calling file will be available within the called file, from that point forward. However, all functions and classes defined in the included file have the global scope.
Example
在以下示例中,我们有一个“myname.php”脚本,其中声明了两个变量。它包含在另一个脚本 test.php 中。变量以全局范围加载。
In the following example, we have a "myname.php" script with two variables declared in it. It is included in another script test.php. The variables are loaded in the global scope.
myname.php
myname.php
<?php
$color = 'green';
$fruit = 'apple';
?>
test.php
test.php
<?php
include "myname.php";
echo "<h2>$fname $lname</h2>";
?>
当浏览器访问 http://localhost/test.php 时,它显示:
When the browser visits http://localhost/test.php, it shows −
Ravi Teja
然而,如果文件包含在函数内部,则变量仅成为函数局部范围的一部分。
However, if the file is included inside a function, the variables are a part of the local scope of the function only.
myname.php
myname.php
<?php
$color = 'green';
$fruit = 'apple';
?>
test.php
test.php
<?php
function showname() {
include "myname.php";
}
echo "<h2>$fname $lname</h2>";
?>
现在当浏览器访问 http://localhost/test.php 时,它显示未定义的变量警告:
Now when the browser visits http://localhost/test.php, it shows undefined variable warnings −
Warning: Undefined variable $fname in C:\xampp\htdocs\test.php on line 7
Warning: Undefined variable $lname in C:\xampp\htdocs\test.php on line 7
include_once statement
与 include 一样,PHP 也有“ include_once ”关键字。唯一的区别在于,如果已包含来自文件中的代码,则不会再次包含,且 “include_once” 返回 true。正如名称所建议的,文件将只包含一次。
Just like include, PHP also has the "include_once" keyword. The only difference is that if the code from a file has already been included, it will not be included again, and "include_once" returns true. As the name suggests, the file will be included just once.
“include_once” 可能在以下情况下使用:同一文件可能在脚本的特定执行过程中包含并评估多次,因此它可以帮助避免重新定义函数、重新赋值变量值等问题。
"include_once" may be used in cases where the same file might be included and evaluated more than once during a particular execution of a script, so it can help avoid problems such as function redefinitions, variable value reassignments, etc.
PHP – Include vs Require
PHP 中的 require 关键字与 include 关键字非常相似。两者的区别在于,失败后 require 会产生致命的 E_COMPILE_ERROR 级别的错误。
The require keyword in PHP is quite similar to the include keyword. The difference between the two is that, upon failure require will produce a fatal E_COMPILE_ERROR level error.
换句话说, require 会停止脚本,而 include 只会发出警告(E_WARNING),允许脚本继续运行。
In other words, require will halt the script, whereas include only emits a warning (E_WARNING) which allows the script to continue.
require_once keyword
“require_once”关键字与 require 类似,但也有细微差别。如果您使用“require_once”,那么 PHP 将检查文件是否已包含,如果是,则不会再包含相同的文件。
The "require_once" keyword is similar to require with a subtle difference. If you are using "require_once", then PHP will check if the file has already been included, and if so, then the same file it will not be included again.
PHP - Date & Time
PHP 的内置函数库具有广泛的函数,可帮助以编程方式处理和操作日期和时间信息。可以使用日期/时间信息的字符串形式或者从当前系统的时间创建 PHP 中的 Date 和 Time 对象。
The built-in library of PHP has a wide range of functions that helps in programmatically handling and manipulating date and time information. Date and Time objects in PHP can be created by passing in a string presentation of date/time information, or from the current system’s time.
PHP 提供定义了许多方法的 DateTime 类。在本章中,我们将会详细了解 PHP 中提供的各种与日期和时间相关的。
PHP provides the DateTime class that defines a number of methods. In this chapter, we will have a detailed view of the various Date and Time related methods available in PHP.
PHP 中的日期/时间功能实现了 ISO 8601 日历,它实现了在格里高利历之前通用的现行的闰年规则。日期和时间信息内部存储为 64 位数字。
The date/time features in PHP implements the ISO 8601 calendar, which implements the current leap-day rules from before the Gregorian calendar was in place. The date and time information is internally stored as a 64-bit number.
Getting the Time Stamp with time()
PHP 的 time() 函数提供了有关当前日期和时间的所需的所有信息。它不需要参数,但返回一个整数。
PHP’s time() function gives you all the information that you need about the current date and time. It requires no arguments but returns an integer.
time(): int
time() 返回的整数表示自 1970 年 1 月 1 日格林尼治标准时间午夜以来的经过的秒数。这个时刻称为 UNIX 紀元,并且从那时起经过的秒数称为时间戳。
The integer returned by time() represents the number of seconds elapsed since midnight GMT on January 1, 1970. This moment is known as the UNIX epoch, and the number of seconds that have elapsed since then is referred to as a time stamp.
<?php
print time();
?>
它将生成以下 output −
It will produce the following output −
1699421347
我们可以将时间戳转换为人类容易理解的形式。
We can convert a time stamp into a form that humans are comfortable with.
Converting a Time Stamp with getdate()
该函数 getdate() 可以选择接受一个时间戳,并返回一个包含有关日期信息的关联数组。如果您省略时间戳,它会使用 time() 返回的当前时间戳。
The function getdate() optionally accepts a time stamp and returns an associative array containing information about the date. If you omit the time stamp, it works with the current time stamp as returned by time().
下表列出了 getdate() 返回的数组中包含的元素。
The following table lists the elements contained in the array returned by getdate().
Sr.No |
Key & Description |
Example |
1 |
seconds Seconds past the minutes (0-59) |
20 |
2 |
minutes Minutes past the hour (0 - 59) |
29 |
3 |
hours Hours of the day (0 - 23) |
22 |
4 |
mday Day of the month (1 - 31) |
11 |
5 |
wday Day of the week (0 - 6) |
4 |
6 |
mon Month of the year (1 - 12) |
7 |
7 |
year Year (4 digits) |
1997 |
8 |
yday Day of year ( 0 - 365 ) |
19 |
9 |
weekday Day of the week |
Thursday |
10 |
month Month of the year |
January |
11 |
0 Timestamp |
948370048 |
现在您可以完全控制日期和时间。您可以按照想要的任何格式格式化此日期和时间。
Now you have complete control over date and time. You can format this date and time in whatever format you want.
Example
看看下面的 example −
Take a look at this following example −
<?php
$date_array = getdate();
foreach ( $date_array as $key => $val ){
print "$key = $val\n";
}
$formated_date = "Today's date: ";
$formated_date .= $date_array['mday'] . "-";
$formated_date .= $date_array['mon'] . "-";
$formated_date .= $date_array['year'];
print $formated_date;
?>
它将生成以下 output −
It will produce the following output −
seconds = 0
minutes = 38
hours = 6
mday = 8
wday = 3
mon = 11
year = 2023
yday = 311
weekday = Wednesday
month = November
0 = 1699421880
Today's date: 8-11-2023
Converting a Time Stamp with date()
date() 函数返回一个表示日期的格式化字符串。通过要传递给它的字符串参数,可以对 date() 返回的格式进行大量的控制。
The date() function returns a formatted string representing a date. You can exercise an enormous amount of control over the format that date() returns with a string argument that you must pass to it.
date(string $format, ?int $timestamp = null): string
date() 可选择接受时间戳,如果省略则使用当前日期和时间。包含在传递给 date() 的格式化字符串中的任何其他数据都将包含在返回值中。
The date() optionally accepts a time stamp if omitted then current date and time will be used. Any other data you include in the format string passed to date() will be included in the return value.
下表列出了格式化字符串可以包含的代码:
The following table lists the codes that a format string can contain −
Sr.No |
Format & Description |
Example |
1 |
a 'am' or 'pm' lowercase |
pm |
2 |
A 'AM' or 'PM' uppercase |
PM |
3 |
d Day of month, a number with leading zeroes |
20 |
4 |
D Day of week (three letters) |
Thu |
5 |
F Month name |
January |
6 |
h Hour (12-hour format - leading zeroes) |
12 |
7 |
H Hour (24-hour format - leading zeroes) |
22 |
8 |
g Hour (12-hour format - no leading zeroes) |
12 |
9 |
G Hour (24-hour format - no leading zeroes) |
22 |
10 |
i Minutes ( 0 - 59 ) |
23 |
11 |
j Day of the month (no leading zeroes |
20 |
12 |
l (Lower 'L') Day of the week |
Thursday |
13 |
L Leap year ('1' for yes, '0' for no) |
1 |
14 |
m Month of year (number - leading zeroes) |
1 |
15 |
M Month of year (three letters) |
Jan |
16 |
r The RFC 2822 formatted date |
Thu, 21 Dec 2000 16:01:07 +0200 |
17 |
n Month of year (number - no leading zeroes) |
2 |
18 |
s Seconds of hour |
20 |
19 |
U Time stamp |
948372444 |
20 |
y Year (two digits) |
06 |
21 |
Y Year (four digits) |
2006 |
22 |
z Day of year (0 - 365) |
206 |
23 |
Z Offset in seconds from GMT |
+5 |
Example
看看下面的 example −
Take a look at this following example −
<?php
print date("m/d/y G.i:s \n", time()) . PHP_EOL;
print "Today is ";
print date("j of F Y, \a\\t g.i a", time());
?>
它将生成以下 output −
It will produce the following output −
11/08/23 11.23:08
Today is 8 2023f November 2023, at 11.23 am
希望您对如何根据您的要求格式化日期和时间的了解很好。供您参考,所有日期和时间函数的完整列表中给出了 PHP Date & Time Functions 。
Hope you have good understanding on how to format date and time according to your requirement. For your reference a complete list of all the date and time functions is given in PHP Date & Time Functions.
PHP - Scalar Type Declarations
提供类型提示的功能自 5.0 版本起已内置于 PHP 中。 Type hinting 指在函数定义中提供参数数据类型的方法。在 PHP 7 之前,在函数中只能对数组、可调用和类使用类型提示。从 PHP 7 起,您还可以对标量数据类型(如 int、string、bool 等)的参数插入类型提示。
The feature of providing type hints has been in PHP since version 5. Type hinting refers to the practice of providing the data type of a parameter in the function definition. Before PHP 7, it was possible to use only the array, callable, and class for type hints in a function. PHP 7 onwards, you can also insert type hints for parameters of scalar data type such as int, string, bool, etc.
PHP 是一种动态(且弱)类型语言。因此,在定义函数时不需要声明参数类型,这是静态类型语言(如 C 或 Java)所必需的。
PHP is a dynamically (and weakly) typed language. Hence, you don’t need to declare the type of the parameter when a function is defined, something which is necessary in a statically type language like C or Java.
PHP 中函数的典型定义如下 −
A typical definition of function in PHP is as follows −
function addition($x, $y) {
echo "First number: $x Second number: $y Addition: " . $x+$y;
}
在此,我们假设参数 $x 和 $y 是数字的。但是,即使传递给函数的值不是数字,PHP 解析器仍会尽最大程度地尝试将变量强制转换为兼容的类型。
Here, we assume that the parameters $x and $y are numeric. However, even if the values passed to the function aren’t numeric, the PHP parser tries to cast the variables into compatible type as far as possible.
如果传递的值之一是数字的字符串表示,而第二个是数字变量,PHP 会将字符串变量强制转换为数字以执行加法操作。
If one of the values passed is a string representation of a number, and the second is a numeric variable, PHP casts the string variable to numeric in order to perform the addition operation.
Example
请查看以下示例:
Take a look at this following example −
<?php
function addition($x, $y) {
echo "First number: " . $x;
echo "\nSecond number: " . $y;
echo "\nAddition: " . $x+$y;
}
$x="10";
$y=20;
addition($x, $y);
?>
它将生成以下 output −
It will produce the following output −
First number: 10
Second number: 20
Addition: 30
但是,如果上述示例中的 $x 是不包含有效数字表示的字符串,则会遇到错误。
However, if $x in the above example is a string that doesn’t hold a valid numeric representation, an error is encountered.
<?php
function addition($x, $y) {
echo "First number: " . $x;
echo "\nSecond number: " . $y;
echo "\nAddition: " . $x+$y;
}
$x="Hello";
$y=20;
addition($x, $y);
?>
运行此代码并查看它如何显示 error 。
Run this code and see how it shows an error.
Scalar Type Declarations in PHP 7
PHP 7 版本引入的一项新功能允许定义一个函数,其参数的数据类型可以在括号中指定。
A new feature introduced with PHP version 7 allows defining a function with parameters whose data type can be specified within the parenthesis.
PHP 7 引入了以下标量类型声明 −
PHP 7 has introduced the following Scalar type declarations −
-
Int
-
Float
-
Bool
-
String
-
Interfaces
-
Array
-
Callable
旧版本的 PHP 只允许使用数组、可调用和类类型作为类型提示。此外,在旧版本的 PHP(PHP 5)中,致命错误为可恢复错误,而新版本(PHP 7)返回可抛出错误。
Older versions of PHP allowed only the array, callable and class types to be used as type hints. Furthermore, in the older versions of PHP (PHP 5), the fatal error used to be a recoverable error while the new release (PHP 7) returns a throwable error.
标量类型声明以两种模式实现 −
Scalar type declaration is implemented in two modes −
-
Coercive Mode − Coercive is the default mode and need not to be specified.
-
Strict Mode − Strict mode has to be explicitly hinted.
Coercive Mode
前面示例中定义的 addition() 函数现在可以重新编写,方法是合并类型声明,如下所示:
The addition() function defined in the earlier example can now be re-written by incorporating the type declarations as follows −
function addition(int $x, int $y) {
echo "First number: $x Second number: $y Addition: " . $x+$y;
}
注意,如果字符串包含一个整数,解析器仍会将不兼容的类型(即字符串)强制转换为 int,就像以前一样。
Note that the parser still casts the incompatible types i.e., string to an int if the string contains an integer as earlier.
Example
请查看以下示例:
Take a look at this following example −
<?php
function addition(int $x, int $y) {
echo "First number: " . $x;
echo "\nSecond number: " . $y;
echo "\nAddition: " . $x+$y;
}
$x="10";
$y=20;
echo addition($x, $y);
?>
它将生成以下 output −
It will produce the following output −
First number: 10
Second number: 20
Addition: 30
很明显,这是因为 PHP 是一种弱类型语言,因为 PHP 尝试将字符串类型的变量强制转换为整数。PHP 7 引入了解决此问题的严格模式功能。
Obviously, this is because PHP is a weakly typed language, as PHP tries to coerce a variable of string type to an integer. PHP 7 has introduced a strict mode feature that addresses this issue.
Strict Mode
为了应对 PHP 的弱类型检查,已引入严格模式。此模式启用一个 declare statement −
To counter the weak type checking of PHP, a strict mode has been introduced. This mode is enabled with a declare statement −
declare (strict_types=1);
您应将此声明放在 PHP 脚本的开头(通常就在 PHP 标签的下方)。这意味着标量的类型严格程度是按文件配置的。
You should put this statement at the top of the PHP script (usually just below the PHP tag). This means that the strictness of typing for scalars is configured on a per-file basis.
在弱模式中,strict_types 标记为 0。将其设为 1 会强制 PHP 解析器检查传递的参数和值的兼容性。在以上代码中添加此声明并检查结果。它将显示以下错误信息:
In the weak mode, the strict_types flag is 0. Setting it to 1 forces the PHP parser to check the compatibility of the parameters and values passed. Add this statement in the above code and check the result. It will show the following error message −
Fatal error: Uncaught TypeError: addition():
Argument #1 ($x) must be of type int, string given,
called in add.php on line 12 and defined in add.php:4
Stack trace:
#0 add.php(12): addition('10', 20)
#1 {main}
thrown in add.php on line 4
Example
以下是函数定义中标量类型声明的另一个示例。启用严格模式后,如果将不兼容的类型作为参数传递,则会引发致命错误。
Here is another example of scalar type declaration in the function definition. The strict mode when enabled raises fatal error if the incompatible types are passed as parameters.
<?php
// Strict mode
// declare(strict_types = 1);
function sum(int ...$ints) {
return array_sum($ints);
}
print(sum(2, '3', 4.1));
?>
取消对该代码开头的 declare 声明的注释并运行它。现在,它将生成一个 error :
Uncomment the declare statement at the top of this code and run it. Now it will produce an error −
Fatal error: Uncaught TypeError:
sum(): Argument #2 must be of type int, string given,
called in add.php on line 9 and defined in add.php:4
Stack trace:
#0 add.php(9): sum(2, '3', 4.1)
#1 {main}
thrown in add.php on line 4
类型提示功能主要由 IDE 用于提示用户有关函数声明中使用的参数的预期类型的信息。以下屏幕截图显示了键入时 VS Code 编辑器弹出函数原型。
The type-hinting feature is mostly used by IDEs to prompt the user about the expected types of the parameters used in the function declaration. The following screenshot shows the VS Code editor popping up the function prototype as you type.
PHP - Return Type Declarations
PHP 版本 7 将标量类型声明功能扩展到了函数的返回值。根据此新规定,返回类型声明指定函数应返回的值的类型。我们可以为返回类型声明以下类型:
PHP version 7 extends the scalar type declaration feature to the return value of a function also. As per this new provision, the return type declaration specifies the type of value that a function should return. We can declare the following types for return types −
-
int
-
float
-
bool
-
string
-
interfaces
-
array
-
callable
为了实现返回类型声明,函数被定义为:
To implement the return type declaration, a function is defined as −
function myfunction(type $par1, type $param2): type {
# function body
return $val;
}
PHP 解析器默认是强制类型。您需要声明 “strict_types=1” 来强制更严格地验证要返回的变量的类型与定义中使用的类型。
PHP parser is coercive typing by default. You need to declare "strict_types=1" to enforce stricter verification of the type of variable to be returned with the type used in the definition.
Example
在以下示例中,division() 函数被定义为返回类型为 int。
In the following example, the division() function is defined with a return type as int.
<?php
function division(int $x, int $y): int {
$z = $x/$y;
return $z;
}
$x=20.5;
$y=10;
echo "First number: " . $x;
echo "\nSecond number: " . $y;
echo "\nDivision: " . division($x, $y);
?>
因为类型检查没有被设为 strict_types=1,所以即使其中一个参数是非整数,也会进行除法。
Since the type checking has not been set to strict_types=1, the division take place even if one of the parameters is a non-integer.
First number: 20.5
Second number: 10
Division: 2
然而,一旦您在脚本开头添加了 strict_types 声明,程序就会引发致命错误消息。
However, as soon as you add the declaration of strict_types at the top of the script, the program raises a fatal error message.
Fatal error: Uncaught TypeError: division(): Argument #1 ($x) must be of type int, float given, called in div.php on line 12 and defined in div.php:3
Stack trace:
#0 div.php(12): division(20.5, 10)
#1 {main}
thrown in div.php on line 3
VS Code 会在运行代码之前警告错误,方法是在错误位置显示错误行:
VS Code warns about the error even before running the code by displaying error lines at the position of error −
Example
要使 division() 函数返回 float 而不是 int,将分子转换为 float,看看 PHP 如何引发致命错误:
To make the division() function return a float instead of int, cast the numerator to float, and see how PHP raises the fatal error −
<?php
// declare(strict_types=1);
function division(int $x, int $y): int {
$z = (float)$x/$y;
return $z;
}
$x=20;
$y=10;
echo "First number: " . $x;
echo "\nSecond number: " . $y;
echo "\nDivision: " . division($x, $y);
?>
取消对开头 declare 声明的注释,然后在此运行此代码来检查它的输出。它将显示一条错误:
Uncomment the declare statement at the top and run this code here to check its output. It will show an error −
First number: 20
Second number: 10PHP Fatal error: Uncaught TypeError: division(): Return value must be of type int, float returned in /home/cg/root/14246/main.php:5
Stack trace:
#0 /home/cg/root/14246/main.php(13): division()
#1 {main}
thrown in /home/cg/root/14246/main.php on line 5
PHP - Operators Types
What are Operators in PHP?
与任何编程语言一样,PHP 也有运算符,它们是符号(有时 keywords ),被预先定义来对一个或多个操作数执行某些通常需要的操作。
As in any programming language, PHP also has operators which are symbols (sometimes keywords) that are predefined to perform certain commonly required operations on one or more operands.
例如,使用表达式“4 + 5”等于 9。此处“4”和“5”称为 operands ,而“+”称为 operator 。
For example, using the expression "4 + 5" is equal to 9. Here "4" and "5" are called operands and "+" is called an operator.
我们在 PHP 中具有以下类型的运算符 −
We have the following types of operators in PHP −
-
Arithmetic Operators
-
Comparison Operators
-
Logical Operators
-
Assignment Operators
-
String Operators
-
Array Operators
-
Conditional (or Ternary Operators)
本章将概述如何在 PHP 中使用这些运算符。在后续章节中,我们将仔细了解每个运算符及其工作原理。
This chapter will provide an overview of how you can use these operators in PHP. In the subsequent chapters, we will take a closer look at each of the operators and how they work.
Arithmetic Operators in PHP
我们使用算术运算符对给定的操作数执行加法、减法、乘法、除法等数学运算。算术运算符(不包括增量和减量运算符)始终对两个操作数起作用,但是这些操作数的类型应该相同。
We use Arithmetic operators to perform mathematical operations like addition, subtraction, multiplication, division, etc. on the given operands. Arithmetic operators (excluding the increment and decrement operators) always work on two operands, however the type of these operands should be the same.
下表高亮显示了 PHP 支持的算术运算符。假设变量“$a”保存了 42,而变量“$b”保存了 20 −
The following table highligts the arithmetic operators that are supported by PHP. Assume variable "$a" holds 42 and variable "$b" holds 20 −
Operator |
Description |
Example |
+ |
Adds two operands |
$a + $b = 62 |
- |
Subtracts second operand from the first |
$a - $b = 22 |
* |
Multiply both operands |
$a * $b = 840 |
/ |
Divide numerator by de-numerator |
$a / $b = 2.1 |
% |
Modulus Operator and remainder of after an integer division |
$a % $b = 2 |
++ |
Increment operator, increases integer value by one |
$a ++ = 43 |
— |
Decrement operator, decreases integer value by one |
$a — = 42 |
Comparison Operators in PHP
您将使用比较运算符来比较两个操作数并找出它们之间的关系。根据比较结果,它们返回一个布尔值(真或假)。
You would use Comparison operators to compare two operands and find the relationship between them. They return a Boolean value (either true or false) based on the outcome of the comparison.
下表突出显示了 PHP 支持的比较运算符。假定变量 $a 存储 10,变量 $b 存储 20,则−
The following table highligts the comparison operators that are supported by PHP. Assume variable $a holds 10 and variable $b holds 20, then −
Operator |
Description |
Example |
== |
Checks if the value of two operands are equal or not, if yes then condition becomes true. |
($a == $b) is not true |
!= |
Checks if the value of two operands are equal or not, if values are not equal then condition becomes true. |
($a != $b) is true |
> |
Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true. |
($a > $b) is false |
< |
Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true. |
($a < $b) is true |
>= |
Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true. |
($a >= $b) is false |
⇐ |
Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true. |
($a ⇐ $b) is true |
Logical Operators in PHP
您可以在 PHP 中使用逻辑运算符对多个表达式一起执行逻辑运算。逻辑运算符始终返回布尔值,无论是真还是假。
You can use Logical operators in PHP to perform logical operations on multiple expressions together. Logical operators always return Boolean values, either true or false.
逻辑运算符通常与条件语句和循环一起使用,以根据布尔条件返回决策。在处理复杂表达式时,您还可以将它们组合起来,以操作布尔值。
Logical operators are commonly used with conditional statements and loops to return decisions according to the Boolean conditions. You can also combine them to manipulate Boolean values while dealing with complex expressions.
下表重点介绍了 PHP 支持的逻辑运算符。
The following table highligts the logical operators that are supported by PHP.
假设变量 $a 保存 10,变量 $b 保存 20,则 -
Assume variable $a holds 10 and variable $b holds 20, then −
Operator |
Description |
Example |
and |
Called Logical AND operator. If both the operands are true then condition becomes true. |
(A and B) is true |
or |
Called Logical OR Operator. If any of the two operands are non zero then condition becomes true. |
(A or B) is true |
&& |
Called Logical AND operator. If both the operands are non zero then condition becomes true. |
(A && B) is true |
Called Logical OR Operator. If any of the two operands are non zero then condition becomes true. |
(A |
|
B) is true |
! |
Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false. |
Assignment Operators in PHP
您可以在 PHP 中使用赋值运算符为给定变量分配或更新新值。赋值运算符的右侧保存值,而赋值运算符的左侧是将值分配到的变量。
You can use Assignment operators in PHP to assign or update the values of a given variable with a new value. The right-hand side of the assignment operator holds the value and the left-hand side of the assignment operator is the variable to which the value will be assigned.
双方的数据类型应该相同,否则您将收到一个错误。赋值运算符的结合性是从右向左。PHP 支持两种类型的赋值运算符 −
The data type of both the sides should be the same, else you will get an error. The associativity of assignment operators is from right to left. PHP supports two types of assignment operators −
-
Simple Assignment Operator − It is the most commonly used operator. It is used to assign value to a variable or constant.
-
Compound Assignment Operators − A combination of the assignment operator (=) with other operators like +, *, /, etc.
下表高亮显示了 PHP 支持的赋值运算符 −
The following table highligts the assignment operators that are supported by PHP −
Operator |
Description |
Example |
= |
Simple assignment operator, Assigns values from right side operands to left side operand |
C = A + B will assign value of A + B into C |
+= |
Add AND assignment operator, It adds right operand to the left operand and assign the result to left operand |
C += A is equivalent to C = C + A |
-= |
Subtract AND assignment operator, It subtracts right operand from the left operand and assign the result to left operand |
C -= A is equivalent to C = C - A |
*= |
Multiply AND assignment operator, It multiplies right operand with the left operand and assign the result to left operand |
C *= A is equivalent to C = C * A |
/= |
Divide AND assignment operator, It divides left operand with the right operand and assign the result to left operand |
C /= A is equivalent to C = C / A |
%= |
Modulus AND assignment operator, It takes modulus using two operands and assign the result to left operand |
C %= A is equivalent to C = C % A |
String Operators in PHP
PHP 中有两个运算符用于处理字符串数据类型 −
There are two operators in PHP for working with string data types −
-
The "." (dot) operator is PHP’s concatenation operator. It joins two string operands (characters of right hand string appended to left hand string) and returns a new string.
-
PHP also has the ".=" operator which can be termed as the concatenation assignment operator. It updates the string on its left by appending the characters of right hand operand.
$third = $first . $second;
$leftstring .= $rightstring;
Array Operators in PHP
PHP 定义了以下一组符号,用作数组数据类型的运算符−
PHP defines the following set of symbols to be used as operators on array data types −
Symbol |
Example |
Name |
Result |
+ |
$a + $b |
Union |
Union of $a and $b. |
== |
$a == $b |
Equality |
TRUE if $a and $b have the same key/value pairs. |
=== |
$a === $b |
Identity |
TRUE if $a and $b have the same key/value pairs in the same order and of the same types. |
!= |
$a != $b |
Inequality |
TRUE if $a is not equal to $b. |
<> |
$a <> $b |
Inequality |
TRUE if $a is not equal to $b. |
!== |
$a !== $b |
Non identity |
TRUE if $a is not identical to $b. |
Conditional Operators in PHP
PHP 中还有一个运算符称为条件运算符。它也被称为三元运算符。它首先为真或假值求值一个表达式,然后根据求值结果执行两个给定语句中的一个。
There is one more operator in PHP which is called conditional operator. It is also known as ternary operator. It first evaluates an expression for a true or false value and then executes one of the two given statements depending upon the result of the evaluation.
Operator |
Description |
Example |
? : |
Conditional Expression |
If Condition is true ? Then value X : Otherwise value Y |
Operator Categories in PHP
我们在上面讨论的所有运算符可以归类为以下几类−
All the operators we have discussed above can be categorised into following categories −
-
Unary prefix operators, which precede a single operand.
-
Binary operators, which take two operands and perform a variety of arithmetic and logical operations.
-
The conditional operator (a ternary operator), which takes three operands and evaluates either the second or third expression, depending on the evaluation of the first expression.
-
Assignment operators, which assign a value to a variable.
Operator Precedence in PHP
运算符的优先级决定了表达式中运算符执行的顺序。例如,在“2+6/3”中,6/3 的除法先执行,然后“2+2”的加法执行,因为除法运算符“/”的优先级高于加法运算符“+”。
Precedence of operators decides the order of execution of operators in an expression. For example, in "2+6/3", division of 6/3 is done first and then the addition of "2+2" takes place because the division operator "/" has higher precedence over the addition operator "+".
要强制在其他运算符之前调用某个运算符,应使用括号。在此示例中,(2+6)/3 首先执行加法,然后执行除法。
To force a certain operator to be called before other, parentheses should be used. In this example, (2+6)/3 performs addition first, followed by division.
某些运算符可能具有相同优先级。在这种情况下,关联顺序(左或右)决定运算顺序。具有相同优先级但非关联的运算符不能并列使用。
Some operators may have the same level of precedence. In that case, the order of associativity (either left or right) decides the order of operations. Operators of same precedence level but are non-associative cannot be used next to each other.
下表按运算符的优先级降序列出了 PHP 运算符 −
The following table lists the PHP operators in their decreasing order of precedence −
Operators |
Purpose |
clone new |
clone and new |
** |
exponentiation |
++ — |
increment/decrement |
~(int) (float) (string) (array) (object) (bool) |
casting |
instanceof |
types |
! |
logical |
* / |
multiplication/division |
% |
modulo |
+ - . |
arithmetic and string |
<< >> |
bitwise shift |
< ⇐ > >= |
comparison |
== != === !== <> <⇒ |
comparison |
& |
bitwise and/references |
^ |
bitwise XOR |
bitwise OR |
&& |
logical and |
|
logical or |
?? |
null coalescing |
? : |
ternary |
= += -= = *= /= .= %= &= |
= ^= <⇐ >>= ??= |
assignment operators |
yield from |
yield from |
yield |
yield |
and |
logical |
xor |
logical |
or |
logical |
PHP - Arithmetic Operators Examples
在 PHP 中,算术运算符用于对数字值执行数学运算。下表重点介绍了 PHP 支持的算术运算符。假设变量“$a”的值为 42,而变量“$b”的值为 20 −
In PHP, arithmetic operators are used to perform mathematical operations on numeric values. The following table highligts the arithmetic operators that are supported by PHP. Assume variable "$a" holds 42 and variable "$b" holds 20 −
Operator |
Description |
Example |
+ |
Adds two operands |
$a + $b = 62 |
- |
Subtracts the second operand from the first |
$a - $b = 22 |
* |
Multiply both the operands |
$a * $b = 840 |
/ |
Divide the numerator by the denominator |
$a / $b = 2.1 |
% |
Modulus Operator and remainder of after an integer division |
$a % $b = 2 |
++ |
Increment operator, increases integer value by one |
$a ++ = 43 |
— |
Decrement operator, decreases integer value by one |
$a — = 42 |
Example
以下示例说明了如何在 PHP 中使用这些算术运算符 −
The following example shows how you can use these arithmetic operators in PHP −
<?php
$a = 42;
$b = 20;
$c = $a + $b;
echo "Addtion Operation Result: $c \n";
$c = $a - $b;
echo "Substraction Operation Result: $c \n";
$c = $a * $b;
echo "Multiplication Operation Result: $c \n";
$c = $a / $b;
echo "Division Operation Result: $c \n";
$c = $a % $b;
echo "Modulus Operation Result: $c \n";
$c = $a++;
echo "Increment Operation Result: $c \n";
$c = $a--;
echo "Decrement Operation Result: $c";
?>
它将生成以下 output −
It will produce the following output −
Addtion Operation Result: 62
Substraction Operation Result: 22
Multiplication Operation Result: 840
Division Operation Result: 2.1
Modulus Operation Result: 2
Increment Operation Result: 42
Decrement Operation Result: 43
PHP - Comparison Operators Examples
在 PHP 中,比较运算符用于比较两个值并确定它们之间的关系。这些运算符基于比较结果返回布尔值,即 True 或 False。
In PHP, Comparison operators are used to compare two values and determine their relationship. These operators return a Boolean value, either True or False, based on the result of the comparison.
下表突出显示了 PHP 支持的比较运算符。假定变量 $a 存储 10,变量 $b 存储 20,则−
The following table highligts the comparison operators that are supported by PHP. Assume variable $a holds 10 and variable $b holds 20, then −
Operator |
Description |
Example |
== |
Checks if the value of two operands are equal or not, if yes then condition becomes true. |
($a == $b) is not true |
!= |
Checks if the value of two operands are equal or not, if values are not equal then condition becomes true. |
($a != $b) is true |
> |
Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true. |
($a > $b) is false |
< |
Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true. |
($a < $b) is true |
>= |
Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true. |
($a >= $b) is false |
⇐ |
Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true. |
($a ⇐ $b) is true |
此外,这些运算符还可以与逻辑运算符(&&、||、!)结合使用,以在 PHP 程序的决策制定中形成复杂的条件。
Additionally, these operators can also be combined with logical operators (&&, ||, !) to form complex conditions for decision making in PHP programs.
Example
以下示例展示了如何在 PHP 中使用这些比较运算符:
The following example shows how you can use these comparison operators in PHP −
<?php
$a = 42;
$b = 20;
if ($a == $b) {
echo "TEST1 : a is equal to b \n";
} else {
echo "TEST1 : a is not equal to b \n";
}
if ($a > $b) {
echo "TEST2 : a is greater than b \n";
} else {
echo "TEST2 : a is not greater than b \n";
}
if ($a < $b) {
echo "TEST3 : a is less than b \n";
} else {
echo "TEST3 : a is not less than b \n";
}
if ($a != $b) {
echo "TEST4 : a is not equal to b \n";
} else {
echo "TEST4 : a is equal to b \n";
}
if ($a >= $b) {
echo "TEST5 : a is either greater than or equal to b \n";
} else {
echo "TEST5 : a is neither greater than nor equal to b \n";
}
if ($a <= $b) {
echo "TEST6 : a is either less than or equal to b \n";
} else {
echo "TEST6 : a is neither less than nor equal to b";
}
?>
它将生成以下 output −
It will produce the following output −
TEST1 : a is not equal to b
TEST2 : a is greater than b
TEST3 : a is not less than b
TEST4 : a is not equal to b
TEST5 : a is either greater than or equal to b
TEST6 : a is neither less than nor equal to b
PHP - Logical Operators Examples
在 PHP 中,逻辑运算符用于组合条件语句。这些运算符允许你通过将多个条件组合在一起创建更复杂的条件。
In PHP, logical operators are used to combine conditional statements. These operators allow you to create more complex conditions by combining multiple conditions together.
逻辑运算符通常用在 if, while 等条件语句中,以及 for 循环中,用以根据特定条件控制程序执行的流程。
Logical operators are generally used in conditional statements such as if, while, and for loops to control the flow of program execution based on specific conditions.
下表重点介绍了 PHP 支持的逻辑运算符。
The following table highligts the logical operators that are supported by PHP.
假设变量 $a 保存 10,变量 $b 保存 20,则 -
Assume variable $a holds 10 and variable $b holds 20, then −
Operator |
Description |
Example |
and |
Called Logical AND operator. If both the operands are true then condition becomes true. |
(A and B) is true |
or |
Called Logical OR Operator. If any of the two operands are non zero then condition becomes true. |
(A or B) is true |
&& |
Called Logical AND operator. The AND operator returns true if both the left and right operands are true. |
(A && B) is true |
Called Logical OR Operator. If any of the two operands are non zero then condition becomes true. |
(A |
|
B) is true |
! |
Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false. |
Example
以下示例演示如何在 PHP 中使用这些逻辑运算符 -
The following example shows how you can use these logical operators in PHP −
<?php
$a = 42;
$b = 0;
if ($a && $b) {
echo "TEST1 : Both a and b are true \n";
} else {
echo "TEST1 : Either a or b is false \n";
}
if ($a and $b) {
echo "TEST2 : Both a and b are true \n";
} else {
echo "TEST2 : Either a or b is false \n";
}
if ($a || $b) {
echo "TEST3 : Either a or b is true \n";
} else {
echo "TEST3 : Both a and b are false \n";
}
if ($a or $b) {
echo "TEST4 : Either a or b is true \n";
} else {
echo "TEST4 : Both a and b are false \n";
}
$a = 10;
$b = 20;
if ($a) {
echo "TEST5 : a is true \n";
} else {
echo "TEST5 : a is false \n";
}
if ($b) {
echo "TEST6 : b is true \n";
} else {
echo "TEST6 : b is false \n";
}
if (!$a) {
echo "TEST7 : a is true \n";
} else {
echo "TEST7 : a is false \n";
}
if (!$b) {
echo "TEST8 : b is true \n";
} else {
echo "TEST8 : b is false";
}
?>
它将生成以下 output −
It will produce the following output −
TEST1 : Either a or b is false
TEST2 : Either a or b is false
TEST3 : Either a or b is true
TEST4 : Either a or b is true
TEST5 : a is true
TEST6 : b is true
TEST7 : a is false
TEST8 : b is false
PHP - Assignment Operators Examples
您可以在 PHP 中使用赋值运算符将值赋给变量。赋值运算符是执行算术或其他操作的速记符号,同时将值赋给变量。例如,“=”运算符将右侧的值赋给左侧变量。
You can use assignment operators in PHP to assign values to variables. Assignment operators are shorthand notations to perform arithmetic or other operations while assigning a value to a variable. For instance, the "=" operator assigns the value on the right-hand side to the variable on the left-hand side.
此外,还有组合赋值运算符,如 +=、-=、*=、/= 和 %=,这些运算符将算术运算与赋值结合在一起。例如,“$x += 5” 是 “$x = $x + 5” 的速记形式,它将 $x 的值增加 5。赋值运算符提供了一种简洁的方法来根据变量当前的值更新变量。
Additionally, there are compound assignment operators like +=, -= , *=, /=, and %= which combine arithmetic operations with assignment. For example, "$x += 5" is a shorthand for "$x = $x + 5", incrementing the value of $x by 5. Assignment operators offer a concise way to update variables based on their current values.
下表高亮显示了 PHP 支持的赋值运算符 −
The following table highligts the assignment operators that are supported by PHP −
Operator |
Description |
Example |
= |
Simple assignment operator. Assigns values from right side operands to left side operand |
C = A + B will assign value of A + B into C |
+= |
Add AND assignment operator. It adds right operand to the left operand and assign the result to left operand |
C += A is equivalent to C = C + A |
-= |
Subtract AND assignment operator. It subtracts right operand from the left operand and assign the result to left operand |
C -= A is equivalent to C = C - A |
*= |
Multiply AND assignment operator. It multiplies right operand with the left operand and assign the result to left operand |
C *= A is equivalent to C = C * A |
/= |
Divide AND assignment operator. It divides left operand with the right operand and assign the result to left operand |
C /= A is equivalent to C = C / A |
%= |
Modulus AND assignment operator. It takes modulus using two operands and assign the result to left operand |
C %= A is equivalent to C = C % A |
Example
以下示例展示了如何在 PHP 中使用这些赋值运算符:
The following example shows how you can use these assignment operators in PHP −
<?php
$a = 42;
$b = 20;
$c = $a + $b;
echo "Addition Operation Result: $c \n";
$c += $a;
echo "Add AND Assignment Operation Result: $c \n";
$c -= $a;
echo "Subtract AND Assignment Operation Result: $c \n";
$c *= $a;
echo "Multiply AND Assignment Operation Result: $c \n";
$c /= $a;
echo "Division AND Assignment Operation Result: $c \n";
$c %= $a;
echo "Modulus AND Assignment Operation Result: $c";
?>
它将生成以下 output −
It will produce the following output −
Addition Operation Result: 62
Add AND Assignment Operation Result: 104
Subtract AND Assignment Operation Result: 62
Multiply AND Assignment Operation Result: 2604
Division AND Assignment Operation Result: 62
Modulus AND Assignment Operation Result: 20
PHP – String Operators
PHP 中有两个用于处理字符串数据类型的运算符:连接运算符 (".") 和连接赋值运算符 (".="). 阅读本章以了解这些运算符如何在 PHP 中工作。
There are two operators in PHP for working with string data types: concatenation operator (".") and the concatenation assignment operator (".="). Read this chapter to learn how these operators work in PHP.
Concatenation Operator in PHP
点运算符 (".") 是 PHP 的连接运算符。它将两个字符串操作数(右手字符串的字符追加到左手字符串)连接起来并返回一个新字符串。
The dot operator (".") is PHP’s concatenation operator. It joins two string operands (characters of right hand string appended to left hand string) and returns a new string.
$third = $first . $second;
Concatenation Assignment Operator in PHP
PHP 还有一个“.=”运算符,可以称之为连接赋值运算符。它通过追加右手操作数的字符来更新在其左边的字符串。
PHP also has the ".=" operator which can be termed as the concatenation assignment operator. It updates the string on its left by appending the characters of right hand operand.
$leftstring .= $rightstring;
Example
以下示例使用连接赋值运算符。两个字符串操作数连接,返回字符串左侧的更新内容:
The following example uses the concatenation assignment operator. Two string operands are concatenated returning the updated contents of string on the left side −
<?php
$x="Hello ";
$y="PHP";
$x .= $y;
echo $x;
?>
它将生成以下 output −
It will produce the following output −
Hello PHP
PHP – Array Operators
PHP 定义了以下一组符号,用作数组数据类型的运算符−
PHP defines the following set of symbols to be used as operators on array data types −
Symbol |
Example |
Name |
Result |
+ |
$a + $b |
Union |
Union of $a and $b. |
== |
$a == $b |
Equality |
TRUE if $a and $b have the same key/value pairs. |
=== |
$a === $b |
Identity |
TRUE if $a and $b have the same key/value pairs in the same order and of the same types. |
!= |
$a != $b |
Inequality |
TRUE if $a is not equal to $b. |
<> |
$a <> $b |
Inequality |
TRUE if $a is not equal to $b. |
!== |
$a !== $b |
Non identity |
TRUE if $a is not identical to $b. |
并集运算符将右侧数组追加到左侧数组。如果数组中都存在键,则将使用左侧数组中的元素,并将忽略右侧数组中的匹配元素。
The Union operator appends the right-hand array appended to left-hand array. If a key exists in both arrays, the elements from the left-hand array will be used, and the matching elements from the right-hand array will be ignored.
Example: Union Opeator in PHP
以下示例展示了如何在 PHP 中使用并集运算符:
The following example shows how you can use the union operator in PHP −
<?php
$arr1=array("phy"=>70, "che"=>80, "math"=>90);
$arr2=array("Eng"=>70, "Bio"=>80,"CompSci"=>90);
$arr3=$arr1+$arr2;
var_dump($arr3);
?>
它将生成以下 output −
It will produce the following output −
array(6) {
["phy"]=>
int(70)
["che"]=>
int(80)
["math"]=>
int(90)
["Eng"]=>
int(70)
["Bio"]=>
int(80)
["CompSci"]=>
int(90)
}
Example: When Two Array are Equal
如果两个数组具有相同的键值对,则称这两个数组相等。
Two arrays are said to be equal if they have the same key-value pairs.
在以下示例中,我们有一个索引数组和另一个关联数组,其键对应于第一个元素中的索引。因此,两者相等。
In the following example, we have an indexed array and other associative array with keys corresponding to index of elements in first. Hence, both are equal.
<?php
$arr1=array(0=>70, 2=>80, 1=>90);
$arr2=array(70,90,80);
var_dump ($arr1==$arr2);
var_dump ($arr2!=$arr1);
?>
它将生成以下 output −
It will produce the following output −
bool(true)
bool(false)
Example: When Two Arrays are Identical
当键值对相同且顺序相同,数组才相同。
Arrays are identical if and only if both of them have same set of key-value pairs and in same order.
<?php
$arr1=array(0=>70, 1=>80, 2=>90);
$arr2=array(70,90,80);
var_dump ($arr1===$arr2);
$arr3=[70,80,90];
var_dump ($arr3===$arr1);
?>
它将生成以下 output −
It will produce the following output −
bool(false)
bool(true)
PHP – Conditional Operators Examples
当需要根据条件设置值时,可以在 PHP 中使用条件运算符。它也被称为 ternary operator 。它首先评估一个表达式的真值或假值,然后根据评估结果执行给定的两个语句中的一个。
You would use conditional operators in PHP when there is a need to set a value depending on conditions. It is also known as ternary operator. It first evaluates an expression for a true or false value and then executes one of the two given statements depending upon the result of the evaluation.
三元运算符提供了一种简洁的方法来编写条件表达式。它们由三部分组成:条件、如果条件评估为真的返回值以及如果条件评估为假的返回值。
Ternary operators offer a concise way to write conditional expressions. They consist of three parts: the condition, the value to be returned if the condition evaluates to true, and the value to be returned if the condition evaluates to false.
Operator |
Description |
Example |
? : |
Conditional Expression |
If Condition is true ? Then value X : Otherwise value Y |
Syntax
其语法如下:
The syntax is as follows −
condition ? value_if_true : value_if_false
三元运算符特别适用于将 if-else 语句缩短为一行。可以使用三元运算符根据条件为变量分配不同的值,而无需多行代码。它可以提高代码的可读性。
Ternary operators are especially useful for shortening if-else statements into a single line. You can use a ternary operator to assign different values to a variable based on a condition without needing multiple lines of code. It can improve the readability of the code.
但是,您应明智地使用三元运算符,否则最终会使代码过于复杂,难以理解。
However, you should use ternary operators judiciously, else you will end up making the code too complex for others to understand.
Example
尝试以下示例以了解条件运算符在 PHP 中是如何工作的。复制并粘贴以下 PHP 程序到 test.php 文件中,并将其放在 PHP 服务器的文档根目录中,然后使用任何浏览器浏览它。
Try the following example to understand how the conditional operator works in PHP. Copy and paste the following PHP program in test.php file and keep it in your PHP Server’s document root and browse it using any browser.
<?php
$a = 10;
$b = 20;
/* If condition is true then assign a to result otheriwse b */
$result = ($a > $b ) ? $a :$b;
echo "TEST1 : Value of result is $result \n";
/* If condition is true then assign a to result otheriwse b */
$result = ($a < $b ) ? $a :$b;
echo "TEST2 : Value of result is $result";
?>
它将生成以下 output −
It will produce the following output −
TEST1 : Value of result is 20
TEST2 : Value of result is 10
PHP - Spread Operator
PHP 将三个点符号(…)识别为 spread operator 。展开运算符有时也称为 splat operator 。此运算符最初在 PHP 7.4 版本中引入。它可以在许多情况下有效使用,例如解包数组。
PHP recognizes the three dots symbol (…) as the spread operator. The spread operator is also sometimes called the splat operator. This operator was first introduced in PHP version 7.4. It can be effectively used in many cases such as unpacking arrays.
Example 1
在下面的示例中,$arr1 中的元素插入到 $arr2 中其自身元素的列表之后。
In the example below, the elements in $arr1 are inserted in $arr2 after a list of its own elements.
<?php
$arr1 = [4,5];
$arr2 = [1,2,3, ...$arr1];
print_r($arr2);
?>
它将生成以下 output −
It will produce the following output −
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
)
Example 2
展开运算符可以在表达式中多次使用。例如,在以下代码中,通过扩展两个数组中的元素来创建一个第三个数组。
The Spread operator can be used more than once in an expression. For example, in the following code, a third array is created by expanding the elements of two arrays.
<?php
$arr1 = [1,2,3];
$arr2 = [4,5,6];
$arr3 = [...$arr1, ...$arr2];
print_r($arr3);
?>
它将生成以下 output −
It will produce the following output −
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 6
)
Example 3
请注意,可以使用 array_merge() 函数获得相同的结果,如下所示:
Note that the same result can be obtained with the use of array_merge() function, as shown below −
<?php
$arr1 = [1,2,3];
$arr2 = [4,5,6];
$arr3 = array_merge($arr1, $arr2);
print_r($arr3);
?>
它将产生相同的 output ——
It will produce the same output −
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 6
)
但是,使用(…)运算符的效率要高得多,因为它避免了函数调用的开销。
However, the use of (…) operator is much more efficient as it avoids the overhead a function call.
Example 4
PHP 8.1.0 还引入了另一项功能,允许在解包参数后使用命名参数。不必为每个参数分别提供值,而是将数组中的值解包到相应的参数中,在数组前使用 …(三个点)。
PHP 8.1.0 also introduced another feature that allows using named arguments after unpacking the arguments. Instead of providing a value to each of the arguments individually, the values from an array will be unpacked into the corresponding arguments, using … (three dots) before the array.
<?php
function myfunction($x, $y, $z=30) {
echo "x = $x y = $y z = $z";
}
myfunction(...[10, 20], z:30);
?>
它将生成以下 output −
It will produce the following output −
x = 10 y = 20 z = 30
Example 5
在以下示例中,函数的返回值是一个数组。然后数组元素被展开和解包。
In the following example, the return value of a function is an array. The array elements are then spread and unpacked.
<?php
function get_squares() {
for ($i = 0; $i < 5; $i++) {
$arr[] = $i**2;
}
return $arr;
}
$squares = [...get_squares()];
print_r($squares);
?>
它将生成以下 output −
It will produce the following output −
Array
(
[0] => 0
[1] => 1
[2] => 4
[3] => 9
[4] => 16
)
PHP - Null Coalescing Operator
Null Coalescing 运算符是 PHP 7 引入的许多新功能之一。“coalescing”一词的意思是将许多事物组合成一个。此运算符用于替换与 isset() 函数结合使用时的三元运算。
The Null Coalescing operator is one of the many new features introduced in PHP 7. The word "coalescing" means uniting many things into one. This operator is used to replace the ternary operation in conjunction with the isset() function.
Ternary Operator in PHP
PHP 有一个三元运算符,由“ ? ”符号表示。三元运算符比较布尔表达式,如果为真则执行第一个操作数,否则执行第二个操作数。
PHP has a ternary operator represented by the "?" symbol. The ternary operator compares a Boolean expression and executes the first operand if it is true, otherwise it executes the second operand.
expr ? statement1 : statement2;
Example
让我们使用三元运算符来检查某个变量是否设置,借助 isset() 函数,如果已声明则返回 true,否则返回 false。
Let us use the ternary operator to check if a certain variable is set or not with the help of the isset() function, which returns true if declared and false if not.
<?php
$x = 1;
$var = isset($x) ? $x : "not set";
echo "The value of x is $var";
?>
它将生成以下 output −
It will produce the following output −
The value of x is 1
现在,让我们删除“x”的声明并重新运行代码:
Now, let’s remove the declaration of "x" and rerun the code −
<?php
# $x = 1;
$var = isset($x) ? $x : "not set";
echo "The value of x is $var";
?>
现在,代码将产生以下 output :
The code will now produce the following output −
The value of x is not set
The Null Coalescing Operator
Null Coalescing 运算符由“??”符号表示。它用作三元运算符与 isset() 结合使用的便捷快捷方式。它返回其第一个操作数(如果存在且不为 null);否则返回其第二个操作数。
The Null Coalescing Operator is represented by the "??" symbol. It acts as a convenient shortcut to use a ternary in conjunction with isset(). It returns its first operand if it exists and is not null; otherwise it returns its second operand.
$Var = $operand1 ?? $operand2;
第一个操作数检查特定变量是否为 null 或不为 null(或是否已设置或未设置)。如果它不是 null,则返回第一个操作数,否则返回第二个操作数。
The first operand checks whether a certain variable is null or not (or is set or not). If it is not null, the first operand is returned, else the second operand is returned.
Example
请看以下示例:
Take a look at the following example −
<?php
# $num = 10;
$val = $num ?? 0;
echo "The number is $val";
?>
它将生成以下 output −
It will produce the following output −
The number is 0
现在取消对将 $num 设置为 10 的第一条语句的注释,并重新运行代码:
Now uncomment the first statement that sets $num to 10 and rerun the code −
<?php
$num = 10;
$val = $num ?? 0;
echo "The number is $val";
?>
现在会生成以下 output −
It will now produce the following output −
The number is 10
空值合并运算符的一个实用应用是在检查用户名是否由客户端浏览器提供时。
A useful application of Null Coalescing operator is while checking whether a username has been provided by the client browser.
Example
下面代码从 URL 中读取 name 变量。如果 URL 中 name 参数确实有值,则会显示一条欢迎信息。但是,如果没有,则用户被称为 Guest。
The following code reads the name variable from the URL. If indeed there is a value for the name parameter in the URL, a Welcome message for him is displayed. However, if not, the user is called Guest.
<?php
$username = $_GET['name'] ?? 'Guest';
echo "Welcome $username";
?>
假设此脚本 “hello.php” 位于 PHP 服务器的 htdocs 文件夹中,在 URL 中输入 http://localhost/hello.php?name=Amar ,浏览器将显示以下消息 −
Assuming that this script "hello.php" is in the htdocs folder of the PHP server, enter http://localhost/hello.php?name=Amar in the URL, the browser will show the following message −
Welcome Amar
如果 http://localhost/hello.php 是 URL,则浏览器将显示以下消息 −
If http://localhost/hello.php is the URL, the browser will show the following message −
Welcome Guest
空值合并运算符用作检查 isset() 函数的三元运算符特定案例的替换项。因此,以下语句给出了类似的结果 −
The Null coalescing operator is used as a replacement for the ternary operator’s specific case of checking isset() function. Hence, the following statements give similar results −
<?php
$username = isset($_GET['name']) ? $_GET['name'] : 'Guest';
echo "Welcome $username";
?>
现在会生成以下 output −
It will now produce the following output −
Welcome Guest
你可以连接 “??” 运算符,如下所示 −
You can chain the "??" operators as shown below −
<?php
$username = $_GET['name'] ?? $_POST['name'] ?? 'Guest';
echo "Welcome $username";
?>
现在会生成以下 output −
It will now produce the following output −
Welcome Guest
如果 $name 变量未通过 GET 或 POST 方法设置,这会将 username 设置为 Guest。
This will set the username to Guest if the variable $name is not set either by GET or by POST method.
PHP - Spaceship Operator
Spaceship 运算符是 PHP 7.0 版本中引入的众多新功能之一。它是一个三向比较运算符。
The Spaceship operator is one of the many new features introduced in PHP with its 7.0 version. It is a three-way comparison operator.
常规比较运算符 (<, >, !=, == 等) 返回 true 或 false(等同于 1 或 0)。另一方面,Spaceship 运算符有三个可能的返回值:-1、0 或 1。该运算符可用于整数、浮点数、字符串、数组、对象等。
The conventional comparison operators (<, >, !=, ==, etc.) return true or false (equivalent to 1 or 0). On the other hand, the spaceship operator has three possible return values: -1,0,or 1. This operator can be used with integers, floats, strings, arrays, objects, etc.
Syntax
用于 Spaceship 运算符的符号为 "<⇒" 。
The symbol used for spaceship operator is "<⇒".
$retval = operand1 <=> operand2
此处,如果操作数 1 小于操作数 2,则 $retval 为 -1;如果两个操作数相等,则为 0;如果操作数 1 大于操作数 2,则为 1。
Here, $retval is -1 if operand1 is less than operand2, 0 if both the operands are equal, and 1 if operand1 is greater than operand2.
Spaceship 运算符作为组合比较运算符实现。常规比较运算符可以被认为仅仅是下表的简写 −
The spaceship operator is implemented as a combined comparison operator. Conventional comparison operators could be considered mere shorthands for <⇒ as the following table shows −
Operator |
<⇒ equivalent |
$a < $b |
($a <⇒ $b) === -1 |
$a ⇐ $b |
($a <⇒ $b) === -1 |
($a <⇒ $b) === 0 |
$a == $b |
($a <⇒ $b) === 0 |
$a != $b |
($a <⇒ $b) !== 0 |
$a >= $b |
($a <⇒ $b) === 1 |
($a <⇒ $b) === 0 |
$a > $b |
($a <⇒ $b) === 1 |
Example 1
以下示例展示了如何在 PHP 中使用 Spaceship 运算符 −
The following example shows how you can use the spaceship operator in PHP −
<?php
$x = 5;
$y = 10;
$z = $x <=> $y/2;
echo "$x <=> $y/2 = $z";
?>
它将生成以下 output −
It will produce the following output −
5 <=> 10/2 = 0
Example 2
更改 $x=4 并检查结果 −
Change $x=4 and check the result −
<?php
$x = 4;
$y = 10;
$z = $x <=> $y/2;
echo "$x <=> $y/2 = $z";
?>
它将生成以下 output −
It will produce the following output −
4 <=> 10/2 = -1
Example 3
更改 $y=7 并再次检查结果 −
Change $y=7 and check the result again −
<?php
$x = 7;
$y = 10;
$z = $x <=> $y/2;
echo "$x <=> $y/2 = $z";
?>
它将生成以下 output −
It will produce the following output −
7 <=> 10/2 = 1
Example 4
当用于字符串操作数时,Spaceship 操作数的作用与 strcmp() 函数相同。
When used with string operands, the spaceship operand works just like the strcmp() function.
<?php
$x = "bat";
$y = "ball";
$z = $x <=> $y;
echo "$x <=> $y = $z";
?>
它将生成以下 output −
It will produce the following output −
bat <=> ball = 1
PHP – Decision Making
计算机程序默认按照输入-处理-输出路径顺序执行。这种顺序流可以借助所有计算机编程语言(包括 PHP)提供的决策控制语句进行更改。
A computer program by default follows a simple input-process-output path sequentially. This sequential flow can be altered with the decision control statements offered by all the computer programming languages including PHP.
Decision Making in a Computer Program
决策是预测程序执行期间出现的条件并根据条件采取指定操作。
Decision-making is the anticipation of conditions occurring during the execution of a program and specified actions taken according to the conditions.
你可以在代码中使用条件语句来做出决策。实现条件逻辑的能力是编程语言基本要求之一。
You can use conditional statements in your code to make your decisions. The ability to implement conditional logic is one of the fundamental requirements of a programming language.
A Typical Decision Making Structure
以下是大多数编程语言中常见的典型决策结构的一般形式 −
Following is the general form of a typical decision making structure found in most of the programming languages −
Decision Making Statements in PHP
PHP 支持以下三个决策表述:
PHP supports the following three decision making statements −
-
if…else statement − Use this statement if you want to execute a set of code when a condition is true and another if the condition is not true.
-
elseif statement − Use this statement with the if…else statement to execute a set of code if one of the several conditions is true
-
switch statement − If you want to select one of many blocks of code to be executed, use the Switch statement. The switch statement is used to avoid long blocks of if..elseif..else code.
几乎所有编程语言(包括 PHP)都定义了 if-else 语句。这允许有条件地执行代码片段。PHP 中使用 if-else 语句的语法类似于 C 中的语法 −
Almost all the programming languages (including PHP) define the if-else statements. It allows for conditional execution of code fragments. The syntax for using the if-else statement in PHP is similar to that of C −
if (expr)
statement1
else
statement2
此处表达式是布尔表达式,对 true 或 false 进行计算
The expression here is a Boolean expression, evaluating to true or false
-
Any expression involving Boolean operators such as <, >, ⇐, >=, !=, etc. is a Boolean expression.
-
If the expression results in true, the subsequent statement – it may be a simple or a compound statement i.e., a group of statements included in pair of braces – will be executed.
-
If the expression is false, the subsequent statement is ignored, and the program flow continues from next statement onwards.
-
The use of else statement is optional. If the program logic requires another statement or a set of statements to be executed in case the expression (after the if keyword) evaluates to false.
elseif 语句是 if 与 else 的组合。它允许你检查多个条件的 TRUE 值,并在其中一个条件计算结果为 TRUE 时执行一组代码。就像 else 语句一样, elseif 语句是可选的。
The elseif statement is a combination of if and else. It allows you to check multiple expressions for TRUE and execute a block of code as soon as one of the conditions evaluates to TRUE. Just like the else statement, the elseif statement is optional.
switch 语句类似于针对同一表达式的一系列 if 语句。我们将在本教程的后几章详细了解这些语句。
The switch statement is similar to a series of if statements on the same expression. We shall learn about these statements in detail in the subsequent chapters of this tutorial.
PHP - If…Else Statement
实现条件逻辑的能力是任何编程语言(包括 PHP)的基本要求。PHP 有三个关键字(也称为 language constructs) – if, elseif 和 else – 根据不同的条件做出决策。
The ability to implement conditional logic is the fundamental requirement of any programming language (PHP included). PHP has three keywords (also called as language constructs) – if, elseif and else – are used to take decision based on the different conditions.
if 关键字是代码片段条件执行的基本结构。 if 关键字通常与 else 关键字结合使用,尽管它并不总是必需的。
The if keyword is the basic construct for the conditional execution of code fragments. More often than not, the if keyword is used in conjunction with else keyword, although it is not always mandatory.
如果你想在条件为 true 时执行一些代码,在相同条件为 false 时执行其他代码,则使用 “if….else” 语句。
If you want to execute some code if a condition is true and another code if the sme condition is false, then use the "if….else" statement.
Syntax
%{s8} 语句在 PHP 中的使用和语法类似于 C 语言。以下是 PHP 中 if 语句的语法 −
The usage and syntax of the if statement in PHP is similar to that of the C language. Here is the syntax of if statement in PHP −
if (expression)
code to be executed if expression is true;
else
code to be executed if expression is false;
if 语句总后跟一个布尔表达式。
The if statement is always followed by a Boolean expression.
-
PHP will execute the statement following the Boolean expression if it evaluates to true.
-
If the Boolean expression evaluates to false, the statement is ignored.
-
If the algorithm needs to execute another statement when the expression is false, it is written after the else keyword.
Example
以下是一个演示 if else 语句用法的简单 PHP 代码。有两个变量 $a 和 $b。该代码识别出它们中的哪一个更大。
Here is a simple PHP code that demonstrates the usage of if else statements. There are two variables $a and $b. The code identifies which one of them is bigger.
<?php
$a=10;
$b=20;
if ($a > $b)
echo "a is bigger than b";
else
echo "a is not bigger than b";
?>
运行上述代码时,它会显示以下 output −
When the above code is run, it displays the following output −
a is not bigger than b
交换 “a” 和 “b” 的值,然后重新运行。现在,你将获得以下输出 −
Interchange the values of "a" and "b" and run again. Now, you will get the following output −
a is bigger than b
Example
如果当前是星期五,下列示例将输出“Have a nice weekend!”,否则将输出“Have a nice day!” −
The following example will output "Have a nice weekend!" if the current day is Friday, else it will output "Have a nice day!" −
<?php
$d = date("D");
if ($d == "Fri")
echo "Have a nice weekend!";
else
echo "Have a nice day!";
?>
它将生成以下 output −
It will produce the following output −
Have a nice weekend!
Using endif in PHP
PHP 代码通常与 HTML 脚本混合在一起。我们可以在 if 部分和 PHP 代码中的 else 部分插入 HTML 代码。PHP 为 if 和 else 语句提供了替代语法。将左大括号改为冒号 (:),将右大括号改为 endif; ,这样可以将 HTML 块添加到 if 和 else 部分。
PHP code is usually intermixed with HTML script. We can insert HTML code in the if part as well as the else part in PHP code. PHP offers an alternative syntax for if and else statements. Change the opening brace to a colon (:) and the closing brace to endif; so that a HTML block can be added to the if and else part.
<?php
$d = date("D");
if ($d == "Fri"): ?>
<h2>Have a nice weekend!</h2>
<?php else: ?>
<h2>Have a nice day!</h2>
<?php endif ?>
确保上述脚本位于 PHP 服务器的文档根目录中。访问 URL http://localhost/hello.php 。如果当前不是星期五,浏览器中应显示以下输出 −
Make sure that the above script is in the document root of PHP server. Visit the URL http://localhost/hello.php. Following output should be displayed in the browser, if the current day is not a Friday −
Have a nice day!
Using elseif in PHP
如果某几个条件中有一个条件是真的,而您希望执行一些代码,则使用 elseif 语句。PHP 中的 elseif 语言结构是 if 和 else 的组合。
If you want to execute some code if one of the several conditions are true, then use the elseif statement. The elseif language construct in PHP is a combination of if and else.
-
Similar to else, it specifies an alternative statement to be executed in case the original if expression evaluates to false.
-
However, unlike else, it will execute that alternative expression only if the elseif conditional expression evaluates to true.
if (expr1)
code to be executed if expr1 is true;
elseif (expr2)
code to be executed if expr2 is true;
else
code to be executed if expr2 is false;
Example
让我们修改上述代码以在星期日、星期五和其他日子显示不同的消息。
Let us modify the above code to display a different message on Sunday, Friday and other days.
<?php
$d = date("D");
if ($d == "Fri")
echo "<h3>Have a nice weekend!</h3>";
elseif ($d == "Sun")
echo "<h3>Have a nice Sunday!</h3>";
else
echo "<h3>Have a nice day!</h3>";
?>
在星期日,浏览器应显示以下 output −
On a Sunday, the browser shall display the following output −
Have a nice Sunday!
Example
这里有另一个示例,说明 if–elselif–else 语句的用法 −
Here is another example to show the use of if–elselif–else statements −
<?php
$x=13;
if ($x%2==0) {
if ($x%3==0)
echo "<h3>$x is divisible by 2 and 3</h3>";
else
echo "<h3>$x is divisible by 2 but not divisible by 3</h3>";
}
elseif ($x%3==0)
echo "<h3>$x is divisible by 3 but not divisible by 2</h3>";
else
echo "<h3>$x is not divisible by 3 and not divisible by 2</h3>";
?>
上述代码还使用 nestedif 语句。
The above code also uses nestedif statements.
当 x 的值为 13、12 和 10 时, output 如下 −
For the values of x as 13, 12 and 10, the output will be as follows −
13 is not divisible by 3 and not divisible by 2
12 is divisible by 2 and 3
10 is divisible by 2 but not divisible by 3
PHP - Switch Statement
PHP 中的 switch 语句可以看作对相同表达式中的一系列 if…else 语句的替代。假设你需要用许多不同的值来比较一个表达式或一个变量,并且根据它等于哪个值执行不同的代码片段。在这样的情况下,你需要使用多重 if…elseif…else 构造。
The switch statement in PHP can be treated as an alternative to a series of if…else statements on the same expression. Suppose you need to compare an expression or a variable with many different values and execute a different piece of code depending on which value it equals to. In such a case, you would use multiple if…elseif…else constructs.
然而,这样的构造会使代码变得很乱,并且难以理解。为了简化这样的代码,可以在 PHP 中使用 switch case 构造,它提供了一个更简洁的替代方案来避免 if..elseif..else 代码的长块。
However, such a construct can make the code quite messy and difficult to follow. To simplify such codes, you can use the switch case construct in PHP that offers a more compact alternative to avoid long blocks of if..elseif..else codes.
下面的 PHP 脚本使用 if elseif 语句:
The following PHP script uses if elseif statements −
if ($x == 0) {
echo "x equals 0";
} elseif ($x == 1) {
echo "i equals 1";
} elseif ($x == 2) {
echo "x equals 2";
}
你可以使用 switch case 语句来获得相同的结果,如下所示:
You can get the same result by using the switch case statements as shown below −
switch ($x) {
case 0:
echo "x equals 0";
break;
case 1:
echo "x equals 1";
break;
case 2:
echo "x equals 2";
break;
}
switch 语句后跟一个表达式,该表达式与每个 case 子句中的值依次进行比较。如果发现表达式与任何 case 匹配,则执行相应的语句块。
The switch statement is followed by an expression, which is successively compared with value in each case clause. If it is found that the expression matches with any of the cases, the corresponding block of statements is executed.
-
The switch statement executes the statements inside the curly brackets line by line.
-
If and when a case statement is found whose expression evaluates to a value that matches the value of the switch expression, PHP starts to execute the statements until the end of the switch block, or the first time it encounters a break statement.
-
If you don’t write a break statement at the end of a case’s statement list, PHP will go on executing the statements of the following case.
Example
尝试移除 break,然后再运行上面的代码。如果 x 的值是 0,那么您会发现输出包括 "x equals 1" 和 "x equals 2" 这两行。
Try to run the above code by removing the breaks. If the value of x is 0, you’ll find that the output includes "x equals 1" as well as "x equals 2" lines.
<?php
$x=0;
switch ($x) {
case 0:
echo "x equals 0 \n";
case 1:
echo "x equals 1 \n";
case 2:
echo "x equals 2";
}
?>
它将生成以下 output −
It will produce the following output −
x equals 0
x equals 1
x equals 2
因此,确保以 break 语句结束每个 case 块很重要。
Thus, it is important make sure to end each case block with a break statement.
The Default Case in Switch
default case 是一个特例。这种 case 与其他 case 都不匹配。使用 default 是可选的,但如果使用,这必须是大括号内最后一个 case。
A special case is the default case. This case matches anything that wasn’t matched by the other cases. Using default is optional, but if used, it must be the last case inside the curly brackets.
您可以将多个 case 组合起来,以模拟与 or 运算符组合在一起的多个逻辑表达式。
You can club more than one cases to simulate multiple logical expressions combined with the or operator.
<?php
$x=10;
switch ($x) {
case 0:
case 1:
case 2:
echo "x between 0 and 2 \n";
break;
default:
echo "x is less than 0 or greater than 2";
}
?>
要比较的值都给在 case clause 中。值可以是一个数字、一个字符串,甚至是一个函数。不过,您不能在 case clause 中使用比较运算符 (<、>、== 或 !=) 作为值。
The values to be compared against are given in the case clause. The value can be a number, a string, or even a function. However you cannot use comparison operators (<, > == or !=) as a value in the case clause.
您可以在 case 子句中选择使用分号,而不是冒号。如果没有匹配的 case,并且也没有 default 分支,那么就不会执行任何代码,就像没有 if 语句为 true 一样。
You can choose to use semicolon instead of colon in the case clause. If no matching case found, and there is no default branch either, then no code will be executed, just as if no if statement was true.
The switch-endswitch Statement
PHP 允许使用替代语法,通过 switch-endswitch 语句来界定 switch 结构。以下版本的 switch case 是可以接受的。
PHP allows the usage of alternative syntax by delimiting the switch construct with switch-endswitch statements. The following version of switch case is acceptable.
<?php
$x=0;
switch ($x) :
case 0:
echo "x equals 0";
break;
case 1:
echo "x equals 1 \n";
break;
case 2:
echo "x equals 2 \n";
break;
default:
echo "None of the above";
endswitch
?>
Using the Break Statement in Switch…Case
显然,您不用编写 break 来终止 default case,因为它在 switch 结构中是最后一个 case。
Obviously, you needn’t write a break to terminate the default case, it being the last case in the switch construct.
Example
请看以下示例:
Take a look at the following example −
<?php
$d = date("D");
switch ($d){
case "Mon":
echo "Today is Monday";
break;
case "Tue":
echo "Today is Tuesday";
break;
case "Wed":
echo "Today is Wednesday";
break;
case "Thu":
echo "Today is Thursday";
break;
case "Fri":
echo "Today is Friday";
break;
case "Sat":
echo "Today is Saturday";
break;
case "Sun":
echo "Today is Sunday";
break;
default:
echo "Wonder which day is this ?";
}
?>
它将生成以下 output −
It will produce the following output −
Today is Monday
PHP – Loop Types
PHP 中的循环用于对同一代码块执行指定次数。PHP 支持以下四种类型的循环。
Loops in PHP are used to execute the same block of code a specified number of times. PHP supports following four loop types.
-
for − Loops through a block of code a specified number of times.
-
foreach − Loops through a block of code for each element in an array.
-
while − Loops through a block of code if and as long as a specified condition is true.
-
do-while − Loops through a block of code once, and then repeats the loop as long as a special condition is true.
此外,我们还将说明在 PHP 中如何使用 continue 和 break 语句来控制循环的执行。
In addition, we will also explain how the continue and break statements are used in PHP to control the execution of loops.
PHP for Loop
当你清楚知道要执行一条或多条语句的次数时,就可以使用 for 语句。
The for statement is used when you know how many times you want to execute a statement or a block of statements.
Syntax
for (initialization; condition; increment){
code to be executed;
}
初始值设定程序用于设置循环迭代计数器的起始值。为此目的,此处可以声明一个变量,而按传统,此变量被命名为 $i。
The initializer is used to set the start value for the counter of the number of loop iterations. A variable may be declared here for this purpose and it is traditional to name it $i.
Example
以下实例进行五次迭代,并在循环的每次遍历中更改两个变量的赋值——
The following example makes five iterations and changes the assigned value of two variables on each pass of the loop −
<?php
$a = 0;
$b = 0;
for( $i = 0; $i<5; $i++ ) {
$a += 10;
$b += 5;
}
echo ("At the end of the loop a = $a and b = $b" );
?>
它将生成以下 output −
It will produce the following output −
At the end of the loop a = 50 and b = 25
PHP foreach Loop
foreach 语句用于循环遍历数组。每次遍历时,当前数组元素的值分配给 $value,数组指针移动一位,在下次遍历中,将处理下一个元素。
The foreach statement is used to loop through arrays. For each pass the value of the current array element is assigned to $value and the array pointer is moved by one and in the next pass next element will be processed.
PHP while Loop
while 语句会在测试表达式为 true 的情况下执行代码块。
The while statement will execute a block of code if and as long as a test expression is true.
如果测试表达式为 true,则会执行代码块。代码执行后,测试表达式将再次进行评估,并且循环将继续进行,直到发现测试表达式为 false。
If the test expression is true then the code block will be executed. After the code has executed the test expression will again be evaluated and the loop will continue until the test expression is found to be false.
Example
此示例在循环的每次迭代中递减变量值,并且计数器会递增,直到达到 10(此时评估为 false)并结束循环。
This example decrements a variable value on each iteration of the loop and the counter increments until it reaches 10 when the evaluation is false and the loop ends.
<?php
$i = 0;
$num = 50;
while($i < 10) {
$num--;
$i++;
}
echo ("Loop stopped at i = $i and num = $num" );
?>
它将生成以下 output −
It will produce the following output −
Loop stopped at i = 10 and num = 40
PHP do-while Loop
do-while 语句会至少执行代码块一次——然后,只要某个条件为真,它就会重复循环。
The do-while statement will execute a block of code at least once - it then will repeat the loop as long as a condition is true.
Example
以下示例会至少递增一次 i 的值,并且只要 i 的值为小于 10,它就会继续递增 i 的变量——
The following example will increment the value of i at least once, and it will continue incrementing the variable i as long as it has a value of less than 10 −
<?php
$i = 0;
$num = 0;
do {
$i++;
}
while( $i < 10 );
echo ("Loop stopped at i = $i" );
?>
它将生成以下 output −
It will produce the following output −
Loop stopped at i = 10
PHP break Statement
PHP break 关键字用于过早终止循环的执行。
The PHP break keyword is used to terminate the execution of a loop prematurely.
break 语句位于语句块内。它会为你提供完全的控制权,无论何时你想要退出循环,都可以退出。退出循环后,会执行循环的后续语句。
The break statement is situated inside the statement block. It gives you full control and whenever you want to exit from the loop you can come out. After coming out of a loop immediate statement to the loop will be executed.
Example
在以下示例中,当计数器值达到 3 时,条件测试变为 true,并且循环终止。
In the following example condition test becomes true when the counter value reaches 3 and loop terminates.
<?php
$i = 0;
while( $i < 10) {
$i++;
if( $i == 3 )break;
}
echo ("Loop stopped at i = $i" );
?>
它将生成以下 output −
It will produce the following output −
Loop stopped at i = 3
PHP continue Statement
PHP continue 关键字用于中止循环的当前迭代,但不会终止循环。
The PHP continue keyword is used to halt the current iteration of a loop but it does not terminate the loop.
与 break 语句类似, continue 语句位于包含循环执行的代码的语句块内,在语句块前面是一个条件测试。对于遇到 continue 语句的遍历,将跳过循环代码的剩余部分,然后开始下一次遍历。
Just like the break statement the continue statement is situated inside the statement block containing the code that the loop executes, preceded by a conditional test. For the pass encountering continue statement, rest of the loop code is skipped and next pass starts.
Example
在以下示例中,循环打印数组的值,但是对于条件变为真的值,它只跳过代码,然后打印下一个值。
In the following example loop prints the value of array but for which condition becomes true it just skip the code and next value is printed.
<?php
$array = array( 1, 2, 3, 4, 5);
foreach( $array as $value ) {
if( $value == 3 )continue;
echo "Value is $value \n";
}
?>
它将生成以下 output −
It will produce the following output −
Value is 1
Value is 2
Value is 4
Value is 5
PHP - For Loop
程序默认情况下会按照语句的顺序进行执行。如果程序流被定向到程序中的任何早期语句,它就构成了循环。PHP 中的 for 语句是构成 PHP 脚本中循环的便捷工具。在本节中,我们将讨论 PHP 的 for 语句。
A program by default follows a sequential execution of statements. If the program flow is directed towards any of earlier statements in the program, it constitutes a loop. The for statement in PHP is a convenient tool to constitute a loop in a PHP script. In this chapter, we will discuss PHP’s for statement.
Flowchart of "for" Loop
以下流程图说明了 for 循环的工作原理——
The following flowchart explains how a for loop works −
for 语句用于在你已知想要执行语句或语句块的次数时。
The for statement is used when you know how many times you want to execute a statement or a block of statements.
Syntax of "for" Loop
PHP 中 for 语句的语法与 C 语言中的 for 语句类似。
The syntax of for statement in PHP is similar to the for statement in C language.
for (expr1; expr2; expr3){
code to be executed;
}
for 关键字后面是圆括号,圆括号内包含三个用分号分隔的表达式。这三个表达式各自可以为空,也可以包含多个用逗号分隔的表达式。括号后面是一条或多条放在大括号中的语句。这些语句构成了循环体。
The for keyword is followed by a parenthesis containing three expressions separated by a semicolon. Each of them may be empty or may contain multiple expressions separated by commas. The parenthesis is followed by one or more statements put inside curly brackets. It forms the body of the loop.
括号中的第一个表达式只在循环开始时执行。它通常充当 initializer ,用于设置循环迭代计数器的起始值。
The first expression in the parenthesis is executed only at the start of the loop. It generally acts as the initializer used to set the start value for the counter of the number of loop iterations.
在每次迭代的开始,都会计算 expr2 。如果计算结果为真,则循环继续并且执行主体块中的语句。如果计算结果为假,则循环执行结束。通常, expr2 指定计数器的最终值。
In the beginning of each iteration, expr2 is evaluated. If it evaluates to true, the loop continues and the statements in the body block are executed. If it evaluates to false, the execution of the loop ends. Generally, the expr2 specifies the final value of the counter.
expr3 在每次迭代的末尾执行。在大多数情况下,此表达式会递增计数器变量。
The expr3 is executed at the end of each iteration. In most cases, this expression increments the counter variable.
Example
for 循环的最一般示例如下 -
The most general example of a for loop is as follows −
<?php
for ($i=1; $i<=10; $i++){
echo "Iteration No: $i \n";
}
?>
以下是它的 @{s0}
−
Here is its output −
Iteration No: 1
Iteration No: 2
Iteration No: 3
Iteration No: 4
Iteration No: 5
Iteration No: 6
Iteration No: 7
Iteration No: 8
Iteration No: 9
Iteration No: 10
An infinite "for" loop
请注意,括号中的所有三个表达式都是可选的。仅含两个分号的 for 语句构成一个无限循环。
Note that all the three expressions in the parenthesis are optional. A for statement with only two semicolons constitutes an infinite loop.
for (; ;) {
Loop body
}
要停止无限迭代,您需要在循环体中使用 break 语句。
To stop the infinite iteration, you need to use a break statement inside the body of the loop.
A decrementing "for" loop
您还可以形成递减 for 循环。为了创建一个从 10 到 1 的 for 循环,请用 10 初始化循环变量,在每次迭代开始时计算的中间表达式检查它是否大于 1。在每次迭代末尾执行的最后一个表达式应该递减它 1。
You can also form a decrementing for loop. To have a for loop that goes from 10 to 1, initialize the looping variable with 10, the expression in the middle that is evaluated at the beginning of each iteration checks whether it is greater than 1. The last expression to be executed at the end of each iteration should decrement it by 1.
<?php
for ($i=10; $i>=1; $i--){
echo "Iteration No: $i \n";
}
?>
它将生成以下 output −
It will produce the following output −
Iteration No: 10
Iteration No: 9
Iteration No: 8
Iteration No: 7
Iteration No: 6
Iteration No: 5
Iteration No: 4
Iteration No: 3
Iteration No: 2
Iteration No: 1
Using the "for…endfor" construct
您还可以使用冒号 (:) 符号来启动循环块,并在块的末尾放置 endfor 语句。
You can also use the ":" (colon) symbol to start the looping block and put endfor statement at the end of the block.
<?php
for ($i=1; $i<=10; $i++):
echo "Iteration No: $i \n";
endfor;
?>
Iterating an indexed array using "for" loop
数组中的每个元素都由以 "0" 开头的递增索引标识。如果存在一个包含 5 个元素的数组,则它的下限为 0,上限为 4(数组大小 -1)。
Each element in the array is identified by an incrementing index starting with "0". If an array of 5 elements is present, its lower bound is 0 and is upper bound is 4 (size of array -1).
要获得数组中的元素数量,有一个 count() 函数。因此,我们可以使用以下 for 语句遍历索引数组 -
To obtain the number of elements in an array, there is a count() function. Hence, we can iterate over an indexed array by using the following for statement −
<?php
$numbers = array(10, 20, 30, 40, 50);
for ($i=0; $i<count($numbers); $i++){
echo "numbers[$i] = $numbers[$i] \n";
}
?>
它将生成以下 output −
It will produce the following output −
numbers[0] = 10
numbers[1] = 20
numbers[2] = 30
numbers[3] = 40
numbers[4] = 50
Iterating an Associative Array Using "for" Loop
PHP 中的关联数组是键值对的集合。箭头符号 (⇒) 用于显示键与其值之间的关联。我们使用 array_keys() 函数获取键的数组。
An associative array in PHP is a collection of key-value pairs. An arrow symbol (⇒) is used to show the association between the key and its value. We use the array_keys() function to obtain array of keys.
下面的 for 循环从代码中定义的关联数组 $capitals 中打印每个州的首府 -
The following for loop prints the capital of each state from an associative array $capitals defined in the code −
<?php
$capitals = array(
"Maharashtra"=>"Mumbai",
"Telangana"=>"Hyderabad",
"UP"=>"Lucknow",
"Tamilnadu"=>"Chennai"
);
$keys=array_keys($capitals);
for ($i=0; $i<count($keys); $i++){
$cap = $keys[$i];
echo "Capital of $cap is $capitals[$cap] \n";
}
?>
以下是它的 @{s0}
−
Here is its output −
Capital of Maharashtra is Mumbai
Capital of Telangana is Hyderabad
Capital of UP is Lucknow
Capital of Tamilnadu is Chennai
Using Nested "for" Loops in PHP
如果在现有循环的体内使用另一个 for 循环,则这两个循环就被称为嵌套的。
If another for loop is used inside the body of an existing loop, the two loops are said to have been nested.
对于外部循环的计数器变量的每个值,内部循环的所有迭代都已完成。
For each value of counter variable of the outer loop, all the iterations of inner loop are completed.
<?php
for ($i=1; $i<=3; $i++){
for ($j=1; $j<=3; $j++){
echo "i= $i j= $j \n";
}
}
?>
它将生成以下 output −
It will produce the following output −
i= 1 j= 1
i= 1 j= 2
i= 1 j= 3
i= 2 j= 1
i= 2 j= 2
i= 2 j= 3
i= 3 j= 1
i= 3 j= 2
i= 3 j= 3
请注意,字符串是一种数组形式。 strlen() 函数给出字符串中的字符数量。
Note that a string is a form of an array. The strlen() function gives the number of characters in a string.
Example
下面的 PHP 脚本使用两个嵌套循环在每行打印一个字符串中递增数量的字符。
The following PHP script uses two nested loops to print incrementing number of characters from a string in each line.
<?php
$str = "TutorialsPoint";
for ($i=0; $i<strlen($str); $i++){
for ($j=0; $j<=$i; $j++){
echo "$str[$j]";
}
echo "\n";
}
?>
它将生成以下 output −
It will produce the following output −
T
Tu
Tut
Tuto
Tutor
Tutori
Tutoria
Tutorial
Tutorials
TutorialsP
TutorialsPo
TutorialsPoi
TutorialsPoin
TutorialsPoint
PHP - Foreach Loop
PHP 中的 foreach 构造函数专门用于迭代数组。如果你在数据类型不同的变量上尝试使用它,PHP 会报错。
The foreach construct in PHP is specially meant for iterating over arrays. If you try to use it on a variable with a different data type, PHP raises an error.
PHP 中的 foreach 循环可用于索引数组和关联数组。有两种使用语法可用——
The foreach loop in PHP can be used with indexed array as well as associative array. There are two types of usage syntaxes available −
foreach (array as $value) {
statements
}
当你想迭代索引数组时,上述方法很有用。下述语法更适合关联数组。
The above method is useful when you want to iterate an indexed array. The syntax below is more suitable for associative arrays.
foreach (array as $key => $value) {
statements
}
然而,这两种方法都适用于索引数组,因为数组中项目索引也充当键。
However, both these approaches work well with indexed array, because the index of an item in the array also acts as the key.
Using "foreach" Loop with an Indexed Array
上述第一种类型的语法在 foreach 关键词前面显示了一个括号。要遍历的数组的名称之后是 as 关键词,然后再是变量。
The first type of syntax above shows a parenthesis in front of the foreach keyword. The name of the array to be traversed is then followed by the "as" keyword and then a variable.
当第一个迭代开始时,第一个数组元素分配给变量。循环块结束后,变量获取下一个元素的值,并重复循环体中的语句,直到数组中的元素耗尽。
When the fist iteration starts, the first element in the array is assigned to the variable. After the looping block is over, the variable takes the value of the next element and repeats the statements in the loop body till the elements in the array are exhausted.
foreach 循环的典型用法如下——
A typical use of foreach loop is as follows −
<?php
$arr = array(10, 20, 30, 40, 50);
foreach ($arr as $val) {
echo "$val \n";
}
?>
Example
PHP 提供了一个非常有用的 array_search() 函数,它返回给定值的键。由于索引本身是索引数组中的键,因此,每个 $val 的 array_search() 会返回每个值的基于零的索引。以下代码展示它是如何工作的——
PHP provides a very useful function in array_search() which returns the key of a given value. Since the index itself is the key in an indexed array, the array_search() for each $val returns the zero based index of each value. The following code demonstrates how it works −
<?php
$arr = array(10, 20, 30, 40, 50);
foreach ($arr as $val) {
$index = array_search($val, $arr);
echo "Element at index $index is $val \n";
}
?>
它将生成以下 output −
It will produce the following output −
Element at index 0 is 10
Element at index 1 is 20
Element at index 2 is 30
Element at index 3 is 40
Element at index 4 is 50
Example
foreach 语法的第二个变体将数组中的每个元素解压缩到两个变量中:一个用于 key ,一个用于 value 。
The second variation of foreach syntax unpacks each element in the array into two variables: one for the key and one for value.
由于在索引数组的情况下,索引本身充当键,因此 $k 变量会连续获取数组中每个元素的增量索引。
Since the index itself acts as the key in case of an indexed array, the $k variable successively takes the incrementing index of each element in the array.
<?php
$arr = array(10, 20, 30, 40, 50);
foreach ($arr as $k=>$v) {
echo "Key: $k => Val: $v \n";
}
?>
它将生成以下 output −
It will produce the following output −
Key: 0 => Val: 10
Key: 1 => Val: 20
Key: 2 => Val: 30
Key: 3 => Val: 40
Key: 4 => Val: 50
Iterating an Associative Array using "foreach" Loop
关联数组是由键值对收集的。若要迭代关联数组,第二种 foreach 语法变体是合适的。数组中的每个元素都解压缩到两个变量中,每个变量都获取键及其值。
An associative array is a collection of key-value pairs. To iterate through an associative array, the second variation of foreach syntax is suitable. Each element in the array is unpacked in two variables each taking up the value of key and its value.
Example
这里有一个使用 foreach 循环遍历各州及其对应首府的数组的示例。
Here is an example in which an array of states and their respective capitals is traversed using the foreach loop.
<?php
$capitals = array(
"Maharashtra"=>"Mumbai", "Telangana"=>"Hyderabad",
"UP"=>"Lucknow", "Tamilnadu"=>"Chennai"
);
foreach ($capitals as $k=>$v) {
echo "Capital of $k is $v \n";
}
?>
它将生成以下 output −
It will produce the following output −
Capital of Maharashtra is Mumbai
Capital of Telangana is Hyderabad
Capital of UP is Lucknow
Capital of Tamilnadu is Chennai
然而,你仍可以使用 foreach 陈述的第一个版本,其中仅数组中每个键值对的值存储在变量中。然后使用之前使用过的 array_search() 函数获取对应于该值的关键。
However, you can still use the first version of foreach statement, where only the value from each key-value pair in the array is stored in the variable. We then obtain the key corresponding to the value using the array_search() function that we had used before.
<?php
$capitals = array(
"Maharashtra"=>"Mumbai", "Telangana"=>"Hyderabad",
"UP"=>"Lucknow", "Tamilnadu"=>"Chennai"
);
foreach ($capitals as $pair) {
$cap = array_search($pair, $capitals);
echo "Capital of $cap is $capitals[$cap] \n";
}
?>
Iterating a 2D Array using "foreach" Loop
可以在 PHP 中声明多维数组,其中数组中的每个元素都是另一个数组本身。请注意外部数组和子数组都可以是索引数组或关联数组。
It is possible to declare a multi-dimensional array in PHP, wherein each element in an array is another array itself. Note that both the outer array as well as the sub-arry may be an indexed array or an associative array.
在下面的示例中,我们有一个二维数组,它可以称为数组或数组。我们需要嵌套循环以遍历嵌套数组结构,如下所示——
In the example below, we have a two-dimensional array, which can be called as an array or arrays. We need nested loops to traverse the nested array structure as follows −
<?php
$twoD = array(
array(1,2,3,4),
array("one", "two", "three", "four"),
array("one"=>1, "two"=>2, "three"=>3)
);
foreach ($twoD as $idx=>$arr) {
echo "Array no $idx \n";
foreach ($arr as $k=>$v) {
echo "$k => $v" . "\n";
}
echo "\n";
}
?>
它将生成以下 output −
It will produce the following output −
Array no 0
0 => 1
1 => 2
2 => 3
3 => 4
Array no 1
0 => one
1 => two
2 => three
3 => four
Array no 2
one => 1
two => 2
three => 3
PHP - While Loop
在 PHP 脚本中创建循环的最简单的办法是使用 while 结构。PHP 中 while 循环的语法与 C 语言中的类似。只要 while 语句中的布尔表达式为 true,循环体块就会被反复执行。
The easiest way to create a loop in a PHP script is with the while construct. The syntax of while loop in PHP is similar to that in C language. The loop body block will be repeatedly executed as long as the Boolean expression in the while statement is true.
以下流程图有助于理解 PHP 中 while 循环如何运行 −
The following flowchart helps in understanding how the while loop in PHP works −
表达式中的值在每次循环开始之前都会被检查。如果 while 表达式从一开始就评估为 false,那么循环甚至不会运行一次。即使表达式在执行块期间变成了 false,在迭代结束之前也不会停止执行。
The value of the expression is checked each time at the beginning of the loop. If the while expression evaluates to false from the very beginning, the loop won’t even be run once. Even if the expression becomes false during the execution of the block, the execution will not stop until the end of the iteration.
while 循环的语法可以这样表示 −
The syntax of while loop can be expressed as follows −
while (expr){
statements
}
Example
以下代码展示了一个 while 循环在 PHP 中如何工作的简单示例。变量 $x 在循环开始之前被初始化为 1。只要变量小于或等于 10,循环体就会被要求执行。循环体中的 echo 语句打印当前的迭代次数并增加 x 的值,这样条件最终会变成 false。
The following code shows a simple example of how the while loop works in PHP. The variable $x is initialized to 1 before the loop begins. The loop body is asked to execute as long as it is less than or equal to 10. The echo statement in the loop body prints the current iteration number, and increments the value of x, so that the condition will turn false eventually.
<?php
$x = 1;
while ($x<=10) {
echo "Iteration No. $x \n";
$x++;
}
?>
它将生成以下 output −
It will produce the following output −
Iteration No. 1
Iteration No. 2
Iteration No. 3
Iteration No. 4
Iteration No. 5
Iteration No. 6
Iteration No. 7
Iteration No. 8
Iteration No. 9
Iteration No. 10
请注意测试条件在每次迭代的开头都会被检查。即使条件在循环内变成了 false,执行也会继续到迭代的结尾。
Note that the test condition is checked at the beginning of each iteration. Even if the condition turns false inside the loop, the execution will not stop until the end of the iteration.
Example
在以下示例中,“x” 在每次迭代中都会增加 3。在第三次迭代中,“x” 变成 9。由于测试条件仍然为 true,所以下一轮会发生,“x” 变成 12。由于条件变为 false,所以循环会停止。
In the following example, "x" is incremented by 3 in each iteration. On the third iteration, "x" becomes 9. Since the test condition is still true, the next round takes place in which "x" becomes 12. As the condition turns false, the loop stops.
<?php
$x = 0;
while ($x<=10){
$x+=3;
echo "Iteration No. $x \n";
}
?>
它将生成以下 output −
It will produce the following output −
Iteration No. 3
Iteration No. 6
Iteration No. 9
Iteration No. 12
Example
不一定总是让循环变量增加。如果循环变量的初始值大于循环应该结束时的值,那么需要对其进行减少。
It is not always necessary to have the looping variable incrementing. If the initial value of the loop variable is greater than the value at which the loop is supposed to end, then it must be decremented.
<?php
$x = 5;
while ($x>0) {
echo "Iteration No. $x \n";
$x--;
}
?>
它将生成以下 output −
It will produce the following output −
Iteration No. 5
Iteration No. 4
Iteration No. 3
Iteration No. 2
Iteration No. 1
Iterating an Array with "while"
PHP 中的一个索引数组是一个元素集合,其中每个元素都通过一个从 0 开始的递增索引进行标识。
An indexed array in PHP is a collection of elements, each of which is identified by an incrementing index starting from 0.
你可以通过组成一个 while 循环来遍历一个数组,重复访问 xth 索引处的元素,直到“x”达到数组的长度。这里“x”是一个计数变量,在每次迭代中都会增加。我们还需要一个 count() 函数来返回数组的大小。
You can traverse an array by constituting a while loop by repeatedly accessing the element at the xth index till "x" reaches the length of the array. Here, "x" is a counter variable, incremented with each iteration. We also need a count() function that returns the size of the array.
Example
请看以下示例:
Take a look at the following example −
<?php
$numbers = array(10, 20, 30, 40, 50);
$size = count($numbers);
$x=0;
while ($x<$size) {
echo "Number at index $x is $numbers[$x] \n";
$x++;
}
?>
它将生成以下 output −
It will produce the following output −
Number at index 0 is 10
Number at index 1 is 20
Number at index 2 is 30
Number at index 3 is 40
Number at index 4 is 50
Nested "while" Loops
你可以在另一个 while 循环中加入一个 while 循环。外部和内部 while 循环都受两个独立变量控制,这些变量在每次迭代后都会增加。
You may include a while loop inside another while loop. Both the outer and inner while loops are controlled by two separate variables, incremented after each iteration.
Example
<?php
$i=1;
$j=1;
while ($i<=3){
while ($j<=3){
echo "i= $i j= $j \n";
$j++;
}
$j=1;
$i++;
}
?>
它将生成以下 output −
It will produce the following output −
i= 1 j= 1
i= 1 j= 2
i= 1 j= 3
i= 2 j= 1
i= 2 j= 2
i= 2 j= 3
i= 3 j= 1
i= 3 j= 2
i= 3 j= 3
要注意“j”是内部 while 循环的计数变量,在它获取所有值之后被重新初始化为 1,因此“i”的下一个值“j”又从 1 开始。
Note that "j" which is the counter variable for the inner while loop is re-initialized to 1 after it takes all the values so that for the next value of "i", "j" again starts from 1.
Traversing the Characters in a String
在 PHP 中,一个字符串可以被认为是有序的字符集合。因此,一个 while 循环,其中一个计数变量从“0”到字符串的长度,可以用来一次获取一个字符。
In PHP, a string can be considered as an indexed collection of characters. Hence, a while loop with a counter variable going from "0" to the length of string can be used to fetch one character at a time.
Example
以下示例统计给定字符串中的元音数量。我们使用 strlen() 来获得长度,使用 str_contains() 来检查字符是否为元音之一。
The following example counts number of vowels in a given string. We use strlen() to obtain the length and str_contains() to check if the character is one of the vowels.
<?php
$line = "PHP is a popular general-purpose scripting language that is especially suited to web development.";
$vowels="aeiou";
$size = strlen($line);
$i=0;
$count=0;
while ($i<$size){
if (str_contains($vowels, $line[$i])) {
$count++;
}
$i++;
}
echo "Number of vowels = $count";
?>
它将生成以下 output −
It will produce the following output −
Number of vowels = 32
Using the "endwhile" Statement
PHP 还允许你为 while 循环使用替代语法。不是在花括号中组合一个以上的语句,而是使用符号“:”(冒号)来标记条件后面的循环体,并在末尾使用 endwhile 语句。
PHP also lets you use an alternative syntax for the while loop. Instead of clubbing more than one statement in curly brackets, the loop body is marked with a ":" (colon) symbol after the condition and the endwhile statement at the end.
Example
<?php
$x = 1;
while ($x<=10):
echo "Iteration No. $x \n";
$x++;
endwhile;
?>
它将生成以下 output −
It will produce the following output −
Iteration No. 1
Iteration No. 2
Iteration No. 3
Iteration No. 4
Iteration No. 5
Iteration No. 6
Iteration No. 7
Iteration No. 8
Iteration No. 9
Iteration No. 10
请注意 endwhile 语句以分号结束。
Note that the endwhile statement ends with a semicolon.
PHP - Do…While Loop
"do…while" 循环是 PHP 中提供的另一种循环构造。此类型的循环类似于 while 循环,只是测试条件在每次迭代的末尾检查,而不是在新迭代的开始检查。
The "do…while" loop is another looping construct available in PHP. This type of loop is similar to the while loop, except that the test condition is checked at the end of each iteration rather than at the beginning of a new iteration.
while 循环在进入循环之前验证真条件,而在 "do…while" 循环中,真条件在重新进入循环之前得到验证。因此,无论真条件如何,"do…while" 循环都保证至少有一次迭代。
The while loop verifies the truth condition before entering the loop, whereas in "do…while" loop, the truth condition is verified before re entering the loop. As a result, the "do…while" loop is guaranteed to have at least one iteration irrespective of the truth condition.
下图通过使用两个比较流程图表示形式,展示了 "while" 循环和 "do…while" 循环的区别。
The following figure shows the difference in "while" loop and "do…while" loop by using a comparative flowchart representation of the two.
构成“do…while”循环的 syntax 与 C 语言中对应的部分类似。
The syntax for constituting a "do…while" loop is similar to its counterpart in C language.
do {
statements;
}
while (expression);
Example
这里是一个“do…while”循环的简单示例,它打印从 1 到 5 的迭代编号。
Here is a simple example of "do…while" loop that prints iteration numbers 1 to 5.
<?php
$i=1;
do{
echo "Iteration No: $i \n";
$i++;
}
while ($i<=5);
?>
它将生成以下 output −
It will produce the following output −
Iteration No: 1
Iteration No: 2
Iteration No: 3
Iteration No: 4
Iteration No: 5
Example
以下代码使用 while 循环,也会生成相同的输出 −
The following code uses a while loop and also generates the same output −
<?php
$i=1;
while ($i<=5){
echo "<h3>Iteration No: $i</h3>";
$i++;
}
?>
因此,可以说“do…while”和“while”循环表现得类似。然而,当计数器变量(在这种情况下为 $i )的初始值被设置为任何大于 while 关键字前面的括号中的测试表达式中使用的一个,就会体现出差异。
Hence, it can be said that the "do…while" and "while" loops behave similarly. However, the difference will be evident when the initial value of the counter variable (in this case $i) is set to any value more than the one used in the test expression in the parenthesis in front of the while keyword.
Example
在以下代码中,使用了两个循环 – while 和 “ do…while ”。 while 循环的计数器变量是 $i ,而“do…while”循环的计数器变量是 $j 。两者都被初始化为 10(任何大于 5 的值)。
In the following code, both the loops – while and "do…while" – are used. The counter variable for the while loop is $i and for "do…while" loop, it is $j. Both are initialized to 10 (any value greater than 5).
<?php
echo "while Loop \n";
$i=10;
while ($i<=5){
echo "Iteration No: $i \n";
$i++;
}
echo "do-while Loop \n";
$j=10;
do{
echo "Iteration No: $j \n";
$j++;
}
while ($j<=5);
?>
它将生成以下 output −
It will produce the following output −
while Loop
do - while Loop
Iteration No: 10
结果表明 while 循环不会执行任何迭代,因为条件在一开始就是假的( $i 被初始化为 10,这大于测试条件 $i⇐5 )。另一方面,“do…while”循环确实会进行第一次迭代,即使计数器变量 $j 被初始化为大于测试条件的值也是如此。
The result shows that the while loop doesn’t perform any iterations as the condition is false at the beginning itself ($i is initialized to 10, which is greater than the test condition $i⇐5). On the other hand, the "do…while" loop does undergo the first iteration even though the counter variable $j is initialized to a value greater than the test condition.
因此,我们可以推断出“do…while”循环至少可以保证一次迭代,因为测试条件是在循环块的末尾验证的。 while 循环可能不会执行任何迭代,因为在进入循环块之前验证了测试条件。
We can thus infer that the "do…while" loop guarantees at least one iteration because the test condition is verified at the end of looping block. The while loop may not do any iteration as the test condition is verified before entering the looping block.
另一个语法差异是“do…while”中的 while 语句以分号结尾。对于 while 循环,圆括号后面是一个大括号循环块。
Another syntactical difference is that the while statement in "do…while" terminates with semicolon. In case of while loop, the parenthesis is followed by a curly bracketed looping block.
除此之外,没有其他差异。可以互换使用这两种类型的循环。
Apart from these, there are no differences. One can use both these types of loops interchangeably.
Decrementing a "do…while" Loop
要设计一个带递减计数的“do…while”,请将计数器变量初始化为一个较大的值,在循环内使用递减运算符 (--) 以在每次迭代时减少计数器的值,并将 while 括号中的测试条件设置为在计数器大于所需最后值时运行循环。
To design a "do…while" with decrementing count, initialize the counter variable to a higher value, use decrement operator (--) inside the loop to reduce the value of the counter on each iteration, and set the test condition in the while parenthesis to run the loop till the counter is greater than the desired last value.
Traverse a String in Reverse Order
在 PHP 中,一个字符串可以被视为一个带有字符的索引数组。我们可以通过运行一个递减的“do…while”循环从尾端到开头依次提取和显示一个个字符,如下所示 −
In PHP, a string can be treated as an indexed array of characters. We can extract and display one character at a time from end to beginning by running a decrementing "do…while" loop as follows −
<?php
$string = "TutorialsPoint";
$j = strlen($string);
do{
$j--;
echo "Character at index $j : $string[$j] \n";
}
while ($j>=1);
?>
它将生成以下 output −
It will produce the following output −
Character at index 13 : t
Character at index 12 : n
Character at index 11 : i
Character at index 10 : o
Character at index 9 : P
Character at index 8 : s
Character at index 7 : l
Character at index 6 : a
Character at index 5 : i
Character at index 4 : r
Character at index 3 : o
Character at index 2 : t
Character at index 1 : u
Character at index 0 : T
Nested "do…while" Loops
就像 for 循环或 while 循环一样,你也可以编写嵌套的“do…while”循环。在以下示例中,上面的“do…while”循环使用 $i 计数迭代,里面的“do…while”循环递增 $j ,并且每次打印 $i*j 的乘积,从而打印出从 1 到 10 的乘法表。
Like the for loop or while loop, you can also write nested "do…while" loops. In the following example, the upper "do…while" loop counts the iteration with $i, the inner "do…while" loop increments $j and each time prints the product of $i*j, thereby printing the tables from 1 to 10.
<?php
$i=1;
$j=1;
do{
print "\n";
do{
$k = sprintf("%4u",$i*$j);
print "$k";
$j++;
}
while ($j<=10);
$j=1;
$i++;
}
while ($i<=10);
?>
它将生成以下 output −
It will produce the following output −
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
5 10 15 20 25 30 35 40 45 50
6 12 18 24 30 36 42 48 54 60
7 14 21 28 35 42 49 56 63 70
8 16 24 32 40 48 56 64 72 80
9 18 27 36 45 54 63 72 81 90
10 20 30 40 50 60 70 80 90 100
PHP - Break Statement
PHP 中的 break 语句和 continue 语句统称为“循环控制语句”。PHP 中的任何类型的循环( for 、 while 或 do-while )都被设计为按照所使用的测试条件运行一定次数的迭代。循环块中的 break 语句将程序流带到块外部,放弃可能剩余的其他迭代。
The break statement along with the continue statement in PHP are known as "loop control statements". Any type of loop (for, while or do-while) in PHP is designed to run for a certain number of iterations, as per the test condition used. The break statement inside the looping block takes the program flow outside the block, abandoning the rest of iterations that may be remaining.
break 语句通常有条件地使用。否则,循环将终止而不完成第一次迭代。
The break statement is normally used conditionally. Otherwise, the loop will terminate without completing the first iteration itself.
syntax 中 break 语句如下所示:
The syntax of break statement is as follows −
while(expr){
if (condition){
break;
}
}
以下 flowchart 说明 break 语句的工作原理:
The following flowchart explains how the break statement works −
Example
以下 PHP 代码展示了在循环中使用 break 的一个简单示例。 while 循环预期执行十次迭代。但是,当计数器超过 3 时,循环中的 break 语句终止循环。
The following PHP code is a simple example of using break in a loop. The while loop is expected to perform ten iterations. However, a break statement inside the loop terminates it when the counter exceeds 3.
<?php
$i = 1;
while ($i<=10){
echo "Iteration No. $i \n";
if ($i>=3){
break;
}
$i++;
}
?>
它将生成以下 output −
It will produce the following output −
Iteration No. 1
Iteration No. 2
Iteration No. 3
可以在 break 关键字的前面指定一个可选的数字参数。这在嵌套循环构造中特别有用。它会说明要中断多少个嵌套的包围结构。默认值为 1,只会中断直接的包围结构。
An optional numeric argument can be given in front of break keyword. It is especially useful in nested looping constructs. It tells how many nested enclosing structures are to be broken out of. The default value is 1, only the immediate enclosing structure is broken out of.
Example
以下示例包含三个嵌套循环:一个 for 循环,其中有一个 while 循环,而 while 循环又包含一个 do-while 循环。
The following example has three nested loops: a for loop inside which there is a while loop which in turn contains a do-while loop.
最里层的循环执行 break 。其前面的数字“2”会将控制从当前作用域传出至 for 循环,而不是直接的 while 循环。
The innermost loop executes the break. The number "2" in front of it takes the control out of the current scope into the for loop instead of the immediate while loop.
<?php
for ($x=1; $x<=3; $x++){
$y=1;
while ($y<=3){
$z=1;
do {
echo "x:$x y:$y z:$z \n";
if ($z==2){
break 2;
}
$z++;
}
while ($z<=3);
$z=1;
$y++;
}
}
?>
它将生成以下 output −
It will produce the following output −
x:1 y:1 z:1
x:1 y:1 z:2
x:2 y:1 z:1
x:2 y:1 z:2
x:3 y:1 z:1
x:3 y:1 z:2
请注意,每当“z”的值变为 2 时,程序就会中断“y”循环。因此,“y”的值始终为 1。
Note that each time the value of "z" becomes 2, the program breaks out of the "y" loop. Hence, the value of "y" is always 1.
PHP - Continue Statement
与 break 语句类似, continue 是 PHP 中的另一个“循环控制语句”。与 break 语句不同, continue 语句会跳过当前迭代,并在条件评估和下一次迭代开始时继续执行。
Like the break statement, continue is another "loop control statement" in PHP. Unlike the break statement, the continue statement skips the current iteration and continues execution at the condition evaluation and then the beginning of the next iteration.
continue 语句可以在任何类型的循环构造中使用,即 for, foreach, while 或 do-while 循环。与 break 类似, continue 关键字通常也以条件方式使用。
The continue statement can be used inside any type of looping constructs, i.e., for, foreach, while or do-while loops. Like break, the continue keyword is also normally used conditionally.
while(expr){
if (condition){
continue;
}
}
以下 flowchart 解释了 continue 语句的工作原理 −
The following flowchart explains how the continue statement works −
Example
以下是一个简单的示例,演示 continue 的用法。预计 for 循环将完成十次迭代。然而,每当计数器 id 为偶数时, continue 语句都会跳过迭代。
Given below is a simple example showing the use of continue. The for loop is expected to complete ten iterations. However, the continue statement skips the iteration whenever the counter id is divisible by 2.
<?php
for ($x=1; $x<=10; $x++){
if ($x%2==0){
continue;
}
echo "x = $x \n";
}
?>
它将生成以下 output −
It will produce the following output −
x = 1
x = 3
x = 5
x = 7
x = 9
Example
continue 语句接受一个可选的数字参数,告诉它应该跳到哪个嵌套循环的末尾。默认值为 1。
The continue statement accepts an optional numeric argument which tells it how many levels of enclosing loops it should skip to the end of. The default is 1.
<?php
for ($i=1; $i<=3; $i++){
for ($j=1; $j<=3; $j++){
for ($k=1; $k<=3; $k++){
if ($k>1){
continue 2;
}
print "i: $i j:$j k: $k\n";
}
}
}
?>
它将生成以下 output −
It will produce the following output −
i: 1 j:1 k: 1
i: 1 j:2 k: 1
i: 1 j:3 k: 1
i: 2 j:1 k: 1
i: 2 j:2 k: 1
i: 2 j:3 k: 1
i: 3 j:1 k: 1
i: 3 j:2 k: 1
i: 3 j:3 k: 1
内嵌 for 循环中的 continue 语句跳过了迭代 2 和 3,并直接跳转到中间循环。因此,输出显示“k”对于“i”和“k”变量的所有值都是 1。
The continue statement in the inner for loop skips the iterations 2 and 3 and directly jumps to the middle loop. Hence, the output shows "k" as 1 for all the values of "i" and "k" variables.
PHP - Arrays
数组是一个数据结构,它在一个变量中存储一个或多个数据值,它们之间存在某种关系。例如,如果你想存储一个班级里 10 个学生的成绩,那么你可以定义一个长度为 10 的数组,而不是定义 10 个不同的变量。
An array is a data structure that stores one or more data values having some relation among them, in a single variable. For example, if you want to store the marks of 10 students in a class, then instead of defining 10 different variables, it’s easy to define an array of 10 length.
PHP 中的数组的行为与 C 中的数组略有不同,因为 PHP 是一个动态类型语言,而 C 是一个静态类型语言。
Arrays in PHP behave a little differently than the arrays in C, as PHP is a dynamically typed language as against C which is a statically type language.
-
An array in PHP is an ordered map that associates values to keys.
-
A PHP array can be used to implement different data structures such as a stack, queue, list (vector), hash table, dictionary, etc.
-
The value part of an array element can be other arrays. This fact can be used to implement tree data structure and multidimensional arrays.
在 PHP 中声明数组有两种方法。一种是使用内置 array() 函数,另一种是使用更短的语法,其中数组元素放在方括号内。
There are two ways to declare an array in PHP. One is to use the built-in array() function, and the other is to use a shorter syntax where the array elements are put inside square brackets.
The array() Function
内置 array() 函数使用提供给它的参数并返回数组类型对象。一个或多个以逗号分隔的参数是数组中的元素。
The built-in array() function uses the parameters given to it and returns an object of array type. One or more comma-separated parameters are the elements in the array.
array(mixed ...$values): array
括号中的每个值可以是单个值(可能是数字、字符串、任何对象,甚至另一个数组),或键值对。键与其值之间的关联由“⇒”符号表示。
Each value in the parenthesis may be either a singular value (it may be a number, string, any object or even another array), or a key-value pair. The association between the key and its value is denoted by the "⇒" symbol.
Using Square Brackets [ ]
除了 array() 函数之外,还可以将以逗号分隔的数组元素置于方括号内以声明数组对象。在这种情况下,元素也可以是单个值、字符串或另一个数组。
Instead of the array() function, the comma-separated array elements may also be put inside the square brackets to declare an array object. In this case too, the elements may be singular values or a string or another array.
$arr1 = [10, "asd", 1.55, true];
$arr2 = ["one"=>1, "two"=>2, "three"=>3];
$arr3 = [ [10, 20, 30],
["Ten", "Twenty", "Thirty"],
["physics"=>70, "chemistry"=>80, "maths"=>90] ];
Types of Arrays in PHP
有三种不同种类的数组,并且每个数组值都使用一个 ID(称为数组索引)访问。
There are three different kind of arrays and each array value is accessed using an ID which is called the array index.
-
Indexed Array − An array which is a collection of values only is called an indexed array. Each value is identified by a positional index staring from "0". Values are stored and accessed in linear fashion.
-
Associative Array − If the array is a collection of key-value pairs, it is called as an associative array. The key component of the pair can be a number or a string, whereas the value part can be of any type. Associative arrays store the element values in association with key values rather than in a strict linear index order.
-
Multi Dimensional Array − If each value in either an indexed array or an associative array is an array itself, it is called a multi dimensional array. Values are accessed using multiple indices
NOTE - 内置数组函数在函数引用 PHP Array Functions 中给出。
NOTE − Built-in array functions is given in function reference PHP Array Functions
值得注意的是,PHP 内部将上述任何类型都视为关联数组本身。对于索引数组(其中每个值具有索引),则索引本身就是其键。var_dump() 函数揭示了这一事实。
It may be noted that PHP internally considers any of the above types as an associative array itself. In case of an indexed array, where each value has index, the index itself is its key. The var_dump() function reveals this fact.
Example
在此示例中, arr1 是一个索引数组。但是,var_dump()(显示任何对象的结构化信息)显示每个值都具有索引作为其键。
In this example, arr1 is an indexed array. However, var_dump()which displays the structured information of any object, shows that each value is having its index as its key.
<?php
$arr1 = [10, "asd", 1.55, true];
var_dump($arr1);
?>
它将生成以下 output −
It will produce the following output −
array(4) {
[0]=>
int(10)
[1]=>
string(3) "asd"
[2]=>
float(1.55)
[3]=>
bool(true)
}
Example
对于多维索引数组也适用相同的原则,其中数组中的每个值都是另一个数组。
The same principle applies to a multi-dimensional index array, where each value in an array is another array.
<?php
$arr1 = [
[10, 20, 30],
["Ten", "Twenty", "Thirty"],
[1.1, 2.2, 3.3]
];
var_dump($arr1);
?>
它将生成以下 output −
It will produce the following output −
array(3) {
[0]=>
array(3) {
[0]=>
int(10)
[1]=>
int(20)
[2]=>
int(30)
}
[1]=>
array(3) {
[0]=>
string(3) "Ten"
[1]=>
string(6) "Twenty"
[2]=>
string(6) "Thirty"
}
[2]=>
array(3) {
[0]=>
float(1.1)
[1]=>
float(2.2)
[2]=>
float(3.3)
}
}
Accessing the Array Elements
要从给定的数组访问任何元素,可以使用数组[键] 语法。
To access any element from a given array, you can use the array[key] syntax.
Example
对于索引数组,将索引放到方括号中,因为索引本身就是键。
For an indexed array, put the index inside the square bracket, as the index itself is anyway the key.
<?php
$arr1 = [10, 20, 30];
$arr2 = array("one"=>1, "two"=>2, "three"=>3);
var_dump($arr1[1]);
var_dump($arr2["two"]);
?>
它将生成以下 output −
It will produce the following output −
int(20)
int(2)
我们将在后续章节中更详细地探讨 PHP 数组的类型。
We shall explore the types of PHP arrays in more details in the subsequent chapters.
PHP - Indexed Array
在 PHP 中,数组元素可以是键值对的集合,也可以仅包含值。如果数组仅包含值,则称之为索引数组,因为每个元素都由一个递增的索引标识,从“0”开始。
In PHP, the array elements may be a collection of key-value pairs or it may contain values only. If the array consists of values only, it is said to be an indexed array, as each element is identified by an incrementing index, starting with "0".
可以使用 array() 函数或方括号语法在 PHP 中创建一个索引数组。
An indexed array in PHP may be created either by using the array() function or with the square bracket syntax.
$arr1 = array("a", 10, 9.99, true);
$arr2 = ["a", 10, 9.99, true];
数组中的每个元素都有一个位置索引,第一个元素的索引为“0”。var_dump() 函数揭示了这些数组的结构化信息,如下所示:
Each element in the array has a positional index, the first element being at index "0". The var_dump() function reveals the structured information of these arrays as −
array(4) {
[0]=>
string(1) "a"
[1]=>
int(10)
[2]=>
float(9.99)
[3]=>
bool(true)
}
我们可以使用索引遍历数组,获取给定索引处的值或修改元素的值。
We can use the index to traverse the array, fetch the value at a given index or modify the value of an element in place.
Traversing an Indexed Array in PHP
任何类型的 PHP 循环都可以用来遍历数组。如果我们想使用 for 或 while 循环,我们必须使用 count() 函数找到数组中的元素数量,并将其值用作计数 for 或 while 循环的测试条件。
Any type of PHP loop can be employed to traverse an array. If we want to use a for or while loop, we have to find the number of elements in the array with count() function and use its value as the test condition for the counted for or while loop.
Example
以下代码使用 for 循环列出索引数组中的所有元素。
The following code uses a for loop to list all the elements in an indexed array.
<?php
$numbers = array(10, 20, 30, 40, 50);
for ($i=0; $i<count($numbers); $i++){
echo "numbers[$i] = $numbers[$i] \n";
}
?>
它将生成以下 output −
It will produce the following output −
numbers[0] = 10
numbers[1] = 20
numbers[2] = 30
numbers[3] = 40
numbers[4] = 50
您还可以使用 while 或 do-while 循环遍历索引数组。同样,我们需要使用 count() 函数找到数组长度。
You can also use a while or do-while loop to traverse an indexed array. Here too, we need to find the array length with count() function.
Example
以下代码以相反的顺序遍历给定的索引数组:
The following code traverses the given indexed array in reverse order −
<?php
$numbers = array(10, 20, 30, 40, 50);
$i = count($numbers)-1;
while ($i>=0){
echo "numbers[$i] = $numbers[$i] \n";
$i--;
}
?>
它将生成以下 output −
It will produce the following output −
numbers[4] = 50
numbers[3] = 40
numbers[2] = 30
numbers[1] = 20
numbers[0] = 10
Accessing the Array Elements Using Index
使用 array[index] 语法,可以访问数组中的任何值。特定索引处的可以分配新值。从而对数组进行修改。
You can access any value from an array using the array[index] syntax. The value at a specific index may be assigned with a new value. The array is thus modified in place.
Example
以下程序从数组 $arr1 中获取值,并以相反的顺序将它们放在 $arr2 中,因此 $arr1 中的第 0 个位置的值将变为 $arr2 中的最后一个值。
The following program fetches the values from an array $arr1 and places them in $arr2 in the reverse order. So the value at 0th position in $arr1 becomes the last value in $arr2.
<?php
$arr1 = array(10, 20, 30, 40, 50);
$size = count($arr1);
for ($i=0; $i<$size; $i++){
$arr2[$size-$i-1] = $arr1[$i];
}
for ($i=0; $i<$size; $i++){
echo "arr1[$i] = $$arr1[$i] arr2[$i] = $$arr2[$i] \n";
}
?>
它将生成以下 output −
It will produce the following output −
arr1[0] = $10 arr2[0] = $50
arr1[1] = $20 arr2[1] = $40
arr1[2] = $30 arr2[2] = $30
arr1[3] = $40 arr2[3] = $20
arr1[4] = $50 arr2[4] = $10
Traversing an Indexed Array Using "foreach" Loop
您还可以使用 foreach 来遍历一个索引数组。看看下面的 example −
You can also use the foreach loop to iterate through an indexed array. Take a look at the following example −
<?php
$arr1 = [10, 20, 30, 40, 50];
foreach ($arr1 as $val){
echo "$val \n";
}
?>
它将生成以下 output −
It will produce the following output −
10
20
30
40
50
请注意,PHP 在内部将索引数组视为关联数组,其中索引被视为键。此事实可以通过数组的 var_dump() 输出得到验证。
Note that PHP internally treats the indexed array as an associative array, with the index being treated as the key. This fact can be verified by the var_dump() output of the array.
Example
我们可以使用 foreach 语法在 key 和 value 变量中解包一个索引数组的每个元素 −
We can unpack each element of an indexed array in the key and value variables with foreach syntax −
<?php
$arr1 = [10, 20, 30, 40, 50];
foreach ($arr1 as $key => $val){
echo "arr1[$key] = $val \n";
}
?>
它将生成以下 output −
It will produce the following output −
arr1[0] = 10
arr1[1] = 20
arr1[2] = 30
arr1[3] = 40
arr1[4] = 50
在 PHP 中,一个数组可能只是值和键值对的组合。PHP 只为没有键的值分配索引。
In PHP, an array may be a mix of only values and key-value pairs. PHP assigns the index only to the values without keys.
Example
在这个示例中,PHP 为数字分配了递增索引,跳过出现的键值对。
In this example, PHP assigns incrementing index to the numbers, skipping the key-value pair appearing in it.
<?php
$arr1 = [10, 20,
"vals" => ["ten", "twenty"],
30, 40, 50];
var_dump($arr1);
?>
它将生成以下 output −
It will produce the following output −
array(6) {
[0]=>
int(10)
[1]=>
int(20)
["vals"]=>
array(2) {
[0]=>
string(3) "ten"
[1]=>
string(6) "twenty"
}
[2]=>
int(30)
[3]=>
int(40)
[4]=>
int(50)
}
PHP - Associative Array
如果 PHP 数组中的每个元素都是键值对,则这样的数组称为 associative array 。在这类型数组中,每个值都由其关联的键而不是索引标识。
If each element in a PHP array is a key-value pair, such an array is called an associative array. In this type of array, each value is identified by its associated key and not an index.
-
Associative arrays are used to implement data structures such as dictionary, maps, trees, etc.
-
In PHP, the "⇒" symbol is used to establish association between a key and its value.
How to Declare an Associative Array in PHP?
可以使用两种声明数组的方法——array() 函数和方括号表示法。
Both the approaches of declaring an array – the array() function and the square bracket notation – can be used.
$arr1 = array(
"Maharashtra"=>"Mumbai",
"Telangana"=>"Hyderabad",
"UP"=>"Lucknow",
"Tamilnadu"=>"Chennai"
);
$arr2 = ["Maharashtra"=>"Mumbai",
"Telangana"=>"Hyderabad",
"UP"=>"Lucknow",
"Tamilnadu"=>"Chennai"];
如果我们调用 var_dump() 函数,则以上两个数组将显示相似的结构 −
If we call the var_dump() function, both the above arrays will show the similar structure −
array(4) {
["Maharashtra"]=>
string(6) "Mumbai"
["Telangana"]=>
string(9) "Hyderabad
["UP"]=>
string(7) "Lucknow"
["Tamilnadu"]=>
string(7) "Chennai"
}
关联数组中每个元素的 key 部分可以是任意数字(整数、浮点数或布尔值)或字符串。 value 部分可以是任意类型。但是,浮点数键会被强制转换为整数。因此,布尔值 true/false 用作“1”或“0”作为键。
The key part of each element in an associative array can be any number (integer, float or Boolean), or a string. The value part can be of any type. However, the float key is cast to an integer. So, a Boolean true/false is used as "1" or "0" as the key.
Example
请看以下示例:
Take a look at the following example −
<?php
$arr1 = array(
10=>"hello",
5.75=>"world",
-5=>"foo",
false=>"bar"
);
var_dump($arr1);
?>
它将生成以下 output −
It will produce the following output −
array(4) {
[10]=>
string(5) "hello"
[5]=>
string(5) "world"
[-5]=>
string(3) "foo"
[0]=>
string(3) "bar"
}
请注意,键 5.75 会四舍五入到 5,并且键“true”反映为“0”。如果同一个键在数组中出现多次,则最后出现的键值对将保留,从而舍弃键与较早值的关联。
Note that the key 5.75 gets rounded to 5, and the key "true" is reflected as "0". If the same key appears more than once in an array, the key-value pair that appears last will be retained, discarding the association of the key with earlier value.
PHP 在内部甚至将索引数组视为关联数组,其中索引实际上是值的键。这意味着 0 索引处的值具有等于“0”的键,依此类推。对索引数组执行 var_dump() 也会显示出 PHP 数组的这一特性。
PHP internally treats even an indexed array as an associative array, where the index is actually the key of the value. It means the value at the 0th index has a key equal to "0", and so on. A var_dump() on an indexed array also brings out this characteristics of a PHP array.
Iterating a PHP Associative Array
foreach 循环是遍历关联数组最简单的方法,并且是最好的方法,尽管也可以使用其他类型的循环方法。
A foreach loop is the easiest and ideal for iterating through an associative array, although any other type of loop can also be used with some maneuver.
Example
让我们看看 foreach 的循环实现,每个键值对都在两个变量中解包。
Let us look at the foreach loop implementation, with each key value pair unpacked in two variables.
<?php
$capitals = array(
"Maharashtra"=>"Mumbai",
"Telangana"=>"Hyderabad",
"UP"=>"Lucknow",
"Tamilnadu"=>"Chennai"
);
foreach ($capitals as $k=>$v) {
echo "Capital of $k is $v \n";
}
?>
它将生成以下 output −
It will produce the following output −
Capital of Maharashtra is Mumbai
Capital of Telangana is Hyderabad
Capital of UP is Lucknow
Capital of Tamilnadu is Chennai
还有另一种在 PHP 中使用 foreach 循环的方法,其中每个元素都存储在一个变量中。然后,我们可以使用 array_search() 分离键和值部分,并使用它们在循环中。
There is another way of using the foreach loop in PHP, where each element is stored in a variable. We can then separate the key and value parts using array_search() and use them in the loop body.
<?php
$capitals = array(
"Maharashtra"=>"Mumbai",
"Telangana"=>"Hyderabad",
"UP"=>"Lucknow",
"Tamilnadu"=>"Chennai"
);
foreach ($capitals as $pair) {
$cap = array_search($pair, $capitals);
echo "Capital of $cap is $capitals[$cap] \n";
}
?>
它将生成以下 output −
It will produce the following output −
Capital of Maharashtra is Mumbai
Capital of Telangana is Hyderabad
Capital of UP is Lucknow
Capital of Tamilnadu is Chennai
若要使用 for、 while 或 do-while 循环,我们首先必须获取所有键的数组(使用 array_keys()),查找大小并将其用作循环语法中的测试条件。
To use for, while or do-while loop, we have to first get the array of all the keys (use array_keys()), find the size and use it as the test condition in the loop syntax.
Example
以下是如何使用 for 循环来遍历关联数组 −
Here is how we can use a for loop to traverse an associative array −
<?php
$capitals = array(
"Maharashtra"=>"Mumbai",
"Telangana"=>"Hyderabad",
"UP"=>"Lucknow",
"Tamilnadu"=>"Chennai"
);
$keys=array_keys($capitals);
for ($i=0; $i<count($keys); $i++){
$cap = $keys[$i];
echo "Capital of $cap is $capitals[$cap] \n";
}
?>
它将生成以下 output −
It will produce the following output −
Capital of Maharashtra is Mumbai
Capital of Telangana is Hyderabad
Capital of UP is Lucknow
Capital of Tamilnadu is Chennai
Accessing the Value with its Key
在关联数组中,键是值而不是索引的标识符。因此,要获取与特定键关联的值,请使用 $arr[key] 语法。同样的语法也可以用于更新特定键的值。
In an associative array, the key is the identifier of value instead of index. Hence, to fetch value associated with a certain key, use $arr[key] syntax. The same can be used to update the value of a certain key.
Example
在以下代码中,声明了一个关联数组 $arr1 。创建了另一个数组 $arr2 ,使其储存 $arr1 中的每对元素,并且每个键的值都加倍。
In the following code, an associative array $arr1 is declared. Another array $arr2 is created such that it stores each pair from $arr1 with the value of each key being doubled.
<?php
$arr1 = array("a"=>10, "b"=>20, "c"=>30, "d"=>40);
foreach ($arr1 as $k=>$v){
$arr2[$k] = $v*2;
}
print_r($arr2);
?>
它将生成以下 output −
It will produce the following output −
Array
(
[a] => 20
[b] => 40
[c] => 60
[d] => 80
)
此处使用的 print_r() 函数以易于理解的人类可读形式显示存储在数组中的数据。
The print_r() function used here displays the data stored in the array in an easy to understand human readable form.
PHP - Multidimensional Array
多维数组是数组的数组。在 PHP 数组中,每个元素可以是另一个数组。如果数组由值或键值对组成,且值属于标量的单数类型,则它是一维数组。如果数组中的每个元素都是一个或多个标量值组成的数组,则它是二维数组。
A multidimensional array is an array of arrays. In a PHP array, each element can be another array. If the array consists of values or key-value pairs with values being of singular scalar types, it is a one-dimensional array. If each element in an array is an array of one or more scalar values, it is a two-dimensional array.
PHP 数组也可能是一个二维关联数组,其中外层数组的每个元素都是键值对,而该值又是另一个关联数组。
A PHP array may be a two-dimensional associative array also, where each element of the outer array is key-value pair, the value being another associative array.
# one dimensional indexed array
$arr = [10, 20, 30, 40];
# one dimensional associative array
$arr = ["key1"=> "val1", "key2" => "val2", "key3" => "val3"];
# two dimensional indexed array
$arr = [
[1,2,3,4],
[10, 20, 30, 40],
[100, 200, 300, 400]
];
# two dimensional associative array
$arr = [
"row1" => ["key11" => "val11", "key12" => "val12", "key13" => "val13"],
"row2" => ["key21" => "val21", "key22" => "val22", "key23" => "val23"],
"row3" => ["key31" => "val31", "key32" => "val32", "key33" => "val33"]
];
Iterating over a 2D Array
遍历二维数组中的所有元素需要两个嵌套的循环。 foreach 循环更适合数组遍历。二维数组类似于行和列中数据的表格形式表示。
Two nested loops will be needed to traverse all the elements in a 2D array. The foreach loop is more suitable for array traversal. A 2D array is like a tabular representation of data in rows and columns.
Example
以下示例展示了如何以表格形式重现二维数组 −
The following example shows how you can reproduce a 2D array in a tabular form −
<?php
$tbl = [
[1,2,3,4],
[10, 20, 30, 40],
[100, 200, 300, 400]
];
echo ("\n");
foreach ($tbl as $row){
foreach ($row as $elem){
$val = sprintf("%5d", $elem);
echo $val;
}
echo "\n";
}
?>
它将生成以下 output −
It will produce the following output −
1 2 3 4
10 20 30 40
100 200 300 400
Example
我们也可以用两个嵌套的 foreach 循环来遍历二维关联数组。在 row-key 和 row-value 变量中解包外层数组的每一行,并用内层 foreach 循环遍历每一行元素。
We can also employ two nested foreach loops to traverse a 2D associative array. Unpack each row of the outer array in row-key and row-value variables and traverse each row elements with the inner foreach loop.
<?php
$tbl = [
"row1" => ["key11" => "val11", "key12" => "val12", "key13" => "val13"],
"row2" => ["key21" => "val21", "key22" => "val22", "key23" => "val23"],
"row3" => ["key31" => "val31", "key32" => "val32", "key33" => "val33"]
];
echo ("\n");
foreach ($tbl as $rk=>$rv){
echo "$rk\n";
foreach ($rv as $k=>$v){
echo "$k => $v ";
}
echo "\n";
}
?>
它将生成以下 output −
It will produce the following output −
row1
key11 => val11 key12 => val12 key13 => val13
row2
key21 => val21 key22 => val22 key23 => val23
row3
key31 => val31 key32 => val32 key33 => val33
Accessing the Elements in a 2D Array
访问和修改数组中元素的 $arr[$key] 语法也可以扩展到二维数组。对于二维索引数组,可以使用表达式 “@ {s7}” 获取和分配第 i 行的第 j 个元素。
The $arr[$key] syntax of accessing and modifying an element in the array can be extended to a 2D array too. For a 2D indexed array, the jth element in the ith row can be fetched and assigned by using the expression "$arr[$i][$j]".
Example
<?php
$tbl = [[1,2,3,4], [10, 20, 30, 40], [100, 200, 300, 400]];
# prints number in index 2 of the row 2
print ("Value at [2], [2] :" . $tbl[2][2]);
?>
它将生成以下 output −
It will produce the following output −
Value at [2], [2] :300
类似地,可以将第 i 行和第 j 列的值设置为另一个值。
Similarly, the value at ith row and jth column may be set to another value.
$tbl[2][2] = 250;
Example
如果它是一个二维关联数组,我们需要使用所需列的行键和键值变量来访问或修改其值。
If it is a 2D associative array, we need to use the row key and key-value variables of the desired column to access or modify its value.
<?php
$tbl = [
"row1" => ["key11" => "val11", "key12" => "val12", "key13" => "val13"],
"row2" => ["key21" => "val21", "key22" => "val22", "key23" => "val23"],
"row3" => ["key31" => "val31", "key32" => "val32", "key33" => "val33"]
];
print "value at row2 - key22 is " . $tbl["row2"]["key22"];
?>
它将生成以下 output −
It will produce the following output −
value at row2 - key22 is val22
Multi-dimensional Array
在上面的示例中,我们有了一个数组,其中每个键的关联值是另一组键值对集合,我们称之为二维数组。该概念可以扩展到任意多个级别。例如,如果内层数组中的每个元素将其键与另一个数组关联,则它将变成一个三维数组。
In the above example, we had an array in which the associated value of each key was another collection of key-value pairs, and we call it as a 2D array. The concept can be extended to any number of levels. For example, if each element in the inner array associates its key to another array, it becomes a three-dimensional array.
以下是 example 三维数组的示例 −
Here is an example of a three-dimensional array −
$arr3D = [
[
[1, 0, 9],
[0, 5, 6],
[1, 0, 3]
],
[
[0, 4, 6],
[0, 0, 1],
[1, 2, 7]
],
];
Example
要遍历这样的三维数组,我们需要三个嵌套的 foreach 循环,如下所示 −
To traverse such a 3D array, we need three nested foreach loops, as shown below −
<?php
$arr3D = [
[[1, 0, 9],[0, 5, 6],[1, 0, 3]],
[[0, 4, 6],[0, 0, 1],[1, 2, 7]],
];
foreach ($arr3D as $arr) {
foreach ($arr as $row) {
foreach ($row as $element) {
echo "$element ";
}
echo "\n";
}
echo "\n";
}
?>
它将生成以下 output −
It will produce the following output −
1 0 9
0 5 6
1 0 3
0 4 6
0 0 1
1 2 7
然而,完全有可能声明一个扩展到任意维度的数组。为此,我们需要一个通用的解决方案来遍历任意维度的数组。
However, it is entirely possible to declare an array extending upto any number of dimensions. For that we need to have a generalized solution to traverse an array of any dimensions.
Recurve Traversal of Multidimensional Array
以下代码展示了一个递归函数,如果某个键的值是另一个数组,它会自己调用自己。如果我们向此函数传递任何数组作为参数,它将被遍历,显示其中的所有 k-v 对。
The following code shows a recursive function that calls itself if the value of a certain key is another array. If we pass any array as an argument to this function, it will be traversed, showing all the k-v pairs in it.
function showarray($arr) {
foreach ($arr as $k=>$v) {
if (is_array($v)) {
showarray($v);
} else {
echo "$k => $v ";
}
}
echo "\n";
}
Example
让我们将上面的三维数组 $arr3D 传递给它并查看结果 −
Let us pass the above 3D array $arr3D to it and see the result −
<?php
$arr3D = [
[[1, 0, 9],[0, 5, 6],[1, 0, 3]],
[[0, 4, 6],[0, 0, 1],[1, 2, 7]],
];
function showarray($arr){
foreach ($arr as $k=>$v){
if (is_array($v)){
showarray($v);
} else {
echo "$k => $v ";
}
}
echo "\n";
}
showarray($arr3D);
?>
它将生成以下 output −
It will produce the following output −
0 => 1 1 => 0 2 => 9
0 => 0 1 => 5 2 => 6
0 => 1 1 => 0 2 => 3
0 => 0 1 => 4 2 => 6
0 => 0 1 => 0 2 => 1
0 => 1 1 => 2 2 => 7
此递归函数可用于任何类型的数组,无论是索引数组还是关联数组,还可以用于任何维度。
This recursive function can be used with any type of array, whether indexed or associative, and of any dimension.
Example
让我们使用一个二维关联数组作为参数来展示 showarray() 函数 −
Let us use a 2D associative array as argument to showarray() function −
<?php
$tbl = [
"row1" => ["key11" => "val11", "key12" => "val12", "key13" => "val13"],
"row2" => ["key21" => "val21", "key22" => "val22", "key23" => "val23"],
"row3" => ["key31" => "val31", "key32" => "val32", "key33" => "val33"]
];
function showarray($arr){
foreach ($arr as $k=>$v){
if (is_array($v)){
showarray($v);
} else {
echo "$k => $v ";
}
}
echo "\n";
}
showarray($tbl);
?>
它将生成以下 output −
It will produce the following output −
key11 => val11 key12 => val12 key13 => val13
key21 => val21 key22 => val22 key23 => val23
key31 => val31 key32 => val32 key33 => val33
PHP Array Functions
PHP Array Functions 允许您通过各种方式与数组进行交互并操作数组。PHP 数组对于存储、管理和操作变量集至关重要。
PHP Array Functions allow you to interact with and manipulate arrays in various ways. PHP arrays are essential for storing, managing, and operating on sets of variables.
PHP 支持简单的和多维数组并且可以是用户创建的或由其他函数创建的。
PHP supports simple and multi-dimensional arrays and may be either user created or created by another function.
Installation
使用 PHP 数组函数不需要安装;它们是 PHP 核心的一部分,并附带标准 PHP 安装。
There is no installation needed to use PHP array functions; they are part of the PHP core and comes alongwith standard PHP installation.
Runtime Configuration
此扩展没有在 php.ini 中定义任何配置指令。
This extension has no configuration directives defined in php.ini.
PHP Array Functions
下表列出了所有与 PHP 数组相关的函数。此处列出的版本表示支持该函数的最早的 PHP 版本。
Following table lists down all the functions related to PHP Array. Here column version indicates the earliest version of PHP that supports the function.
Sr.No |
Function & Description |
Version |
1 |
array()Create an array |
4.2.0 |
2 |
array_change_key_case()Returns an array with all keys in lowercase or uppercase |
4.2.0 |
3 |
array_chunk()Splits an array into chunks of arrays |
4.2.0 |
3 |
array_column()Return the values from a single column in the input array |
5.5.0 |
4 |
array_combine()Creates an array by using one array for keys and another for its values |
5 |
5 |
array_count_values()Returns an array with the number of occurrences for each value |
4 |
6 |
array_diff()Compares array values, and returns the differences |
4 |
7 |
array_diff_assoc()Compares array keys and values, and returns the differences |
4 |
8 |
array_diff_key()Compares array keys, and returns the differences |
5 |
9 |
array_diff_uassoc()Compares array keys and values, with an additional user-made function check, and returns the differences |
5 |
10 |
array_diff_ukey()Compares array keys, with an additional user-made function check, and returns the differences |
5 |
11 |
array_fill()Fills an array with values |
4 |
12 |
array_fill_keys()Fill an array with values, specifying keys |
5 |
13 |
array_filter()Filters elements of an array using a user-made function |
4 |
14 |
array_flip()Exchanges all keys with their associated values in an array |
4 |
15 |
array_intersect()Compares array values, and returns the matches |
4 |
16 |
array_intersect_assoc()Compares array keys and values, and returns the matches |
4 |
17 |
array_intersect_key()Compares array keys, and returns the matches |
5 |
18 |
array_intersect_uassoc()Compares array keys and values, with an additional user-made function check, and returns the matches |
5 |
19 |
array_intersect_ukey()Compares array keys, with an additional user-made function check, and returns the matches |
5 |
20 |
array_key_exists()Checks if the specified key exists in the array |
4 |
21 |
array_keys()Returns all the keys of an array |
4 |
22 |
array_map()Sends each value of an array to a user-made function, which returns new values |
4 |
23 |
array_merge()Merges one or more arrays into one array |
4 |
24 |
array_merge_recursive()Merges one or more arrays into one array |
4 |
25 |
array_multisort()Sorts multiple or multi-dimensional arrays |
4 |
26 |
array_pad()Inserts a specified number of items, with a specified value, to an array |
4 |
27 |
array_pop()Deletes the last element of an array |
4 |
28 |
array_product()Calculates the product of the values in an array |
5 |
29 |
array_push()Inserts one or more elements to the end of an array |
4 |
30 |
array_rand()Returns one or more random keys from an array |
4 |
31 |
array_reduce()Returns an array as a string, using a user-defined function |
4 |
32 |
array_reverse()Returns an array in the reverse order |
4 |
33 |
array_search()Searches an array for a given value and returns the key |
4 |
34 |
array_shift()Removes the first element from an array, and returns the value of the removed element |
4 |
35 |
array_slice()Returns selected parts of an array |
4 |
36 |
array_splice()Removes and replaces specified elements of an array |
4 |
37 |
array_sum()Returns the sum of the values in an array |
4 |
38 |
array_udiff()Compares array values in a user-made function and returns an array |
5 |
39 |
array_udiff_assoc()Compares array keys, and compares array values in a user-made function, and returns an array |
5 |
40 |
array_udiff_uassoc()Compares array keys and array values in user-made functions, and returns an array |
5 |
41 |
array_uintersect()Compares array values in a user-made function and returns an array |
5 |
42 |
array_uintersect_assoc()Compares array keys, and compares array values in a user-made function, and returns an array |
5 |
43 |
array_uintersect_uassoc()Compares array keys and array values in user-made functions, and returns an array |
5 |
44 |
array_unique()Removes duplicate values from an array |
4 |
45 |
array_unshift()Adds one or more elements to the beginning of an array |
4 |
46 |
array_values()Returns all the values of an array |
4 |
47 |
array_walk()Applies a user function to every member of an array |
3 |
48 |
array_walk_recursive()Applies a user function recursively to every member of an array |
5 |
49 |
arsort()Sorts an array in reverse order and maintain index association |
3 |
50 |
asort()Sorts an array and maintain index association |
3 |
51 |
compact()Create array containing variables and their values |
4 |
52 |
count()Counts elements in an array, or properties in an object |
3 |
53 |
current()Returns the current element in an array |
3 |
54 |
each()Returns the current key and value pair from an array |
3 |
55 |
end()Sets the internal pointer of an array to its last element |
3 |
56 |
extract()Imports variables into the current symbol table from an array |
3 |
57 |
in_array()Checks if a specified value exists in an array |
4 |
58 |
key()Fetches a key from an array |
3 |
59 |
krsort()Sorts an array by key in reverse order |
3 |
60 |
ksort()Sorts an array by key |
3 |
61 |
list()Assigns variables as if they were an array |
3 |
62 |
natcasesort()Sorts an array using a case insensitive "natural order" algorithm |
4 |
63 |
natsort()Sorts an array using a "natural order" algorithm |
4 |
64 |
next()Advance the internal array pointer of an array |
3 |
65 |
pos()Alias of current() |
3 |
66 |
prev()Rewinds the internal array pointer |
3 |
67 |
range()Creates an array containing a range of elements |
3 |
68 |
reset()Sets the internal pointer of an array to its first element |
3 |
69 |
rsort()Sorts an array in reverse order |
3 |
70 |
shuffle()Shuffles an array |
3 |
71 |
sizeof()Alias of count() |
3 |
72 |
sort()Sorts an array |
3 |
73 |
uasort()Sorts an array with a user-defined function and maintain index association |
3 |
74 |
uksort()Sorts an array by keys using a user-defined function |
3 |
75 |
usort()Sorts an array by values using a user-defined function |
3 |
PHP Array Constants
Sr.No |
Constant & Description |
1 |
CASE_LOWER Used with array_change_key_case() to convert array keys to lower case |
2 |
CASE_UPPER Used with array_change_key_case() to convert array keys to upper case |
3 |
SORT_ASC Used with array_multisort() to sort in ascending order |
4 |
SORT_DESC Used with array_multisort() to sort in descending order |
5 |
SORT_REGULAR Used to compare items normally |
6 |
SORT_NUMERIC Used to compare items numerically |
7 |
SORT_STRING Used to compare items as strings |
8 |
SORT_LOCALE_STRING Used to compare items as strings, based on the current locale |
9 |
COUNT_NORMAL |
10 |
COUNT_RECURSIVE |
11 |
EXTR_OVERWRITE |
12 |
EXTR_SKIP |
13 |
EXTR_PREFIX_SAME |
14 |
EXTR_PREFIX_ALL |
15 |
EXTR_PREFIX_INVALID |
16 |
EXTR_PREFIX_IF_EXISTS |
17 |
EXTR_IF_EXISTS |
18 |
EXTR_REFS |
PHP - Constant Arrays
在 PHP 5.6 版本之前,无法声明常量数组。从 PHP 5.6 开始,可以使用“const”关键字声明常量数组。从 PHP 7 开始,常量数组也可以通过 define() 函数来形成。
It was not possible to declare a constant array before PHP version 5.6. From PHP 5.6 onwards, you can use the "const" keyword to declare a constant array. From PHP 7 onwards, constant arrays can also be formed with define() function.
常量数组是形成后无法修改的数组。与普通数组不同,它的标识符不以“$”符号开头。
A constant array is an array which cannot be modified after it has been formed. Unlike a normal array, its identifier doesn’t start with the "$" sign.
声明常量数组的较旧语法为:
The older syntax for declaring constant array is −
const ARR = array(val1, val2, val3);
Example
<?php
const FRUITS = array(
"Watermelon",
"Strawberries",
"Pomegranate",
"Blackberry",
);
var_dump(FRUITS);
?>
它将生成以下 output −
It will produce the following output −
array(4) {
[0]=>
string(10) "Watermelon"
[1]=>
string(12) "Strawberries"
[2]=>
string(11) "Pomegranate"
[3]=>
string(10) "Blackberry"
}
您还可以在 PHP 中使用传统的方括号语法来声明常量数组:
You can also use the conventional square bracket syntax to declar a constant array in PHP −
const FRUITS = [
"Watermelon",
"Strawberries",
"Pomegranate",
"Blackberry",
];
Example
无法修改常量数组中的任何元素。因此,以下代码会引发致命错误:
It is not possible to modify any element in a constant array. Hence, the following code throws a fatal error −
<?php
const FRUITS = [
"Watermelon",
"Strawberries",
"Pomegranate",
"Blackberry",
];
FRUITS[1] = "Mango";
?>
它将生成以下 output −
It will produce the following output −
PHP Fatal error: Cannot use temporary expression in write context
Constant Arrays PHP 7 Onwards
较新版本的 PHP 允许您通过 define() 函数来声明常量数组。
The newer versions of PHP allow you to declare a constant array with define() function.
<?php
define ('FRUITS', [
"Watermelon",
"Strawberries",
"Pomegranate",
"Blackberry",
]);
print_r(FRUITS);
?>
它将生成以下 output −
It will produce the following output −
Array
(
[0] => Watermelon
[1] => Strawberries
[2] => Pomegranate
[3] => Blackberry
)
您还可以使用 array() 函数在此声明常量数组。
You can also use the array() function to declare the constant array here.
define ('FRUITS', array(
"Watermelon",
"Strawberries",
"Pomegranate",
"Blackberry",
));
Example
也可以声明一个 associative constant array 。以下是一个示例:
It is also possible to declare an associative constant array. Here is an example −
<?php
define ('CAPITALS', array(
"Maharashtra" => "Mumbai",
"Telangana" => "Hyderabad",
"Gujarat" => "Gandhinagar",
"Bihar" => "Patna"
));
print_r(CAPITALS);
?>
它将生成以下 output −
It will produce the following output −
Array
(
[Maharashtra] => Mumbai
[Telangana] => Hyderabad
[Gujarat] => Gandhinagar
[Bihar] => Patna
)
PHP - Functions
与大多数编程语言类似,PHP 中的一个函数是一段有组织的可重用代码,用于执行一个相关的单一操作。函数为您的应用程序提供了更好的模块化和高程度的代码重用。
Like most of the programming languages, a function in PHP is a block of organized, reusable code that is used to perform a single, related action. Functions provide better modularity for your application and a high degree of code reuse.
PHP 通过通过定义独立的可重用函数块来安排处理逻辑,支持结构化编程方法。这种方法的主要优点是代码变得易于理解、开发和维护。
PHP supports a structured programming approach by arranging the processing logic by defining blocks of independent reusable functions. The main advantage of this approach is that the code becomes easy to follow, develop and maintain.
下图显示了如何将工资计算过程逐步细化为独立的可重用函数。
The following figure shows how the process of salary computation is successively broken down to independent and reusable functions.
Types of Functions
你已经见过了很多诸如 fopen() 和 fread() 等函数。它们是内置函数,但 PHP 也允许你创建你自己的函数。PHP 中有两种类型的函数−
You already have seen many functions like fopen() and fread() etc. They are built-in functions but PHP gives you option to create your own functions as well. There are two types of functions in PHP −
-
Built-in functions − PHP’s standard library contains a large number of built-in functions for string processing, file IO, mathematical computations and more.
-
User-defined functions − You can create user-defined functions too, specific to the requirements of the programming logic.
可以通过传递必要的数据(称为 parameters 或 arguments ),让一个函数从任何其他函数调用。被调用的函数将它的结果返回给调用环境。
A function may be invoked from any other function by passing required data (called parameters or arguments). The called function returns its result back to the calling environment.
这里有两部分需要明确 −
There are two parts which should be clear to you −
-
Creating a PHP Function
-
Calling a PHP Function
事实上,你几乎不需要创建你自己的 PHP 函数,因为已经为不同的区域创建了超过 1000 个内置库函数,你只需要根据你的需求调用它们。
In fact you hardly need to create your own PHP function because there are already more than 1000 built-in library functions created for different area and you just need to call them according to your requirement.
请参阅 PHP Function Reference ,了解一组有用的函数。
Please refer to PHP Function Reference for a complete set of useful functions.
User-defined Functions in PHP
创建你自己的 PHP 函数非常容易。我们从一个简单的示例开始,然后再详细阐述它的工作原理。假设你想创建一个 PHP 函数,当你在浏览器中调用它时,它会简单的写一条消息。
Its very easy to create your own PHP function. Let’s start with a simple example after which we will elaborate how it works. Suppose you want to create a PHP function which will simply write a simple message on your browser when you will call it.
Example
在这个示例中,我们创建了一个名为 writeMessage() 的函数,然后调用它来打印一条简单消息 −
In this example, we create a function called writeMessage() and then call it to print a simple message −
<?php
/* Defining a PHP Function */
function writeMessage() {
echo "You are really a nice person, Have a nice time!";
}
/* Calling a PHP Function */
writeMessage();
?>
它将生成以下 output −
It will produce the following output −
You are really a nice person, Have a nice time!
Creating a Function in PHP
现在,让我们详细了解这个过程。第一步是编写一个函数,然后你可以根据需要多次调用它。要创建一个新 function ,请使用 function 关键字,后跟你想使用的函数名称。在名称前面放上一个圆括号,它可能包含也可能不包含参数。在其后面放上一个用大括号分隔的语句块。此函数块中包含每次调用此函数时要执行的语句。
Now let’s understand the process in detail. The first step is to write a function and then you can call it as many times as required. To create a new function, use the function keyword, followed by the name of the function you may want to use. In front of the name, put a parenthesis, which may or may not contain arguments. It is followed by a block of statements delimited by curly brackets. This function block contains the statements to be executed every time the function is called.
定义函数的一般 syntax 如下:
The general syntax of defining a function is as follows −
function foo($arg_1, $arg_2, $arg_n) {
statements;
return $retval;
}
如果这个函数打算将某些结果返回给调用环境,则应在函数块中的最后一个语句中设置 return 语句。不必有 return 语句,因为即使没有它,程序流也会返回给调用者,尽管不会携带任何值。
If the function is intended to return some result back to the calling environment, there should be a return statement as the last statement in the function block. It is not mandatory to have a return statement, as even without it, the program flow goes back to the caller, albeit without carrying any value with it.
任何有效的 PHP 代码都可能显示在某个函数内部,甚至可以是其他函数和类定义。函数的名称必须遵循与用于形成变量名称相同的规则。它应以字母或下划线开头,后面可以跟任意数量的字母、数字或下划线。
Any valid PHP code may appear inside a function, even other functions and class definitions. Name of the function must follow the same rules as used to form the name of a variable. It should start with a letter or underscore, followed by any number of letters, numbers, or underscores.
下面是 PHP 中的一个简单函数。每当被调用时,它就会显示消息“你好,世界”。
Here is a simple function in PHP. Whenever called, it is expected to display the message "Hello World".
function sayhello() {
echo "Hello World";
}
Calling a Function in PHP
一旦定义了一个函数,就可以在 PHP 代码的任何地方多次调用它。请注意,函数不会自动调用。要调用某个函数,请在语句中使用它的名称;函数名称后面跟着一个分号。
Once a function is defined, it can be called any number of times, from anywhere in the PHP code. Note that a function will not be called automatically. To call the function, use its name in a statement; the name of the function followed by a semicolon.
<?php
# define a function
function sayhello(){
echo "Hello World";
}
# calling the function
sayhello();
?>
它将生成以下 output −
It will produce the following output −
Hello World
假设上述脚本 "hello.php" 出现在 PHP 服务器的文档根目录中,打开浏览器并输入 URL,如下 http://localhost/hello.php 。你应该在浏览器窗口中看到消息“你好,世界”。
Assuming that the above script "hello.php" is present in the document root folder of the PHP server, open the browser and enter the URL as http://localhost/hello.php. You should see the "Hello World" message in the browser window.
在此示例中,函数会被定义,而没有任何参数或返回值。在随后的章节中,我们将了解如何定义和传递参数,以及如何让一个函数返回一些值。还将详细解释 PHP 函数的一些高级特性,例如递归函数、按值或引用调用函数等。
In this example, the function is defined without any arguments or any return value. In the subsequent chapters, we shall learn about how to define and pass arguments, and how to make a function return some value. Also, some advanced features of PHP functions such as recursive functions, calling a function by value vs by reference, etc. will also be explained in detail.
PHP - Function Parameters
PHP 中的某个函数可能会被定义为接受一个或多个参数。在定义函数时,函数参数是函数名称前面圆括号内的表达式的逗号分隔列表。参数可以是任何标量类型(数字、字符串或布尔值)、数组、对象,甚至可以是另一个函数。
A function in PHP may be defined to accept one or more parameters. Function parameters are a comma-separated list of expressions inside the parenthesis in front of the function name while defining a function. A parameter may be of any scalar type (number, string or Boolean), an array, an object, or even another function.
function foo($arg_1, $arg_2, $arg_n) {
statements;
return $retval;
}
在函数体中,参数作为要处理的变量。因此,它们遵循与任何普通变量相同的命名约定,即它们应以 “$” 开头,可以包含字母、数字和下划线。
The arguments act as the variables to be processed inside the function body. Hence, they follow the same naming conventions as any normal variable, i.e., they should start with "$" and can contain alphabets, digits and underscore.
Note −定义的参数数量没有限制。
Note − There is no restriction on how many parameters can be defined.
当需要调用参数化的函数时,你必须确保传递给它的值与函数定义中的参数数量相同。
When a parameterized function needs to be called, you have to make sure that the same number of values as in the number of arguments in function’s definition, are passed to it.
foo(val1, val2, val_n);
使用参数定义的函数可以产生根据传递的值动态改变的结果。
A function defined with parameters can produce a result that changes dynamically depending on the passed values.
Example
以下代码包含一个带有两个参数的 addition() 函数定义,并显示这两个参数的和。运行时输出取决于传递给函数的两个值。
The following code contains the definition of addition() function with two parameters, and displays the addition of the two. The run-time output depends on the two values passed to the function.
<?php
function addition($first, $second) {
$result = $first+$second;
echo "First number: $first \n";
echo "Second number: $second \n";
echo "Addition: $result";
}
addition(10, 20);
$x=100;
$y=200;
addition($x, $y);
?>
它将生成以下 output −
It will produce the following output −
First number: 10
Second number: 20
Addition: 30
First number: 100
Second number: 200
Addition: 300
Formal and Actual Arguments
有时, argument 一词被用来表示 parameter 。实际上,这两个术语之间存在一定的差异。
Sometimes the term argument is used for parameter. Actually, the two terms have a certain difference.
-
A parameter refers to the variable used in function’s definition, whereas an argument refers to the value passed to the function while calling.
-
An argument may be a literal, a variable or an expression
-
The parameters in a function definition are also often called as formal arguments, and what is passed is called actual arguments.
-
The names of formal arguments and actual arguments need not be same. The value of the actual argument is assigned to the corresponding formal argument, from left to right order.
-
The number of formal arguments defined in the function and the number of actual arguments passed should be same.
Example
当实际实参数量少于形式实参数量时,PHP 会引发 ArgumentCountError 。但是,如果实际实参比形式实参多,额外的实际实参会被忽略。
PHP raises an ArgumentCountError when the number of actual arguments is less than formal arguments. However, the additional actual arguments are ignored if they are more than the formal arguments.
<?php
function addition($first, $second) {
$result = $first+$second;
echo "First number: $first \n";
echo "Second number: $second \n";
echo "Addition: $result \n";
}
# Actual arguments more than formal arguments
addition(10, 20, 30);
# Actual arguments fewer than formal arguments
$x=10;
$y=20;
addition($x);
?>
它将生成以下 output −
It will produce the following output −
First number: 10
Second number: 20
Addition: 30
PHP Fatal error: Uncaught ArgumentCountError: Too few arguments
to function addition(), 1 passed in /home/cg/root/20048/main.php
on line 16 and exactly 2 expected in /home/cg/root/20048/main.php:2
Arguments Type Mismatch
PHP 是一种动态类型语言,因此它在使用实际实参的值复制形式实参时不强制进行类型检查。然而,如果函数体内的任何语句尝试对不支持特定数据类型执行特定操作,PHP 会引发异常。
PHP is a dynamically typed language, hence it doesn’t enforce type checking when copying the value of an actual argument with a formal argument. However, if any statement inside the function body tries to perform an operation specific to a particular data type which doesn’t support it, PHP raises an exception.
在上面的 addition() 函数中,假定传递的是数值实参。如果传递字符串实参,PHP 不会有任何反对意见,但执行加法的语句会遇到异常,因为字符串类型未定义“+”运算。
In the addition() function above, it is assumed that numeric arguments are passed. PHP doesn’t have any objection if string arguments are passed, but the statement performing the addition encounters exception because the "+" operation is not defined for string type.
Example
请看以下示例:
Take a look at the following example −
<?php
function addition($first, $second) {
$result = $first+$second;
echo "First number: $first \n";
echo "Second number: $second \n";
echo "Addition: $result";
}
# Actual arguments are strings
$x="Hello";
$y="World";
addition($x, $y);
?>
它将生成以下 output −
It will produce the following output −
PHP Fatal error: Uncaught TypeError: Unsupported operand types: string + string in hello.php:5
但是,PHP 是一种弱类型语言。它会尝试尽可能将变量强制转换为兼容类型。因此,如果传递的值之一是数字的字符串表示,而第二个是数值变量,则 PHP 会将字符串变量强制转换为数值类型来执行加法运算。
However, PHP is a weakly typed language. It attempts to cast the variables into compatible type as far as possible. Hence, if one of the values passed is a string representation of a number and the second is a numeric variable, then PHP casts the string variable to numeric in order to perform the addition operation.
Example
请看以下示例:
Take a look at the following example −
<?php
function addition($first, $second) {
$result = $first+$second;
echo "First number: $first \n";
echo "Second number: $second \n";
echo "Addition: $result";
}
# Actual arguments are strings
$x="10";
$y=20;
addition($x, $y);
?>
它将生成以下 output −
It will produce the following output −
First number: 10
Second number: 20
Addition: 30
PHP - Call by Value
默认情况下,PHP 对传递给函数的参数使用“按值调用”机制。当一个函数被调用时,实际实参的值会被复制到函数定义的形式实参中。
By default, PHP uses the "call by value" mechanism for passing arguments to a function. When a function is called, the values of actual arguments are copied to the formal arguments of the function’s definition.
在函数体执行期间,如果任何 formal arguments 的值发生任何变化,它不会反映在 actual arguments 中。
During the execution of the function body, if there is any change in the value of any of the formal arguments, it is not reflected in the actual arguments.
-
Actual Arguments − The arguments that are passed in a function call.
-
Formal Arguments − The arguments that are declared in a function definition.
Example
让我们考虑以下代码中使用的函数−
Let us consider the function used in the code below −
<?php
function change_name($nm) {
echo "Initially the name is $nm \n";
$nm = $nm."_new";
echo "This function changes the name to $nm \n";
}
$name = "John";
echo "My name is $name \n";
change_name($name);
echo "My name is still $name";
?>
它将生成以下 output −
It will produce the following output −
My name is John
Initially the name is John
This function changes the name to John_new
My name is still John
在这个示例中, change_name() 函数将 _new 附加到它传入的字符串参数。然而,传入它的变量的值在函数执行之后仍然保持不变。
In this example, the change_name() function appends _new to the string argument passed to it. However, the value of the variable that was passed to it remains unchanged after the function’s execution.
事实上,形式参数充当函数的局部变量。此类变量只可以在其初始化的范围内访问。对于函数而言,用花括号 "{ }" 标记的主体就是其范围。此范围内任何变量都不可用于其外部的代码。因此,任何局部变量的处理都不会影响外部的世界。
Formal arguments, in fact, behave as local variables for the function. Such a variable is accessible only inside the scope in which it is initialized. For a function, its body marked by the curly brackets "{ }" is its scope. Any variable inside this scope is not available for the code outside it. Hence, manipulation of any local variable has no effect on the world outside.
“按值调用”方法适合使用传给它的值来执行计算的函数。它执行某些计算并返回结果,无需改变传入它的参数的值。
The "call by value" method is suitable for a function that uses the values passed to it. It performs certain computation and returns the result without having to change the value of parameters passed to it.
Note − 执行公式类型计算的任何函数都是按值调用的示例。
Note − Any function that performs a formula-type computation is an example of call by value.
Example
请看以下示例:
Take a look at the following example −
<?php
function addFunction($num1, $num2) {
$sum = $num1 + $num2;
return $sum;
}
$x = 10;
$y = 20;
$num = addFunction($x, $y);
echo "Sum of the two numbers is : $num";
?>
它将生成以下 output −
It will produce the following output −
Sum of the two numbers is : 30
Example
下面是通过按值传递参数来调用函数的另一个示例。该函数将接收到的数字增加 1,但这不会影响传入它的变量。
Here is another example of calling a function by passing the argument by value. The function increments the received number by 1, but that doesn’t affect the variable passed to it.
<?php
function increment($num) {
echo "The initial value: $num \n";
$num++;
echo "This function increments the number by 1 to $num \n";
}
$x = 10;
increment($x);
echo "Number has not changed: $x";
?>
它将生成以下 output −
It will produce the following output −
The initial value: 10
This function increments the number by 1 to 11
Number has not changed: 10
PHP 也支持在调用时将变量的引用传递给函数。我们将在下一章讨论它。
PHP also supports passing the reference of variables to the function while calling. We shall discuss it in the next chapter.
PHP - Call by Reference
PHP 默认使用“按值调用”机制来将参数传递给函数。如果函数内的参数改变,这些改变不会反映在函数外部。要允许函数修改其参数,必须使用“按引用调用”机制。
PHP uses the "call by value" mechanism, by default, for passing arguments to a function. If the arguments within the function are changed, the changes do not reflect outside of the function. To allow a function to modify its arguments, the "call by reference" mechanism must be used.
在 PHP 中,一个引用变量充当原始或寄主变量的“别名”,以便它们两个可以读写单个值。换句话说,两个不同名称的变量可以访问相同的值,它们的行为就好像它们是同一个变量。
In PHP, a reference variable acts as an "alias" to the original or host variable so that both of them can read and write a single value. In other words, variables of two different names can access to the same value and they behave as if they are the same variable.
以下 PHP 脚本将帮助你理解什么是引用。在此, $var 是一个普通字符串变量。我们将 $var1 声明为 $var 的引用,为后者附加“&”符号。
The following PHP script will help in understanding what references are. Here, $var is a normal string variable. We declare $var1 as a reference to $var, append "&" symbol to the latter.
$var = "Hello";
$var1 = &$var;
当我们说 $var1 是 $var 的别名或引用时,这意味着它值的任何更改也会更改 $var 的值,反之亦然。
When we say that $var1 is an alias or reference of $var, it means any change in its value will also change the value of $var, and vice versa.
Example
以下示例演示了 PHP 中“按引用调用”的工作方式 −
The following example demonstrates how "call by reference" works in PHP −
<?php
$var = "Hello";
$var1 = &$var;
$var1 = "Hello World";
echo "var=$var var1=$var1" . PHP_EOL;
$var = "How are you?";
echo "var=$var var1=$var1" . PHP_EOL;
?>
它将生成以下 output −
It will produce the following output −
var=Hello World var1=Hello World
var=How are you? var1=How are you?
Calling a PHP Function by Reference
要按引用调用函数,你需要声明由“&”符号作为前缀的名称形式参数。
To call a function by reference, you need to declare the formal arguments with name prefixed by "&" symbol.
function callref(&$arg1, &$arg2) {
Statements;
}
对函数的调用就像“按值调用”方法一样。
The call to the function is just as in "call by value" method.
callref($x, $y);
当调用函数时, $arg1 成为 $x 的引用, $arg2 成为 $y 的引用。
When the function is invoked, $arg1 becomes a reference to $x and $arg2 becomes a reference to $y.
如果在函数体内部, $arg1 或 $arg2 (或两者)的值改变,它还会导致 $x 和 $y 的值改变。
If, inside the function body, the value of $arg1 or $arg2 (or both) changes, it also causes the values of $x and $y to change.
Example
让我们来看看以下示例 −
Let us have a look at the following example −
<?php
function change_name(&$nm) {
echo "Initially the name is $nm" . PHP_EOL;
$nm = $nm."_new";
echo "This function changes the name to $nm" . PHP_EOL;
}
$name = "John";
echo "My name is $name" . PHP_EOL;
change_name($name);
echo "My name now is $name" . PHP_EOL;
?>
变量 $name 传递给函数 change_name() 。引用变量 &$nm 成为它的引用变量。 $nm 的任何更改都会反映在函数外的 $name 中。
The variable $name is passed to the function change_name(). A reference variable &$nm becomes its reference variable. Any change in $nm is reflected in $name outside the function.
它将生成以下 output −
It will produce the following output −
My name is John
Initially the name is John
This function changes the name to John_new
My name now is John_new
Swapping Two Variables
在以下 PHP 代码中,我们按值传递参数来调用函数。该函数尝试交换它们的值。
In the following PHP code, we call a function by passing the arguments by value. The function attempts to swap their values.
在函数内部,它们的值发生变化,但这不反映在函数执行后的实际参数中。
Inside the function, their values are changed, but this swap doesn’t reflect in the actual arguments after the execution of the function.
当使用引用方式传递参数来调用相同函数时,交换效果也会反映在实际参数中。
When the same function is called by passing the arguments by reference, the swap effect is reflected in the actual arguments as well.
<?php
function swap_value($a, $b) {
echo "Initial values a = $a b = $b \n";
$c = $a; $a = $b; $b = $c;
echo "Swapped values a = $a b = $b \n";
}
$x = 10; $y =20;
echo "Actual arguments x = $x y = $y \n\n";
swap_value($x, $y);
echo "Actual arguments do not change after the function: \n";
echo "x = $x y = $y \n\n";
function swap_ref(&$a, &$b) {
echo "Initial values a = $a b = $b \n";
$c = $a; $a = $b; $b = $c;
echo "Swapped values a = $a b = $b \n";
}
swap_ref($x, $y);
echo "Actual arguments get changed after the function: \n";
echo "x = $x y = $y";
?>
它将生成以下 output −
It will produce the following output −
Actual arguments x = 10 y = 20
Initial values a = 10 b = 20
Swapped values a = 20 b = 10
Actual arguments do not change after the function:
x = 10 y = 20
Initial values a = 10 b = 20
Swapped values a = 20 b = 10
Actual arguments get changed after the function:
x = 20 y = 10
Return by Reference
除了 PHP 中的函数能按引用接受参数,它还能返回引用。要定义返回引用的函数,请在函数名称前添加“&”符号。
Just as a function in PHP can accept arguments by reference, it can also return a reference. To define a function that returns a reference, prefix the name of the function by "&" symbol.
Example
下面的代码显示了返回引用的函数示例。它返回 $x ,这是 myfunction() 内部的 local 静态变量。由于在其前添加了“&”符号, $a (存储返回值的变量)成为 &x 的引用。因此,对 $a 中的任何更改也会更改 $x 的值。
The following code shows the example of a function returning a reference. It returns $x, which is a local static variable inside myfunction(). Since "&" symbol is prepended to it, $a (the variable that stores the return value) becomes a reference to &x. As a result, any change in $a will also change the value of $x.
<?php
function &myfunction(){
static $x=10;
echo "x Inside function: $x \n";
return $x;
}
$a=&myfunction();
echo "Returned by Reference: $a \n";
$a=$a+10;
$a=&myfunction();
?>
它将生成以下 output −
It will produce the following output −
x Inside function: 10
Returned by Reference: 10
x Inside function: 20
PHP - Default Arguments
类似于支持命令式编程的大多数语言,PHP 中的函数可以有一个或多个具有默认值的参数。因此,可以调用这样的函数而无需向其传递任何值。如果没有任何要传递的值,函数会获取其默认值进行处理。如果函数调用确实提供了值,则默认值将被覆盖。
Like most of the languages that support imperative programming, a function in PHP may have one or more arguments that have a default value. As a result, such a function may be called without passing any value to it. If there is no value meant to be passed, the function will take its default value for processing. If the function call does provide a value, the default value will be overridden.
function fun($arg1 = val1, $arg2 = val2) {
Statements;
}
可以采用不同方式调用这样的函数 −
Such a function can be called in different ways −
fun(); # Function will use defaults for both arguments
fun($x); # Function passes $x to arg1 and uses default for arg2
fun($x, $y); # Both arguments use the values passed
Example 1
在此,我们定义一个名为 greeting() 的函数,具有两个参数,两个参数都有 string 作为其默认值。我们通过传递一个字符串、两个字符串和无任何参数来调用它。
Here we define a function called greeting() with two arguments, both having string as their default values. We call it by passing one string, two strings and without any argument.
<?php
function greeting($arg1="Hello", $arg2="world") {
echo $arg1 . " ". $arg2 . PHP_EOL;
}
greeting();
greeting("Thank you");
greeting("Welcome", "back");
greeting("PHP");
?>
它将生成以下 output −
It will produce the following output −
Hello world
Thank you world
Welcome back
PHP world
Example 2
你可以定义一个仅具部分参数默认值且必须向其他参数传递值的函数。
You can define a function with only some of the arguments with default value, and the others to which the value must be passed.
<?php
function greeting($arg1, $arg2="World") {
echo $arg1 . " ". $arg2 . PHP_EOL;
}
# greeting(); ## This will raise ArgumentCountError
greeting("Thank you");
greeting("Welcome", "back");
?>
它将生成以下 output −
It will produce the following output −
Thank you World
Welcome back
第一个调用(无参数)引发 ArgumentCountError ,因为你必须为第一个参数传递值。如果仅传递一个值,则它将被列表中的第一个参数使用。
The first call (without argument) raises ArgumentCountError because you must pass value for the first argument. If only one value is passed, it will be used by the first argument in the list.
不过,如果你在无默认值的参数之前使用 default 声明参数,则只有在同时为两个参数传递值的情况下才能调用这样的函数。无法出现第一个参数使用默认值、第二个参数使用传递值的情况。
However, if you declare arguments with default before arguments without defaults, such function can be only called if values for both are passed. You cannot have a situation where the first argument uses the default, and the second using the passed value.
greeting() 函数现在具有默认 $arg1 和无默认 $arg2 值。
The greeting() function now has $arg1 with default and $arg2 without any default value.
function greeting($arg1="Hello", $arg2) {
echo $arg1 . " ". $arg2 . PHP_EOL;
}
如果你传递一个字符串“PHP”−
If you pass a string "PHP" −
greeting("PHP");
意图在于将结果打印为“Hello PHP”,则会显示以下错误消息。
with the intension to print the result as "Hello PHP", the following error message will be displayed.
PHP Fatal error: Uncaught ArgumentCountError: Too few arguments to function
greeting(), 1 passed in hello.php on line 10 and exactly 2 expected
Example 3
让我们定义函数 percent() 来计算三门科目的成绩百分比。
Let’s define a function percent() that calculates the percentage of marks in three subjects.
假设每门科目的成绩为 100 分,则函数定义中的 $total 参数的默认值为 300。
Assuming that the marks in each subject are out of 100, the $total argument in the function definition is given a default value as 300.
<?php
function percent($p, $c, $m, $ttl=300) {
$per = ($p+$c+$m)*100/$ttl;
echo "Marks obtained: \n";
echo "Physics = $p Chemistry = $c Maths = $m \n";
echo "Percentage = $per \n";
}
percent(50, 60, 70);
?>
它将生成以下 output −
It will produce the following output −
Marks obtained:
Physics = 50 Chemistry = 60 Maths = 70
Percentage = 60
不过,如果每门科目的最高分数为 50,则你必须向此函数传递第四个值,否则将根据 300 而不是 150 计算百分比。
However, if the maximum marks in each subject is 50, then you must pass the fourth value to the function, otherwise the percentage will be calculated out of 300 instead of 150.
<?php
function percent($p, $c, $m, $ttl=300) {
$per = ($p+$c+$m)*100/$ttl;
echo "Marks obtained: \n";
echo "Physics = $p Chemistry = $c Maths = $m \n";
echo "Percentage = $per \n";
}
percent(30, 35, 40, 150);
?>
它将生成以下 output −
It will produce the following output −
Marks obtained:
Physics = 30 Chemistry = 35 Maths = 40
Percentage = 70
PHP - Named Arguments
PHP 的 8.0 版本引入了命名参数功能。它是对调用函数时传递位置参数现有机制的扩展。
The feature of Named Arguments has been introduced in PHP with the version 8.0. It is an extension of the existing mechanism of passing positional arguments to a function while calling.
默认情况下,传递的参数值会复制到相同位置上的对应形式参数。PHP 中的此命名参数功能使得可以根据参数名称传递值,而不是根据位置传递。
By default, values of passed arguments are copied to the corresponding formal arguments at the same position. This feature of named arguments in PHP makes it possible to pass the value based on the parameter name instead of the position.
如果我们定义了一个函数,如下所示 −
If we have a function defined as follows −
function myfunction($x, $y) {
statement1;
statement2;
. . .
}
并且它被调用为 −
and it is called as −
myfunction(10, 20);
在此情况下,这些值按声明顺序传递给变量“x”和“y”。也就是说,第一个值传递给第一个参数,第二个值传递给第二个参数,以此类推。变量“x”和“y”是位置自变量。
In this case, the values are passed to the variables "x" and "y" in the order of declaration. It means, the first value to the first argument, second value to second argument and so on. The variables "x" and "y" are positional arguments.
要通过已命名自变量传递值,请指定要将参数传递给其值的变量名。参数的名称是形式参数的名称,但没有“$”符号。要传递的值放在冒号“:”符号的前面。
To pass the values by named arguments, specify the parameter name to which argument the value is to be passed. The name of the parameter is the name of formal argument without the "$" symbol. The value to be passed is put in front of the ":" symbol.
myfunction(x:10, y:20);
Example
这是一段展示了如何在 PHP 中使用 named arguments 的代码 −
Here is the code that demonstrates how you can use named arguments in PHP −
<?php
function myfunction($x, $y) {
echo "x = $x y = $y";
}
myfunction(x:10, y:20);
?>
它将生成以下 output −
It will produce the following output −
x = 10 y = 20
通过使用 named arguments ,可以按照任何顺序传递值,并且不一定要与参数在函数定义中声明的顺序相同。我们可以按如下所示调用 myfunction() ,它将生成相同的结果。
Using named arguments makes it possible to pass the values in any order, and not necessarily in the same order in which the arguments are declared in the function definition. We can call myfunction() as shown below and it will produce the same result.
myfunction(y:20, x:10);
借助此特性,自变量将独立于顺序并且可以自文档化。它还可以跳过带有默认值的自变量。
With this feature, the arguments become order-independent and self-documenting. It also makes it possible to skip the arguments with default values arbitrarily.
Combining Named Arguments with Positional Arguments
可以将已命名自变量与位置自变量结合使用,条件是已命名自变量必须位于位置自变量之后。
Named arguments can be combined with positional arguments, with the condition that, the named arguments must come after the positional arguments.
Example
<?php
function myfunction($x, $y, $z) {
echo "x = $x y = $y z = $z";
}
myfunction(10, z:20, y:30);
?>
它将生成以下 output −
It will produce the following output −
x = 10 y = 30 z = 20
但是,如果你尝试将 $z 作为位置自变量处理,
However, if you try to treat $z as a positional argument,
myfunction(x:10, y:20, 30);
在此情况下,PHP 将遇到以下 error −
In this case, PHP will encounter the following error −
PHP Fatal error: Cannot use positional argument after
named argument in hello.php on line 7
Passing Named Arguments from an Array
PHP 8.1.0 还引入了另一项特性,该特性允许在取消参数打包后使用已命名自变量。可以使用数组中的“…”(三个点),而不是单独向每个参数提供值,将数组中的值解包到对应的参数中。
PHP 8.1.0 also introduced another feature that allows using named argument after unpacking the arguments. Instead of providing values to each argument individually, the values in an array an be unpacked into the corresponding arguments, using "…" (three dots) before the array.
Example
<?php
function myfunction($x, $y, $z=30) {
echo "x = $x y = $y z = $z";
}
myfunction(...[10, 20], z:30);
?>
它将生成以下 output −
It will produce the following output −
x = 10 y = 20 z = 30
请注意,多次传递同一个参数将导致异常,如下所示 −
Note that passing the same parameter multiple times results in an exception as follows −
myfunction(x:10, z:20, x:20);
Error −
Error −
PHP Fatal error: Uncaught Error: Named parameter $x
overwrites previous argument in hello.php:7
PHP - Variable Arguments
在 PHP 中,可以编写一个能够接受具有可变数量元素的自变量列表的函数。要声明可变自变量列表,应在自变量名称前加上“…”(三个点)符号。所传递的值将收集到一个数组中,该数组名称是自变量的名称。
In PHP, it is possible to write a function capable of accepting a list of arguments with variable number of elements. To declare a variable argument list, the name of the argument is prepended by the "…" (three dots) symbol. The values passed are collected into an array with the argument’s name.
function myfunction(...$arg) {
Statement1;
Statement2;
}
要调用这样的函数,请在圆括号中添加任意数量用逗号分隔的值。
To call such a function, put any number of comma-separated values in the parenthesis.
myfunction(v1, v2, v3, . . . , vn);
在函数中声明的形式自变量是传递的所有值的一个数组。我们可以使用任何适当的内置数组函数来执行此过程。
The formal argument declared in the function is an array of all the values passed. We van use any of the appropriate built_in array functions to perform the process.
Example
在以下示例中,用户定义的函数 myfunction() 能够接收可变数量的值并找到它们的平均值。
In the following example, the user defined function myfunction() is capable of receiving variable number of values and finds their average.
<?php
function myfunction(...$numbers) {
$avg = array_sum($numbers)/count($numbers);
return $avg;
}
$avg = myfunction(5, 12, 9, 23, 8);
echo "average = $avg";
?>
它将生成以下 output −
It will produce the following output −
average = 11.4
尝试更改所传递数组的大小,并再次运行该程序。
Try changing the size of the passed array and run the program again.
可以在函数里面使用 foreach 循环来遍历数组。该函数可以在可变长度的自变量之前具有任何位置自变量。在所接收的值中,位置自变量将首先填充,而其他值将复制到数组中。
You can use a foreach loop to traverse the array inside the function. The function may have any positional arguments before the variable length argument. From the received values, the positional arguments will be populated first, leaving others to be copied to the array.
Example
<?php
function myfunction($x, ...$numbers) {
echo "First number: $x" . PHP_EOL;
echo "Remaining numbers: ";
foreach ($numbers as $n) {
echo "$n ";
}
}
myfunction(5, 12, 9, 23, 8, 41);
?>
它将生成以下 output −
It will produce the following output −
First number: 5
Remaining numbers: 12 9 23 8 41
Variadic Functions
即使没有“…”语法,也可以处理传递给函数的可变数量的自变量。PHP 具有内置的函数,例如 func_num_args()、func_get_arg() 和 func_get_args(),这些函数可以用类似的结果来使用。
It is possible to process a variable number of arguments to a function, even without the "…" syntax. PHP has built_in functions like func_num_args(), func_get_arg() and func_get_args(), which can be used with similar result.
-
func_num_args() − Returns the number of arguments passed to the function.
-
func_get_arg() − Returns an item from the argument list
-
func_get_args() − Returns an array comprising a function’s argument list
Example
上面可变参数的示例可以用以下函数重写:
The above example of variable arguments can be rewritten with these functions as below −
<?php
function myfunction() {
$sum = 0;
foreach (func_get_args() as $n) {
$sum += $n;
}
return $sum;
}
echo myfunction(5, 12, 9, 23, 8, 41);
?>
它将生成以下 output −
It will produce the following output −
98
Example
该程序输出传递给函数的所有数字:
This program prints all the numbers passed to the function −
<?php
function myfunction() {
$len = func_num_args();
echo "Numbers : ";
$i=0;
for ($i=0; $i<$len; $i++)
echo func_get_arg($i) . " ";
}
myfunction(5, 12, 9, 23, 8, 41);
?>
它将生成以下 output −
It will produce the following output −
Numbers : 5 12 9 23 8 41
PHP - Returning Values
PHP 函数的函数体中可以有一个可选的 return 语句作为其最后一个语句。PHP 中的大多数内置函数都返回一个特定值。例如,strlen() 函数返回字符串的长度。类似地,用户定义的函数也可以返回某个值。
A PHP function can have an optional return statement as the last statement in its function body. Most of the built-in functions in PHP return a certain value. For example the strlen() function returns the length of a string. Similarly, a user defined function can also return a certain value.
函数是一个独立、完整且可重复使用的语句块。调用时,它执行某个任务,并将程序控制权送回其被调用的位置,即使没有使用 return 语句也是如此。return 语句允许它将一个值与控制权一起带回调用环境。
A function is an independent, complete and reusable block of statements. When called, it performs a certain task and sends the program control back to position from where it was called even if return statement is not used. The return statement allows it to take a value, along with the control, back to the calling environment.
function foo($arg_1, $arg_2) {
statements;
return $retval;
}
函数可以返回任何类型的数据,包括标量变量、数组和对象。没有前面表达式的 return 关键字返回 null,并且等同于函数根本没有返回值。
A function may return any type of data, including scalar variables, arrays and objects. A return keyword without any expression in front of it returns null, and is equivalent to a function not having a return value at all.
函数返回的值可以存储在变量中,可以放入表达式中,或者如果出现在 print 或 echo 中,则显示在输出中。
The value returned by a function can be stored in a variable, can be put in an expression, or, if appearing inside print or echo, is displayed in the output.
$res = foo($x, $y);
它允许在程序中进一步使用函数的返回值。
It allows the returned value of a function to be used further in the program.
Example
让我们修改前一章中的 addition() 函数,以包含一个 return 语句来返回加法结果。
Let us modify the addition() function in the previous chapter to include a return statement to return the result of addition.
<?php
function addition($first, $second) {
$result = $first+$second;
return $result;
}
$x=10;
$y=20;
$z = addition($x, $y);
echo "First number: $x Second number: $y Addition: $z". PHP_EOL;
?>
它将生成以下 output −
It will produce the following output −
First number: 10 Second number: 20 Addition: 30
PHP 中的函数可以具有任意数量的参数,但只能返回一个值。该函数在首次遇到 return 语句时就返回调用环境,放弃函数体中其余的语句。
A function in PHP may have any number of arguments, but can return only one value. The function goes back to the calling environment as soon as it comes across a return statement for the first time, abandoning the rest of statements in the function body.
Example
如果您尝试在 return 语句中包含多个值,则会遇到以下 PHP 解析错误:
If you try to include more than one values in the return statement, a PHP parse error is encountered as below −
<?php
function raiseto($x) {
$sqr = $x**2;
$cub = $x**3;
return $sqr, $cub;
}
$a = 5;
$val = raiseto($a);
?>
它将生成以下 output −
It will produce the following output −
PHP Parse error: syntax error, unexpected token ",", expecting ";"
Conditional Return
可以在不同的条件语句下执行多个 return 语句。
You can have multiple return statements executed under different conditional statements.
Example
在以下程序中, raiseto() 函数返回索引参数(分别是 2 或 3)的一个数的平方或立方。
In the following program, the raiseto() function returns either the square or the cube of a number of the index argument, that is either 2 or 3, respectively.
<?php
function raiseto($x, $i) {
if ($i == 2) {
return $x**2;
} elseif ($i==3) {
return $x**3;
}
}
$a = 5;
$b = 2;
$val = raiseto($a, $b);
echo "$a raised to $b = $val" . PHP_EOL;
$x = 7;
$y = 3;
echo "$x raised to $y = " . raiseto($x, $y) . PHP_EOL;
?>
它将生成以下 output −
It will produce the following output −
5 raised to 2 = 25
7 raised to 3 = 343
Return Multiple Values as Array
PHP 中的函数只能返回一个值。不过,这个一个值可以是多个值构成的数组。我们可以利用此功能一次返回一个数的平方和立方。
The function in PHP is capable of returning only a single value. However, that single value can be an array of more than one values. We can take the advantage of this feature to return the square as well as the cube of a number at once.
Example
请看以下示例:
Take a look at the following example −
<?php
function raiseto($x){
$sqr = $x**2;
$cub = $x**3;
$ret = ["sqr" => $sqr, "cub" => $cub];
return $ret;
}
$a = 5;
$val = raiseto($a);
echo "Square of $a: " . $val["sqr"] . PHP_EOL;
echo "Cube of $a: " . $val["cub"] . PHP_EOL;
?>
它将生成以下 output −
It will produce the following output −
Square of 5: 25
Cube of 5: 125
PHP - Passing Functions
在 PHP 中,除了标量类型、数组和对象以外,您还可以将函数作为其参数之一传递给一个函数。如果定义一个函数来接受另一个函数作为参数,那么传递的函数将在该函数内部调用。PHP 的标准库具有某些此类型的内置函数,其中要传递的参数之一是一个函数,它可能是另一个内置函数,甚至是一个用户定义的函数。
To a function in PHP, in addition to scalar types, arrays, and objects, you can also pass a function as one of its arguments. If a function is defined to accept another function as an argument, the passed function will be invoked inside it. PHP’s standard library has certain built-in functions of this type, where one of the arguments to be passed is a function, which may be another built-in function or even a user defined function.
array_map
array_map() 是其中一个内置函数。此函数的第一个参数是一个回调函数。其他的参数可能是一个或多个数组。回调函数应用于数组的所有元素。
The array_map() is one of the built-in functions. The first argument to this function is a callback function. There may be one or more arrays as the other arguments. The callback function is applied to all the elements of arrays.
array_map(?callable $callback, array $array, array ...$arrays): array
array_map() 函数返回一个数组。它包含将回调函数应用于传递为其他参数的数组的相应元素后得到的结果。
The array_map() function returns an array. It contains the result of applying the callback function to the corresponding elements of arrays passed as other arguments.
Example
在以下示例中,我们有一个 square() 函数,用于计算传递给它的数字的平方。该函数又作为 array_map() 函数的参数使用,以及另一个数字数组。每个数字都先后传递给 square() 函数。结果数组是平方的列表。
In the following example, we have a square() function that computes the square of a number passed to it. This function in turn is used as an argument for array_map() function, along with another array of numbers. Each number is successively passed to the squares() function. The resultant array is a list of squares.
<?php
function square($number) {
return $number * $number;
}
$arr = [1, 2, 3, 4, 5];
$squares = array_map('square', $arr);
var_dump($squares);
?>
它将生成以下 output −
It will produce the following output −
array(5) {
[0]=>
int(1)
[1]=>
int(4)
[2]=>
int(9)
[3]=>
int(16)
[4]=>
int(25)
}
call_user_func
将函数传递给另一个函数的另一示例是 call_user_func()。顾名思义,它调用另一个用户定义的回调函数,并将其他参数传递到回调。
Another example of passing a function to another function is call_user_func(). As the name suggests, it calls another user defined callback function, and the other arguments are passed to the callback.
call_user_func(callable $callback, mixed ...$args): mixed
Example
在下面的示例中,square() 函数被反复调用,传递数组中的每个数字。
In the example below, the square() function is invoked repeatedly, passing each number in an array.
<?php
function square($number) {
return $number * $number;
}
$arr = [1, 2, 3, 4, 5];
foreach($arr as $a) {
echo "square of $a:" . call_user_func("square", $a). PHP_EOL;
}
?>
它将生成以下 output −
It will produce the following output −
square of 1:1
square of 2:4
square of 3:9
square of 4:16
square of 5:25
usort
作为函数传递的另一个示例,我们来看一下 usort() 函数。
As another example of passing function, we take a look a usort() function.
usort(array &$array, callable $callback): true
第一个参数是一个数组。数组将按照作为第二个参数的回调函数进行排序。
The first parameter is an array. The array is sorted as per the callback function, which is the second parameter.
回调参数是一个比较函数,如果第一个参数被认为分别小于、等于或大于第二个参数,则该函数必须返回一个小于零、等于零或大于零的整数。
The callback parameter is a comparison function must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second.
Example
这里有一个示例。我们首先有一个 mysort() 函数。如果第一个数字小于、等于或大于第二个数字,它将比较两个数字并返回“-1”、“0”或“1”。
Here is an example. First we have a mysort() function. It compares two numbers and returns "-1", "0" or "1" if the first number is less than, equal to or greater than second number.
usort() 的第一个参数是 mysort() 函数,第二个参数是一个数组。首先,前两个数字将传递给 mysort()。如果它返回 1,那么它们将被交换。接下来,第二个和第三个数字被传递,如果比较返回 1,则它们将被交换。相同的过程重复进行,以便按升序排列数组元素。
The first argument to usort() is the mysort() function, and the second one is an array. To begin with, the first two numbers are passed to mysort(). If it returns 1, they are swapped. Next, the second and third numbers are passed and swapped if the comparison returns 1. The same process repeats so that the array elements are arranged in ascending order.
<?php
function mysort($a, $b) {
if ($a == $b) {
return 0;
}
return ($a < $b) ? -1 : 1;
}
$a = array(3, 2, 5, 6, 1);
usort($a, "mysort");
foreach ($a as $key => $value) {
echo "$key: $value\n";
}
?>
它将生成以下 output −
It will produce the following output −
0: 1
1: 2
2: 3
3: 5
4: 6
Pass Callback to User-defined Function
除了上述的内置函数外,你还可以定义你自己接受其中一个参数作为另一个函数的函数。
Apart from the above built-in functions, you can define your own function that accepts one of the arguments as another function.
在下面的示例中,我们有两个函数, square() 和 cube() ,它们返回给定数字的平方和平方根。
In the example below, we have two functions, square() and cube(), that return the square and cube of a given number.
接下来,是 myfunction() ,它的第一个参数用作一个可变函数,第二个参数传递给 myfunction() 。
Next, there is myfunction(), whose first argument is used as a variable function and the second argument to myfunction() is passed to it.
因此,myfunction() 在内部调用 square() 或 cube() 来返回给定数字的平方或立方。
Thus, myfunction() internally calls square() or cube() to return either square or cube of a given number.
Example
<?php
function myfunction($function, $number) {
$result = $function($number);
return $result;
}
function cube($number) {
return $number ** 2;
}
function square($number) {
return $number ** 3;
}
$x = 5;
$cube = myfunction('cube', $x);
$square = myfunction('square', $x);
echo "Square of $x = $square" . PHP_EOL;
echo "Cube of $x = $cube" . PHP_EOL;
?>
它将生成以下 output −
It will produce the following output −
Square of 5 = 125
Cube of 5 = 25
PHP - Recursive Functions
递归函数是一种在满足特定条件之前不断调用自身的函数。在 PHP 中,可以定义递归函数。
A recursive function is such a function that calls itself until a certain condition is satisfied. In PHP, it is possible to defines a recursive function.
-
Recursion is used when a certain problem is defined in terms of itself.
-
Sometimes, it can be tedious to solve a problem using iterative approach. Recursive approach provides a very concise solution to seemingly complex problems.
-
Recursion in PHP is very similar to the one in C and C++.
-
Recursive functions are particularly used in traversing nested data structures, and searching or sorting algorithms.
-
Binary tree traversal, heap sort and finding shortest route are some of the cases where recursion is used.
Calculation of Factorial using Recursion
递归最流行的例子是阶乘的计算。数学上阶乘定义为 -
The most popular example of recursion is calculation of factorial. Mathematically factorial is defined as −
n! = n × (n-1)!
可以看出,我们使用阶乘本身来定义阶乘。因此,这是一个编写递归函数的合适情况。
It can be seen that we use factorial itself to define factorial. Hence, this is a fit case to write a recursive function.
让我们展开上述定义,计算 5 的阶乘值
Let us expand the above definition for calculation of factorial value of 5
5! = 5 × 4!
5 × 4 × 3!
5 × 4 × 3 × 2!
5 × 4 × 3 × 2 × 1!
5 × 4 × 3 × 2 × 1
= 120
虽然我们可以使用循环执行此计算,但其递归函数涉及通过递减数字依次调用它,直到数字变为 1。
While we can perform this calculation using a loop, its recursive function involves successively calling it by decrementing the number till it reaches 1.
Example
以下为计算阶乘的递归函数。
Following is the recursive function to calculate factorial.
<?php
function factorial ($n) {
if ($n == 1) {
echo $n . PHP_EOL;
return 1;
} else {
echo "$n * ";
return $n*factorial($n-1);
}
}
echo "Factorial of 5 = " . factorial(5);
?>
它将生成以下 output −
It will produce the following output −
5 * 4 * 3 * 2 * 1
Factorial of 5 = 120
Binary Search using Recursion
让我们看另一个理解递归工作原理的示例。眼前的问题是检查给定数字是否存在于列表中。
Let us have a look at another example to understand how recursion works. The problem at hand is to check whether a given number is present in a list.
虽然我们可以使用 for 循环并比较每个数字,对列表中的特定数字执行顺序查找,但顺序查找效率不高,尤其是在列表过大的情况下。在此,我们可以使用二分查找算法检查索引“high”是否大于索引“low”。根据“mid”变量中存在的值,再次调用该函数以搜索该元素。
While we can perform a sequential search for a certain number in the list using a for loop and comparing each number, the sequential search is not efficient especially if the list is too large. Here, we can use the binary search algorithm that checks if the index 'high' is greater than index 'low. Based on the value present at 'mid' variable, the function is called again to search for the element.
我们有一个数字列表,按升序排列。然后,找到列表的中点,并将检查限制在中点的左侧或右侧,具体取决于所需的数字是否小于或大于中点的数字。
We have a list of numbers, arranged in ascending order. Then, we find the midpoint of the list and restrict the checking to either left or right of midpoint depending on whether the desired number is less than or greater than the number at midpoint.
下图显示了二分查找的工作原理 -
The following diagram shows how binary search works −
Example
以下代码实现了递归二分查找技术 -
The following code implements the recursive binary searching technique −
<?php
function bsearch($my_list, $low, $high, $elem) {
if ($high >= $low) {
$mid = intval(($high + $low)/2);
if ($my_list[$mid] == $elem)
return $mid;
elseif ($my_list[$mid] > $elem)
return bsearch($my_list, $low, $mid - 1, $elem);
else
return bsearch($my_list, $mid + 1, $high, $elem);
}
else
return -1;
}
$list = [5,12,23, 45, 49, 67, 71, 77, 82];
$num = 67;
$result = bsearch($list,0,count($list)-1, $num);
if ($result != -1)
echo " Number $num found at index " . $result;
else
echo "Element not found!";
?>
它将生成以下 output −
It will produce the following output −
Number 67 found at index 5
你可以查看给定列表中存在的不同数字的输出,以及列表中不存在的数字的输出。
You can check the output for different numbers present in the given list, as well as those which are not present in the list.
PHP - Type Hints
PHP 支持在函数定义中的变量、类中属性或实例变量的声明时使用“类型提示”。PHP 被广泛认为是一种弱类型语言。在 PHP 中,在给变量赋值之前无需声明其类型。
PHP supports using "type hints" at the time of declaring variables in the function definition and properties or instance variables in a class. PHP is widely regarded as a weakly typed language. In PHP, you need not declare the type of a variable before assigning it any value.
PHP 解析器尽可能地尝试将变量转换为兼容类型。因此,如果传递的值之一是数字的字符串表示,而第二个是数字变量,PHP 会将字符串变量转换为数字以执行加法运算。
The PHP parser tries to cast the variables into compatible type as far as possible. Hence, if one of values passed is a string representation of a number, and the second is a numeric variable, PHP casts the string variable to numeric in order to perform the addition operation.
Example
请看以下示例:
Take a look at the following example −
<?php
function addition($x, $y) {
echo "First number: $x Second number: $y Addition: " . $x+$y;
}
$x="10";
$y=20;
addition($x, $y);
?>
它将生成以下 output −
It will produce the following output −
First number: 10 Second number: 20 Addition: 30
但是,如果上述示例中的 $x 是不包含有效数字表现形式的字符串,则会出现错误。
However, if $x in the above example is a string that doesn’t hold a valid numeric representation, then you would encounter an error.
<?php
function addition($x, $y) {
echo "First number: $x Second number: $y Addition: " . $x+$y;
}
$x="Hello";
$y=20;
addition($x, $y);
?>
它将生成以下 output −
It will produce the following output −
PHP Fatal error: Uncaught TypeError: Unsupported operand types: string + int in hello.php:5
类型提示从 PHP 5.6 版本起受支持。这意味着您可以明确说明代码中声明变量的预期类型。PHP 允许您对函数参数、返回值和类属性进行类型提示。有了它,就可以编写更健壮的代码。
Type-hinting is supported from PHP 5.6 version onwards. It means you can explicitly state the expected type of a variable declared in your code. PHP allows you to type-hint function arguments, return values, and class properties. With this, it is possible to write more robust code.
让我们在上述程序的加法函数中结合类型提示 −
Let us incorporate type-hinting in the addition function in the above program −
function addition($x, $y) {
echo "First number: $x Second number: $y Addition: " . $x+$y;
}
类型提示特性主要由 IDE(集成开发环境)用于提示用户在函数声明中使用的参数的预期类型。
The type-hinting feature is mostly used by IDEs (Integrated Development Environment) to prompt the user about the expected types of the parameters used in function declaration.
下图显示了 VS 代码编辑器在您键入时弹出的函数原型 −
The following figure shows the VS Code editor popping up the function prototype as you type −
如果光标悬停在函数名称上,则会显示参数和返回值的类型声明 −
If the cursor hovers on the name of the function, the type declarations for the parameters and the return value are displayed −
请注意,仅在变量声明中使用数据类型并不能防止引发不匹配的类型异常,因为 PHP 是一种动态类型语言。换句话说,$x="10" 和 $y=20 仍然会计算结果为 30,而 $x="Hello" 会使解析器引发错误。
Note that by merely using the data types in the variable declarations doesn’t prevent the unmatched type exception raised, as PHP is a dynamically typed language. In other words, $x="10" and $y=20 will still result in the addition as 30, where as $x="Hello" makes the parser raise the error.
strict_types
PHP 可以强制执行更严格的类型转换规则,这样就不会将“10”隐式转换为 10。这可以通过在 declare() 语句中将 strict_types 指令设置为 1 来强制执行。declare() 语句必须是 PHP 代码中的第一个语句,紧跟在“ <?php ”标签后面。
PHP can be made to impose stricter rules for type conversion, so that "10" is not implicitly converted to 10. This can be enforced by setting strict_types directive to 1 in a declare() statement. The declare() statement must be the first statement in the PHP code, just after the "<?php" tag.
Example
<?php
declare (strict_types=1);
function addition(int $x, int $y) {
echo "First number: $x Second number: $y Addition: " . $x+$y;
}
$x=10;
$y=20;
addition($x, $y);
?>
它将生成以下 output −
It will produce the following output −
First number: 10 Second number: 20 Addition: 30
现在,如果将 $x 设置为“10”,则不会发生隐式转换,从而导致以下错误 −
Now, if $x is set to "10", the implicit conversion wont take place, resulting in the following error −
PHP Fatal error: Uncaught TypeError: addition(): Argument #1 ($x) must be of type int, string given
VS Code IDE 也指示出相同的影响错误 −
The VS Code IDE too indicates the error of the same effect −
从 PHP 7 开始,类型提示支持已扩展到函数返回值,以防止意外的返回值。您可以通过在冒号 (:) 符号后添加预期类型,在参数列表后添加类型提示返回值。
From PHP 7 onwards with type-hinting support has been extended for function returns to prevent unexpected return values. You can type-hint return values by adding the intended type after the parameter list prefixed with a colon (:) symbol.
Example
让我们为上述加法函数的返回值添加一个类型提示 −
Let us add a type hint to the return value of the addition function above −
<?php
declare (strict_types=1);
function addition(int $x, int $y) : int {
return $x+$y;
}
$x=10;
$y=20;
$result = addition($x, $y);
echo "First number: $x Second number: $y Addition: " . $result;
?>
同样,如果发现该函数返回的不是整数,则 IDE 会在您运行之前指示原因。
Here too, if the function is found to return anything other than an integer, the IDE indicates the reason even before you run.
Union Types
PHP 在其 8.0 版本中引入了联合类型。你现在可以为单个声明指定多个类型。这些数据类型由“ | ”符号分隔。
The PHP introduced union types with its version 8.0. You can now specify more than one type for a single declaration. The data types are separated by the "|" symbol.
Example
在下面的 addition() 函数定义中, $x 和 $y 参数可以是 int 或 float 类型。
In the definition of addition() function below, the $x and $y arguments can be of int or float type.
<?php
declare (strict_types=1);
function addition(int|float $x, int|float $y) : float {
return $x+$y;
}
$x=10.55;
$y=20;
$result = addition($x, $y);
echo "First number: $x Second number: $y Addition: " . $result;
?>
Type-hinting in Class
在 PHP 7.4 版本中,您可以从版本 7.4 开始在声明类属性和方法时使用类型提示。
In PHP, from version 7.4 onwards, you can use the type hints in declaration of class properties and methods.
Example
在以下示例中,类构造函数使用类型提示 −
In the following example, the class constructor uses type hints −
<?php
declare (strict_types=1);
class Student {
public $name;
public $age;
public function __construct(string $name, int $age) {
$this->name = $name;
$this->age = $age;
}
public function dispStudent() {
echo "Name: $this->name Age: $this->age";
}
}
$s1 = new Student("Amar", 21);
$s1->dispStudent();
?>
也可以在声明类属性时使用类型提示。
It is also possible to use type hints in the declaration of class properties.
class Student {
public string $name;
public int $age;
public function __construct($name, $age) {
$this->name = $name;
$this->age = $age;
}
public function dispStudent() {
echo "Name: $this->name Age: $this->age";
}
}
程序开发中最常见的错误是 type errors 。类型提示功能有助于减少它们。
The most commonly encountered errors during program development are type errors. The type-hinting feature helps in reducing them.
PHP - Variable Scope
在 PHP 中,变量的作用域是其被定义并可访问的上下文的程度。通常,任何没有循环或函数等的简单顺序 PHP 脚本都只有一个作用域。在 "<?php" 和 "?>" 标记内声明的任何变量都从其定义的点起对整个程序都可用。
In PHP, the scope of a variable is the context within which it is defined and accessible to the extent in which it is accessible. Generally, a simple sequential PHP script that doesn’t have any loop or a function etc., has a single scope. Any variable declared inside the "<?php" and "?>" tag is available throughout the program from the point of definition onwards.
根据作用域,PHP 变量可以是以下三种类型中的任何一种 −
Based on the scope, a PHP variable can be any of these three types −
主脚本中的变量也可以通过 include 或 require 语句引入任何其他脚本。
A variable in a main script is also made available to any other script incorporated with include or require statements.
Example
在下面的示例中,“test.php”脚本被包含在主脚本中。
In the following example, a "test.php" script is included in the main script.
main.php
main.php
<?php
$var=100;
include "test.php";
?>
test.php
test.php
<?php
echo "value of \$var in test.php : " . $var;
?>
当主脚本执行时,它将显示以下 output −
When the main script is executed, it will display the following output −
value of $var in test.php : 100
但是,当脚本具有用户定义的函数时,所有内部变量都有一个局部作用域。因此,无法在外部访问在一个函数内定义的变量。在函数外部(上方)定义的变量具有全局作用域。
However, when the script has a user defined function, any variable inside has a local scope. As a result, a variable defined inside a function can’t be accessed outside. Variables defined outside (above) the function have a global scope.
Example
请看以下示例:
Take a look at the following example −
<?php
$var=100; // global variable
function myfunction() {
$var1="Hello"; // local variable
echo "var=$var var1=$var1" . PHP_EOL;
}
myfunction();
echo "var=$var var1=$var1" . PHP_EOL;
?>
它将生成以下 output −
It will produce the following output −
var= var1=Hello
var=100 var1=
PHP Warning: Undefined variable $var in /home/cg/root/64504/main.php on line 5
PHP Warning: Undefined variable $var1 in /home/cg/root/64504/main.php on line 8
Note 全局变量不会自动在函数的局部作用域内可用。此外,无法在外部访问函数内的变量。
Note that a global variable is not automatically available within the local scope of a function. Also, the variable inside a function is not accessible outside.
The "global" Keyword
要使全局变量能够在函数的局部作用域内访问,应该通过使用“ global ”关键字显式地完成。
To enable access to a global variable inside local scope of a function, it should be explicitly done by using the "global" keyword.
Example
PHP 脚本如下所示 −
The PHP script is as follows −
<?php
$a=10;
$b=20;
echo "Global variables before function call: a = $a b = $b" . PHP_EOL;
function myfunction() {
global $a, $b;
$c=($a+$b)/2;
echo "inside function a = $a b = $b c = $c" . PHP_EOL;
$a=$a+10;
}
myfunction();
echo "Variables after function call: a = $a b = $b c = $c";
?>
它将生成以下 output −
It will produce the following output −
Global variables before function call: a = 10 b = 20
inside function a = 10 b = 20 c = 15
Variables after function call: a = 20 b = 20 c =
PHP Warning: Undefined variable $c in /home/cg/root/48499/main.php on line 12
现在可以在函数内处理全局变量。此外,对函数内全局变量所做的任何更改都将在全局名称空间中反映出来。
Global variables can now be processed inside the function. Moreover, any changes made to the global variables inside the function will be reflected in the global namespace.
$GLOBALS Array
PHP 将所有全局变量存储在一个称为 $GLOBALS 的关联数组中。变量的名称和值组成键值对。
PHP stores all the global variables in an associative array called $GLOBALS. The name and value of the variables form the key-value pair.
Example
在以下 PHP 脚本中,$GLOBALS 数组用于访问全局变量 −
In the following PHP script, $GLOBALS array is used to access global variables −
<?php
$a=10;
$b=20;
echo "Global variables before function call: a = $a b = $b" . PHP_EOL;
function myfunction() {
$c=($GLOBALS['a']+$GLOBALS['b'])/2;
echo "c = $c" . PHP_EOL;
$GLOBALS['a']+=10;
}
myfunction();
echo "Variables after function call: a = $a b = $b c = $c";
?>
它将生成以下 output −
It will produce the following output −
Global variables before function call: a = 10 b = 20
c = 15
PHP Warning: Undefined variable $c in C:\xampp\htdocs\hello.php on line 12
Variables after function call: a = 20 b = 20 c =
Static Variable
使用 static 关键字定义的变量不会在每次调用函数时初始化。此外,它保留其上一次调用的值。
A variable defined with static keyword is not initialized at every call to the function. Moreover, it retains its value of the previous call.
Example
请看以下示例:
Take a look at the following example −
<?php
function myfunction() {
static $x=0;
echo "x = $x" . PHP_EOL;
$x++;
}
for ($i=1; $i<=3; $i++) {
echo "call to function :$i : ";
myfunction();
}
?>
它将生成以下 output −
It will produce the following output −
call to function :1 : x = 0
call to function :2 : x = 1
call to function :3 : x = 2
PHP - Strict Typing
PHP 通常被认为是一种弱类型语言。在 PHP 中,不需要在将值分配给变量之前声明变量的类型。PHP 解析器会尽力将变量转换为兼容的类型。
PHP is widely regarded as a weakly typed language. In PHP, you need not declare the type of a variable before assigning it any value. The PHP parser tries to cast the variables into compatible type as far as possible.
例如,如果传递的值之一是数字的字符串表示,而第二个是数字变量,PHP 会将字符串变量转换为数字来执行加法运算。
For example, if one of the values passed is a string representation of a number, and the second is a numeric variable, PHP casts the string variable to numeric in order to perform the addition operation.
Example
请看以下示例:
Take a look at the following example −
<?php
function addition($x, $y) {
echo "First number: $x Second number: $y Addition: " . $x+$y;
}
$x="10";
$y=20;
addition($x, $y);
?>
它将生成以下 output −
It will produce the following output −
First number: 10 Second number: 20 Addition: 30
但是,如果上述示例中的 $x 是不包含有效数字表示的字符串,则会出现错误。
However, if $x in the above example is a string that doesn’t hold a valid numeric representation, then you will encounter an error.
<?php
function addition($x, $y) {
echo "First number: $x Second number: $y Addition: " . $x+$y;
}
$x="Hello";
$y=20;
addition($x, $y);
?>
它将生成以下 output −
It will produce the following output −
PHP Fatal error: Uncaught TypeError: Unsupported operand
types: string + int in hello.php:5
Type Hints
类型提示从 PHP 5.6 版本起受支持。这意味着您可以明确说明代码中声明变量的预期类型。PHP 允许您对函数参数、返回值和类属性进行类型提示。有了它,就可以编写更健壮的代码。
Type-hinting is supported from PHP 5.6 version onwards. It means you can explicitly state the expected type of a variable declared in your code. PHP allows you to type-hint function arguments, return values, and class properties. With this, it is possible to write more robust code.
让我们在上述程序的加法函数中结合类型提示 −
Let us incorporate type-hinting in the addition function in the above program −
function addition(int $x, int $y) {
echo "First number: $x Second number: $y Addition: " . $x+$y;
}
请注意,仅仅在变量声明中使用数据类型并不能防止出现不匹配的类型异常,因为 PHP 是一种动态类型语言。换而言之,$x=”10”和$y=20 仍然会导致加法结果为 30,而$x=”Hello”会让解析器发出错误。
Note that by merely using the data types in the variable declarations doesn’t prevent the unmatched type exception raised, as PHP is a dynamically typed language. In other words, $x="10" and $y=20 will still result in the addition as 30, whereas $x="Hello" makes the parser raise the error.
Example
<?php
function addition($x, $y) {
echo "First number: $x \n";
echo "Second number: $y \n";
echo "Addition: " . $x+$y . "\n\n";
}
$x=10;
$y=20;
addition($x, $y);
$x="10";
$y=20;
addition($x, $y);
$x="Hello";
$y=20;
addition($x, $y);
?>
它将生成以下 output −
It will produce the following output −
First number: 10
Second number: 20
Addition: 30
First number: 10
Second number: 20
Addition: 30
First number: Hello
Second number: 20
PHP Fatal error: Uncaught TypeError: Unsupported operand
types: string + int in hello.php:5
strict_types
可以使 PHP 强制实施更严格的类型转换规则,这样就不会将“10”隐式转换为 10。这可以通过在 declare() 语句中将 strict_types 指令设置为 1 来强制执行。
PHP can be made to impose stricter rules for type conversion, so that "10" is not implicitly converted to 10. This can be enforced by setting strict_types directive to 1 in a declare() statement.
declare() 语句必须是 PHP 代码中的第一条语句,紧挨在“<?php”标签后面。
The declare() statement must be the first statement in the PHP code, just after the "<?php" tag.
Example
请看以下示例:
Take a look at the following example −
<?php
declare (strict_types=1);
function addition(int $x, int $y) {
echo "First number: $x Second number: $y Addition: " . $x+$y;
}
$x=10;
$y=20;
addition($x, $y);
?>
它将生成以下 output −
It will produce the following output −
First number: 10 Second number: 20 Addition: 30
现在,如果将 $x 设置为“10”,则不会执行隐式强制转换,导致以下 error −
Now, if $x is set to "10", the implicit conversion won’t take place, resulting in the following error −
PHP Fatal error: Uncaught TypeError: addition(): Argument #1
($x) must be of type int, string given
从 PHP 7 开始,已对函数返回类型提示支持进行了扩展以防止意外返回值。您可以在参数列表后添加带冒号 (:) 符号前缀的目标类型,从而对返回值进行类型提示。
From PHP 7 onwards, type-hinting support has been extended for function returns to prevent unexpected return values. You can type-hint the return values by adding the intended type after the parameter list prefixed with a colon (:) symbol.
Example
我们为以下 division() 函数的返回值添加类型提示。
Let us add a type hint to the return value of the division() function below.
<?php
declare (strict_types=1);
function division(int $x, int $y) : int {
return $x/$y;
}
$x=10;
$y=20;
$result = division($x, $y);
echo "First number: $x Second number: $y Addition: " . $result;
?>
因为该函数返回 0.5,而这并非 int 类型(也就是函数返回值所使用的类型提示),所以会显示以下 error −
Because the function returns 0.5, which is not of int type (that is, the type hint used for the return value of the function), the following error is displayed −
Fatal error: Uncaught TypeError: division(): Return value must be
of type int, float returned in hello.php:5
PHP - Anonymous Functions
What are Anonymous Functions?
PHP 允许定义匿名函数。通常,当我们在 PHP 中定义一个函数时,我们会为其提供一个名称,该名称用于在需要时调用该函数。相比之下, anonymous function 是一个在定义时没有任何指定名称的函数。此类函数也称为 closure 或 lambda function 。
PHP allows defining anonymous functions. Normally, when we define a function in PHP, we usually provide it a name which is used to call the function whenever required. In contrast, an anonymous function is a function that doesn’t have any name specified at the time of definition. Such a function is also called closure or lambda function.
有时,您可能只想使用一次函数。匿名函数最常见的一个用法是创建一个 inline callback function 。
Sometimes, you may want a function for one time use only. The most common use of anonymous functions is to create an inline callback function.
匿名函数是使用 Closure 类实现的。闭包是一个匿名函数,它围绕其定义的环境进行闭合。
Anonymous functions are implemented using the Closure class. Closure is an anonymous function that closes over the environment in which it is defined.
用于定义匿名函数的 syntax 如下 −
The syntax for defining an anonymous function is as follows −
$var=function ($arg1, $arg2) { return $val; };
请注意, function 关键字与左括号之间没有函数名,并且函数定义后有一个分号。这意味着匿名函数定义是表达式。当分配给变量时,可以使用变量名稍后调用匿名函数。
Note that there is no function name between the function keyword and the opening parenthesis, and the fact that there is a semicolon after the function definition. This implies that anonymous function definitions are expressions. When assigned to a variable, the anonymous function can be called later using the variable’s name.
Anonymous Function as a Callback
匿名函数通常用作回调。回调函数用作另一个函数的参数之一。匿名函数即时执行,其返回值成为父函数的参数,父函数可以是内置函数,也可以是用户定义的函数。
Anonymous functions are often used as callbacks. Callback functions are used as one of the arguments of another function. An anonymous function is executed on the fly and its return value becomes the argument of the parent function, which may be either a built-in or a user-defined function.
Example
在此示例中,我们在 usort() 函数中使用匿名函数,这是一个使用用户定义的比较函数按值对数组进行排序的内置函数。
In this example, we use an anonymous function inside the usort() function, a built in function that sorts an array by values using a user-defined comparison function.
<?php
$arr = [10,3,70,21,54];
usort ($arr, function ($x , $y) {
return $x > $y;
});
foreach ($arr as $x){
echo $x . "\n";
}
?>
它将生成以下 output −
It will produce the following output −
3
10
21
54
70
Example
以下示例使用匿名函数计算数组中连续数字之后的累积和。在此,我们使用 array_walk() 函数。此函数将用户定义的函数应用于数组中的每个元素。
The following example uses an anonymous function to calculate the cumulative sum after successive numbers in an array. Here, we use the array_walk() function. This function applies a user defined function to each element in the array.
<?php
$arr=array(1,2,3,4,5);
array_walk($arr, function($n){
$s=0;
for($i=1;$i<=$n;$i++){
$s+=$i;
}
echo "Number: $n Sum: $s". PHP_EOL;
});
?>
它将生成以下 output −
It will produce the following output −
Number: 1 Sum: 1
Number: 2 Sum: 3
Number: 3 Sum: 6
Number: 4 Sum: 10
Number: 5 Sum: 15
Anonymous Function as Closure
闭包也是一个匿名函数,可以在 " use " 关键字的帮助下访问其作用域之外的变量。
Closure is also an anonymous function that can access the variables outside its scope with the help of the "use" keyword.
Example
请看以下示例 −
Take a look a the following example −
<?php
$maxmarks=300;
$percent=function ($marks) use ($maxmarks) {
return $marks*100/$maxmarks;
};
$m = 250;
echo "Marks = $m Percentage = ". $percent($m);
?>
它将生成以下 output −
It will produce the following output −
Marks = 250 Percentage = 83.333333333333
PHP - Arrow Functions
箭头函数在 PHP 7.4 版本中引入。箭头函数为编写匿名函数提供了更简单、更简洁的语法。在 PHP 7.4 中,引进了关键词 " fn " 来定义箭头函数,取代原先通用的关键词 " function "。
Arrow functions were introduced in PHP 7.4 version. Arrow functions provide a simpler and more concise syntax for writing anonymous functions. With PHP 7.4, a keyword "fn" has been introduced for defining arrow functions, instead of the conventional use of the "function" keyword.
fn (argument_list) => expr
-
There is only one expression after the "⇒" symbol, and its value is the return value of the arrow function.
-
The arrow function doesn’t have an explicit return statement.
-
Like in the anonymous function, the arrow function is assigned to a variable for it to be called.
Example
以下示例展示了如何在 PHP 中使用箭头函数 −
The following example demonstrates how you can use the arrow function in PHP −
<?php
$add = fn ($a, $b) => $a + $b;
$x = 10;
$y = 20;
echo " x: $x y: $y Addition: " . $add($x, $y);
?>
它将生成以下 output −
It will produce the following output −
x: 10 y: 20 Addition: 30
Using the Arrow Function as a Callback Function
还可以使用箭头函数作为回调函数。回调函数作为其他函数的参数之一来使用。箭头函数会即时执行,而 "⇒" 符号后面的表达式的值会变成父函数的参数,父函数可以是内置函数,也可以是用户定义的函数。
We can also use the arrow function as a callback function. Callback functions are used as one of the arguments of another function. The arrow function is executed on the fly and the value of the expression after "⇒" becomes the argument of the parent function, which may be either a built-in or a user-defined function.
Example
在本例中,我们使用箭头函数在 usort() 函数中,这是一个内置函数,使用用户自定义比较函数按值对数组进行排序。
In this example, we use an arrow function inside usort() function, a built_in function that sorts an array by values using a user-defined comparison function.
<?php
$arr = [10,3,70,21,54];
usort ($arr, fn ($x , $y) => $x > $y);
foreach ($arr as $x){
echo $x . "\n";
}
?>
它将生成以下 output −
It will produce the following output −
3
10
21
54
70
Accessing Variables from the Parent Scope
箭头函数可以自动访问父作用域中的变量。与匿名函数不同,它不需要 " use " 关键词才能作为闭包。当表达式中使用的变量在父函数中定义时,会隐式按值捕获它。
Arrow functions can automatically access variables from the parent scope. Unlike the anonymous functions, the "use" keyword is not necessary for it to act as a closure. When a variable used in the expression is defined in the parent scope, it will be implicitly captured by-value.
<?php
$maxmarks=300;
$percent=fn ($marks) => $marks*100/$maxmarks;
$m = 250;
echo "Marks = $m Percentage = ". $percent($m);
?>
它将生成以下 output −
It will produce the following output −
Marks = 250 Percentage = 83.333333333333
Example
即使嵌套,箭头函数也可以自动按值捕获变量。
Arrow functions capture variables by value automatically, even when nested.
在以下示例中,在另一个箭头函数的表达式中定义了一个箭头函数。
In the following example, an arrow function is defined in the expression part of another arrow function.
<?php
$z = 1;
$fn = fn($x) => fn($y) => $x * $y + $z;
$x = 5;
$y = 10;
echo "x:$x y:$y \n";
echo "Result of nested arrow functions: " . ($fn($x)($y));
?>
它将生成以下 output −
It will produce the following output −
x:5 y:10
Result of nested arrow functions: 51
与匿名函数一样,箭头函数语法允许任意函数签名,包括参数和返回类型、默认值、变参,以及引用传递和返回。
Just like anonymous functions, the arrow function syntax allows arbitrary function signatures, including parameter and return types, default values, variadics, as well as by-reference passing and returning.
PHP Variable Handling Functions
PHP Variable Handling Functions 是内置的 PHP 库函数,使我们能够以多种方式操作和测试 PHP 变量。
PHP Variable Handling Functions are the inbuilt PHP library functions that enable us to manipulate and test php variables in various ways.
Installation
无需安装即可使用 PHP 变量处理函数;它们是 PHP 内核的一部分,并随标准 PHP 安装一起提供。
There is no installation needed to use PHP variable handling functions; they are part of the PHP core and comes alongwith standard PHP installation.
Runtime Configuration
此扩展没有在 php.ini 中定义任何配置指令。
This extension has no configuration directives defined in php.ini.
PHP Variable Handling Functions
下表列出了与 PHP 变量处理相关的所有函数。此处的版本列表示支持该函数的 PHP 最早版本。
Following table lists down all the functions related to PHP Variable Handling. Here column version indicates the earliest version of PHP that supports the function.
Sr.No |
Function & Description |
Version |
1 |
boolval()Function returns boolean value of a defined variable, i.e, it returns TRUE or FALSE. |
5.5.0 |
2 |
debug_zval_dump()Function is used to dump a string representation of an internal zend value to output. |
4.2.0 |
3 |
doubleval()Function is used to return the float value of a defined variable. |
4, 5, 7, 8 |
4 |
empty()Function is used to check if the defined variable has empty value. |
4, 5, 7, 8 |
5 |
floatval()Function used return the float value of a defined variable. |
4.2.0, 5, 7, 8 |
6 |
get_defined_vars()Function is used to return all the defined variables as an array. |
4.0.4, 5, 7, 8 |
7 |
get_resource_id()Function is used to return an integer identifier for the given resource. |
8 |
8 |
get_resource_type()Function returns the type a resource of the variable defined. |
4.0.2, 5, 7, 8 |
9 |
gettype()Function returns the type of the variable defined. |
4, 5, 7, 8 |
10 |
intval()Function returns integer value of the defined variable. |
4, 5, 7, 8 |
11 |
is_array()Function is used to check if the defined variable is an array. |
4, 5, 7, 8 |
12 |
is_bool()Function is used to check if the defined variable is boolean, i.e, it returns true or false value. |
4, 5, 7, 8 |
13 |
is_callable()Function is used to check if the data in the variable can be called as a function |
4.0.6, 5, 7, 8 |
14 |
is_countable()Function is used to check if the data in the variable is countable. |
7.3.0, 8 |
15 |
is_double()Function is used to check if the variable defined is of the type float |
4, 5, 7, 8 |
16 |
is_float()Function is used to check if the variable defined is an float |
4, 5, 7, 8 |
17 |
is_int()Function is used to check if the variable defined is of the type integer |
4, 5, 7, 8 |
18 |
is_integer()Function is used to check if the variable defined is an integer. |
4, 5, 7, 8 |
19 |
is_iterable()Function is used to check if the data in the variable is an iterable value. |
7.1.0, 8 |
20 |
is_long()Function is used to check if the variable is of the type integer. |
4, 5, 7, 8 |
21 |
is_null()Function checks if the variable has NULL value. |
4.0.4, 5, 7, 8 |
22 |
is_numeric()Function is used to check if the defined variable is numeric or a numeric string. |
4, 5, 7, 8 |
23 |
is_object()Function is used to check if the variable is an object. |
4, 5, 7, 8 |
24 |
is_real()Function is used to check if the defined variable is of the type float or not. |
4, 5, 7, 8 |
25 |
is_resource()Function is used to check if the defined variable is resource. |
4, 5, 7, 8 |
26 |
is_scalar()Function is used to check if the defined variable is scalar. |
4.0.5, 5, 7, 8 |
27 |
is_string()Function is used to check if the defined variable is of the type string. |
4, 5, 7, 8 |
28 |
isset()Function is used to check if a variable is declared and set. |
4, 5, 7, 8 |
29 |
print_r()Function used to represent or print the data of a variable into a readable format. |
4, 5, 7, 8 |
30 |
serialize()Function used to convert the data of a variable into a storable representation, so that the data can be stored in a file, a memory buffer |
4, 5, 7, 8 |
31 |
settype()Function is used to set a variable into a specific type. |
4, 5, 7, 8 |
32 |
strval()Function is used to return a string value of a variable. |
4, 5, 7, 8 |
33 |
unserialize()Function is used to unserialize the data of a variable. i.e, this function returns actual data of a serialize variable. |
4, 5, 7, 8 |
34 |
unset()Function used unset a variable |
4, 5, 7, 8 |
35 |
var_dump()Function is used to dump information about one or more variables. The information holds type and value of the variable(s). |
4, 5, 7, 8 |
36 |
var_export()Function returns structured information about a variable. |
4.2.0, 5, 7, 8 |
PHP - Local Variables
作用域可以定义为变量在其中声明的程序中可用性的范围。PHP 变量可以是四种作用域类型之一−
Scope can be defined as the range of availability a variable has to the program in which it is declared. PHP variables can be one of four scope types −
-
Local Variables
-
Global Variables
-
Static Variables
-
Function Parameters
Local Variables
在函数中声明的变量被视为局部变量;也就是说,它只能在该函数中引用。该函数之外的任何赋值都将被视为与函数中包含的变量完全不同的变量 -
A variable declared in a function is considered local; that is, it can be referenced solely in that function. Any assignment outside of that function will be considered to be an entirely different variable from the one contained in the function −
<?php
$x = 4;
function assignx () {
$x = 0;
print "\$x inside function is $x. \n";
}
assignx();
print "\$x outside of function is $x.";
?>
这会产生以下结果 −
This will produce the following result −
$x inside function is 0.
$x outside of function is 4.
PHP - Global Variables
在 PHP 中,任何可以在 PHP 脚本中的任何地方访问的变量都称为 global variable 。如果变量在脚本中的所有函数或类之外声明,它将成为全局变量。
In PHP, any variable that can be accessed from anywhere in a PHP script is called as a global variable. If the variable is declared outside all the functions or classes in the script, it becomes a global variable.
虽然全局变量可以在函数外部直接访问,但它们不会自动在函数内部提供。
While global variables can be accessed directly outside a function, they aren’t automatically available inside a function.
Example
在下面的脚本中, $name 对于函数 sayhello() 是全局的。
In the script below, $name is global for the function sayhello().
<?php
$name = "Amar";
function sayhello() {
echo "Hello " . $name;
}
sayhello();
?>
但是,该变量在该函数内部不可访问。因此,您将收到 error message "未定义变量 $name"。
However, the variable is not accessible inside the function. Hence, you will get an error message "Undefined variable $name".
Hello
PHP Warning: Undefined variable $name in /home/cg/root/93427/main.php on line 5
Example
要在函数内访问,您需要在变量之前使用 "global" 关键字。
To get access within a function, you need to use the "global" keyword before the variable.
<?php
$name = "Amar";
function sayhello() {
GLOBAL $name;
echo "Hello " . $name;
}
sayhello();
?>
它将生成以下 output −
It will produce the following output −
Hello Amar
如果某个函数访问全局变量并修改该变量,则修改后的值在函数调用完成后随处可用。
If a function accesses a global variable and modifies it, the modified value is available everywhere after the function call is completed.
让我们在 sayhello() 函数中更改 $name 的值,并在调用该函数后检查其值。
Let us change the value of $name inside the sayhello() function and check its value after the function is called.
Example
请查看以下示例:
Take a look at this following example −
<?php
$name = "Amar";
function sayhello() {
GLOBAL $name;
echo "Global variable name: $name" .PHP_EOL;
$name = "Amarjyot";
echo "Global variable name changed to: $name" .PHP_EOL;
}
sayhello();
echo "Global variable name after function call: $name" .PHP_EOL;
?>
它将生成以下 output −
It will produce the following output −
Global variable name: Amar
Global variable name changed to: Amarjyot
Global variable name after function call: Amarjyot
The $GLOBALS Array
PHP 维护一个名为 $GLOBALS 的关联数组,其中包含在全局作用域中声明的所有变量及其值。$GLOBALS 数组还存储许多称为超全局变量的预定义变量以及用户定义的全局变量。
PHP maintains an associative array named $GLOBALS that holds all the variables and their values declared in a global scope. The $GLOBALS array also stores many predefined variables called as superglobals, along with the user defined global variables.
任何全局变量也可以在任何函数内访问,方法是在访问箭头元素的常规语法中使用它。例如,全局变量 $name 的值由 $GLOBALS["name"] 提供。
Any of the global variables can also be accessed inside any function with the help of a regular syntax of accessing an arrow element. For example, the value of the global variable $name is given by $GLOBALS["name"].
Example
在下面的示例中,在 addition() 函数内部访问了两个全局变量 $x 和 $y。
In the following example, two global variable $x and $y are accessed inside the addition() function.
<?php
$x = 10;
$y = 20;
function addition() {
$z = $GLOBALS['x']+$GLOBALS['y'];
echo "Addition: $z" .PHP_EOL;
}
addition();
?>
它将生成以下 output −
It will produce the following output −
Addition: 30
Example
您还可以通过将它添加到 $GLOBALS 数组中将任何局部变量添加到全局作用域。让我们在全局作用域中添加 $z 。
You can also add any local variable into the global scope by adding it in the $GLOBALS array. Let us add $z in the global scope.
<?php
$x = 10;
$y = 20;
function addition() {
$z = $GLOBALS['x']+$GLOBALS['y'];
$GLOBALS['z'] = $z;
}
addition();
echo "Now z is the global variable. Addition: $z" .PHP_EOL;
?>
它将生成以下 output −
It will produce the following output −
Now z is the global variable. Addition: 30
Including One PHP Script in Another
您可以在另一个 PHP 脚本中包含一个 PHP 脚本。包含在脚本中的变量被添加到包含它的 PHP 脚本的全局作用域中。
You can include one PHP script in another. Variables declared in the included script are added in the global scope of the PHP script in which it is included.
这是 "a.php" 文件 -
Here is "a.php" file −
<?php
include 'b.php';
function addition() {
$z = $GLOBALS['x']+$GLOBALS['y'];
echo "Addition: $z" .PHP_EOL;
}
addition();
?>
其中包括有 $x 和 $y 变量的 “b.php”,因此它们变成 “a.php” 脚本中 addition() 函数的全局变量。
It includes "b.php" that has the $x and $y variables, so they become the global variables for the addition() function in "a.php" script.
<?php
$x = 10;
$y = 20;
?>
在实现singleton模式、获取嵌入式系统寄存器以及一个变量被多个函数使用时,通常会使用全局变量。
Global variables are generally used while implementing singleton patterns, and accessing registers in embedded systems and also when a variable is being used by many functions.
PHP - Superglobals
PHP 解析器会用很多预定义变量及其全局命名空间填充当前脚本。预定义变量称为 “ PHP superglobals ”。
The PHP parser populates the current script with a number of predefined variables in its global namespace. The predefined variables are known as "PHP superglobals".
-
Any user defined variable declared outside of any function, method, or class also is a global variable. However, to access it, you need to use the global keyword.
-
In contrast, superglobals are always available anywhere in the PHP script, without mentioning them with the global keyword.
PHP 中的大多数超级全局变量都是关联数组,并且 Web 服务器会填充它们。因此,如果在命令行环境中运行脚本,则某些超级全局变量可能为空。
Most of the superglobals in PHP are associative arrays, and the web server populates them. Hence, if a script is run in the command-line environment, some of the superglobals may be empty.
PHP 中的超级全局变量列表包括以下内容:
The list of superglobal variables in PHP includes the following −
-
$GLOBALS
-
$_SERVER
-
$_GET
-
$_POST
-
$_FILES
-
$_COOKIE
-
$_SESSION
-
$_REQUEST
-
$_ENV
在本章中,我们将对 PHP 中的这些超级全局变量进行简要介绍。在后续章节中,我们将详细讨论这些超级全局变量。
In this chapter, we will have a brief introduction to these superglobal variables in PHP. In the subsequent chapters, we will discuss these superglobal variables in detail.
$GLOBALS
它是对所有全局定义变量的引用的关联数组。变量名称构成键,其内容是关联数组的值。
It is an associative array of references to all globally defined variables. Names of variables form keys and their contents are values of associative array.
$_SERVER
所有与服务器和执行环境相关的信息都包含在该关联数组中。
All the server and execution environment related information is available in this associative array.
5.4.0 之前的 PHP 版本包含以 $HTTP_SERVER_VARS 形式存在相同信息的变量,但现在已删除。
PHP versions prior to 5.4.0 contained $HTTP_SERVER_VARS contained the same information but has now been removed.
$_GET
它是通过附加至 HTTP 请求的 URL 的查询字符串传给当前脚本的变量的关联数组。请注意,除了 GET 请求之外,还通过包含查询字符串的所有请求填充该数组。
It is an associative array of variables passed to the current script via query string appended to URL of HTTP request. Note that the array is populated by all requests with a query string in addition to GET requests.
查询字符串是所有变量及其值按“var=val”形式形成的列表,并以“&”符号连接起来。
A query string is a list of all variables and their values in the form var=val and concatenated by the "&" symbol.
查询字符串本身在问号“?”之后附加至 PHP 脚本名称。例如 http://localhost/hello.php?first_name=Amar&last_name=Sharma 。
The query string itself is appended to the name of PHP script after the "?" symbol. For example, http://localhost/hello.php?first_name=Amar&last_name=Sharma.
$_POST
它是由 HTTP POST 方法通过查询字符串传给 URL 的键-值对的关联数组,在请求中使用 URLEncoded 或 multipart/form-data 内容类型。
It is an associative array of key-value pairs passed to a URL by HTTP POST method that uses URLEncoded or multipart/form-data content-type in request.
$HTTP_POST_VARS 还包含与 $_POST 相同的信息,但不是超级全局变量,现已不赞成使用。向服务器发送具有 POST 请求的数据的最简单方法是将 HTML 表单的方法属性指定为 POST。
$HTTP_POST_VARS also contains the same information as $_POST, but is not a superglobal, and now been deprecated. The easiest way to send data to a server with POST request is specifying the method attribute of HTML form as POST.
$_FILES
变量 $_FILES 是一个关联数组,其中包含通过 HTTP POST 方法上传的项。当一个 HTML 表单包含一个文件类型的输入元素,它的 enctype 属性设置为 multipart/form-data,并且方法属性设置为 HTTP POST 方法时,就会上传一个文件。
The variable $_FILES is an associative array containing items uploaded via HTTP POST method. A file is uploaded when a HTML form contains an input element with file type, its enctype attribute set to multipart/form-data, and the method attribute set to HTTP POST method.
$_COOKIE
Cookie 是服务器存储在客户端计算机上的文本文件,并且它们被保留以用于跟踪目的。
Cookies are text files stored by a server on the client computer and they are kept of use tracking purpose.
超级全局变量 $_COOKIE 将通过 HTTP 请求传递给当前 PHP 脚本的变量以 cookie 的形式存储起来。
The superglobal $_COOKIE stores variables passed to the current PHP script along with the HTTP request in the form of cookies.
$_SESSION
HTTP 会话是用户与服务器建立连接的时间持续期,以及连接被终止的时间之间的时间段。在此间隔期间,某些数据是以会话变量的形式永久存在于页面中。
An HTTP session is the time duration between the time a user establishes connection with a server and the time the connection is terminated. During this interval, some data is persistently available across pages in the form of session variables.
$_SESSION 超全局变量是会话变量的关联数组,当前脚本可以使用。
The $_SESSION superglobal is an associative array of session variables available to the current script.
$_REQUEST
$_REQUEST 是一个关联数组,它包含 $_GET、$_POST 和 $_COOKIE 变量的内容。
$_REQUEST is an associative array which is a collection of contents of $_GET, $_POST and $_COOKIE variables.
这些变量的顺序由 "php.ini" 文件中 requests_order 和 varables_order 设置的值决定。
The order of these variables is decided by the values of requests_order and varables_order settings in the "php.ini" file.
PHP - $GLOBALS
$GLOBALS 是 PHP 中的 " superglobal " 或 " automatic global " 变量之一。它在脚本的各个范围中均可用。无需在函数或方法中访问它时执行 " global $variable; "。
$GLOBALS is one of the "superglobal" or "automatic global" variables in PHP. It is available in all scopes throughout a script. There is no need to do "global $variable;" to access it within functions or methods.
$GLOBALS 是对所有全局定义的变量的引用的关联数组。变量的名称形成键,其内容是关联数组的值。
$GLOBALS is an associative array of references to all globally defined variables. The names of variables form keys and their contents are the values of an associative array.
Example
此示例显示 $GLOBALS 数组包含全局变量的名称和内容−
This example shows $GLOBALS array containing the name and contents of global variables −
<?php
$var1="Hello";
$var2=100;
$var3=array(1,2,3);
echo $GLOBALS["var1"] . "\n";
echo $GLOBALS["var2"] . "\n";
echo implode($GLOBALS["var3"]) . "\n";
?>
它将生成以下 output −
It will produce the following output −
Hello
100
123
Example
在以下示例中, $var1 在全局命名空间和函数内部的局部变量中进行定义。全局变量从 $GLOBALS 数组中提取。
In the following example, $var1 is defined in the global namespace as well as a local variable inside the function. The global variable is extracted from the $GLOBALS array.
<?php
function myfunction() {
$var1="Hello PHP";
echo "var1 in global namespace: " . $GLOBALS['var1']. "\n";
echo "var1 as local variable: ". $var1;
}
$var1="Hello World";
myfunction();
?>
它将生成以下 output −
It will produce the following output −
var1 in global namespace: Hello World
var1 as local variable: Hello PHP
Example
在 PHP 8.1.0 版本之前,全局变量可以通过 $GLOBALS 数组的副本进行修改。
Prior to PHP version 8.1.0, global variables could be modified by a copy of $GLOBALS array.
<?php
$a = 1;
$globals = $GLOBALS;
$globals['a'] = 2;
var_dump($a);
?>
它将生成以下 output −
It will produce the following output −
int(1)
在此处, $globals 是 $GLOBALS 超全局变量的副本。修改副本中键为 "a" 的元素值至 2,实际上会更改 $a 的值。
Here, $globals is a copy of the $GLOBALS superglobal. Changing an element in the copy, with its key as "a" to 2, actually changes the value of $a.
它将生成以下 output −
It will produce the following output −
int(2)
Example
在 PHP 8.1.0 及更高版本中,$GLOBALS 是全局符号表的只读副本。也就是说,全局变量无法通过其副本进行修改。与上述操作相同的操作不会将 $a 更改为 2。
As of PHP 8.1.0, $GLOBALS is a read-only copy of the global symbol table. That is, global variables cannot be modified via their copy. The same operation as above won’t change $a to 2.
<?php
$a = 1;
$globals = $GLOBALS;
$globals['a'] = 2;
var_dump($a);
?>
它将生成以下 output −
It will produce the following output −
int(1)
PHP - $_SERVER
$_SERVER 是 PHP 中的超全局变量。其中包含有关 HTTP 标头、路径以及脚本位置等信息。
$_SERVER is a superglobal in PHP. It holds information regarding HTTP headers, path and script location, etc.
-
$_SERVER is an associative array and it holds all the server and execution environment related information.
-
Most of the entries in this associative array are populated by the web server. The entries may change from one web server to other, as servers may omit some, or provide others.
-
For a PHP script running on the command line, most of these entries will not be available or have any meaning.
-
PHP will also create additional elements with values from request headers. These entries will be named "HTTP_" followed by the header name, capitalized and with underscores instead of hyphens.
-
For example, the "Accept-Language" header would be available as $_SERVER['HTTP_ACCEPT_LANGUAGE'].
-
PHP versions prior to 5.4.0 had $HTTP_SERVER_VARS which contained the same information but it has now been removed.
下表列出了 $_SERVER 数组的一些重要服务器变量,后跟对其值的说明。
The following table lists some of the important server variables of the $_SERVER array followed by the description of their values.
Sr.No |
Server Variables & Description |
1 |
PHP_SELF Stores filename of currently executing script. |
2 |
SERVER_ADDR This property of array returns the IP address of the server under which the current script is executing. |
3 |
SERVER_NAME Name of server host under which the current script is executing. In case of a server running locally, localhost is returned. |
4 |
QUERY_STRING A query string is the string of key value pairs separated by the "&" symbol and appended to the URL after the "?" symbol. For example, [role="bare"]http://localhost/testscript?name=xyz&age=20 URL returns trailing query string |
5 |
REQUEST_METHOD HTTP request method used for accessing a URL, such as POST, GET, POST, PUT or DELETE. In the above query string example, a URL attached to query string with the "?" symbol requests the page with GET method |
6 |
DOCUMENT_ROOT Returns the name of the directory on the server that is configured as the document root. On XAMPP apache server, it returns htdocs as the name of document root c:/xampp/htdocs |
7 |
REMOTE_ADDR IP address of the machine from where the user is viewing the current page. |
8 |
SERVER_PORT Port number on which the web server is listening to the incoming request. Default is 80 |
Example
从 XAMPP 服务器的文档根目录调用的以下脚本将列出所有服务器变量 −
The following script invoked from document root of XAMPP server lists all the server variables −
<?php
foreach ($_SERVER as $k=>$v)
echo $k . "=>" . $v . "\n";
?>
它将生成以下 output −
It will produce the following output −
MIBDIRS=>C:/xampp/php/extras/mibs
MYSQL_HOME=>\xampp\mysql\bin
OPENSSL_CONF=>C:/xampp/apache/bin/openssl.cnf
PHP_PEAR_SYSCONF_DIR=>\xampp\php
PHPRC=>\xampp\php
TMP=>\xampp\tmp
HTTP_HOST=>localhost
HTTP_CONNECTION=>keep-alive
HTTP_SEC_CH_UA=>"Chromium";v="116", "Not)
A;Brand";v="24", "Google Chrome";v="116"
HTTP_SEC_CH_UA_MOBILE=>?0
HTTP_SEC_CH_UA_PLATFORM=>"Windows"
HTTP_DNT=>1
HTTP_UPGRADE_INSECURE_REQUESTS=>1
HTTP_USER_AGENT=>Mozilla/5.0 (Windows NT 10.0; Win64; x64)
AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36
HTTP_ACCEPT=>text/html,application/xhtml+xml,application/xml;
q=0.9,image/avif,image/webp,image/apng,*/*;
q=0.8,application/signed-exchange;v=b3;q=0.7
HTTP_SEC_FETCH_SITE=>none
HTTP_SEC_FETCH_MODE=>navigate
HTTP_SEC_FETCH_USER=>?1
HTTP_SEC_FETCH_DEST=>document
HTTP_ACCEPT_ENCODING=>gzip, deflate, br
HTTP_ACCEPT_LANGUAGE=>en-US,en;q=0.9,mr;q=0.8
PATH=>C:\Python311\Scripts\;
C:\Python311\;C:\WINDOWS\system32;
C:\WINDOWS;C:\WINDOWS\System32\Wbem;
C:\WINDOWS\System32\WindowsPowerShell\v1.0\;
C:\WINDOWS\System32\OpenSSH\;C:\xampp\php;
C:\Users\user\AppData\Local\Microsoft\WindowsApps;
C:\VSCode\Microsoft VS Code\bin
SystemRoot=>C:\WINDOWS
COMSPEC=>C:\WINDOWS\system32\cmd.exe
PATHEXT=>.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.PY;.PYW
WINDIR=>C:\WINDOWS
SERVER_SIGNATURE=>
Apache/2.4.56 (Win64) OpenSSL/1.1.1t PHP/8.0.28 Server at localhost Port 80
SERVER_SOFTWARE=>Apache/2.4.56 (Win64) OpenSSL/1.1.1t PHP/8.0.28
SERVER_NAME=>localhost
SERVER_ADDR=>::1
SERVER_PORT=>80
REMOTE_ADDR=>::1
DOCUMENT_ROOT=>C:/xampp/htdocs
REQUEST_SCHEME=>http
CONTEXT_PREFIX=>
CONTEXT_DOCUMENT_ROOT=>C:/xampp/htdocs
SERVER_ADMIN=>postmaster@localhost
SCRIPT_FILENAME=>C:/xampp/htdocs/hello.php
REMOTE_PORT=>54148
GATEWAY_INTERFACE=>CGI/1.1
SERVER_PROTOCOL=>HTTP/1.1
REQUEST_METHOD=>GET
QUERY_STRING=>
REQUEST_URI=>/hello.php
SCRIPT_NAME=>/hello.php
PHP_SELF=>/hello.php
REQUEST_TIME_FLOAT=>1694802456.9816
REQUEST_TIME=>1694802456
PHP - $_REQUEST
在 PHP 中,$_REQUEST 是一个超级全局变量。它是一个关联数组,由 $_GET、$_POST 和 $_COOKIE 变量的内容集合组成。
In PHP, $_REQUEST is a superglobal variable. It is an associative array which is a collection of contents of $_GET, $_POST and $_COOKIE variables.
-
The settings in your "php.ini" file decides the composition of this variable.
-
One of the directives in "php.ini" is request_order, which decides the order in which PHP registers GET, POST and COOKIE variables.
-
The presence and order of variables listed in this array is defined according to the PHP variables_order.
-
If a PHP script is run from the command line, the argc and argv variables are not included in the $_REQUST array because their values are taken from the $_SERVER array, which in turn is populated by the web server.
$_REQUEST with GET Method
将以下脚本保存在 Apache 服务器的文档文件夹中。如果你在 Windows 上使用 XAMPP 服务器,请将脚本作为 “hello.php” 放在 “c:/xampp/htdocs” 文件夹中。
Save the following script in the document folder of the Apache server. If you are using XAMPP server on Windows, place the script as "hello.php" in the "c:/xampp/htdocs" folder.
<html>
<body>
<?php
echo "<h3>First Name: " . $_REQUEST['first_name'] . "<br />"
. "Last Name: " . $_REQUEST['last_name'] . "</h3>";
?>
</body>
</html>
启动 XAMPP 服务器并在浏览器窗口中输入 http://localhost/hello.php?first_name=Amar&last_name=Sharma 作为 URL。
Start the XAMPP server and enter http://localhost/hello.php?first_name=Amar&last_name=Sharma as the URL in a browser window.
你应该可以获得 output 如下 −
You should get the output as −
$_REQUEST with POST Method
在文档根目录下,将以下脚本保存为 “hello.html”。
Under the document root, save the following script as "hello.html".
<html>
<body>
<form action="hello.php" method="post">
First Name: <input type="text" name="first_name" /> <br />
Last Name: <input type="text" name="last_name" />
<input type="submit" value="Submit" />
</form>
</body>
</html>
在您的浏览器中,输入 URL “http://localhost/hello.html”。您应该在浏览器窗口中获得类似的 output 。
In your browser, enter the URL "http://localhost/hello.html". You should get the similar output in the browser window.
您还可以在 HTML 脚本中嵌入 PHP 代码,并使用 PHP_SELF 变量将表单 POST 到自身 −
You may also embed the PHP code inside the HTML script and POST the form to itself with the PHP_SELF variable −
<html>
<body>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
<p>First Name: <input type="text" name="first_name" /></p>
<p>Last Name: <input type="text" name="last_name" /></p>
<input type="submit" value="Submit" />
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST")
echo "<h3>First Name: " . $_REQUEST['first_name'] . "<br />"
. "Last Name: " . $_REQUEST['last_name'] . "</h3>";
?>
</body>
</html>
它将生成以下 output −
It will produce the following output −
PHP - $_POST
$_POST 是 PHP 中的一个预定义的超全局变量。它是由 HTTP POST 方法(在请求中使用 URLEncoded 或 multipart/form-data 内容类型)传递给 URL 的键值对关联数组。
$_POST is one of the predefined or superglobal variables in PHP. It is an associative array of key-value pairs passed to a URL by the HTTP POST method that uses URLEncoded or multipart/form-data content-type in the request.
-
$HTTP_POST_VARS also contains the same information as $_POST, but is not a superglobal, and now been deprecated.
-
The easiest way to send data to a server with POST request is specifying the method attribute of HTML form as POST.
假设浏览器中的 URL 为“http://localhost/hello.php”,method=POST 在 HTML 表单“hello.html”中设置如下 −
Assuming that the URL in the browser is "http://localhost/hello.php", method=POST is set in a HTML form "hello.html" as below −
<html>
<body>
<form action="hello.php" method="post">
<p>First Name: <input type="text" name="first_name"/> </p>
<p>Last Name: <input type="text" name="last_name" /> </p>
<input type="submit" value="Submit" />
</form>
</body>
</html>
此练习的“hello.php”脚本(位于文档根目录文件夹中)如下:
The "hello.php" script (in the document root folder) for this exercise is as follows:
<?php
echo "<h3>First name: " . $_POST['first_name'] . "<br /> " .
"Last Name: " . $_POST['last_name'] . "</h3>";
?>
现在,在你的浏览器中打开 http://localhost/hello.html 。你应该在屏幕上获得以下输出 −
Now, open http://localhost/hello.html in your browser. You should get the following output on the screen −
当您按下 Submit 按钮时,数据将使用 POST 方法提交至“hello.php”。
As you press the Submit button, the data will be submitted to "hello.php" with the POST method.
您还可以将 HTML 表单与 hello.php 中的 PHP 代码混合使用,并使用“PHP_SELF”变量将其表单数据发布给自己 -
You can also mix the HTML form with PHP code in hello.php, and post the form data to itself using the "PHP_SELF" variable −
<html>
<body>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
<p>First Name: <input type="text" name="first_name"/> </p> <br />
<p>Last Name: <input type="text" name="last_name" /></p>
<input type="submit" value="Submit" />
</form>
<?php
echo "<h3>First Name: " . $_POST['first_name'] . "<br /> " .
"Last Name: " . $_POST['last_name'] . "</h3>";
?>
</body>
</html>
它将生成以下 output −
It will produce the following output −
PHP - $_GET
$_GET 是 PHP 中的超级全局变量之一。它是一个关联数组,其中包含通过附加到 HTTP 请求 URL 的查询字符串传递给当前脚本的变量。请注意,除了 GET 请求外,所有带有查询字符串的请求都会填充此数组。
$_GET is one of the superglobals in PHP. It is an associative array of variables passed to the current script via the query string appended to the URL of HTTP request. Note that the array is populated by all requests with a query string in addition to GET requests.
$HTTP_GET_VARS 包含相同的基本信息,但它现在已被弃用。
$HTTP_GET_VARS contains the same initial information, but that has now been deprecated.
默认情况下,客户端浏览器通过使用 HTTP GET 方法向服务器上的 URL 发送请求。附加到 URL 的查询字符串可能包含由“&”符号连接的键值对。$_GET 关联数组存储这些键值对。
By default, the client browser sends a request for the URL on the server by using the HTTP GET method. A query string attached to the URL may contain key value pairs concatenated by the "&" symbol. The $_GET associative array stores these key value pairs.
将以下脚本保存在 Apache 服务器的文档文件夹中。如果您在 Windows 上使用 XAMPP 服务器,请将脚本作为“hello.php”放置在“c:/xampp/htdocs”文件夹中。
Save the following script in the document folder of Apache server. If you are using XAMPP server on Windows, place the script as "hello.php" in the "c:/xampp/htdocs" folder.
<?php
echo "<h3>First Name: " . $_REQUEST['first_name'] . "<br />" .
"Last Name: " . $_REQUEST['last_name'] . "</h3>";
?>
启动 XAMPP 服务器,并在浏览器窗口中输入“http://localhost/hello.php?first_name=Mukesh&last_name=Sinha”作为 URL。您应该获得以下 output −
Start the XAMPP server, and enter "http://localhost/hello.php?first_name=Mukesh&last_name=Sinha" as the URL in a browser window. You should get the following output −
当 HTML 表单数据被提交到带有 GET 动作的 URL 时,$_GET 数组也会被填充。
The $_GET array is also populated when a HTML form data is submitted to a URL with GET action.
在文档根目录下,将以下脚本保存为“ hello.html ”−
Under the document root, save the following script as "hello.html" −
<html>
<body>
<form action="hello.php" method="get">
<p>First Name: <input type="text" name="first_name"/></p>
<p>Last Name: <input type="text" name="last_name" /></p>
<input type="submit" value="Submit" />
</form>
</body>
</html>
在您的浏览器中,输入 URL "http://localhost/hello.html" −
In your browser, enter the URL "http://localhost/hello.html" −
您应该在浏览器窗口中获得类似的 output −
You should get a similar output in the browser window −
在以下示例中,htmlspecialchars() 用于将字符转换为 HTML 实体 −
In the following example, htmlspecialchars() is used to convert characters in HTML entities −
Character |
Replacement |
& (ampersand) |
& |
" (double quote) |
" |
' (single quote) |
' or ' |
< (less than) |
< |
> (greater than) |
> |
假设浏览器中的 URL 为“http://localhost/hello.php?name=Suraj&age=20”−
Assuming that the URL in the browser is "http://localhost/hello.php?name=Suraj&age=20" −
<?php
echo "Name: " . htmlspecialchars($_GET["name"]) . "";
echo "Age: " . htmlspecialchars($_GET["age"]) . "<br/>";
?>
它将生成以下 output −
It will produce the following output −
Name: Suraj
Age: 20
PHP - $_FILES
$_FILES 是 PHP 中的“超全局”或自动全局变量之一。它在脚本的整个范围内都可用。$_FILES 变量是一个关联数组,包含通过 HTTP POST 方法上传的项目。
$_FILES is one of the 'superglobal', or automatic global, variables in PHP. It is available in all scopes throughout a script. The variable $_FILES is an associative array containing items uploaded via HTTP POST method.
当 HTML 表单包含文件类型的输入元素,其 enctype 属性设置为 multipart/form-data,且方法属性设置为 HTTP POST 方法时,便会上传文件。
A file is uploaded when a HTML form contains an input element with a file type, its enctype attribute set to multipart/form-data, and the method attribute set to HTTP POST method.
$HTTP_POST_FILES 也包含相同的信息,但它不是超全局的,并且现在已被弃用。
$HTTP_POST_FILES also contains the same information, but it is not a superglobal, and it has now been deprecated.
以下 HTML 脚本包含一个 input 元素的 file 类型的表单——
The following HTML script contains a form with input element of file type −
<input type="file" name="file">
这个“输入类型”渲染出一个带有文件标题的按钮。点击时,会出现一个文件对话框。您可以选择要上传的文件。
This "input type" renders a button captioned as file. When clicked, a file dialogbox pops up. You can choose a file to be uploaded.
服务器上的 PHP 脚本可以访问 $_FILES 变量中的文件数据。
The PHP script on the server can access the file data in $_FILES variable.
$_FILES 数组包含以下属性−
The $_FILES array contains the following properties −
-
$_FILES['file']['name'] − The original name of the file that the user has chosen to be uploaded.
-
$_FILES['file']['type'] − The mime type of the file. An example would be "image/gif". This mime type is however not checked on the PHP side.
-
$_FILES['file']['size'] − The size, in bytes, of the uploaded file.
-
$_FILES['file']['tmp_name'] − The temporary filename of the file in which the uploaded file was stored on the server.
-
$_FILES['file']['full_path'] − The full path as submitted by the browser. Available as of PHP 8.1.0.
-
$_FILES['file']['error'] − The error code associated with this file upload.
error codes 如下枚举−
The error codes are enumerated as below −
Error Codes |
Description |
UPLOAD_ERR_OK (Value=0) |
There is no error, the file uploaded with success. |
UPLOAD_ERR_INI_SIZE (Value=1) |
The uploaded file exceeds the upload_max_filesize directive in php.ini. |
UPLOAD_ERR_FORM_SIZE (Value=2) |
The uploaded file exceeds the MAX_FILE_SIZE. |
UPLOAD_ERR_PARTIAL (Value=3) |
The uploaded file was only partially uploaded. |
UPLOAD_ERR_NO_FILE (Value=4) |
No file was uploaded. |
UPLOAD_ERR_NO_TMP_DIR (Value=6) |
Missing a temporary folder. |
UPLOAD_ERR_CANT_WRITE (Value=7) |
Failed to write file to disk. |
UPLOAD_ERR_EXTENSION (Value=8) |
A PHP extension stopped the file upload. |
Example
以下“test.html”包含一个 HTML 表单,其 enctype 设置为 multiform/form-data。它还具有一个输入文件元素,该元素在表单上显示一个按钮,供用户选择要上传的文件。将此文件保存在 Apache 服务器的文档根目录中。
The following "test.html" contains a HTML form whose enctype is set to multiform/form-data. It also has an input file element which presents a button on the form for the user to select file to be uploaded. Save this file in the document root folder of your Apache server.
<html>
<body>
<form action="hello.php" method="POST" enctype="multipart/form-data">
<p><input type="file" name="file"></p>
<p><input type ="submit" value="submit"></p>
</form>
</body>
</html>
上述 HTML 在浏览器窗口中渲染一个名为“选择文件”的按钮。要打开文件对话框,请单击“选择文件”按钮。当出现所选文件的名称时,请单击 submit 按钮。
The above HTML renders a button named "Choose File" in the browser window. To open a file dialog box, click the "Choose File" button. As the name of selected file appears, click the submit button.
Example
文档根目录中的服务器端 PHP 脚本 ( upload.php ) 如下读取变量 $_FILES 数组−
The server-side PHP script (upload.php) in the document root folder reads the variables $_FILES array as follows −
<?php
echo "Filename: " . $_FILES['file']['name']."<br>";
echo "Type : " . $_FILES['file']['type'] ."<br>";
echo "Size : " . $_FILES['file']['size'] ."<br>";
echo "Temp name: " . $_FILES['file']['tmp_name'] ."<br>";
echo "Error : " . $_FILES['file']['error'] . "<br>";
?>
它将生成以下 output −
It will produce the following output −
Filename: abc.txt
Type : text/plain
Size : 556762
Temp name: C:\xampp\tmp\phpD833.tmp
Error : 0
Example
在 PHP 中,您可以使用 HTML 数组功能上传多个文件 −
In PHP, you can upload multiple files using the HTML array feature −
<html>
<body>
<form action="hello.php" method="POST" enctype="multipart/form-data">
<input type="file" name="files[]"/>
<input type="file" name="files[]"/>
<input type ="submit" value="submit"/>
</form>
</body>
</html>
现在,将 PHP 脚本 ( hello.php ) 更改为 −
Now, change the PHP script (hello.php) to −
<?php
foreach ($_FILES["files"]["name"] as $key => $val) {
echo "File uploaded: $val <br>";
}
?>
浏览器将显示多个“选择文件”按钮。在您通过单击“提交”按钮上传选定的文件后,浏览器将显示名称为文件,以响应 URL http://localhost/hello.html ,如下图所示 −
The browser will show multiple "Choose File" buttons. After you upload the selected files by clicking the "Submit" button, the browser will show the names of files in response to the URL http://localhost/hello.html as shown below −
PHP - $_ENV
$_ENV 是 PHP 中的超级全局变量。它是一个关联数组,其中存储在当前脚本中可用的所有环境变量。 $HTTP_ENV_VARS 也包含相同的信息,但它不是超级全局变量,并且现在已弃用。
$_ENV is a superglobal variable in PHP. It is an associative array that stores all the environment variables available in the current script. $HTTP_ENV_VARS also contains the same information, but it is not a superglobal, and it has now been deprecated.
将环境变量导入到全局命名空间中。其中大多数变量是由 PHP 解析器正在运行之下的 shell 提供的。因此,不同平台上的环境变量列表可能不同。
The environment variables are imported into the global namespace. Most of these variables are provided by the shell under which the PHP parser is running. Hence, the list of environment variables may be different on different platforms.
如果 PHP 作为服务器模块或 CGI 处理器运行,这个数组 ($_ENV) 还包括 CGI 变量。
This array ($_ENV) also includes CGI variables in case PHP is running as a server module or a CGI processor.
我们可以使用 foreach 循环来显示所有可用的环境变量 −
We can use the foreach loop to display all the environment variables available −
<?php
foreach ($_ENV as $k=>$v)
echo $k . " => " . $v . "<br>";
?>
在 Windows 操作系统和 XAMPP 服务器上,您可能会获得以下环境变量列表 −
On a Windows OS and with XAMPP server, you may get the list of environment variables as follows −
Variable |
Value |
ALLUSERSPROFILE |
C:\ProgramData |
APPDATA |
C:\Users\user\AppData\Roaming |
CommonProgramFiles |
C:\Program Files\Common Files |
CommonProgramFiles(x86) |
C:\Program Files (x86)\Common Files |
CommonProgramW6432 |
C:\Program Files\Common Files |
COMPUTERNAME |
GNVBGL3 |
ComSpec |
C:\WINDOWS\system32\cmd.exe |
DriverData |
C:\Windows\System32\Drivers\DriverData |
HOMEDRIVE |
C − |
HOMEPATH |
\Users\user |
LOCALAPPDATA |
C:\Users\user\AppData\Local |
LOGONSERVER |
\\GNVBGL3 |
MOZ_PLUGIN_PATH |
C:\Program Files (x86)\ Foxit Software\ Foxit PDF Reader\plugins\ |
NUMBER_OF_PROCESSORS |
8 |
OneDrive |
C:\Users\user\OneDrive |
OneDriveConsumer |
C:\Users\user\OneDrive |
OS |
Windows_NT |
Path |
C:\Python311\Scripts\; C:\Python311\; C:\WINDOWS\system32; C:\WINDOWS; C:\WINDOWS\System32\Wbem; C:\WINDOWS\System32\WindowsPowerShell\ v1.0\; C:\WINDOWS\System32\OpenSSH\; C:\xampp\php; C:\Users\user\AppData\Local\Microsoft\ WindowsApps; C:\VSCode\Microsoft VS Code\bin |
PATHEXT |
.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE; .WSF;.WSH;.MSC;.PY;.PYW |
PROCESSOR_ARCHITECTURE |
AMD64 |
PROCESSOR_IDENTIFIER |
Intel64 Family 6 Model 140 Stepping 1, GenuineIntel |
PROCESSOR_LEVEL |
6 |
PROCESSOR_REVISION |
8c01 |
ProgramData |
C:\ProgramData |
ProgramFiles |
C:\Program Files |
ProgramFiles(x86) |
C:\Program Files (x86) |
ProgramW6432 |
C:\Program Files |
PSModulePath |
C:\Program Files\WindowsPowerShell\Modules; C:\WINDOWS\system32\WindowsPowerShell\v1.0\ Modules |
PUBLIC |
C:\Users\Public |
SystemDrive |
C − |
SystemRoot |
C:\WINDOWS |
TEMP |
C:\Users\user\AppData\Local\Temp |
TMP |
C:\Users\user\AppData\Local\Temp |
USERDOMAIN |
GNVBGL3 |
USERDOMAIN_ROAMINGPROFILE |
GNVBGL3 |
USERNAME |
user |
USERPROFILE |
C:\Users\user |
windir |
C:\WINDOWS |
ZES_ENABLE_SYSMAN |
1 |
__COMPAT_LAYER |
RunAsAdmin Installer |
AP_PARENT_PID |
10608 |
你也可以访问各个环境变量的值。此代码获取 PATH 环境变量 −
You can access the value of individual environment variable too. This code fetches the PATH environment variable −
<?php
echo "Path: " . $_ENV['Path'];
?>
它将生成以下 output −
It will produce the following output −
Path:
C:\Python311\Scripts\;C:\Python311\;C:\WINDOWS\system32;
C:\WINDOWS;C:\WINDOWS\System32\Wbem;
C:\WINDOWS\System32\WindowsPowerShell\v1.0\;
C:\WINDOWS\System32\OpenSSH\;C:\xampp\php;
C:\Users\mlath\AppData\Local\Microsoft\WindowsApps;
C:\VSCode\Microsoft VS Code\bin
Note − $_ENV 数组可能会产生空的结果,具体取决于“php.ini”设置“variables_order”。你可能需要编辑“php.ini”文件并设置 variables_order="EGPCS" 而不是 variables_order="GPCS" 值。
Note − The $_ENV array may yield empty result, depending on "php.ini" setting "variables_order". You may have to edit the "php.ini" file and set variables_order="EGPCS" instead of variables_order="GPCS" value.
The getenv() Function
PHP 库提供了 getEnv() 函数来检索所有环境变量或特定环境变量的值。
The PHP library provides the getenv() function to retrieve the list of all the environment variables or the value of a specific environment variable.
以下脚本显示所有可用环境变量的值 −
The following script displays the values of all the available environment variables −
<?php
$arr=getenv();
foreach ($arr as $key=>$val)
echo "$key=>$val";
?>
要获取特定变量的值,请使用其名称作为 getEnv() 函数的参数 −
To obtain the value of a specific variable, use its name as the argument for the getenv() function −
<?php
echo "Path: " . getenv("PATH");
?>
The putenv() Function
PHP 还提供了 putenv() 函数来创建新的环境变量。该环境变量只会存在于当前请求的持续时间内。
PHP also provides the putenv() function to create a new environment variable. The environment variable will only exist for the duration of the current request.
应避免更改某些环境变量的值。默认情况下,用户只能设置以“ PHP_ ”开头的环境变量(比如 PHP_FOO=BAR)。
Changing the value of certain environment variables should be avoided. By default, users will only be able to set the environment variables that begin with "PHP_" (e.g. PHP_FOO=BAR).
“php.ini”中的“safe_mode_protected_env_vars”指令包含一个环境变量逗号分隔列表,最终用户无法使用 putenv() 更改这些变量。
The "safe_mode_protected_env_vars" directive in "php.ini" contains a comma-delimited list of environment variables that the end user won’t be able to change using putenv().
<?php
putenv("PHP_TEMPUSER=GUEST");
echo "Temp user: " . getenv("PHP_TEMPUSER");
?>
浏览器将显示以下 output −
The browser will display the following output −
Temp user: GUEST
PHP - $_COOKIE
PHP 超全局变量 $_COOKIE 存储通过 HTTP 请求以 cookie 形式传递给当前 PHP 脚本的变量。$HTTP_COOKIE_VARS 也包含相同信息,但它不是超全局变量,现在已被弃用。
The PHP superglobal $_COOKIE stores the variables passed to the current PHP script along with the HTTP request in the form of cookies. $HTTP_COOKIE_VARS also contains the same information, but it is not a superglobal, and it has now been deprecated.
What is a Cookie?
cookie 是由服务器存储在客户端计算机上的文本文件,用于跟踪目的。PHP 透明支持 HTTP cookie。cookie 通常在 HTTP 标头中设置。JavaScript 也可以直接在浏览器上设置 cookie。
Cookies are text files stored by a server on the client computer and they are kept for tracking purpose. PHP transparently supports HTTP cookies. Cookies are usually set in an HTTP header. JavaScript can also sets a cookie directly on a browser.
服务器脚本将一组 cookie 发送到浏览器。它将此信息存储在本地计算机上以供将来使用。下次当浏览器向 Web 服务器发送任何请求时,它会将这些 cookie 信息发送到服务器,而服务器则使用这些信息来识别用户。
The server script sends a set of cookies to the browser. It stores this information on the local machine for future use. Next time, when the browser sends any request to the web server, it sends those cookies information to the server and the server uses that information to identify the user.
The setcookie() Function
PHP 提供 setcookie 函数来创建要随 HTTP 响应一起发送到客户端的 cookie 对象。
PHP provides the setcookie function to create a cookie object to be sent to the client along with the HTTP response.
setcookie(name, value, expire, path, domain, security);
Parameters
-
Name − Name of the cookie stored.
-
Value − This sets the value of the named variable.
-
Expiry − This specifes a future time in seconds since 00:00:00 GMT on 1st Jan 1970.
-
Path − Directories for which the cookie is valid.
-
Domain − Specifies the domain name in very large domains.
-
Security − 1 for HTTPS. Default 0 for regular HTTP.
How to Set Cookies
查看以下 example 。此脚本设置一个名为 username 的 Cookie(如果尚未设置)。
Take a look at the following example. This script sets a cookie named username if it is not already set.
Example
<?php
if (isset($_COOKIE['username'])) {
echo "<h2>Cookie username already set: " . $_COOKIE['username'] . "</h2>";
} else {
setcookie("username", "Mohan Kumar");
echo "<h2>Cookie username is now set.</h2>";
}
?>
从 Apache 服务器的根文档运行此脚本来。您应看到以下消息作为 output −
Run this script from the document root of the Apache server. You should see this message as the output −
Cookie username is now set
如果重新执行此脚本,则现在已设置 Cookie。
If this script is re-executed, the cookie is now already set.
Cookie username already set: Mohan Kumar
How to Remove Cookies
要删除 cookie,请设置具有已经过期的日期的 cookie,以便浏览器触发 cookie 删除机制。
To delete a cookie, set the cookie with a date that has already expired, so that the browser triggers the cookie removal mechanism.
<?php
setcookie("username", "", time() - 3600);
echo "<h2>Cookie username is now removed</h2>";
?>
浏览器现在将显示以下 output −
The browser will now show the following output −
Cookie username is now removed
Setting Cookies Using the Array Notation
您还可以使用 Cookie 名称中的数组符号来设置数组 Cookie。
You may also set the array cookies by using the array notation in the cookie name.
setcookie("user[three]", "Guest");
setcookie("user[two]", "user");
setcookie("user[one]", "admin");
如果 Cookie 名称包含句点 (.),则 PHP 将它们替换为下划线 (_)。
If the cookie name contains dots (.), then PHP replaces them with underscores (_).
PHP - $_SESSION
PHP 中的超全局变量之一 $_SESSION 是当前脚本中可用的会话变量的关联数组。 $HTTP_SESSION_VARS 也包含相同的信息,但它不是超全局变量,现在已被弃用。
One of the superglobal variables in PHP, $_SESSION is an associative array of session variables available in the current script. $HTTP_SESSION_VARS also contains the same information, but it is not a superglobal, and it has now been deprecated.
What is a Session?
会话是另一种方法,可跨整个网站的页面访问数据。它是用户与服务器建立连接时和连接终止时之间的持续时间。在此时间段内,用户可能会导航到不同的页面。许多时候,希望某些数据持续可在所有页面中使用。这由 session variables 实现。
A Session is an alternative way to make data accessible across the pages of an entire website. It is the time duration between the time a user establishes a connection with a server and the time the connection is terminated. During this interval, the user may navigate to different pages. Many times, it is desired that some data is persistently available across the pages. This is facilitated by session variables.
会话在服务器临时目录中创建一个文件,其中存储已注册的会话变量及其值。此数据在该访问期间可供站点上的所有页面使用。
A session creates a file in a temporary directory on the server where the registered session variables and their values are stored. This data will be available to all the pages on the site during that visit.
服务器为每个会话分配一个唯一的 SESSIONID。由于 HTTP 是一种无状态协议,因此当会话终止时,会话变量中的数据将自动删除。
The server assigns a unique SESSIONID to each session. Since HTTP is a stateless protocol, data in session variables is automatically deleted when the session is terminated.
The session_start() Function
为了启用对会话数据的访问,必须调用 session_start() 函数。session_start() 基于通过 GET 或 POST 请求或通过 Cookie 传递的会话标识符创建一个会话或恢复当前会话。
In order to enable access to session data, the session_start() function must be invoked. session_start() creates a session or resumes the current one based on a session identifier passed via a GET or POST request, or passed via a cookie.
session_start(array $options = []): bool
如果会话已成功启动,则此函数返回 true ,否则返回 false 。
This function returns true if a session was successfully started, else it returns false.
Handling Session Variables
要创建新的会话变量,请在 $_SESSION 数组中添加键值对 −
To create a new session variable, add a key-value pair in the $_SESSION array −
$_SESSION[ "var"]=value;
要读回会话变量的值,您可以使用 echo/print 语句或 var_dump() 或 print_r() 函数。
To read back the value of a session variable, you can use echo/print statements, or var_dump() or print_r() functions.
echo $_SESSION[ "var"];
要获取当前会话中所有会话变量的列表,可以使用 foreach 循环遍历 $_SESSION −
To obtain the list of all the session variables in the current session, you can use a foreach loop to traverse the $_SESSION −
foreach ($_SESSION as $key=>$val)
echo $key . "=>" . $val;
若要手动清除所有会话数据,则有 session_destroy() 函数。也可以通过调用 unset() 函数来释放特定的会话变量。
To manually clear all the session data, there is session_destroy() function. A specific session variable may also be released by calling the unset() function.
unset($_SESSION[ "var"]);
List of Session Functions
在 PHP 中,有许多用于管理会话数据的内置函数。
In PHP, there are many built-in functions for managing the session data.
Session Functions |
Description |
session_abort |
Discard session array changes and finish session |
session_cache_expire |
Return current cache expire |
session_cache_limiter |
Get and/or set the current cache limiter |
session_commit |
Alias of session_write_close |
session_create_id |
Create new session id |
session_decode |
Decodes session data from a session encoded string |
session_destroy |
Destroys all data registered to a session |
session_encode |
Encodes the current session data as a session encoded string |
session_gc |
Perform session data garbage collection |
session_get_cookie_params |
Get the session cookie parameters |
session_id |
Get and/or set the current session id |
session_is_registered |
Find out whether a global variable is registered in a session |
session_module_name |
Get and/or set the current session module |
session_name |
Get and/or set the current session name |
session_regenerate_id |
Update the current session id with a newly generated one |
session_register_shutdown |
Session shutdown function |
session_register |
Register one or more global variables with the current session |
session_reset |
Re-initialize session array with original values |
session_save_path |
Get and/or set the current session save path |
session_set_cookie_params |
Set the session cookie parameters |
session_set_save_handler |
Sets user-level session storage functions |
session_start |
Start new or resume existing session |
session_status |
Returns the current session status |
session_unregister |
Unregister a global variable from the current session |
session_unset |
Free all session variables |
session_write_close |
Write session data and end session |
Example
以下 PHP 脚本呈现一个 HTML 表单。表格数据用于创建三个会话变量。一个超链接将浏览器带到另一个页面,该页面读回会话变量。
The following PHP script renders an HTML form. The form data is used to create three session variables. A hyperlink takes the browser to another page, which reads back the session variables.
将此代码另存为“test.php”,并将其保存在文档根目录中,然后使用浏览器打开它。输入数据,然后按 Submit 按钮。
Save this code as "test.php" in the document root folder, and open it in a client browser. Enter the data and press the Submit button.
<html>
<body>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
<h3>User's ID: <input type="text" name="ID"/></h3>
<h3>Your Name: <input type="text" name="name"/></h3>
<h3>Enter Age: <input type="text" name="age"/></h3>
<input type="submit" value="Submit"/>
</form>
<?php
session_start();
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$_SESSION['UserID'] = $_POST['ID'];
$_SESSION['Name'] = $_POST['name'];
$_SESSION['age'] = $_POST['age'];
}
echo "Following Session Variables Created: \n";
foreach ($_SESSION as $key=>$val)
echo "<h3>" . $key . "=>" . $val . "</h3>";
echo "<br/>" . '<a href="hello.php">Click Here</a>';
?>
</body>
</html>
单击“提交”按钮后,它将显示创建的所有会话变量的列表 -
When you click the "Submit" button, it will show a list of all the session variables created −
接下来,在“hello.php”文件中保存以下脚本。
Next, have the following script in the "hello.php" file and save it.
<?php
session_start();
echo "<h2>Following Session variables Read:</h2>";
foreach ($_SESSION as $key=>$val)
echo "<h3>" . $key . "=>" . $val . "</h3>";
?>
现在,点击“test.php”页面上的链接导航到“hello.php”。它将显示被读到的会话变量 -
Now, follow the link on the "test.php" page to navigate to "hello.php". It will show the session variables that are read −
PHP - File Handling
在 PHP 中,文件是一种资源对象,可以按线性方式从其中读写数据。“文件处理”一词是指 PHP 中的一组函数,这些函数允许使用 PHP 代码对磁盘文件执行读/写操作。
In PHP, a file is a resource object, from which data can be read or written to in a linear fashion. The term "file handling" refers to a set of functions in PHP that enable read/write operations on disk files with PHP code.
一个文件对象会被分类为 stream 。可以在其上执行线性读/写操作的任何资源都是流。其他类似流的对象是 TCP 套接字、标准输入流,即由“php://stdin”表示的系统键盘、由“php://stdout”表示的标准输出流以及错误流“php://stderr”。
A file object is classified as a stream. Any resource on which linear read/write operations are done is a stream. Other stream-like objects are the TCP sockets, standard input stream, i.e., a system keyboard represented by "php://stdin", the standard output stream represented by "php://stdout", and the error stream "php://stderr".
Note − 常量 STDIN、STDOUT 和 STDERR 分别代表相应的标准流。
Note − Tthe constants STDIN, STDOUT, and STDERR stand for the respective standard streams.
尽管 PHP 被视为用于开发 Web 应用程序的服务器端脚本语言,但 PHP 还有一个命令行界面来执行控制台 IO 操作。
Although PHP is regarded as a server-side scripting language for developing web applications, PHP also has a command-line interface to perform console IO operations.
Example
PHP 中的 readline() 函数接受来自标准键盘的用户输入,echo/print 语句在控制台上呈现输出。
The readline() function in PHP accepts the user input from a standard keyboard, and echo/print statements render the output on the console.
<?php
$str = readline("Type something:");
echo $str;
?>
它将生成以下 output −
It will produce the following output −
C:\xampp\php>php hello.php
Type something: Are you enjoying this PHP tutorial?
Are you enjoying this PHP tutorial?
Example
我们可以通过从“php://stdin”读取输入并将其输出到“php://stdout”来获得相同的效果。
We can obtain the same effect by reading the input from "php://stdin" and outputting it to "php://stdout".
<?php
$f = fopen("php://stdin", "r");
echo "Type something: ";
$str = fgets($f);
$f1 = fopen("php://stdout", "w");
fputs($f1, $str);
?>
这里,fopen() 函数用于打开 stdin 流以进行读取并打开 stdout 流以进行写入。
Here, the fopen() function is used to open the stdin stream for reading and the stdout stream for writing.
Example
PHP 支持各种流协议,以用于流相关函数,例如 fopen()、file_exists() 等。使用 php_get_wrappers() 函数获取所有已注册包装器的列表。
PHP supports a variety of stream protocols for stream related functions such as fopen(), file_exists(), etc. Use php_get_wrappers() function to get a list of all the registered wrappers.
<?php
print_r(stream_get_wrappers());
?>
它将生成以下 output −
It will produce the following output −
Array
(
[0] => php
[1] => file
[2] => glob
[3] => data
[4] => http
[5] => ftp
[6] => zip
[7] => compress.zlib
[8] => compress.bzip2
[9] => https
[10] => ftps
[11] => phar
)
这些流被引用为“scheme://target”。例如,文件流为“file://xyz.txt”。
The streams are referenced as "scheme://target". For instance, the file stream is "file://xyz.txt".
在应用程序运行之前,来自控制台的输入数据存储在计算机的主内存(RAM)中。此后,来自 RAM 的内存内容会被擦除。
The input data from the console is stored in the computer’s main memory (RAM) until the application is running. Thereafter, the memory contents from RAM are erased.
我们希望以一种方式存储它,以便在永久介质(如磁盘文件)中需要时可以检索它。因此,我们将使用磁盘文件(而非标准流(键盘用于输入,显示设备用于输出))来读取数据,以及将数据存储的目的位置。
We would like to store it in such a way that it can be retrieved whenever required in a persistent medium such as a disk file. Hence, instead of the standard streams (keyboard for input and the display device for output), we will use the disk files for reading the data, and destination for storing the data.
除了上面示例中使用(使用标准流的 IO 操作)的读写模式外,还可以使用“r+”和“w+”(用于同时读写)、“b”(用于二进制模式)等各种其他模式打开文件流。
In addition to the read and write modes as used in the above example (IO operations with standard streams), the file stream can be opened in various other modes like "r+" and "w+" for simultaneous read/write, "b" for binary mode, etc.
要打开磁盘文件以便读取并获取其引用指针,请使用 fopen() 函数。
To open a disk file for reading and obtain its reference pointer, use the fopen() function.
$handle = fopen('file://' . __DIR__ . '/data.txt', 'r');
“file://”方案是默认方案。因此,可以很容易地将其删除,尤其是在处理本地文件时。
The "file://" scheme is the default. Hence, it can be easily dropped, especially when dealing with local files.
Note − 强烈建议关闭已打开的流。为此,请使用 fclose() 函数。
Note − It is always recommended to close the stream that was opened. Use the fclose() function for this purpose.
fclose($handle);
PHP 包含几个用于在文件流上执行读/写操作的内置函数。在后续的章节中,我们将探索文件系统函数。
PHP has several built-in functions for performing read/write operations on the file stream. In the subsequent chapters, we shall explore the filesystem functions.
PHP - Open File
PHP 的内置函数库提供了 fopen() 函数来打开一个文件或任何其他流,并返回其“引用指针”,也称为“句柄”。
PHP’s built-in function library provides fopen() function to open a file or any other stream and returns its "reference pointer", also called as "handle".
PHP 中的 fopen() 函数类似于 C 中的 fopen(),只不过在 C 中,它无法打开 URL。
The fopen() function in PHP is similar to fopen() in C, except that in C, it cannot open a URL.
Syntax of fopen()
fopen() 函数具有以下签名 -
The fopen() function has the following signature −
fopen(
string $filename,
string $mode,
bool $use_include_path = false,
?resource $context = null
): resource|false
参数 $filename 和 $mode 是必需的。以下是参数说明 -
The $filename and $mode parameters are mandatory. Here’s the explanation of the parameters −
-
$filename − This parameter is a string representing the resource to be opened. It may be a file in the local filesystem, or on a remote server with the scheme:// prefix.
-
$mode − A string that represents the type of access given to the file/resource.
-
$use_include_path − A Boolean optional parameter can be set to '1' or true if you want to search for the file in the include_path, too.
-
$context − A context stream resource.
Modes of Opening a File
PHP 允许文件以以下模式打开 -
PHP allows a file to be opened in the following modes −
Modes |
Description |
r |
Open a file for read only. |
w |
Open a file for write only. creates a new file even if it exists. |
a |
Open a file in append mode |
x |
Creates a new file for write only. |
r+ |
Open a file for read/write. |
w+ |
Open a file for read/write. creates a new file even if it exists. |
a+ |
Open a file for read/write in append mode. |
x+ |
Creates a new file for read/write. |
c |
Open the file for writing, if it doesn’t exist. However, if it exists, it isn’t truncated (as in w mode). |
c++ |
Open the file for read/write, if it doesn’t exist. However, if it exists, it isn’t truncated (as in w mode). |
e |
Set close-on-exec flag on the opened file descriptor. Only available in PHP compiled on POSIX.1-2008 conform systems. |
如果 fopen() 函数执行成功,则会返回绑定到文件流的“文件指针”或“句柄”资源。不过,如果失败,则会返回 FALSE,并发出 E_WARNING。
If the fopen() function is successfully executed, it returns a "file pointer" or "handle" resource bound to the file stream. However, if it fails, it returns false with E_WARNING being emitted.
$handle = fopen('a.txt, 'r');
var_dump($handle);
如果文件存在于当前目录中,则由 output 显示成功 −
If the file exists in the current directory, the success is shown by the output −
resource(5) of type (stream)
如果不显示,则会得到以下 error message −
If not, you get the following error message −
Warning: fopen(a.txt): Failed to open stream:
No such file or directory in a.php on line 2
bool(false)
Examples
以下示例显示了 fopen() 函数的不同用法 −
The following examples show different usages of the fopen() function −
<?php
$handle = fopen("hello.txt", "w");
$handle = fopen("c:/xampp/htdocs/welcome.png", "rb");
$handle = fopen("http://localhost/hello.txt", "r");
?>
请注意,当文件名是目录时,此函数也可能成功。在这种情况下,您可能需要使用 is_dir() 函数检查它是否是在执行任何读/写操作之前是一个文件。
Note that this function may also succeed when the filename is a directory. In that case, you may need to use the is_dir() function to check whether it is a file before doing any read/write operations.
一旦打开文件,就可以使用 fwrite() 或 fputs() 等函数在其中写入数据,并使用 fread() 和 fgets() 函数从中读取数据。
Once a file is opened, you can write data in it with the help of functions such as fwrite() or fputs(), and read data from it with fread() and fgets() functions.
PHP - Read File
PHP 中有许多选项可用于读取已使用 fopen() 函数打开的文件中的数据。PHP 库中的以下内置函数可以帮助我们执行读取操作 −
There are a number of options in PHP for reading data from a file that has been opened with the fopen() function. The following built-in functions in PHP’s library can help us perform the read operation −
-
fgets() − gets a line from the file pointer.
-
fgetc() − returns a string with a single character from the file pointer.
-
fread() − reads a specified number of bytes from the file pointer.
-
fscanf() − reads data from the file and parses it as per the specified format.
The fgets() Function
fgets() 函数可以从一个打开的文件中返回一行。此函数停止在指定长度的新行或 EOF 处返回,以先遇到的为准,并在失败时返回 false 。
The fgets() function can return a line from an open file. This function stops returning on a new line at a specified length or EOF, whichever comes first and returns false on failure.
fgets(resource $stream, ?int $length = null): string|false
此处, $stream 参数是使用 fopen() 函数以读取或读/写模式打开的文件的文件指针或句柄,而 $length 是指定要读取的字节数的可选参数。
Here, the $stream parameter is a file pointer or handle to the file opened with the fopen() function with read or read/write mode, and $length is an optional parameter specifying the number of bytes to be read.
当读取到“length-1”个字节或遇到新行时,读取操作结束,以先遇到的为准。
The read operation ends when "length-1" bytes are read or a newline is encountered, whichever is first.
Example
以下代码从“hello.txt”文件中读取第一行可用行 −
The following code reads the first available line from the "hello.txt" file −
<?php
$file = fopen("hello.txt", "r");
$str = fgets($file);
echo $str;
fclose($file);
?>
它将生成以下 output −
It will produce the following output −
Hello World
Example
你可以将 fgets() 函数放在一个循环中,以便在到达文件结尾之前读取文件。
You can put the fgets() function in a loop to read the file until the end of file is reached.
<?php
$file = fopen("hello.txt", "r");
while(! feof($file)) {
echo fgets($file). "<br>";
}
fclose($file);
?>
它将生成以下 output −
It will produce the following output −
Hello World
TutorialsPoint
PHP Tutorials
此处,我们使用了 feof() 函数,如果文件指针处于 EOF,则返回 true;否则,返回 false。
Here, we have used the feof() function which returns true if the file pointer is at EOF; otherwise returns false.
The fgetc() Function
fgetc() 函数返回从文件句柄当前位置读取的单个字符。遇到 EOF 时,它返回 false 。
The fgetc() function returns a single character read from the current position of the file handle. It returns false when EOF is encountered.
fgetc(resource $stream): string|false
此处, $stream 参数是使用 fopen() 函数以读取或读/写模式打开的文件的文件指针或句柄。
Here, the $stream parameter is a file pointer or handle to the file opened with the fopen() function with read or read/write mode.
Example
以下代码显示从“hello.txt”文件中读取的第一个字符 −
The following code displays the first character read from the "hello.txt" file −
<?php
$file = fopen("hello.txt", "r");
$str = fgets($file);
echo $str;
fclose($file);
?>
它将生成以下 output −
It will produce the following output −
H
Example
你还可以将 fgetc() 函数放在一个循环内,以便逐个字符地读取文件,直到到达 EOF。
You can also put the fgetc() function inside a loop to read the file character by character until it reaches EOF.
<?php
$file = fopen("hello.txt", "r");
while(! feof($file)) {
$char = fgetc($file);
if ($char == "\n")
echo "<br>";
echo $char;
}
fclose($file);
?>
它将生成以下 output −
It will produce the following output −
Hello World
TutorialsPoint
PHP Tutorials
The fread() Function
PHP 中的 fread() 函数是用于从文件读取数据的二进制安全函数。虽然 fgets() 函数仅从文本文件中读取,但 fread() 函数可以以二进制模式读取文件。
The fread() function in PHP is a binary-safe function for reading data from a file. While the fgets() function reads only from a text file, the fread() function can read a file in binary mode.
fread(resource $stream, int $length): string|false
此处, $stream 参数是使用 fopen() 函数以二进制读取或读/写模式( rb 或 rb+ )打开的文件的文件指针或句柄。 $length 参数指定要读取的字节数。
Here, the $stream parameter is a file pointer or handle to the file opened with the fopen() function with binary read or read/write mode (rb or rb+). The $length parameter specifies number of bytes to be read.
如果未给出 $length 参数,PHP 将尝试读取整个文件,直到到达 EOF,但需符合指定的块大小。
If the $length parameter is not given, PHP tries to read the entire file until EOF is reached, subject to the chunk size specified.
Example
以下代码读取一个文本文件 −
The following code reads a text file −
<?php
$name = "hello.txt";
$file = fopen($name, "r");
$data = fread($file, filesize($name));
echo $data;
fclose($file);
?>
它将生成以下 output −
It will produce the following output −
Hello World TutorialsPoint PHP Tutorials
Example
你还可以读取以 rb 模式打开的图像文件等非 ASCII 文件。
You can also read a non-ASCII file such as an image file opened in rb mode.
<?php
$name = "welcome.png";
$file = fopen($name, "rb");
$data = fread($file, filesize($name));
var_dump($data);
fclose($file);
?>
浏览器将“var_dump”信息显示为以下内容 −
The browser displays the "var_dump" information as the following −
The fscanf() Function
PHP 中的 fscanf() 函数从文件流中读取输入并根据特定格式对其进行解析,从而将其转换为指定类型的变量。每次调用此函数都将从文件中读取一行。
The fscanf() function in PHP reads the input from a file stream and parses it according to the specified format, thereby converts it into the variables of respective types. Each call to the function reads one line from the file.
fscanf(resource $stream, string $format, mixed &...$vars): array|int|false|null
此处, $stream 参数是指针,它指向使用 fopen() 函数打开的文件,且为读取模式。同时, $format 为一个字符串,包含下列一个或多个格式说明符 −
Here, the $stream parameter is the handle to the file opened with the fopen() function and in read mode. And, $format is a string containing one or more of the following formatting specifiers −
-
%% − Returns a percent
-
%b − Binary number
-
%c − The character according to the ASCII value
-
%f − Floating-point number
-
%F − Floating-point number
-
%o − Octal number
-
%s − String
-
%d − Signed decimal number
-
%e − Scientific notation
-
%u − Unsigned decimal number
-
%x − Hexadecimal number for lowercase letters
-
%X − Hexadecimal number for uppercase letters
$vars 是一个可选参数,它通过引用指定包含解析值的变量。
$vars is an optional parameter that specifies variables by reference which will contain the parsed values.
假设“employees.txt”文件在与下方 PHP 脚本相同的目录中。文本文件中每一行都有每个员工的 name, email, post 和 salary ,由制表符分隔。
Assuming that the "employees.txt" file is available in the same directory in which the PHP script given below is present. Each line in the text file has name, email, post and salary of each employee, separated by tab character.
Example
以下 PHP 脚本使用 fscanf() 函数中的格式说明符读取文件 −
The following PHP script reads the file using the format specifiers in fscanf() function −
<?php
$fp = fopen("employees.txt", "r");
while ($employee_info = fscanf($fp, "%s\t%s\t%s\t%d\n")) {
list ($name, $email, $post, $salary) = $employee_info;
echo "<b>Name</b>: $name <b>Email</b>:
$email <b>Salary</b>: Rs. $salary <br>";
}
fclose($fp);
?>
它将生成以下 output −
It will produce the following output −
Name: Ravishankar Email: ravi@gmail.com Salary: Rs. 40000
Name: Kavita Email: kavita@hotmail.com Salary: Rs. 25000
Name: Nandkumar Email: nandu@example.com Salary: Rs. 30000
PHP - Write File
PHP 内置函数库提供了两个函数来执行文件流的写操作。这些函数是 fwrite() 和 fputs() 。
PHP’s built-in function library provides two functions to perform write operations on a file stream. These functions are fwrite() and fputs().
为了能在文件中写入数据,必须以写模式 (w)、追加模式 (a)、读/写模式 (r+ 或 w+) 或二进制写/追加模式 (rb+、wb+ 或 wa) 打开此文件。
To be able to write data in a file, it must be opened in write mode (w), append mode (a), read/write mode (r+ or w+) or binary write/append mode (rb+, wb+ or wa).
The fputs() Function
fputs() 函数将字符串写入以可写模式打开的文件中。
The fputs() function writes a string into the file opened in a writable mode.
fputs(resource $stream, string $string, int $length)
此处, $stream 参数是对以可写模式打开的文件句柄。 $string 参数是要写入的数据,而 $length 是可选参数,指定要写入的最大字节数。
Here, the $stream parameter is a handle to a file opened in a writable mode. The $string parameter is the data to be written, and $length is an optional parameter that specifies the maximum number of bytes to be written.
fputs() 函数返回写入的字节数,或 false (如果函数执行失败)。
The fputs() function returns the number of bytes written, or false if the function is unsuccessful.
Example
以下代码打开了新文件,其中写入了一个字符串,并返回写入的字节数。
The following code opens a new file, writes a string in it, and returns the number of bytes written.
<?php
$fp = fopen("hello.txt", "w");
$bytes = fputs($fp, "Hello World\n");
echo "bytes written: $bytes";
fclose($fp);
?>
它将生成以下 output −
It will produce the following output −
bytes written: 12
Example
如果需要在较早存在的文件中添加文本,必须以追加模式打开此文件( a )。让我们在前面的示例中再向同一文件中添加一个字符串。
If you need to add text in an earlier existing file, it must be opened in append mode (a). Let us add one more string in the same file in previous example.
<?php
$fp = fopen("hello.txt", "a");
$bytes = fputs($fp, "Hello PHP");
echo "bytes written: $bytes";
fclose($fp);
?>
如果你在文本编辑器中打开“hello.txt”文件,你应该可以看到其中的这两行。
If you open the "hello.txt" file in a text editor, you should see both the lines in it.
Example
在以下 PHP 脚本中,一个已存在的文件 (hello.txt) 会在循环中逐行读取,并且每行会被写入到另一个文件 (new.txt)
In the following PHP script, an already existing file (hello.txt) is read line by line in a loop, and each line is written to another file (new.txt)
假设“hello.txt”包含以下文本:
It is assumed thar "hello.txt" consists of following text −
Hello World
TutorialsPoint
PHP Tutorials
以下是创建现有文件副本的 PHP 代码:
Here is the PHP code to create a copy of an existing file −
<?php
$file = fopen("hello.txt", "r");
$newfile = fopen("new.txt", "w");
while(! feof($file)) {
$str = fgets($file);
fputs($newfile, $str);
}
fclose($file);
fclose($newfile);
?>
新创建的“new.txt”文件应具有完全相同的内容。
The newly created "new.txt" file should have exactly the same contents.
The fwrite() Function
frwrite() 函数是 fread() 函数的对应函数。它执行二进制安全写操作。
The frwrite() function is a counterpart of fread() function. It performs binary-safe write operations.
fwrite(resource $stream, string $data, ?int $length = null): int|false
此处, $stream 参数是指向以可写模式打开的文件的资源。要写入该文件的数据提供在 $data 参数中。可以提供可选的 $length 参数来指定要写入的字节数。其应该是 int ,在写入长度字节之后或到达数据的末尾(以先发生的为准)后,写操作会停止。
Here, the $stream parameter is a resource pointing to the file opened in a writable mode. Data to be written to the file is provided in the $data parameter. The optional $length parameter may be provided to specify the number of bytes to be written. It should be int, writing will stop after length bytes have been written or the end of data is reached, whichever comes first.
fwrite() 函数返回写入的字节数,或 false (如果失败,则会同时返回 E_WARNING)。
The fwrite() function returns the number of bytes written, or false on failure along with E_WARNING.
Example
以下程序打开新文件,执行写操作并显示写入的字节数。
The following program opens a new file, performs write operation and displays the number of bytes written.
<?php
$file = fopen("/PhpProject/sample.txt", "w");
echo fwrite($file, "Hello Tutorialspoint!!!!!");
fclose($file);
?>
Example
在下面给出的示例代码中,一个现有文件“welcome.png”是以二进制读模式打开的。fread() 函数用于将它的字节读入“$data”变量中,然后将其写入到另一个文件“new.png”:
In the example code given below, an existing file "welcome.png" in opened in binary read mode. The fread() function is used to read its bytes in "$data" variable, and in turn written to another file "new.png" −
<?php
$name = "welcome.png";
$file = fopen($name, "rb");
$newfile = fopen("new.png", "wb");
$size = filesize($name);
$data = fread($file, $size);
fwrite($newfile, $data, $size);
fclose($file);
fclose($newfile);
?>
运行以上代码。当前目录中现在应有一个现有“welcome.png”文件的副本。
Run the above code. The current directory should now have a copy of the existing "welcome.png" file.
PHP - File Existence
在对文件执行任何处理之前,经常实用的是检查你现在尝试打开的文件是否确实存在。如果不是,程序可能会发出 runtime exception 。
It is often handy to check if the file you are trying to open really exists in the first place before performing any processing on it. Otherwise, the program is likely to raise a runtime exception.
PHP 的内置函数库在这方面提供了一些实用函数。本章中讨论的某些函数有 −
PHP’s built-in library provides some utility functions in this regard. Some of the functions we shall discuss in this chapter are −
-
file_exists() − tests if the file exists
-
is_file() − if the handle returned by the fopen() refers to a file or directory.
-
is_readable() − test if the file you have opened allows reading data
-
is_writable() − test if writing data in the file is allowed
The file_exists() Function
此函数适用于文件和目录。它检查给定的文件或目录是否存在。
This function works with a file as well as a directory. It checks whether the given file or directory exists or not.
file_exists(string $filename): bool
此函数的唯一参数是一个字符串,代表带有完整路径的文件/目录。此函数将根据文件是否存在返回真或假。
The only parameter to this function is a string representing the file/directory with full path. The function returns true or false depending upon the file exists or not.
Example
以下程序检查文件 “hello.txt” 是否存在。
The following program checks if the file "hello.txt" exists or not.
<?php
$filename = 'hello.txt';
if (file_exists($filename)) {
$message = "The file $filename exists";
} else {
$message = "The file $filename does not exist";
}
echo $message;
?>
如果当前目录中存在文件,则消息为 −
If the file does exist in the current directory, the message is −
The file hello.txt exists
如果没有,则消息为 −
If not, the message is −
The file hello.txt does not exist
Example
指向文件的字符串可能具有相对路径或绝对路径。假设 “hello.txt” 文件可在当前目录中的 “hello” 子目录中找到。
The string pointing to the file may have a relative or absolute path. Assuming that "hello.txt" file is available in a "hello" subdirectory which is inside the current directory.
<?php
$filename = 'hello/hello.txt';
if (file_exists($filename)) {
$message = "The file $filename exists";
} else {
$message = "The file $filename does not exist";
}
echo $message;
?>
它将生成以下 output −
It will produce the following output −
The file hello/hello.txt exists
Example
尝试如下指定绝对路径 −
Try giving the absolute path as below −
<?php
$filename = 'c:/xampp/htdocs/hello.txt';
if (file_exists($filename)) {
$message = "The file $filename exists";
} else {
$message = "The file $filename does not exist";
}
echo $message;
?>
它将生成以下 output −
It will produce the following output −
The file c:/xampp/htdocs/hello.txt exists
The is_file() Function
file_exists() 函数为现有文件和目录返回 true 。 is_file() 函数可帮助您确定是否为文件。
The file_exists() function returns true for existing file as well as directory. The is_file() function helps you to determine if it’s a file.
is_file ( string $filename ) : bool
以下示例展示了 is_file() 函数的工作原理 −
The following example shows how the is_file() function works −
<?php
$filename = 'hello.txt';
if (is_file($filename)) {
$message = "$filename is a file";
} else {
$message = "$filename is a not a file";
}
echo $message;
?>
output 表明它是一个文件 −
The output tells that it is a file −
hello.txt is a file
现在,将 “$filename” 更改为目录并查看结果 −
Now, change the "$filename" to a directory, and see the result −
<?php
$filename = hello;
if (is_file($filename)) {
$message = "$filename is a file";
} else {
$message = "$filename is a not a file";
}
echo $message;
?>
现在,您将得知 “hello” 不是文件。
Now you will be told that "hello" is not a file.
请注意, is_file() 函数接受 $filename ,仅在 $filename 为文件且存在时返回 true 。
Note that The is_file() function accepts a $filename and returns true only if the $filename is a file and exists.
The is_readable() Function
有时,您可能希望事先检查是否可以读取文件。 is_readable() 函数可以确定此事实。
Sometimes, you may want to check before hand if the file can be read from or not. The is_readable() function can ascertain this fact.
is_readable ( string $filename ) : bool
Example
下面是 is_readable() 函数工作原理的示例 −
Given below is an example of how the is_readable() function works −
<?php
$filename = 'hello.txt';
if (is_readable($filename)) {
$message = "$filename is readable";
} else {
$message = "$filename is not readable";
}
echo $message;
?>
它将生成以下 output −
It will produce the following output −
hello.txt is readable
The is_writable() Function
您可以使用 is_writable() 函数检查文件是否存在以及是否可以在给定文件上执行写操作。
You can use the is_writable() function to can check if a file exists and if it is possible to perform write operation on the given file.
is_writable ( string $filename ) : bool
Example
以下示例展示了 is_writable() 函数的工作原理 −
The following example shows how the is_writable() function works −
<?php
$filename = 'hello.txt';
if (is_writable($filename)) {
$message = "$filename is writable";
} else {
$message = "$filename is not writable";
}
echo $message;
?>
对于普通归档文件,程序指出它是可写的。但是,将其属性更改为 “read_only” 并运行程序。您现在获得 −
For a normal archived file, the program tells that it is writable. However, change its property to "read_only" and run the program. You now get −
hello.txt is writable
PHP - Download File
大多数现代浏览器允许自动下载特定类型的文件,无需任何服务器端代码(例如 PHP 脚本)。例如,zip 文件或 EXE 文件。
Most modern browsers allow files of certain types to be downloaded automatically, without any server-side code such as a PHP script. For example, a zip file, or an EXE file.
如果 HTML 超链接指向 ZIP 或 EXE 文件,浏览器将下载它并弹出一个保存对话框。但是,不会下载文本文件、图像文件等,而是在浏览器中打开,您可将其保存到本地文件系统。
If an HTML hyperlink points to a ZIP or EXE file, the browser downloads it and pops up a save dialog. However, text files, image files, etc., are not downloaded but opened in the browser, which you can save to your local filesystem.
The readfile() Function
要下载此类文件(而不是让浏览器自动打开它们),我们可以使用 PHP 内置函数库中的 readfile() 函数。
To download such files (instead of the browser automatically opening them), we can use the readfile() function in PHP’s built-in function library.
readfile(string $filename,
bool $use_include_path = false,
?resource $context = null)
: int|false
此函数读取文件并将其写入输出缓冲区。
This function reads a file and writes it to the output buffer.
第二个参数 $use_include_path 默认情况下为 false,因此将下载当前目录中的文件。如果设置为 true ,将搜索添加到 php.ini 配置的 include_path 设置中的目录以找到要下载的文件。
The second parameter $use_include_path is false by default, hence the file in the current directory will be downloaded. If set to true, the directories added to the include_path setting of php.ini configuration will be searched to locate the file to be downloaded.
readfile() 函数返回已读取的字节数,即使已成功完成。
The readfile() function returns the number of bytes read or false even it is successfully completed or not.
Example
以下 PHP 脚本展示了 readfile() 函数的用法。
The following PHP script shows the usage of readfile() function.
要下载文件, Content-Type 响应头应该设置为 application/octect-stream 。此 MIME 类型是二进制文件的默认值。浏览器通常不会执行该文件,甚至不会询问是否应该执行。
To download a file, the Content-Type response header should be set to application/octect-stream. This MIME type is the default for binary files. Browsers usually don’t execute it, or even ask if it should be executed.
此外,将 Content-Disposition 头设置为 attachment 将提示“另存为”对话框弹出。
Additionally, setting the Content-Disposition header to attachment prompts the "Save As" dialog to pop up.
<?php
$filePath = 'welcome.png';
// Set the Content-Type header to application/octet-stream
header('Content-Type: application/octet-stream');
// Set the Content-Disposition header to the filename of the downloaded file
header('Content-Disposition: attachment; filename="'. basename($filePath).'"');
// Read the contents of the file and output it to the browser.
readfile($filePath);
?>
将在文档根文件夹中将上述脚本保存为“download.php”。确保要下载的文件存在于同一文件夹中。
Save the above script as "download.php" in the document root folder. Make sure that the file to be downloaded is present in the same folder.
启动服务器,并在浏览器中访问 http://localhost/download.php 。你将获得如下所示的“另存为”对话框 -
Start the server and visit http://localhost/download.php in the browser. You will get a "Save As" dialog as below −
你可以选择一个名称并下载该文件。
You can select a name and download the file.
对于大型文件,你可以从文件流中以一定预定义大小的块中读取它。如果 Content-Disposition 头设置为“attachment”(如前一个示例中),浏览器将提供将其保存在本地文件系统中。
For a large file, you can read it from the file stream in the chunk of a certain predefined size. The browser offers to save it in the local filesystem, if the Content-Disposition head is set to "attachment", as in the previous example.
<?php
$filename = 'welcome.png';
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . basename($filename) . '"');
$handle = fopen($filename, 'rb');
$buffer = '';
$chunkSize = 1024 * 1024;
ob_start();
while (!feof($handle)) {
$buffer = fread($handle, $chunkSize);
echo $buffer;
ob_flush();
flush();
}
fclose($handle);
?>
PHP - Copy File
你可以通过三种不同的方式将现有文件复制到新文件中 -
You can copy an existing file to a new file in three different ways −
-
Reading a line from one and writing to another in a loop
-
Reading entire contents to a string and writing the string to another file
-
Using PHP’s built-in function library includes copy() function.
Method 1
在第一种方法中,你可以从现有文件中读取每一行,并写入新文件,直到现有文件到达文件结尾。
In the first approach, you can read each line from an existing file and write into a new file till the existing file reaches the end of file.
在以下 PHP 脚本中,一个已存在的文件 (hello.txt) 会在循环中逐行读取,并且每行会被写入到另一个文件 (new.txt)
In the following PHP script, an already existing file (hello.txt) is read line by line in a loop, and each line is written to another file (new.txt)
我们假设“hello.txt”包含以下文本 -
It is assumed that "hello.txt" contains the following text −
Hello World
TutorialsPoint
PHP Tutorials
Example
以下是创建现有文件副本的 PHP 代码:
Here is the PHP code to create a copy of an existing file −
<?php
$file = fopen("hello.txt", "r");
$newfile = fopen("new.txt", "w");
while(! feof($file)) {
$str = fgets($file);
fputs($newfile, $str);
}
fclose($file);
fclose($newfile);
?>
新创建的“new.txt”文件应具有完全相同的内容。
The newly created "new.txt" file should have exactly the same contents.
Method 2
我们在此处从 PHP 库中使用了两个内置函数 -
Here we use two built-in functions from the PHP library −
file_get_contents(
string $filename,
bool $use_include_path = false,
?resource $context = null,
int $offset = 0,
?int $length = null
): string|false
此函数将整个文件读入字符串。$filename 参数是一个字符串,它包含要读取的文件的名称。
This function reads the entire file into a string. The $filename parameter is a string containing the name of the file to be read
另一个函数是 -
The other function is −
file_put_contents(
string $filename,
mixed $data,
int $flags = 0,
?resource $context = null
): int|false
此函数会 将 $data 中的内容放入 $filename 中。它会返回写入的字节数。
The function puts the contents of $data in $filename. It returns the number of bytes written.
Example
在下面的示例中,我们在字符串 $data 中读取了“hello.txt”中的内容,并将其用作参数写入“test.txt”文件中。
In the following example, we read contents of "hello.txt" in a string $data, and use it as a parameter to write into "test.txt" file.
<?php
$source = "hello.txt";
$target = "test.txt";
$data = file_get_contents($source);
file_put_contents($target, $data);
?>
Method 3
PHP 提供 copy() 函数,专门用于执行复制操作。
PHP provides the copy() function, exclusively to perform copy operation.
copy(string $from, string $to, ?resource $context = null): bool
$from 参数是一个包含现有文件内容的字符串。 $to 参数也是一个字符串,其中包含要创建的新文件名称。如果目标文件已存在,它将被覆盖。
The $from parameter is a string containing the existing file. The $to paramenter is also a string containing the name of the new file to be created. If the target file already exists, it will be overwritten.
复制操作会根据文件是否成功复制返回 true 或 false 。
The copy operation will return true or false based on the file being successfully copied or not.
PHP - Append File
在 PHP 中,fopen() 函数返回一个文件指针,用于不同的打开模式,例如“w”表示写入模式,“r”表示读取模式,“r+”或“r+”表示同时读/写操作,以及“a”表示追加模式。
In PHP, the fopen() function returns the file pointer of a file used in different opening modes such as "w" for write mode, "r" read mode and "r+" or "r+" mode for simultaneous read/write operation, and "a" mode that stands for append mode.
当使用“w”模式参数打开文件时,它总是打开一个新文件。这意味着如果文件已经存在,其内容将丢失。随后的 fwrite() 函数将数据放在文件开始位置。
When a file is opened with "w" mode parameter, it always opens a new file. It means that if the file already exists, its content will be lost. The subsequent fwrite() function will put the data at the starting position of the file.
假设存在一个名为 “new.txt” 的文件,内容如下:
Assuming that a file "new.txt" is present with the following contents −
Hello World
TutorialsPoint
PHP Tutorial
以下语句 -
The following statement −
$fp = fopen("new.txt", "w");
删除所有现有数据,然后再写入新内容。
Erases all the existing data before new contents are written.
Read/Write Mode
显然,在以“r”模式打开文件时无法添加新数据。然而, “r+”或“w+” mod 在“r/w”模式下打开文件,但在打开文件后一个 fwrite() 语句仍会覆盖内容。
Obviously, it is not possible to add new data if the file is opened with "r" mode. However, "r+" or "w+" mod opens the file in "r/w" mode, but still a fwrite() statement immediately after opening a file will overwrite the contents.
Example
查看以下代码:
Take a look at the following code −
<?php
$fp = fopen("new.txt", "r+");
fwrite($fp, "PHP-MySQL Tutorial\n");
fclose($fp);
?>
利用这段代码, “new.txt” 文件的内容现在变为 −
With this code, the contents of the "new.txt" file will now become −
PHP-MySQL Tutorial
lsPoint
PHP Tutorial
为了确保新内容添加到现有文件的末尾,我们需要在写操作前手动将文件指针放置到末尾。(文件的初始指针位置在第 0 个字节)
To ensure that the new content is added at the end of the existing file, we need to manually put the file pointer to the end, before write operation. (The initial file pointer position is at the 0th byte)
The fseek() Function
PHP 的 fseek() 函数让您能够将文件指针放置到任何您想要的位置 −
PHP’s fseek() function makes it possible to place the file pointer anywhere you want −
fseek(resource $stream, int $offset, int $whence = SEEK_SET): int
$whence 参数是指从哪里计算偏移量。它的值有 −
The $whence parameter is from where the offset is counted. Its values are −
-
SEEK_SET − Set position equal to offset bytes.
-
SEEK_CUR − Set position to current location plus offset.
-
SEEK_END − Set position to end-of-file plus offset.
Example
因此,我们需要利用 fseek() 函数将指针移动到末尾,如此以下代码,将新内容添加到末尾。
So, we need to move the pointer to the end with the fseek() function as in the following code which adds the new content to the end.
<?php
$fp = fopen("new.txt", "r+");
fseek($fp, 0, SEEK_END);
fwrite($fp, "\nPHP-MySQL Tutorial\n");
fclose($fp);
?>
现在检查 “new.txt” 的内容。它将具有以下文本 −
Now check the contents of "new.txt". It will have the following text −
Hello World
TutorialsPoint
PHP Tutorial
PHP-MySQL Tutorial
Append Mode
无需手动将指针移动到末尾,fopen() 函数中的“a”参数会以追加模式打开文件。每个 fwrite() 语句将内容添加到现有内容的末尾,通过自动将指针移动到 SEEK_END 位置。
Instead of manually moving the pointer to the end, the "a" parameter in fopen() function opens the file in append mode. Each fwrite() statement adds the content at the end of the existing contents, by automatically moving the pointer to SEEK_END position.
<?php
$fp = fopen("new.txt", "a");
fwrite($fp, "\nPHP-MySQL Tutorial\n");
fclose($fp);
?>
fopen() 函数允许的模式之一是“r+”模式,使用此模式,文件执行读/追加操作。要读取任何位置的数据,您可以利用 fseek() 将指针置于所需的字节。但是,每次 fwrite() 操作仅在末尾写新内容。
One of the allowed modes for fopen() function is "r+" mode, with which the file performs read/append operation. To read data from any position, you can place the pointer to the desired byte by fseek(). But, every fwrite() operation writes new content at the end only.
Example
在下面的程序中,文件在“a+”模式下打开。为了读取第一行,我们使文件位置偏移 0——从开头开始。然而, fwrite() 语句仍然在末尾添加新内容,并且不会覆盖后面的一行,就像在“r+”模式打开时那样。
In the program below, the file is opened in "a+" mode. To read the first line, we shift the file position to 0the position from beginning. However, the fwrite() statement still adds new content to the end and doesn’t overwrite the following line as it would have if the opening mode "r+" mode.
<?php
$fp = fopen("new.txt", "a+");
fseek($fp, 0, SEEK_SET);
$data = fread($fp, 12);
echo $data;
fwrite($fp, "PHP-File Handling");
fclose ($fp);
?>
因此,如果文件在“r+/w+”模式或“a/a+”模式下打开,我们就可以追加数据到现有文件。
Thus, we can append data to an existing file if it is opened in "r+/w+" mode or "a/a+" mode
PHP - Delete File
PHP 没有 delete 关键字或 delete() 函数。它提供 unlink() 函数代替,此函数被调用时会从文件系统中删除一个文件。它类似于 Unix/C 中的 unlink 函数。
PHP doesn’t have either a delete keyword or a delete() function. Instead, it provides the unlink() function, which when called, deletes a file from the filesystem. It is similar to Unix/C unlink function.
如果无法完成删除操作,则 PHP 会返回 false 并显示一条 E_WARNING 消息。
If the delete operation could not be completed, PHP returns false and shows an E_WARNING message.
unlink(string $filename, ?resource $context = null): bool
unlink() 函数的必需字符串参数是一个字符串,该字符串指明要删除的文件。
The mandatory string parameter to unlink() function is a string that refers to the file to be deleted.
Example
以下代码演示了 unlink() 函数的简单用法:
The following code demonstrates a simple use of the unlink() function −
<?php
$file = "my_file.txt";
if (unlink($file)) {
echo "The file was deleted successfully.";
} else {
echo "The file could not be deleted.";
}
?>
Deleting the Symlink to a File
unlink() 函数还可以删除到某个文件符号链接。但是,删除符号链接并不会删除原始文件。符号链接是既存文件的一个捷径。
The unlink() function can also delete a symlink to a file. However, deleting a symlink doesn’t delete the original file. A symlink is a shortcut to an existing file.
在 Windows 中,以管理员权限打开命令提示符,并使用 mlink 命令和 /h 开关来创建到某个文件的符号链接。( /j 开关用于目录的符号链接)
In Windows, open a command prompt with administrator privilege and use the mlink command with /h switch to create a symlink to a file. (/j switch is used for symlink to a folder)
mklink /h hellolink.lnk hello.txt
Hardlink created for hellolink.lnk <<===>> hello.txt
在 Ubuntu Linux 中,要创建到某个文件的符号链接,可使用以下命令:
In Ubuntu Linux, to create a symbolic link to a file, you would use the following command −
ln -s /path/to/original_file /path/to/symlink
要创建到某个目录的符号链接,可使用以下命令:
To create a symbolic link to a directory, you would use the following command −
ln -s /path/to/original_directory /path/to/symlink
在 PHP 中,还有一个 symlink() 函数用于此目的。
In PHP, there is also a symlink() function for the purpose.
symlink(string $target, string $link): bool
Example
使用以下代码创建符号链接:
Create a symlink with the following code −
<?php
$target = 'hello.txt';
$link = 'hellolink.lnk';
symlink($target, $link);
echo readlink($link);
?>
现在,删除上面创建的符号链接:
Now delete the symlink created above −
unlink("hellolink.lnk");
如果查看当前工作目录,则符号链接将被删除,而原始文件保持不变。
If you check the current working directory, the symlink will be deleted, leaving the original file intact.
How to Rename a File in PHP
您可借助操作系统的控制台中相应命令来更改现有文件的名称。例如,Linux 终端中的“ mv command ”或 Windows 命令提示符中的“ rename command ”可帮助您更改某个文件的名称。
You can change the name of an existing file with the help of respective command from the console of an operating system. For example, the "mv command in Linux terminal or the "rename command" in Windows command prompt helps you to change the name of a file.
但是,要以编程方式重命名文件,PHP 的内置库中包含一个 rename() 函数。
However, to rename a file programmatically, PHP’s built-in library includes a rename() function.
以下是 rename() 函数的 syntax -
Here is the syntax of the rename() function −
rename(string $from, string $to, ?resource $context = null): bool
$from 和 $to 两个字符串分别是文件的名字,文件已经存在和新的。rename() 函数会尝试将 $from 重命名为 $to ,如果需要的话,它会将其移动到其他目录。
Both $from and $to strings are the names of files, existing and new respectively. The rename() function attempts to rename $from to $to, moving it between directories if necessary.
如果你正在重命名 file 而 $to 已存在,那么 $to 将被覆写。如果你正在重命名 directory 而 $to 存在,那么这个函数将抛出一个 warning 。
If you are renaming a file and $to already exists, then it will be overwritten. If you are renaming a directory and $to exists, then this function will emit a warning.
将“hello.txt”重命名为“test.txt” -
To change the name of "hello.txt" to "test.txt" −
<?php
rename("hello.txt", "test.txt");
?>
你也可以使用一个间接的方法来重命名文件。复制一个已存在的文件,并且删除原件。这样也可以将“hello.txt”重命名为“test.txt” -
You can also employ a little indirect approach for renaming a file. Make a copy of an existing file and delete the original one. This also renames "hello.txt" to "test.txt" −
copy("hello.txt", "test.txt");
unlink("hello.txt");
PHP – Handle CSV File
流行的电子表格程序使用 CSV 文件格式(代表以逗号分隔的值)来将工作表数据导出为纯文本。文件中的每一行代表工作表中的一行,每一列中的值都用逗号分隔。
Popular spreadsheet programs use the CSV file format (which stands for Comma Separated Values) to export worksheet data in plain text. Each line in the file represents one row of the worksheet, with values in each column separated by commas.
PHP 的文件系统函数库提供了两个函数 - fgetcsv() 和 fputcsv() - 分别用来从 CSV 文件读取数据到一个数组中,并将数组元素放入 CSV 文件中。
PHP’s filesystem function library provides two functions – fgetcsv() and fputcsv() – respectively to read data from a CSV file into an array and put the array elements in a CSV file.
The fgetcsv() Function
getcsv() 函数从文件指针读取一行,并将其解析为 CSV 字段。
The getcsv() function reads the line from the file pointer, and parses it into CSV fields.
fgetcsv(
resource $stream,
?int $length = null,
string $separator = ",",
string $enclosure = "\"",
string $escape = "\\"
): array|false
$stream 参数是一个文件资源的句柄,在 read mode 中打开。用来解析字段的默认分隔符号是逗号,如果你需要,可以指定任何其他符号。
The $stream parameter is a handle to the file resource, opened in read mode. The default separator symbol to parse the fields is comma, you can specify any other symbol if required.
fgetcsv() 函数返回一个包含字段的索引数组。如果函数遇到任何错误,它会返回 false 。
The fgetcsv() function returns an indexed array containing the fields. If the function encounters any error, it returns false.
为了演示 fgetcsv() 函数的使用,将以下文本保存在当前工作目录中,作为“hello.txt”。
To demonstrate the use of fgetcsv() function, store the following text as "hello.txt" in the current working directory.
Name, Email, Post, Salary
Ravishankar, ravi@gmail.com, Manager, 40000
Kavita, kavita@hotmail.com, Assistant, 25000
Nandkumar, nandu@example.com, Programmer, 30000
Example
下面的 PHP 代码从这个文件读取 CSV 数据,并返回一个数组。然后在 HTML 表格中呈现数组中的字段 -
The following PHP code reads the CSV data from this file, and returns an array. The fields in the array are then rendered in a HTML table −
<?php
$filename = 'hello.csv';
$data = [];
// open the file
$f = fopen($filename, 'r');
if ($f === false) {
die('Cannot open the file ' . $filename);
}
// read each line in CSV file at a time
while (($row = fgetcsv($f)) !== false) {
$data[] = $row;
}
// close the file
fclose($f);
echo "<table border=1>";
foreach ($data as $row) {
echo "<tr>";
foreach($row as $val) {
echo "<td>$val</td>";
}
echo "</tr>";
}
echo "</table>";
?>
它将生成以下 output −
It will produce the following output −
Name |
Post |
Salary |
|
Ravishankar |
Manager |
40000 |
|
Kavita |
Assistant |
25000 |
|
Nandkumar |
Programmer |
30000 |
The fputcsv() Function
fputcsv() 函数将一个索引数组及其元素(由逗号分隔)放入 CSV 文件当前文件指针的位置。
Te fputcsv() function puts an indexed array with its elements separated by commas, at the current file pointer position of a CSV file.
fputcsv(
resource $stream,
array $fields,
string $separator = ",",
string $enclosure = "\"",
string $escape = "\\",
string $eol = "\n"
): int|false
目标文件必须以写模式打开。第二个强制参数是一个包含由逗号分隔的字段的数组。就像在 fgetcsv() 函数中,默认分隔符号是逗号。
The target file must be opened in write mode. The second mandatory parameter is an array consisting of comma separated fields. As in case of fgetcsv() function, the default separator is comma.
Example
在下面的代码中,一个由逗号分隔的值组成的二维数组被写入一个 CSV 文件。
In the following code, a two dimensional array of comma separated values is written into a CSV file.
<?php
$data = [
["Name", "Email", "Post", "Salary"],
["Ravishankar", "ravi@gmail.com", "Manager", "40000"],
["Kavita", "kavita@hotmail.com", "Assistant", "25000"],
["Nandkumar", "nandu@example.com", "Programmer", "30000"],
];
$filename = 'employee.csv';
// open csv file for writing
$f = fopen($filename, 'w');
if ($f === false) {
die('Error opening the file ' . $filename);
}
// write each row at a time to a file
foreach ($data as $row) {
fputcsv($f, $row);
}
// close the file
fclose($f);
?>
在上面程序执行之后,“employee.csv”文件应当被创建在当前工作目录中。
The "employee.csv" file should be created in the current working directory, after the above program is executed.
PHP – File Permissions
权限的概念是 Unix/Linux 文件系统核心的内容。权限决定了谁可以访问文件,以及如何访问文件。Linux 中的文件权限受 chmod command 控制,该命令可以在 Linux 终端里面运行。PHP 提供了 chmod() function ,您可以使用它以编程方式处理文件权限。
The concept of permissions is at the core of Unix/Linux file system. The permissions determine who can access a file and how one can access a file. File permissions in Linux are manipulated by the chmod command, which can be run inside the Linux terminal. PHP provides the chmod() function with which you can handle file permissions programmatically.
仅当您在 Linux 操作系统上工作时,PHP 的 chmod() 函数才是有效的。它在 Windows 上不起作用,因为 Windows操作系统的文件权限控制机制不同。
PHP’s chmod() function is effective only when you are working on a Linux OS. It doesn’t work on Windows, as Windows OS has a different mechanism of controlling file permissions.
要查看启用的文件权限,请使用 “ ls -l ” 命令获取文件列表(长列表)
To view the permissions enabled on a file, obtain the list of files using the "ls -l" command (long listing)
mvl@GNVBGL3:~$ ls -l
-rwxr-xr-x 1 mvl mvl 16376 May 5 21:52 a.out
-rw-r--r-- 1 mvl mvl 83 May 5 21:52 hello.cpp
-rwxr-xr-x 1 mvl mvl 43 Oct 11 14:50 hello.php
-rwxr-xr-x 1 mvl mvl 43 May 8 10:01 hello.py
drwxr-xr-x 5 mvl mvl 4096 Apr 20 21:52 myenv
第一列包含每个文件的权限标志。第三列和第四列指出每个文件的所有者和组,然后是大小、日期和时间以及文件名。
The first column contains permission flags of each file. Third and fourth columns indicate the owner and group of each file, followed by size, date and time, and the file name.
权限字符串有十个字符,它们的含义解释如下 −
The permissions string has ten characters, their meaning is described as follows −
Position |
Meaning |
1 |
"d" if a directory, "-" if a normal file |
2, 3, 4 |
read, write, execute permission for user (owner) of file |
5, 6, 7 |
read, write, execute permission for group |
8, 9, 10 |
read, write, execute permission for other (world) |
权限字符串中的字符有以下含义:
The characters in the permission string have following meaning −
Value |
Meaning |
- |
Flag is not set. |
r |
File is readable. |
w |
File is writable. For directories, files may be created or removed. |
x |
File is executable. For directories, files may be listed. |
如果你考虑上述列表中的第一个条目:
If you consider the first entry in the above list −
-rwxr-xr-x 1 mvl mvl 16376 May 5 21:52 a.out
“a.out”文件由用户“mvl”和组“mvl”所有。它是一个普通文件,所有者有“读/写/执行”权限,并且组和其他人有“读/执行”权限。
The "a.out" file is owned by the user "mvl" and group "mvl". It is a normal file with "read/write/execute" permissions for the owner, and "read/ execute" permissions for the group as well as others.
权限标志的二进制和八进制表示可以用下表理解:
The binary and octal representation of permission flags can be understood with the following table −
Octal Digit |
Binary Representation (rwx) |
Permission |
0 |
000 |
none |
1 |
001 |
execute only |
2 |
010 |
write only |
3 |
011 |
write and execute |
4 |
100 |
read only |
5 |
101 |
read and execute |
6 |
110 |
read and write |
7 |
111 |
read, write, and execute (full permissions) |
The chmod() Function
chmod() 函数可以更改指定文件的权限。成功时返回 true ,否则在失败时返回 false 。
The chmod() function can change permissions of a specified file. It returns true on success, otherwise false on failure.
chmod(string $filename, int $permissions): bool
chmod() 函数尝试将指定文件( $filename )的模式更改为权限中给定的模式。
The chmod() function attempts to change the mode of the specified file ($filename) to that given in permissions.
第二个参数 $permissions 是一个四位八进制数字的八进制数。第一位始终为零,第二位指定所有者的权限,第三位指定所有者的用户组权限,第四位指定其他人权限。每位数字都是每种类型的权限值的和。
The second parameter $permissions is an octal number with four octal digits. The first digit is always zero, second specifies permissions for the owner, third for the owner’s user group and fourth for everybody else. Each digit is the sum of values for each type of permission.
1 |
Execute Permission |
2 |
Write Permission |
4 |
Read Permission |
$permissions 参数的默认值是 0777 ,这意味着目录在启用执行、写和读权限时创建。
The default value of $permissions parameters is 0777, which means the directory is created with execute, write and read permissions enabled.
Example
请看以下示例:
Take a look at the following example −
<?php
// Read and write for owner, nothing for everybody else
chmod("/PhpProject/sample.txt", 0600);
// Read and write for owner, read for everybody else
chmod("/PhpProject/sample.txt", 0644);
// Everything for owner, read and execute for everybody else
chmod("/PhpProject/sample.txt", 0755);
// Everything for owner, read for owner's group
chmod("/PhpProject/sample.txt", 0740);
?>
The chown() Function
chown() 函数尝试将文件 filename 的所有者更改为新用户。请注意,只有超级用户可以更改文件的拥有者。
The chown() function attempts to change the owner of the file filename to a new user. Note that only the superuser may change the owner of a file.
chown(string $filename, string|int $user): bool
Example
请看以下示例:
Take a look at the following example −
<?php
// File name and username to use
$file_name= "index.php";
$path = "/PhpProject/backup: " . $file_name ;
$user_name = "root";
// Set the user
chown($path, $user_name);
// Check the result
$stat = stat($path);
print_r(posix_getpwuid(fileowner($path)));
?>
The chgrp() Function
chgrp() 函数尝试将文件 filename 的组更改为组。
The chgrp() function attempts to change the group of the file filename to group.
chgrp(string $filename, string|int $group): bool
只有 superuser 可以随意更改文件的组;其他用户可以将文件的组更改为该用户所属的任何组。
Only a superuser may change the group of a file arbitrarily; other users may change the group of a file to any group of which that user is a member.
Example
请看以下示例:
Take a look at the following example −
<?php
$filename = "/PhpProject/sample.txt";
$format = "%s's Group ID @ %s: %d\n";
printf($format, $filename, date('r'), filegroup($filename));
chgrp($filename, "admin");
clearstatcache(); // do not cache filegroup() results
printf($format, $filename, date('r'), filegroup($filename));
?>
它将生成以下 output −
It will produce the following output −
/PhpProject/sample.txt's Group ID @ Fri, 13 Oct 2023 07:42:21 +0200: 0
/PhpProject/sample.txt's Group ID @ Fri, 13 Oct 2023 07:42:21 +0200: 0
PHP – Create Directory
计算机文件按层次顺序存储在本地存储设备 (称为 drive ) 中,其中一个目录包含一个或多个文件以及子目录。用于创建和管理目录的各自 DOS 命令在操作系统 Windows、Linux 等等中进行定义。
Computer files are stored in the local storage device (called drive) in a hierarchical order, where a directory contains one or more files as well as subdirectories. Respective DOS commands defined in operating systems Windows, Linux etc. are used to create and manage directories.
PHP 提供了目录管理函数来创建目录、更改当前目录和移除某个目录。
PHP provides directory management functions to create a directory, change the current directory and remove a certain directory.
本章讨论在 PHP 中使用以下目录函数的情况 -
This chapter discusses the usage of the following directory functions in PHP −
The mkdir() Function
mkdir() 函数创建一个新目录,它的路径作为函数之一的参数给出
The mkdir() function creates a new directory whose path is given as one of the parameters to the function
mkdir(
string $directory,
int $permissions = 0777,
bool $recursive = false,
?resource $context = null
): bool
Parameters
-
$directory − The first parameter $directory is mandatory. It is a string with either absolute or relative path of the new directory to be created.
-
$permissions − The second parameter $permissions is an octal number with four octal digits. The first digit is always zero, second specifies permissions for the owner, third for the owner’s user group and fourth for everybody else.
每个数字都是针对每种权限类型的值的和 -
Each digit is the sum of values for each type of permission −
-
1 = execute permission
-
2 = write permission
-
4 = read permission
$permissions 参数的默认值为 0777 ,这意味着创建目录时启用了执行、写入和读取权限。
The default value of $permissions parameters is 0777, which means the directory is created with execute, write and read permissions enabled.
请注意,在 Windows 操作系统上工作时,$permissions 参数会被忽略。
Note that the $permissions parameter is ignored when working on Windows OS.
-
$recursive − If true, then any parent directories to the directory specified will also be created, with the same permissions.
-
$context − This optional parameter is the stream resource.
mkdir() 函数返回真或假,表示函数是否已成功执行。
The mkdir() function returns either true or false, indicating if the function has been successfully executed or not.
Examples
以下是 mkdir() 函数的一些示例。
Here are some examples of mkdir() function.
对 mkdir() 的以下调用在当前工作目录中创建子目录。点表示路径是相对的。
The following call to mkdir() creates a subdirectory inside the current working directory. The dot indicates that the path is relative.
$dir = "./mydir/";
mkdir($dir);
我们可以提供包含要创建的目录的绝对路径的字符串参数。
We can give the string parameter that contains the absolute path of the directory to be created.
$dir = "c:/newdir/";
mkdir($dir);
由于 $recursive 参数设置为 true,对 mkdir() 的以下调用包含当前目录内的嵌套目录结构。
The following call to mkdir() contains nested directory structure inside the current directory, as the $recursive parameter is set to true.
$dirs = "./dir1/dir2/dir3/";
mkdir($dirs, 0777, true);
Windows 资源管理器将显示嵌套目录结构,如下所示:
The Windows explorer will show the nested directory structure as follows −
The chdir() Function
PHP 中的 chdir() 函数对应于 Linux/Windows 中的 chdir 或 cd 命令。它会导致根据需要更改当前目录。
The chdir() function in PHP corresponds to the chdir or cd command in Linux/Windows. It causes the current directory to be changed as required.
chdir(string $directory): bool
此函数的字符串参数是要将当前目录更改到的目录的绝对或相对路径。它返回真或假。
The string parameter to this function is either an absolute or relative path of a directory to which the current directory needs to be changed to. It returns true or false.
The getcwd() Function
getcwd() 函数的工作方式类似于 Ubuntu Linux 中的 pwd 命令,并返回当前工作目录的路径。
The getcwd() function works similar to pwd command in Ubuntu Linux, and returns the path to the current working directory.
Example
使用以下代码段,PHP 会在更改当前工作目录之前和之后显示当前工作目录。新当前目录中创建了一些文件。使用 scandir() 函数列出文件。
With the following code snippet, PHP displays the current working directory before and after changing the current working directory. A couple of files are created inside the new current directory. With the scandir() function, the files are listed.
<?php
echo "current directory: ". getcwd() . PHP_EOL;
$dir = "./mydir";
chdir($dir);
echo "current directory changed to: ". getcwd() .PHP_EOL;
$fp = fopen("a.txt", "w");
fwrite($fp, "Hello World");
fclose($fp);
copy("a.txt", "b.txt");
$dir = getcwd();
foreach(scandir($dir) as $file)
echo $file . PHP_EOL;
?>
它将生成以下 output −
It will produce the following output −
current directory: C:\xampp\php
current directory changed to: C:\xampp\php\mydir
.
..
a.txt
b.txt
PHP – Listing Files
Windows 命令 DIR 和 Linux 命令 ls 都显示当前目录中的文件列表。可以使用不同的交换机操作这些命令,以便对显示的文件列表应用条件。PHP 提供了一些用于以编程方式列出给定目录中文件的方法。
Windows command DIR and Linux command ls both display the list of files in the current directory. These commands can be operated with different switches to apply conditions on the list of files displayed. PHP provides a couple of options for programmatically listing files in a given directory.
The readdir() Function
PHP 中的 opendir() 函数类似于 fopen() 函数。它返回目录句柄,以便可以按序列化方式读取目录的内容。
The opendir() function in PHP is similar to fopen() function. It returns handles to the directory so that the contents of the directory can be read from in a serialized manner.
opendir(string $directory, ?resource $context = null): resource|false
此函数打开一个目录句柄,用于后续的 closedir()、readdir() 和 rewinddir() 调用。
This function opens up a directory handle to be used in the subsequent closedir(), readdir(), and rewinddir() calls.
readdir() 函数读取 opendir() 函数返回的流句柄中的下一个可用条目。
The readdir() function reads the next available entry from the stream handle returned by opendir() function.
readdir(?resource $dir_handle = null): string|false
此处, dir_handle 是先前使用 opendir() 打开的目录句柄,如果未指定,则假定为由 opendir() 打开的最后一个链接。
Here, dir_handle is the directory handle previously opened with opendir().not specified, the last link opened by opendir() is assumed.
closedir() 函数类似于 fclose() 函数。它关闭目录句柄。
The closedir() function is similar to fclose() function. It closes the directory handle.
closedir(?resource $dir_handle = null): void
该函数关闭 dir_handle 指示的目录流。该流必须先前由 opendir() 打开。
The function closes the directory stream indicated by dir_handle. The stream must have previously been opened by opendir().
Example
以下 PHP 代码从当前登录目录中一次读取一个文件。
The following PHP code reads one file at a time from the currently logged directory.
<?php
$dir = getcwd();
// Open a known directory, and proceed to read its contents
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
echo "filename:" . $file . "\n";
}
closedir($dh);
}
}
?>
The scandir() Function
scandir() 函数检索给定目录中的文件和子目录。
The scandir() function retrieves the files ans subdirectories inside a given directory.
scandir(string $directory,
int $sorting_order = SCANDIR_SORT_ASCENDING,
?resource $context = null): array|false
“sorting_order” 默认按升序排列。如果将此可选参数设置为 SCANDIR_SORT_DESCENDING,则排序顺序变为按降序排列。如果它设置为 SCANDIR_SORT_NONE,则结果将变为未排序。
The "sorting_order" by default is alphabetical in ascending order. If this optional parameter is set to SCANDIR_SORT_DESCENDING, then the sort order becomes alphabetical in descending order. If it is set to SCANDIR_SORT_NONE, then the result becomes unsorted.
Example
使用以下 PHP 代码,scandir() 函数返回给定目录中的文件数组。
With the following PHP code, the scandir() function returns an array of files in the given directory.
<?php
$dir = "c:/xampp/php/mydir/";
$files = scandir($dir);
var_dump($files);
?>
它将生成以下 output −
It will produce the following output −
array(4) {
[0]=>
string(1) "."
[1]=>
string(2) ".."
[2]=>
string(5) "a.txt"
[3]=>
string(5) "b.txt"
}
你可以使用 foreach 循环遍历 scandir() 函数返回的数组。
You can use a foreach loop to traverse the array returned by the scandir() function.
<?php
$dir = "c:/xampp/php/mydir/";
$files = scandir($dir);
foreach ($files as $file)
echo $file . PHP_EOL;
?>
它将生成以下 output −
It will produce the following output −
.
..
a.txt
b.txt
Object Oriented Programming in PHP
我们可以想象我们的宇宙是由不同的物体组成的,如太阳、地球、月亮等。类似地,我们可以想象我们的汽车是由不同的物体组成的,如车轮、方向盘、齿轮等。同样,还存在面向对象编程概念,该概念将所有内容都假设为对象,并使用不同的对象实现软件。
We can imagine our universe made of different objects like sun, earth, moon etc. Similarly we can imagine our car made of different objects like wheel, steering, gear etc. Same way there is object oriented programming concepts which assume everything as an object and implement a software using different objects.
Object Oriented Concepts
在我们详细介绍之前,让我们定义与面向对象编程相关的术语。
Before we go in detail, lets define important terms related to Object Oriented Programming.
-
Class − This is a programmer-defined data type, which includes local functions as well as local data. You can think of a class as a template for making many instances of the same kind (or class) of object.
-
Object − An individual instance of the data structure defined by a class. You define a class once and then make many objects that belong to it. Objects are also known as instance.
-
Member Variable − These are the variables defined inside a class. This data will be invisible to the outside of the class and can be accessed via member functions. These variables are called attribute of the object once an object is created.
-
Member function − These are the function defined inside a class and are used to access object data.
-
Inheritance − When a class is defined by inheriting existing function of a parent class then it is called inheritance. Here child class will inherit all or few member functions and variables of a parent class.
-
Parent class − A class that is inherited from by another class. This is also called a base class or super class.
-
Child Class − A class that inherits from another class. This is also called a subclass or derived class.
-
Polymorphism − This is an object oriented concept where same function can be used for different purposes. For example function name will remain same but it take different number of arguments and can do different task.
-
Overloading − a type of polymorphism in which some or all of operators have different implementations depending on the types of their arguments. Similarly functions can also be overloaded with different implementation.
-
Data Abstraction − Any representation of data in which the implementation details are hidden (abstracted).
-
Encapsulation − refers to a concept where we encapsulate all the data and member functions together to form an object.
-
Constructor − refers to a special type of function which will be called automatically whenever there is an object formation from a class.
-
Destructor − refers to a special type of function which will be called automatically whenever an object is deleted or goes out of scope.
Defining PHP Classes
在 PHP 中定义新类的通用格式如下 −
The general form for defining a new class in PHP is as follows −
<?php
class phpClass {
var $var1;
var $var2 = "constant string";
function myfunc ($arg1, $arg2) {
[..]
}
[..]
}
?>
下面是每一行的说明 −
Here is the description of each line −
-
The special form class, followed by the name of the class that you want to define.
-
A set of braces enclosing any number of variable declarations and function definitions.
-
Variable declarations start with the special form var, which is followed by a conventional $ variable name; they may also have an initial assignment to a constant value.
-
Function definitions look much like standalone PHP functions but are local to the class and will be used to set and access object data.
Example
下面是一个示例,定义了一个 Books 类型的类 −
Here is an example which defines a class of Books type −
<?php
class Books {
/* Member variables */
var $price;
var $title;
/* Member functions */
function setPrice($par){
$this->price = $par;
}
function getPrice(){
echo $this->price ."<br/>";
}
function setTitle($par){
$this->title = $par;
}
function getTitle(){
echo $this->title ." <br/>";
}
}
?>
变量 $this 是一个特殊变量,它引用同个对象,即它自身。
The variable $this is a special variable and it refers to the same object ie. itself.
Creating Objects in PHP
一旦定义了类,您就可以创建任意多的该类类型的对象。以下是如何使用 new 操作符创建对象的示例。
Once you defined your class, then you can create as many objects as you like of that class type. Following is an example of how to create object using new operator.
$physics = new Books;
$maths = new Books;
$chemistry = new Books;
在这里,我们创建了三个对象,这些对象相互独立且单独存在。下一步,我们将看到如何访问成员函数和处理成员变量。
Here we have created three objects and these objects are independent of each other and they will have their existence separately. Next we will see how to access member function and process member variables.
Calling Member Functions
创建对象之后,您将能够调用与该对象相关联的成员函数。一个成员函数将只能处理相关对象的成员变量。
After creating your objects, you will be able to call member functions related to that object. One member function will be able to process member variable of related object only.
下面的示例演示了如何通过调用成员函数为这三本书设置标题和价格。
Following example shows how to set title and prices for the three books by calling member functions.
$physics->setTitle( "Physics for High School" );
$chemistry->setTitle( "Advanced Chemistry" );
$maths->setTitle( "Algebra" );
$physics->setPrice( 10 );
$chemistry->setPrice( 15 );
$maths->setPrice( 7 );
现在可以调用另一个成员函数来获取上述示例中设置的值 -
Now you call another member functions to get the values set by in above example −
$physics->getTitle();
$chemistry->getTitle();
$maths->getTitle();
$physics->getPrice();
$chemistry->getPrice();
$maths->getPrice();
这会产生以下结果 −
This will produce the following result −
Physics for High School
Advanced Chemistry
Algebra
10
15
7
Constructor Functions
构造函数是一种特殊类型的函数,它在创建对象时会自动调用。因此,通过构造函数初始化许多内容,我们可以充分利用此行为。
Constructor Functions are special type of functions which are called automatically whenever an object is created. So we take full advantage of this behaviour, by initializing many things through constructor functions.
PHP 提供了一个名为 __construct() 的特殊函数来定义一个构造函数。您可以将任意数量的参数传递到构造函数中。
PHP provides a special function called __construct() to define a constructor. You can pass as many as arguments you like into the constructor function.
以下示例将为 Books 类创建一个构造函数,它将在创建对象时初始化该书的价格和标题。
Following example will create one constructor for Books class and it will initialize price and title for the book at the time of object creation.
function __construct( $par1, $par2 ) {
$this->title = $par1;
$this->price = $par2;
}
现在我们无需单独调用 set 函数来设置价格和标题。我们只能在创建对象时初始化这两个成员变量。查看以下示例 -
Now we don’t need to call set function separately to set price and title. We can initialize these two member variables at the time of object creation only. Check following example below −
$physics = new Books( "Physics for High School", 10 );
$maths = new Books ( "Advanced Chemistry", 15 );
$chemistry = new Books ("Algebra", 7 );
/* Get those set values */
$physics->getTitle();
$chemistry->getTitle();
$maths->getTitle();
$physics->getPrice();
$chemistry->getPrice();
$maths->getPrice();
这会产生以下结果 −
This will produce the following result −
Physics for High School
Advanced Chemistry
Algebra
10
15
7
Destructor
像构造函数一样,您可以使用函数 __destruct() 定义一个析构函数。您可以在析构函数中释放所有资源。
Like a constructor function you can define a destructor function using function __destruct(). You can release all the resources with-in a destructor.
Inheritance
PHP 类定义可以使用 extends 子句从父类定义中继承,语法如下:
PHP class definitions can optionally inherit from a parent class definition by using the extends clause. The syntax is as follows −
class Child extends Parent {
<definition body>
}
继承的效果是子类(或子类或派生类)具有以下特征 -
The effect of inheritance is that the child class (or subclass or derived class) has the following characteristics −
-
Automatically has all the member variable declarations of the parent class.
-
Automatically has all the same member functions as the parent, which (by default) will work the same way as those functions do in the parent.
以下示例继承 Books 类并根据要求添加更多功能。
Following example inherit Books class and adds more functionality based on the requirement.
class Novel extends Books {
var $publisher;
function setPublisher($par){
$this->publisher = $par;
}
function getPublisher(){
echo $this->publisher. "<br />";
}
}
现在,除了继承的函数之外,Novel 类还保留了两个其他成员函数。
Now apart from inherited functions, class Novel keeps two additional member functions.
Function Overriding
子类中的函数定义覆盖了父类中同名定义。在子类中,我们可以修改从父类继承的函数的定义。
Function definitions in child classes override definitions with the same name in parent classes. In a child class, we can modify the definition of a function inherited from parent class.
在以下示例中,getPrice 和 getTitle 函数被覆盖以返回一些值。
In the following example getPrice and getTitle functions are overridden to return some values.
function getPrice() {
echo $this->price . "<br/>";
return $this->price;
}
function getTitle(){
echo $this->title . "<br/>";
return $this->title;
}
Public Members
除非您另行指明,否则类的属性和方法均为公共的。也就是说,它们可以在三种可能的情况下进行访问 -
Unless you specify otherwise, properties and methods of a class are public. That is to say, they may be accessed in three possible situations −
-
From outside the class in which it is declared
-
From within the class in which it is declared
-
From within another class that implements the class in which it is declared
到目前为止,我们已经看到了所有成员作为公共成员。如果您希望限制类成员的可访问性,则可以将类成员定义为 private 或 protected 。
Till now we have seen all members as public members. If you wish to limit the accessibility of the members of a class then you define class members as private or protected.
Private members
通过将成员设为私有,你可以限制其对声明它的类的可访问性。私有成员无法从继承声明它的类的类中引用,也无法从类外部访问。
By designating a member private, you limit its accessibility to the class in which it is declared. The private member cannot be referred to from classes that inherit the class in which it is declared and cannot be accessed from outside the class.
可以使用成员前面的 private 关键字将类成员设为私有。
A class member can be made private by using private keyword infront of the member.
class MyClass {
private $car = "skoda";
$driver = "SRK";
function __construct($par) {
// Statements here run every time
// an instance of the class
// is created.
}
function myPublicFunction() {
return("I'm visible!");
}
private function myPrivateFunction() {
return("I'm not visible outside!");
}
}
当 MyClass 类使用 extends 被另一个类继承时,myPublicFunction() 将可见,$driver 也将可见。扩展类不会意识到或访问 myPrivateFunction 和 $car,因为它们声明为私有。
When MyClass class is inherited by another class using extends, myPublicFunction() will be visible, as will $driver. The extending class will not have any awareness of or access to myPrivateFunction and $car, because they are declared private.
Protected members
受保护的属性或方法在其声明所在的类以及该类的扩展类中都可以访问。受保护的成员在除了这两种类之外的类中不可用。可以使用成员前面的 protected 关键字将类成员设为受保护。
A protected property or method is accessible in the class in which it is declared, as well as in classes that extend that class. Protected members are not available outside of those two kinds of classes. A class member can be made protected by using protected keyword in front of the member.
以下是 MyClass 的不同版本 −
Here is different version of MyClass −
class MyClass {
protected $car = "skoda";
$driver = "SRK";
function __construct($par) {
// Statements here run every time
// an instance of the class
// is created.
}
function myPublicFunction() {
return("I'm visible!");
}
protected function myPrivateFunction() {
return("I'm visible in child class!");
}
}
Interfaces
接口的定义是为了向实现者提供通用的函数名称。不同的实现者可以根据其要求来实现这些接口。你可以说,接口是开发人员实现的骨架。
Interfaces are defined to provide a common function names to the implementers. Different implementors can implement those interfaces according to their requirements. You can say, interfaces are skeletons which are implemented by developers.
从 PHP5 起,可以像这样定义接口 −
As of PHP5, it is possible to define an interface, like this −
interface Mail {
public function sendMail();
}
然后,如果另一个类实现了该接口,如下所示 −
Then, if another class implemented that interface, like this −
class Report implements Mail {
// sendMail() Definition goes here
}
Constants
常量有点像变量,因为它含有值,但实际上更像函数,因为常量是不可变的。一旦声明常量,它就不会改变。
A constant is somewhat like a variable, in that it holds a value, but is really more like a function because a constant is immutable. Once you declare a constant, it does not change.
声明常量很简单,在 MyClass 的此版本中就是如此 −
Declaring one constant is easy, as is done in this version of MyClass −
class MyClass {
const requiredMargin = 1.7;
function __construct($incomingValue) {
// Statements here run every time
// an instance of the class
// is created.
}
}
在此类中,requiredMargin 是一个常量。它使用关键字 const 声明,在任何情况下都不得将其更改为 1.7 以外的任何值。请注意,与变量名称不同,常量的名称没有前导 $。
In this class, requiredMargin is a constant. It is declared with the keyword const, and under no circumstances can it be changed to anything other than 1.7. Note that the constant’s name does not have a leading $, as variable names do.
Abstract Classes
抽象类是不可实例化的,只能继承。使用关键字 abstract 声明抽象类,如下所示 −
An abstract class is one that cannot be instantiated, only inherited. You declare an abstract class with the keyword abstract, like this −
从抽象类继承时,父类的类声明中标记为 abstract 的所有方法都必须由子类定义;此外,这些方法必须使用相同的可见性定义。
When inheriting from an abstract class, all methods marked abstract in the parent’s class declaration must be defined by the child; additionally, these methods must be defined with the same visibility.
abstract class MyAbstractClass {
abstract function myAbstractFunction() {
}
}
请注意,抽象类中的函数定义也必须以前导关键字 abstract 为前缀。在非抽象类中不能有抽象函数定义。
Note that function definitions inside an abstract class must also be preceded by the keyword abstract. It is not legal to have abstract function definitions inside a non-abstract class.
Static Keyword
将类成员或方法声明为 static 使它们可以在不需要实例化类的情况下对其进行访问。声明为 static 的成员无法通过实例化的类对象进行访问(虽然静态方法可以访问)。
Declaring class members or methods as static makes them accessible without needing an instantiation of the class. A member declared as static can not be accessed with an instantiated class object (though a static method can).
尝试使用以下示例 −
Try out following example −
<?php
class Foo {
public static $my_static = 'foo';
public function staticValue() {
return self::$my_static;
}
}
print Foo::$my_static . "\n";
$foo = new Foo();
print $foo->staticValue() . "\n";
?>
Final Keyword
PHP 5 引入了 final 关键字,它通过使用 final 作为定义前缀来阻止子类覆盖方法。如果类本身被定义为 final,则无法使用其扩展。
PHP 5 introduces the final keyword, which prevents child classes from overriding a method by prefixing the definition with final. If the class itself is being defined final then it cannot be extended.
以下示例导致致命错误:无法覆盖 final 方法 BaseClass::moreTesting()
Following example results in Fatal error: Cannot override final method BaseClass::moreTesting()
<?php
class BaseClass {
public function test() {
echo "BaseClass::test() called<br>";
}
final public function moreTesting() {
echo "BaseClass::moreTesting() called<br>";
}
}
class ChildClass extends BaseClass {
public function moreTesting() {
echo "ChildClass::moreTesting() called<br>";
}
}
?>
Calling parent constructors
让我们通过显式地调用父类的构造函数,然后再执行实例化子类时除了需要的所有其他操作之外的任何操作来编写它,而不是为子类编写一个全新的构造函数。这里有一个简单的示例 −
Instead of writing an entirely new constructor for the subclass, let’s write it by calling the parent’s constructor explicitly and then doing whatever is necessary in addition for instantiation of the subclass. Here’s a simple example −
class Name {
var $_firstName;
var $_lastName;
function Name($first_name, $last_name) {
$this->_firstName = $first_name;
$this->_lastName = $last_name;
}
function toString() {
return($this->_lastName .", " .$this->_firstName);
}
}
class NameSub1 extends Name {
var $_middleInitial;
function NameSub1($first_name, $middle_initial, $last_name) {
Name::Name($first_name, $last_name);
$this->_middleInitial = $middle_initial;
}
function toString() {
return(Name::toString() . " " . $this->_middleInitial);
}
}
- In this example, we have a parent class (Name), which has a two-argument constructor, and a subclass (NameSub1), which has a three-argument constructor. The constructor of NameSub1 functions by calling its parent constructor explicitly using the
-
syntax (passing two of its arguments along) and then setting an additional field. Similarly, NameSub1 defines its non constructor toString() function in terms of the parent function that it overrides.
NOTE − 构造函数可以使用与类名称相同的名称来定义。它在上面的示例中被定义。
NOTE − A constructor can be defined with the same name as the name of a class. It is defined in above example.
PHP – Classes and Objects
类和对象的概念对于 PHP 的面向对象编程方法至关重要。 class 是其对象的模板描述。它包括处理属性的属性和函数。 object 是其类的实例。它的特点是类中定义的属性和函数。
The concept of classes and objects is central to PHP’s object-oriented programming methodology. A class is the template description of its objects. It includes the properties and functions that process the properties. An object is the instance of its class. It is characterized by the properties and functions defined in the class.
Defining a Class in PHP
PHP 有一个用于定义类的关键词 “ class ”。同样,PHP 提供了关键词 “ new ” 来声明任何给定类的对象。
To define a class, PHP has a keyword "class". Similarly, PHP provides the keyword "new" to declare an object of any given class.
在 PHP 中定义新类的通用格式如下 −
The general form for defining a new class in PHP is as follows −
<?php
class phpClass {
var $var1;
var $var2 = "constant string";
function myfunc ($arg1, $arg2) {
[..]
}
[..]
}
?>
关键词 class 后跟您想要定义的类的名称。类名称遵循与 PHP 变量相同的命名约定。后跟一对花括号,其中包含任意数量的变量声明(属性)和函数定义。
The keyword class is followed by the name of the class that you want to define. Class name follows the same naming conventions as used for a PHP variable. It is followed by a pair of braces enclosing any number of variable declarations (properties) and function definitions.
变量声明以另一个保留关键字 var 开始,后跟一个常规 $variable 名称;它们还可以对常量值进行初始赋值。
Variable declarations start with another reserved keyword var, which is followed by a conventional $variable name; they may also have an initial assignment to a constant value.
函数定义看起来很像独立的 PHP 函数,但它们是类中的本地函数,并且将用于设置和访问对象数据。类中的函数也称为方法。
Function definitions look much like standalone PHP functions but are local to the class and will be used to set and access object data. Functions inside a class are also called methods.
Example
这里有一个定义 Book 类型类的示例 −
Here is an example which defines a class of Book type −
class Book {
/* Member variables */
var $price;
var $title;
/* Member functions */
function setPrice($par){
$this->price = $par;
}
function getPrice(){
echo $this->price ."<br/>";
}
function setTitle($par){
$this->title = $par;
}
function getTitle(){
echo $this->title ." <br/>";
}
}
当从对象上下文中调用方法时,伪变量 $this 可用。 $this 指的是调用对象。
The pseudo-variable $this is available when a method is called from within an object context. $this refers to the calling object.
Book 类有两个 member variables (或属性): $title 和 $price 。成员变量(有时也称为实例变量)通常对每个对象具有不同的值;就像每本书的标题和价格都不同一样。
The Book class has two member variables (or properties) - $title and $price. The member variables (also sometimes called instance variables) usually have different values for each object; like each book has a title and price different from the other.
Book 类有函数(在类中定义的函数称为 methods )setTitle() 和 setPrice()。这些函数与对象和参数一起调用,用于分别设置标题和价格成员变量的值。
The Book class has functions (functions defined inside the class are called methods) setTitle() and setPrice(). These functions are called with reference to an object and a parameter, used to set the value of title and price member variables respectively.
Book 类还有 getTitle() 和 getPrice() 方法。调用时,它们会返回其引用已传递的对象的标题和价格。
The Book class also has getTitle() and getPrice() methods. When called, they return the title and price of the object whose reference is passed.
一旦定义了一个类,您就可以使用 new 运算符声明一个或多个对象。
Once a class is defined, you can declare one or more objects, using new operator.
$b1 = new Book;
$b2 = new Book;
new 运算符为每个对象的成员变量和方法分配所需的内存。在这里,我们创建了两个对象,这些对象彼此独立,并且它们将分别存在。
The new operator allocates the memory required for the member variables and methods of each object. Here we have created two objects and these objects are independent of each other and they will have their existence separately.
每个对象都可以通过 “ → ” 运算符访问其成员变量和方法。例如, b1 对象的 $title 属性为 “ $b1→title ”,要调用 setTitle() 方法,请使用 “ $b1→setTitle() ” 语句。
Each object has access to its member variables and methods with the "→" operator. For example, the $title property of b1 object is "$b1→title" and to call setTitle() method, use the "$b1→setTitle()" statement.
要设置 b1 对象的标题和价格,
To set the title and price of b1 object,
$b1->setTitle("PHP Programming");
$b1->setPrice(450);
同样,以下语句获取 b1 书的标题和价格 −
Similarly, the following statements fetch the title and price of b1 book −
echo $b1->getPrice();
echo $b1->getTitle();
Example
下面给出了一个完整的 PHP 脚本,该脚本定义 Book 类、声明两个对象和调用成员函数。
Given below is the complete PHP script that defines Book class, declares two objects and calls the member functions.
<?php
class Book {
/* Member variables */
var $price;
var $title;
/* Member functions */
function setPrice($par){
$this->price = $par;
}
function getPrice(){
echo $this->price ."\n";
}
function setTitle($par){
$this->title = $par;
}
function getTitle(){
echo $this->title ."\n";
}
}
$b1 = new Book;
$b2 =new Book;
$b1->setTitle("PHP Programming");
$b1->setPrice(450);
$b2->setTitle("PHP Fundamentals");
$b2->setPrice(275);
$b1->getTitle();
$b1->getPrice();
$b2->getTitle();
$b2->getPrice();
?>
它将生成以下 output −
It will produce the following output −
PHP Programming
450
PHP Fundamentals
275
PHP – Constructor and Destructor
与大多数面向对象语言一样,您还可以在 PHP 中的类中定义一个构造函数。当您使用 new 运算符声明一个对象时,它的成员变量不会分配任何值。构造函数用于在声明时初始化每个新对象。PHP 还支持具有析构函数,该析构函数会从内存中销毁对象,因为它不再有任何引用。
As in most of the object-oriented languages, you can define a constructor function in a class in PHP also. When you declare an object with the new operator, its member variables are not assigned any value. The constructor function is used to initialize every new object at the time of declaration. PHP also supports having a destructor function that destroys the object from the memory as it no longer has any reference.
The __construct() Function
PHP 提供了一个用于初始化对象的 __construct()
函数。
PHP provides a __construct() function that initializes an object.
__construct(mixed ...$values = ""): void
类内部的构造函数方法在每个新创建的对象上自动调用。请注意,定义构造函数不是强制性的。但是,如果存在,它适用于在使用对象之前它可能需要的任何初始化。
The constructor method inside a class is called automatically on each newly created object. Note that defining a constructor is not mandatory. However, if present, it is suitable for any initialization that the object may need before it is used.
您可以向构造函数传递任意多个参数。__construct()
函数没有任何返回值。
You can pass as many as arguments you like into the constructor function. The __construct() function doesn’t have any return value.
让我们在前一章中使用的 Book
类中定义一个构造函数
Let us define a constructor in the Book class used in the previous chapter
<?php
class Book {
/* Member variables */
var $price;
var $title;
/*Constructor*/
function __construct(){
$this->title = "PHP Fundamentals";
$this->price = 275;
}
/* Member functions */
function getPrice() {
echo "Price: $this->price \n";
}
function getTitle(){
echo "Title: $this->title \n";
}
}
$b1 = new Book;
$b1->getTitle();
$b1->getPrice();
?>
它将生成以下 output −
It will produce the following output −
Title: PHP Fundamentals
Price: 275
Parameterized Constructor
$b1
的成员变量已经初始化,而无需调用 setTitle()
和 setPrice()
方法,因为构造函数在对象声明后立即被调用。但是,此构造函数将针对每个对象调用,因此每个对象都具有 title
和 price
属性的相同值。
The member variables of $b1 have been initialized without having to call setTitle() and setPrice() methods, because the constructor was called as soon as the object was declared. However, this constructor will be called for each object, and hence each object has the same values of title and price properties.
要使用不同的值初始化每个对象,请使用带参数定义 @{s1}
函数。
To initialize each object with different values, define the __construct() function with parameters.
将 @{s2}
函数的定义更改为以下内容 −
Change the definition of __construct() function to the following −
function __construct($param1, $param2) {
$this->title = $param1;
$this->price = $param2;
}
要初始化对象,请在声明中将值传递给括号内的参数。
To initialize the object, pass values to the parameters inside a parenthesis in the declaration.
$b1 = new Book("PHP Fundamentals", 375);
Example
现在,每个对象都可以对成员变量具有不同的值。
Now, you can have each object with different values to the member variables.
<?php
class Book {
/* Member variables */
var $price;
var $title;
/*Constructor*/
function __construct($param1, $param2) {
$this->title = $param1;
$this->price = $param2;
}
/* Member functions */
function getPrice(){
echo "Price: $this->price \n";
}
function getTitle(){
echo "Title: $this->title \n";
}
}
$b1 = new Book("PHP Fundamentals", 375);
$b2 = new Book("PHP Programming", 450);
$b1->getTitle();
$b1->getPrice();
$b2->getTitle();
$b2->getPrice();
?>
它将生成以下 output −
It will produce the following output −
Title: PHP Fundamentals
Price: 375
Title: PHP Programming
Price: 450
Constructor Overloading
方法重载是面向对象编程中的一个重要概念,其中一个类可能具有多个构造函数定义,每个定义具有不同的参数数量。然而,PHP 不支持方法重载。可以通过在构造函数中使用带默认值的参数来克服此限制。
Method overloading is an important concept in object-oriented programming, where a class may have more than one definitions of constructor, each having different number of arguments. However, PHP doesn’t support method overloading. This limitation may be overcome by using arguments with default values in the constructor function.
将 __construct() 函数更改为以下内容:
Change the __construct() function to the following −
function __construct($param1="PHP Basics", $param2=380) {
$this->title = $param1;
$this->price = $param2;
}
现在,声明一个不带参数的对象,而另一个带参数。不带参数的对象将用默认参数初始化,带参数的对象将用传递的值初始化。
Now, declare an object without passing parameters, and the other with parameters. One without parameters will be initialized with default arguments, the other with the values passed.
$b1 = new Book();
$b2 = new Book("PHP Programming", 450);
它将生成以下 output −
It will produce the following output −
Title: PHP Basics
Price: 380
Title: PHP Programming
Price: 450
Type Declaration in Constructor
由于 PHP(7.0 及更高版本)允许对函数参数使用标量类型声明,因此可以将 __construct() 函数定义为:
Since PHP (version 7.0 onwards) allows scalar type declarations for function arguments, the __construct() function may be defined as −
function __construct(string $param1="PHP Basics", int $param2=380) {
$this->title = $param1;
$this->price = $param2;
}
在 PHP 的早期版本中,允许使用类的名称来定义构造函数,但此功能自 PHP 8.0 起已弃用。
In the earlier versions of PHP, using the name of class to define a constructor function was allowed, but this feature has been deprecated since PHP version 8.0.
The __destruct() Function
PHP 还有一个 __destructor() 函数。它实现了一个析构函数概念,与其他面向对象语言(如 C++)中的概念类似。只要不存在对特定对象的引用,就会调用析构函数方法。
PHP also has a __destructor() function. It implements a destructor concept similar to that of other object-oriented languages, as in C++. The destructor method will be called as soon as there are no other references to a particular object.
__destruct(): void
_destruct() 函数没有任何参数,也没有任何返回值。_destruct() 函数在任何对象超出作用域时都会自动调用这一事实可以通过在函数中放置 var_dump($this) 来验证。
The _destruct() function doesn’t have any parameters, neither does it have any return value. The fact that the _destruct() function is automatically called when any object goes out of scope, can be verified by putting var_dump($this) inside the function.
如上所述, $this 携带对调用对象的引用;转储显示,成员变量被设置为 NULL
As mentioned above, $this carries the reference to the calling object, the dump shows that the member variables are set to NULL
在 Book 类中添加析构函数,如下所示:
Add destructor function in the Book class as follows −
function __destruct() {
var_dump($this);
echo "object destroyed";
}
当程序退出时,将显示以下 output :
As the program exits, the following output will be displayed −
object(Book)#1 (2) {
["price"]=>
NULL
["title"]=>
NULL
}
object destroyed
PHP – Access Modifiers
在 PHP 中,关键字 public, private 和 protected 被称为 access modifiers 。这些关键字控制类属性和方法的可访问性或可见性的范围。其中一个关键字在声明成员变量和定义成员函数时添加为前缀。
In PHP, the keywords public, private and protected are known as the access modifiers. These keywords control the extent of accessibility or visibility of the class properties and methods. One of these keywords is prefixed while declaring the member variables and defining member functions.
PHP 代码是否可以自由地访问类成员,或者被限制获取,或者有条件地访问,是由这些关键字决定的 -
Whether the PHP code has free access to a class member, or it is restricted from getting access, or it has a conditional access, is determined by these keywords −
-
Public − class members are accessible from anywhere, even from outside the scope of the class, but only with the object reference.
-
Private − class members can be accessed within the class itself. It prevents members from outside class access even with the reference of the class instance.
-
Protected − members can be accessed within the class and its child class only, nowhere else.
数据封装原则是面向对象编程方法的基础。它指的是将对象的成员数据或属性置于类外部环境的访问范围之外,只允许通过类中可用的方法或函数进行受控访问。
The principle of data encapsulation is the cornerstone of the object-oriented programming methodology. It refers to the mechanism of keeping the data members or properties of an object away from the reach of the environment outside the class, allowing controlled access only through the methods or functions available in the class.
要实现封装,类的成员数据被设为 private ,方法被设为 public 。
To implement encapsulation, data members of a class are made private and the methods are made public.
Public Members
在 PHP 中,类成员(成员变量和其他成员函数)默认情况下是公开的。
In PHP, the class members (both member variables as well as member functions) are public by default.
Example
在下面的程序中,对象的成员变量 title 和 price 在类的外部可以自由访问,因为它们默认情况下是公开的,除非另行指定。
In the following program, the member variables title and price of the object are freely accessible outside the class because they are public by default, if not otherwise specified.
<?php
class Book {
/* Member variables */
var $price;
var $title;
/*Constructor*/
function __construct(string $param1="PHP Basics", int $param2=380) {
$this->title = $param1;
$this->price = $param2;
}
function getPrice() {
echo "Title: $this->price \n";
}
function getTitle() {
echo "Price: $this->title \n";
}
}
$b1 = new Book();
echo "Title : $b1->title Price: $b1->price";
?>
它将生成以下 output −
It will produce the following output −
Title : PHP Basics Price: 380
Private Members
如上所述,封装原则要求不得直接访问成员变量。只有方法应具有对数据成员的访问权。因此,我们需要将成员变量设为私有,并将方法设为公有。
As mentioned above, the principle of encapsulation requires that the member variables should not be accessible directly. Only the methods should have the access to the data members. Hence, we need to make the member variables private and methods public.
<?php
class Book {
/* Member variables */
private $price;
private $title;
/*Constructor*/
function __construct(string $param1="PHP Basics", int $param2=380) {
$this->title = $param1;
$this->price = $param2;
}
public function getPrice() {
echo "Price: $this->price \n";
}
public function getTitle() {
echo "Title: $this->title \n;";
}
}
$b1 = new Book();
$b1->getTitle();
$b1->getPrice();
echo "Title : $b1->title Price: $b1->price";
?>
Output
现在,getTitle() 和 getPrice() 函数是公共的,能够访问私有成员变量 title 和 price。但是,如果尝试直接显示标题和价格,会遇到错误,因为它们不是公共的。
Now, the getTitle() and getPrice() functions are public, able to access the private member variables title and price. But, while trying to display the title and price directly, an error is encountered as they are not public.
Title: PHP Basics
Price: 380
Fatal error: Uncaught Error: Cannot access private property
Book::$title in hello.php:31
Protected Members
针对类成员指定受保护的访问权在类继承的情况下有效。我们知道可以从类的外围访问公有成员,并且不能从类的外围访问私有成员。
The effect of specifying protected access to a class member is effective in case of class inheritance. We know that public members are accessible from anywhere outside the class, and private members are denied access from anywhere outside the class.
protected 关键字允许访问同一类的对象和继承类中的对象,但禁止访问其他环境。
The protected keyword grants access to an object of the same class and an object of its inherited class, denying it to any other environment.
让我们在 Book 类示例中将标题成员设置为受保护的,将价格保留为私有的。
Let us set the title member in Book class example to protected, leaving price to private.
class Book {
/* Member variables */
private $price;
protected $title;
# rest of the code kept as it is
}
$b1 = new Book();
$b1->getTitle();
$b1->getPrice();
PHP 允许访问两个成员变量,因为该对象属于同一个类。
PHP allows the both the member variables to be accessed, as the object belongs to the same class.
让我们添加一个 mybook 类,它继承了 Book 类 −
Let us add a mybook class that inherits the Book class −
class mybook extends Book {
# no additional members defined
}
它的对象仍能访问成员变量,因为子类继承了父类的公有和受保护的成员。
whose object is still able to access the member variables, as the child class inherits public and protected members of the parent class.
但是,将 mybook 类作为一个独立的类(不扩展 Book 类)并定义一个 getmytitle() 函数,该函数尝试访问 Book 类的受保护标题成员变量。
However, make mybook class as an independent class (not extending Book class) and define a getmytitle() function that tries to access protected title member variable of Book class.
class mybook {
public function getmytitle($b) {
echo "Title: $b->title <br/>";
}
}
$b1 = new mybook();
$b = new Book();
$b1->getmytitle($b);
由于 getmytitle() 函数尝试打印 Book 对象的标题,因此一个显示 Cannot access protected property Book::$title 的错误消息被引发。
As the getmytitle() function tries to print title of Book object, an error message showing Cannot access protected property Book::$title is raised.
Example
尝试运行以下代码 −
Try to run the following code −
<?php
class Book {
private $price;
protected $title;
function __construct(string $param1="PHP Basics", int $param2=380) {
$this->title = $param1;
$this->price = $param2;
}
public function getPrice(){
echo "Price: $this->price <br/>";
}
public function getTitle(){
echo "Title: $this->title <br/>";
}
}
class mybook {
public function getmytitle($b) {
echo "Title: $b->title <br/>";
}
}
$b1 = new mybook();
$b = new Book();
$b1->getmytitle($b);
?>
它将生成以下 output −
It will produce the following output −
PHP Fatal error: Uncaught Error: Cannot access protected property
Book::$title in /home/cg/root/97848/main.php:18
因此,可以看到受保护的成员只能被同一类和继承类的对象访问。对于所有其他环境,受保护的成员不可访问。
Hence, it can be seen that the protected member is accessible by object of same class and inherited class only. For all other environment, protected members are not accessible.
可访问性规则可以由下表总结:
The accessibility rules can be summarized by the following table −
PHP – Inheritance
继承是面向对象编程方法论的基本原则之一。继承是一种软件建模方法,它允许扩展现有类以构建新类,而不是从头开始构建新类。
Inheritance is one of the fundamental principles of object-oriented programming methodology. Inheritance is a software modelling approach that enables extending the capability of an existing class to build new class instead of building from scratch.
PHP 提供了所有功能在其对象模型中实现继承。在 PHP 软件开发中纳入继承会产生代码再利用,消除冗余的代码重复及逻辑组织。
PHP provides all the functionality to implement inheritance in its object model. Incorporating inheritance in PHP software development results in code reuse, remove redundant code duplication and logical organization.
想象一下你需要设计一个新类,这个类的大部分功能已经在一个现有类中得到明确定义。继承允许你扩展现有类,添加或移除其功能,并开发一个新类。事实上,PHP 有“extends”关键词来建立现有类和新类之间的继承关系。
Imagine that you need to design a new class whose most of the functionality already well defined in an existing class. Inheritance lets you to extend the existing class, add or remove its features and develop a new class. In fact, PHP has the "extends" keyword to establish inheritance relationship between existing and new classes.
class newclass extends oldclass {
...
...
}
当一个新类(以后将被称为继承类、子类、子类,等等)与一个现有类(将被称为基类、超类、父类,等等)拥有“是”关系时,继承就会出现。
Inheritance comes into picture when a new class (henceforth will be called inherited class, sub class, child class, etc.) possesses "IS A" relationship with an existing class (which will be called base class, super class, parent class, etc.).
在 PHP 中,当通过扩展另一个类来定义一个新类时,子类会继承来自父类的公共和受保护的方法、属性和常量。你可以自由重写继承方法的功能,否则它将按照父类中定义的那样保留其功能。
In PHP, when a new class is defined by extending another class, the subclass inherits the public and protected methods, properties and constants from the parent class. You are free to override the functionality of an inherited method, otherwise it will retain its functionality as defined in the parent class.
Example
请看以下示例:
Take a look at the following example −
<?php
class myclass {
public function hello() {
echo "Hello from the parent class" . PHP_EOL;
}
public function thanks() {
echo "Thank you from parent class" . PHP_EOL;
}
}
class newclass extends myclass {
public function thanks() {
echo "Thank you from the child class" . PHP_EOL;
}
}
# object of parent class
$obj1 = new myclass;
$obj1->hello();
$obj1->thanks();
# object of child class
$obj2 = new newclass;
$obj2->hello();
$obj2->thanks();
?>
它将生成以下 output −
It will produce the following output −
Hello from the parent class
Thank you from parent class
Hello from the parent class
Thank you from the child class
如前所述,子类继承父类的公共和受保护成员(属性和方法)。子类可以引入其他属性或方法。
As mentioned before, the child class inherits public and protected members (properties and methods) of the parent. The child class may introduce additional properties or methods.
在下面的示例中,我们使用 Book class 作为父类。在这里,我们创建一个 ebook class 来扩展 Book 类。新类有一个其他属性 - format (表示电子书的文件格式 - EPUB,PDF,MOBI 等)。电子书类定义了两个新方法来初始化和输出电子书数据,分别是 getebook() 和 dispebook() 。
In the following example, we use the Book class as the parent class. Here, we create an ebook class that extends the Book class. The new class has an additional property – format (indicating ebook’s file format – EPUB, PDF, MOBI etc). The ebook class defines two new methods to initialize and output the ebbok data – getebook() and dispebook() respectively.
Example
完整的继承示例代码如下所示:
The complete code of inheritance example is given below −
<?php
class Book {
/* Member variables */
protected int $price;
protected string $title;
public function getbook(string $param1, int $param2) {
$this->title = $param1;
$this->price = $param2;
}
public function dispbook() {
echo "Title: $this->title Price: $this->price \n";
}
}
class ebook extends Book {
private string $format;
public function getebook(string $param1, int $param2, string $param3) {
$this->title = $param1;
$this->price = $param2;
$this->format = $param3;
}
public function dispebook() {
echo "Title: $this->title Price: $this->price\n";
echo "Format: $this->format \n";
}
}
$eb = new ebook;
$eb->getebook("PHP Fundamentals", 450, "EPUB");
$eb->dispebook();
?>
浏览器 output 如下所示:
The browser output is as shown below −
Title: PHP Fundamentals Price: 450
Format: EPUB
如果你仔细查看 getebook() 函数,前两个赋值语句实际上是 getebook() 函数,而电子书类已经继承了它。因此,我们可以使用 parent 关键字和范围解析运算符调用它。
If you take a closer look at the getebook() function, the first two assignment statements are in fact there getbook() function, which the ebook class has inherited. Hence, we can call it with parent keyword and scope resolution operator.
用以下代码更改 getebook() 函数代码:
Change the getebook() function code with the following −
public function getebook(string $param1, int $param2, string $param3) {
parent::getbook($param1, $param2);
$this->format = $param3;
}
同样,dispebook() 函数中的第一个 echo 语句被一个调用替换,后者位于父类中调用 dispbook() 函数:
Similarly, the first echo statement in dispebook() function is replaced by a call to the dispbook() function in parent class −
public function dispebook() {
parent::dispbook();
echo "Format: $this->format<br/>";
}
Constructor in Inheritance
父类构造函数中的构造函数由子类继承,但是如果子类定义了一个构造函数,则无法在子类中直接调用它。
The constructor in the parent class constructor is inherited by the child class but it cannot be directly called in the child class if the child class defines a constructor.
为了运行一个父构造函数,需要在子构造函数内调用 parent::__construct() 。
In order to run a parent constructor, a call to parent::__construct() within the child constructor is required.
Example
请看以下示例:
Take a look at the following example −
<?php
class myclass{
public function __construct(){
echo "This is parent constructor". PHP_EOL;
}
}
class newclass extends myclass {
public function __construct(){
parent::__construct();
echo "This is child class destructor" . PHP_EOL;
}
}
$obj = new newclass();
?>
它将生成以下 output −
It will produce the following output −
This is parent constructor
This is child class destructor
然而,如果子类没有构造函数,那么它可以从父类继承构造函数,就像一个普通的类方法(如果它不是声明为私有的)。
However, if the child does not have a constructor, then it may be inherited from the parent class just like a normal class method (if it was not declared as private).
Example
请看以下示例:
Take a look at the following example −
<?php
class myclass{
public function __construct(){
echo "This is parent constructor". PHP_EOL;
}
}
class newclass extends myclass{ }
$obj = new newclass();
?>
它将生成以下 output −
It will produce the following output −
This is parent constructor
PHP 不允许通过扩展多个父类来开发一个类。你可以有 hierarchical inheritance ,其中类 B 扩展类 A,类 C 扩展类 B,以此类推。但是 PHP 不支持 multiple inheritance ,其中类 C 试图同时扩展类 A 和类 B。然而,我们可以扩展一个类并实现一个或多个 interfaces 。我们将在后续章节之一中学习关于接口的内容。
PHP doesn’t allow developing a class by extending more than one parents. You can have hierarchical inheritance, wherein class B extends class A, class C extends class B, and so on. But PHP doesn’t support multiple inheritance where class C tries to extend both class A and class B. We can however extend one class and implement one or more interfaces. We shall learn about interfaces in one of the subsequent chapters.
PHP – Class Constants
PHP 允许将类中的标识符定义为具有常量值的“类常量”,它在每个类基础上保持不变。为了与类中的变量或属性区分开来,该常量的名称没有前缀通常的“$”符号,并由“const”限定符定义。请注意,PHP 程序还可以使用 define() 函数创建全局常量。
PHP allows an identifier in a class to be defined as a "class constant" with a constant value, the one that remains unchanged on a per class basis. To differentiate from a variable or property within class, the name of the constant is not prefixed with the usual "$" symbol and is defined with the "const" qualifier. Note that a PHP program can also have a global constant created using the define() function.
常量的默认可见性是公共的,尽管可以在定义中使用其他修饰符。常量的值必须是一个表达式,而不是变量、函数调用或属性。常量值通过作用域解析运算符通过类名进行访问。在方法中,可以通过 self 变量引用它。
The default visibility of a constant is public, although other modifiers may be used in the definition. The value of a constant must be an expression and not a variable, nor a function call/property. The value of a constant is accessed through the class name using the scope resolution operator. Inside a method though, it can be referred to through self variable.
class SomeClass {
const CONSTANT = 'constant value';
}
echo SomeClass::CONSTANT;
Constant names are case sensitive 。传统上,常量的名称使用大写字母。
Constant names are case sensitive. Conventionally, the names of constants are in upper case.
Example
此示例显示了如何定义和访问类常量:
This example shows how a Class Constant is defined and accessed −
<?php
class square {
const PI=M_PI;
var $side=5;
function area() {
$area=$this->side**2*self::PI;
return $area;
}
}
$s1=new square();
echo "PI=". square::PI . "\n";
echo "area=" . $s1->area();
?>
它将生成以下 output −
It will produce the following output −
PI=3.1415926535898
area=78.539816339745
Class Constant as Expression
在这个示例中,类常量被分配一个表达式 -
In this example, the class constant is assigned an expression −
<?php
const X = 22;
const Y=7;
class square {
const PI=X/Y;
var $side=5;
function area() {
$area=$this->side**2*self::PI;
return $area;
}
}
$s1=new square();
echo "PI=". square::PI . "\n";
echo "area=" . $s1->area();
?>
它将生成以下 output −
It will produce the following output −
PI=3.1428571428571
area=78.571428571429
Class Constant Visibility Modifiers
请看以下示例:
Take a look at the following example −
<?php
class example {
const X=10;
private const Y=20;
}
$s1=new example();
echo "public=". example::X. "\n";
echo "private=" . $s1->Y ."\n";
echo "private=" . $example::Y ."\n";
?>
它将生成以下 output −
It will produce the following output −
public=10
PHP Notice: Undefined property: example::$Y in line 11
private=
PHP Fatal error: Uncaught Error: Cannot access private const example::Y
PHP – Abstract Classes
PHP 中的保留字列表包括“abstract”关键字。当用“abstract”关键字定义一个类时,它不能被实例化,即,你不能声明这样的类的对象。抽象类可以由另一个类扩展。
The list of reserved words in PHP includes the "abstract" keyword. When a class is defined with the "abstract" keyword, it cannot be instantiated, i.e., you cannot declare a new object of such a class. An abstract class can be extended by another class.
abstract class myclass {
// class body
}
如上所述,你可以 cannot declare an object of this class 。因此,下面的语句 -
As mentioned above, you cannot declare an object of this class. Hence, the following statement −
$obj = new myclass;
将导致一个 error 消息,如下所示 -
will result in an error message as shown below −
PHP Fatal error: Uncaught Error: Cannot instantiate abstract class myclass
一个抽象类可能包含属性、常量或方法。类成员可以是 public、private 或 protected 类型的。一个类中的一个或更多方法也可以被定义为抽象的。
An abstract class may include properties, constants or methods. The class members may be of public, private or protected type. One or more methods in a class may also be defined as abstract.
如果一个类中的任何方法是抽象的,类本身必须是一个抽象类。换句话说,一个普通的类不能有在其中定义的抽象方法。
If any method in a class is abstract, the class itself must be an abstract class. In other words, a normal class cannot have an abstract method defined in it.
这会产生一个 error -
This will raise an error −
class myclass {
abstract function myabsmethod($arg1, $arg2);
function mymethod() #this is a normal method {
echo "Hello";
}
}
error message 将被显示为 -
The error message will be shown as −
PHP Fatal error: Class myclass contains 1 abstract method
and must therefore be declared abstract
你可以使用一个抽象类作为一个父类并用一个子类扩展它。但是,子类必须为父类中的每一个抽象方法提供具体的实现,否则会遇到错误。
You can use an abstract class as a parent and extend it with a child class. However, the child class must provide concrete implementation of each of the abstract methods in the parent class, otherwise an error will be encountered.
Example
在以下代码中, myclass 是一个 abstract class , myabsmethod() 作为其 abstract method 。它派生的类是 mynewclass ,但它没有实现其父类中的抽象方法。
In the following code, myclass is an abstract class with myabsmethod() as an abstract method. Its derived class is mynewclass, but it doesn’t have the implementation of the abstract method in its parent.
<?php
abstract class myclass {
abstract function myabsmethod($arg1, $arg2);
function mymethod() {
echo "Hello";
}
}
class newclass extends myclass {
function newmethod() {
echo "World";
}
}
$m1 = new newclass;
$m1->mymethod();
?>
在这种情况下 error message -
The error message in such a situation is −
PHP Fatal error: Class newclass contains 1 abstract method and must
therefore be declared abstract or implement the remaining
methods (myclass::myabsmethod)
它表明新类应该实现抽象方法或者应该被声明为抽象类。
It indicates that newclass should either implement the abstract method or it should be declared as an abstract class.
Example
在以下 PHP 脚本中,我们有 marks 作为抽象类,其中 percent() 是其中一个抽象方法。另一个 student 类扩展了 marks 类并实现了其 percent() 方法。
In the following PHP script, we have marks as an abstract class with percent() being an abstract method in it. Another student class extends the marks class and implements its percent() method.
<?php
abstract class marks {
protected int $m1, $m2, $m3;
abstract public function percent(): float;
}
class student extends marks {
public function __construct($x, $y, $z) {
$this->m1 = $x;
$this->m2 = $y;
$this->m3 = $z;
}
public function percent(): float {
return ($this->m1+$this->m2+$this->m3)*100/300;
}
}
$s1 = new student(50, 60, 70);
echo "Percentage of marks: ". $s1->percent() . PHP_EOL;
?>
它将生成以下 output −
It will produce the following output −
Percentage of marks: 60
Difference between Interface and Abstract Class in PHP
PHP 中抽象类的概念与接口非常相似。但是,接口和抽象类之间有几个不同之处。
The concept of abstract class in PHP is very similar to interface. However, there are a couple of differences between an interface and an abstract class.
Abstract class |
Interface |
Use abstract keyword to define abstract class |
Use interface keyword to define interface |
Abstract class cannot be instantiated |
Interface cannot be instantiated. |
Abstract class may have normal and abstract methods |
Interface must declare the methods with arguments and return types only and not with any body. |
Abstract class is extended by child class which must implement all abstract methods |
Interface must be implemented by another class, which must provide functionality of all methods in the interface. |
Can have public, private or protected properties |
Properties cannot be declared in interface |
PHP – Interfaces
正如类是其对象的一个模板一样,PHP 中的 interface 可称为类模板。我们知道当一个类被实例化时,该类中定义的属性和方法对它可用。类似地,PHP 中的接口声明了方法及其参数和返回值。这些方法没有任何方法体,即在接口中没有定义任何功能。
Just as a class is a template for its objects, an interface in PHP can be called as a template for classes. We know that when a class is instantiated, the properties and methods defined in a class are available to it. Similarly, an interface in PHP declares the methods along with their arguments and return value. These methods do not have any body, i.e., no functionality is defined in the interface.
一个 concrete 类必须实现接口中的方法。换句话说,当一个类实现一个接口时,它必须为接口中的所有方法提供功能。
A concrete class has to implement the methods in the interface. In other words, when a class implements an interface, it must provide the functionality for all methods in the interface.
接口的定义方式与类的定义方式相同,只是使用了关键字 interface 来代替类。
An interface is defined in the same way as a class is defined, except that the keyword "interface" is used in place of class.
interface myinterface {
public function myfunction(int $arg1, int $arg2);
public function mymethod(string $arg1, int $arg2);
}
注意,接口中的方法没有任何功能。这些方法的定义必须由实现此接口的类提供。
Note that the methods inside the interface have not been provided with any functionality. Definitions of these methods must be provided by the class that implements this interface.
当定义一个子类时,我们使用关键字 extends 。在这种情况下,该类必须使用关键字 implements 。
When we define a child class, we use the keyword "extends". In this case, the class that must use the keyword "implements".
必须定义接口中声明的所有方法,参数和返回值的数量和类型均相同。
All the methods declared in the interface must be defined, with the same number and type of arguments and return value.
class myclass implements myinterface {
public function myfunction(int $arg1, int $arg2) {
## implementation of myfunction;
}
public function mymethod(string $arg1, int $arg2) {
# implementation of mymethod;
}
}
Note 在接口中声明的所有方法都必须为公共方法。
Note that all the methods declared in an interface must be public.
Example
让我们定义一个名为 shape 的接口。形状具有一定的面积。你有不同几何形状的形状,如矩形、圆形等,每个形状都有一个面积,使用不同的公式计算。因此形状接口声明了一个方法 area(),该方法返回一个浮点值。
Let us define an interface called shape. A shape has a certain area. You have shapes of different geometrical appearance, such as rectangle, circle etc., each having an area, calculated with different formula. Hence the shape interface declares a method area() that returns a float value.
interface shape {
public function area(): float;
}
接下来,我们将定义一个实现形状接口的圆形类,为了实现,该类必须提供接口中函数的具体实现。此处,圆形类中的 area() 函数计算给定半径的圆的面积。
Next, we shall define a circle class that implements shape interface, to implement, the class must provide a concrete implementation of the functions in the interface. Here, the area() function in circle class calculates the area of a circle of a given radius.
class circle implements shape {
var $radius;
public function __construct($arg1) {
$this->radius = $arg1;
}
public function area(): float {
return pow($this->radius,2)*pi();
}
}
现在我们可以声明一个圆形类的对象并调用 area() 方法。
We can now declare an object of circle class, and call the area() method.
$cir = new circle(5);
echo "Radius : " . $cir->radius . " Area of Circle: " . $cir->area(). PHP_EOL;
一个接口可以由任意数量的类(它们可能彼此无关)实现,只要实现类提供接口中每个方法的功能即可。
An interface can be implemented by any number of classes (which may be unrelated to each other) provided the implementing class provides functionality of each method in the interface.
这里有一个实现形状的 Square 类。area() 方法返回边属性的平方。
Here is a Square class that implements shape. The area() method returns the square of the side property.
class square implements shape {
var $side;
public function __construct($arg1) {
$this->side = $arg1;
}
public function area(): float {
return pow($this->side, 2);
}
}
类似地,创建一个 Square 对象并调用 area() 方法。
Similarly, create a Square object and call the area() method.
Example
以下是形状接口的完整代码,由圆形和 Square 类实现 −
Given below is the complete code for a shape interface, implemented by circle and Square classes −
<?php
interface shape {
public function area(): float;
}
class square implements shape {
var $side;
public function __construct($arg1) {
$this->side = $arg1;
}
public function area(): float {
return pow($this->side, 2);
}
}
class circle implements shape {
var $radius;
public function __construct($arg1) {
$this->radius = $arg1;
}
public function area(): float {
return pow($this->radius,2)*pi();
}
}
$sq = new square(5);
echo "Side: " . $sq->side . " Area of Square: ". $sq->area() . PHP_EOL;
$cir = new circle(5);
echo "Radius: " . $cir->radius . " Area of Circle: " . $cir->area(). PHP_EOL;
?>
它将生成以下 output −
It will produce the following output −
Side: 5 Area of Square: 25
Radius: 5 Area of Circle: 78.539816339745
Multiple Inheritance in PHP
PHP 无法建立扩展两个父类的子类。换句话说,该语句 −
PHP doesn’t have the provision to build a child class that extends two parent classes. In other words, the statement −
class child extends parent1, parent2
不受支持。但是,PHP 支持扩展一个父类并实现一个或多个接口的子类。
is not accepted. However, PHP does support having a child class that extends one parent class, and implementing one or more interfaces.
我们来看看下面的示例,该示例显示了一个扩展另一个类并实现一个接口的类。
Let use look at the following example that shows a class that extends another and implements an interface.
首先,父类标记。它具有三个实例变量或属性 $m1、$m2、$m3,分别表示三个科目中的分数。提供了一个 constructor 来初始化对象。
First, the parent class marks. It has three instance variables or properties $m1, $m2, $m3 representing the marks in three subjects. A constructor is provided to initialize the object.
class marks {
protected int $m1, $m2, $m3;
public function __construct($x, $y, $z) {
$this->m1 = $x;
$this->m2 = $y;
$this->m3 = $z;
}
}
我们现在提供一个名为 percent 的接口,它声明了一个方法 percent(),它应该返回一个浮点数,但没有函数体。
We now provide an interface called percent that declares a method percent(), which should return a float but doesn’t have a function body.
interface percent {
public function percent(): float;
}
我们现在开发一个类来扩展 marks 类并在接口中为 percent() 方法提供实现。
We now develop a class that extends marks class and provides implementation for percent() method in the interface.
class student extends marks implements percent {
public function percent(): float {
return ($this->m1+$this->m2+$this->m3)*100/300;
}
}
student 类继承了父构造函数,但提供了 parent() 方法的实现,该方法返回分数的百分比。
The student class inherits the parent constructor, but provides implementation of parent() method that returns the percentage of marks.
Example
完整代码如下所示:
The complete code is as follows −
<?php
class marks {
protected int $m1, $m2, $m3;
public function __construct($x, $y, $z) {
$this->m1 = $x;
$this->m2 = $y;
$this->m3 = $z;
}
}
interface percent {
public function percent(): float;
}
class student extends marks implements percent {
public function percent(): float {
return ($this->m1+$this->m2+$this->m3)*100/300;
}
}
$s1 = new student(50, 60, 70);
echo "Percentage of marks: ". $s1->percent() . PHP_EOL;
?>
它将生成以下 output −
It will produce the following output −
Percentage of marks: 60
PHP 中的接口定义了一个方法框架,用于类提供自己的不同但具体的实现。
The interface in PHP defines a framework of methods that classes use to provide a different but concrete implementation of their own.
PHP – Traits
在 PHP 中,一个类只能从一个父类继承,多重继承在 PHP 中没有定义。PHP 中的 Traits 被引入来克服此限制。你可以在一个 Trait 中定义一个或多个方法,它们可以自由地重复用于各种独立的类。
In PHP, a class can inherit only from one parent class, multiple inheritance is not defined in PHP. Traits in PHP have been introduced to overcome this limitation. You can define one or more method in a trait, which can be reused freely in various independent classes.
Syntax
"trait" 关键字按以下语法使用 −
The "trait" keyword is used as per the following syntax −
trait mytrait {
function method1() {
/*function body*/
}
function method2() {
/*function body*/
}
}
为了能够调用 Trait 中的方法,需要使用 use 关键字将其提供给另一个类。
To be able to call the methods in a trait, it needs to made available to another class with use keyword.
Example
Trait 类似于一个类,但只用于以细粒度和一致的方式对功能进行分组。无法单独实例化 Trait。
A Trait is similar to a class, but only intended to group functionality in a fine-grained and consistent way. It is not possible to instantiate a Trait on its own.
<?php
trait mytrait {
public function hello() {
echo "Hello World from " . __TRAIT__ . "";
}
}
class myclass {
use mytrait;
}
$obj = new myclass();
$obj->hello();
?>
它将生成以下 output −
It will produce the following output −
Hello World from mytrait
Example
一个 Trait 可以用在多个类中。以下示例有一个带有 avg() 函数 int 的 mytrait。它在 marks 类中使用。percent() 方法在内部从 Trait 调用 avg() 函数。
A trait can be used in more than one classes. The following example has a mytrait with avg() function int it. It is used inside a marks class. The percent() method internally calls the avg() function from the trait.
请看以下示例:
Take a look at the following example −
<?php
trait mytrait {
function avg($x, $y) {
return ($x+$y)/2;
}
}
class marks {
use mytrait;
private int $m1, $m2;
function __construct($x, $y) {
$this->m1 = $x;
$this->m2 = $y;
}
function percent():float {
return $this->avg($this->m1, $this->m2);
}
}
$obj = new marks(50, 60);
echo "percentage: " . $obj->percent();
?>
它将生成以下 output −
It will produce the following output −
percentage: 55
Using Multiple Traits
一个类可以使用多个 Trait。这里我们有两个 Trait,每个 Trait 都带有一个函数,分别对两个数字进行加法和乘法。两者都在第三个类中使用。
A class can use more than one traits. Here we have two traits with one function each performing addition and multiplication of two numbers. Both are used inside a third class.
<?php
trait addition {
function add($x, $y) {
return $x+$y;
}
}
trait multiplication {
function multiply($x, $y) {
return $x*$y;
}
}
class numbers {
use addition, multiplication;
private int $m1, $m2;
function __construct($x, $y) {
$this->m1 = $x;
$this->m2 = $y;
}
function calculate():array {
$arr = [$this->add($this->m1, $this->m2), $this->multiply($this->m1, $this->m2)];
return $arr;
}
}
$obj = new numbers(50, 60);
$res = $obj->calculate();
echo "Addition: " . $res[0] . PHP_EOL;
echo "Multiplication: " . $res[1] . PHP_EOL;
?>
它将生成以下 output −
It will produce the following output −
Addition: 110
Multiplication: 3000
Overriding Trait Function
当一个类使用某个 Trait 时,其函数对它可用,就像子类继承父类方法一样。Trait 函数也可以被覆盖。
When a class uses a certain trait, its function are available to it just as a child class inherits the parent methods. The trait function may also be overridden.
<?php
trait mytrait {
public function sayHello() {
echo 'Hello World!';
}
}
class myclass {
use mytrait;
public function sayHello() {
echo 'Hello PHP!';
}
}
$o = new myclass();
$o->sayHello();
?>
它将生成以下 output −
It will produce the following output −
Hello PHP!
The "insteadof" Keyword
有时,两个以上的 Trait 可能会具有相同名称的函数。因此,在类中使用它们会导致模棱两可的情况。PHP 提供 insteadof 关键字来告知解析器你要使用哪个 Trait 函数。
Sometimes, more two traits might have same name of the function. Hence, using them in a class creates ambiguous situation. PHP provides insteadof keyword to tell the parser function from which trait you intend to use.
<?php
trait mytrait {
public function sayHello() {
echo 'Hello World!';
}
}
trait newtrait {
public function sayHello() {
echo 'Hello PHP!';
}
}
class myclass {
use mytrait, newtrait{
newtrait::sayHello insteadof mytrait;
}
}
$o = new myclass();
$o->sayHello();
?>
它将生成以下 output −
It will produce the following output −
Hello PHP!
Aliasing a Trait Function
如果你希望能够从这两个 Trait 中调用函数,即使它们具有同名的函数,解决方法是为其中一个指定别名。
If you want to be able to call functions from both traits even if they have function with same name, a workaround is to specify an alias name to one of them.
Example
在以下示例中,我们将 mytrait 中的 sayHello() 称为 hello() −
In the following example, we will call sayHello() from mytrait as hello() −
<?php
trait mytrait {
public function sayHello() {
echo 'Hello World!' . PHP_EOL;
}
}
trait newtrait {
public function sayHello() {
echo 'Hello PHP!' . PHP_EOL;
}
}
class myclass {
use mytrait, newtrait{
mytrait::sayHello as hello;
newtrait::sayHello insteadof mytrait;
}
}
$o = new myclass();
$o->hello();
$o->sayHello();
?>
它将生成以下 output −
It will produce the following output −
Hello World!
Hello PHP!
PHP – Static Methods
PHP 中的 "static" 关键字用于在 PHP 类中定义静态属性和静态方法。需要注意的是,static 关键字也用于定义静态变量和静态匿名函数。本章讨论 PHP 类中的静态方法。
The "static" keyword in PHP is used to define static properties and static methods in a PHP class. It may be noted that the static keyword is also used to define static variable, and static anonymous functions. This chapter discusses static methods in a PHP class.
在类定义中,用 static 限定符声明的函数成为其静态方法。
In a class definition, a function declared with a static qualifier becomes its static method.
class myclass {
public static function myStaticMethod() {
// ...
}
您无需创建类的实例即可调用该类的静态方法。通过作用域解析运算符,根据类名称来调用该静态方法。静态方法调用的语法为 −
You don’t need to create the instance of the class to call its static method. The static method is called by the class name though the scope resolution operator. The syntax of a static method call is −
myclass::myStaticMethod();
由于无需创建类的实例即可调用静态方法,因此伪变量 $this 在静态方法内部不可用。允许由对象调用一个静态方法,尽管将一个实例方法作为静态方法调用会引发错误。
As the static methods are callable without creating an instance of the class, the pseudo-variable $this is not available inside static methods. A static method is allowed to be called by an object, although calling an instance method as a static method raises error.
Example
请看以下示例:
Take a look at the following example −
<?php
class myclass {
/* Member variables */
static int $var1 = 0;
public static function mystaticmethod() {
echo "This is a static method". PHP_EOL;
}
public function myinstancemethod() {
echo "This is an instance method". PHP_EOL;
}
}
myclass::mystaticmethod();
$obj = new myclass;
$obj->myinstancemethod();
$obj->mystaticmethod();
myclass::myinstancemethod();
?>
它将生成以下 output −
It will produce the following output −
This is a static method
This is an instance method
This is a static method
PHP Fatal error: Uncaught Error: Non-static method
myclass::myinstancemethod() cannot be called statically
The "self" Keyword in Static Method
如果您需要从在同一类中定义的实例方法内部调用静态方法,那么必须使用引用类名称的 self 关键字,然后是作用域解析运算符(如 self::mystaticmethod)
If you need to call a static method from inside an instance method defined in the same class, you have to use self keyword referring to the name of the class, followed by the scope resolution operator (such as self::mystaticmethod)
<?php
class myclass {
/* Member variables */
static int $var1 = 0;
public static function mystaticmethod() {
echo "This is a static method". PHP_EOL;
}
public function myinstancemethod() {
echo "This is an instance method". PHP_EOL;
echo "calling static method from instance method" . PHP_EOL;
self::mystaticmethod();
}
}
$obj = new myclass;
$obj->myinstancemethod();
?>
它将生成以下 output −
It will produce the following output −
This is an instance method
calling static method from instance method
This is a static method
Using the "parent" Keyword
在继承的情况下,通过引用“parent”关键字,可以根据派生类的对象或从派生类的实例方法内部调用在基类中定义的静态方法。
In case of inheritance, a static method defined in a base class may be called by an object of derived class, or from inside an instance method of the derived class, by referring it with the "parent" keyword.
Example
请看以下示例:
Take a look at the following example −
<?php
class myclass {
/* Member variables */
static int $var1 = 0;
public static function mystaticmethod() {
echo "This is a static method". PHP_EOL;
}
public function myinstancemethod() {
echo "This is an instance method". PHP_EOL;
echo "calling static method from instance method" . PHP_EOL;
self::mystaticmethod();
}
}
class mynewclass extends myclass {
public function myfunction() {
echo "This an instance method of the derived class" . PHP_EOL;
echo "Calling static method of the parent class" . PHP_EOL;
parent::mystaticmethod();
}
}
$obj = new mynewclass;
mynewclass::mystaticmethod();
$obj->myfunction();
?>
它将生成以下 output −
It will produce the following output −
This is a static method
This an instance method of the derived class
Calling static method of the parent class
This is a static method
Static Method Inside Another Class
完全可以从一个类中调用另一个类中的静态方法。您必须用其类名称和作用域解析运算符限定其名称。
It is entirely possible to call the static method from one class in another. You have to qualify its name with its class name followed by the scope resolution operator.
Example
请看以下示例:
Take a look at the following example −
<?php
class myclass {
/* Member variables */
static int $var1 = 0;
public static function mystaticmethod() {
echo "This is a static method". PHP_EOL;
}
}
#this is not a derived class
class mynewclass {
public function myfunction() {
echo "This an instance method" . PHP_EOL;
echo "Calling static method of the another class" . PHP_EOL;
myclass::mystaticmethod();
}
}
$obj = new mynewclass;
$obj->myfunction();
?>
它将生成以下 output −
It will produce the following output −
This an instance method
Calling static method of another class
This is a static method
由于 $this 伪变量对于静态方法不可用,因此对象实例变量无法在静态方法内部访问。它只能处理该类的静态属性。
Since $this pseudo-variable is not available for a static method, the instance variables of an object cannot be accessed inside a static method. It can process only the static properties of the class.
Example
请看以下示例:
Take a look at the following example −
<?php
class myclass {
/* Member variables */
static int $var1 = 0;
function __construct() {
self::$var1++;
echo "object number ". self::$var1 . PHP_EOL;
}
public static function mystaticmethod() {
echo "Number of objects available: " . self::$var1 . PHP_EOL;
}
}
for ($i=1; $i<=3; $i++) {
$obj = new myclass;
}
myclass::mystaticmethod();
?>
它将生成以下 output −
It will produce the following output −
object number 1
object number 2
object number 3
Number of objects available: 3
PHP – Static Properties
PHP 中的“static”关键字用于在 PHP 类中定义静态属性和静态方法。可能注意的是,static 关键字也用于定义静态变量和静态匿名函数。阅读本章以了解 PHP 类中的静态属性。
The "static" keyword in PHP is used to define static properties and static methods in a PHP class. It may be noted that the static keyword is also used to define static variable, and static anonymous functions. Read this chapter to learn about the static properties in a PHP class.
在类定义中,用静态限定声明的变量将成为其静态属性。static 关键字可以在访问修饰符之前或之后出现。
In a class definition, a variable declared with a static qualifier becomes its static property. The static keyword may appear before or after the access modifier.
static private $var1;
public static $var2;
如果您想使用类型提示,该类型一定不能在 static 关键字之前。
If you want to use type hints, the type must not be before the static keyword.
static private string $var1;
public static float $var2;
静态属性在类中的值无法通过其对象(使用 → 运算符)访问。这样做会产生一条指出 Accessing static property myclass::$var1 as non static 的通知。相反,可以使用由“::”符号表示的范围解析运算符访问静态属性。
The value of the static property in a class is not accessible by its object (with the → operator). Doing so will result in a notice stating Accessing static property myclass::$var1 as non static. Instead, the static properties are accessed using the Scope Resolution Operator represented by the "::" symbol.
Example
请看以下示例:
Take a look at the following example −
<?php
class myclass {
static string $var1 = "My Class";
function __construct() {
echo "New object declared" . PHP_EOL;
}
}
$obj = new myclass;
echo "accessing static property with scope resolution operator: " . myclass::$var1 . PHP_EOL;
echo "accessing static property with -> operator: ". $obj->var1 . PHP_EOL;
?>
它将生成以下 output −
It will produce the following output −
New object declared
accessing static property with scope resolution operator: My Class
PHP Notice: Accessing static property myclass::$var1 as non static in hello.php on line 14
The "self" Keyword
要从方法内部访问静态属性,请使用 self 关键字引用当前类。在以下示例中,该类具有一个整型静态属性,每当声明一个新对象时都会对其进行递增。
To access the static property from inside a method, refer to the current class with the self keyword. In the following example, the class has an integer static property, which is incremented every time a new object is declared.
<?php
class myclass {
/* Member variables */
static int $var1 = 0;
function __construct(){
self::$var1++;
echo "object number ". self::$var1 . PHP_EOL;
}
}
for ($i=1; $i<=3; $i++) {
$obj = new myclass;
}
?>
它将生成以下 output −
It will produce the following output −
object number 1
object number 2
object number 3
The "parent" Keyword
可以将基类的静态属性通过引用 parent 关键字用在继承类的函数中。您需要使用“parent::static_property”语法。
The static property of a base class can be used inside a function of the inherited class by referring to the base by parent keyword. You need to use the "parent::static_property" syntax.
Example
查看以下示例 −
Take look at the following example −
<?php
class myclass {
/* Member variables */
static int $var1 = 0;
function __construct() {
self::$var1++;
echo "object number ". self::$var1 . PHP_EOL;
}
}
class newclass extends myclass{
function getstatic() {
echo "Static property in parent class: " . parent::$var1 . PHP_EOL;
}
}
$obj = new newclass;
$obj->getstatic();
?>
它将生成以下 output −
It will produce the following output −
object number 1
Static property in parent class: 1
PHP - Namespaces
我们经常将文件组织在不同的文件夹中。通常情况下,一个文件夹包含与某个目标、应用程序或类别相关联的文件。一个文件夹不能包含两个具有相同名称的文件,尽管不同的文件夹可能具有一个名称相同的文件,因此每个文件路径都是不同的。
We often organize the files in different folders. Usually a folder contains files related to a certain objective, or application or category. A folder can’t contain two files with the same name, though different folders may have a file of the same name so that the path of each file is different.
PHP 中的命名空间理念有点类似。在 PHP 中,命名空间允许在不同上下文中使用相同名称的类或函数或常量,而不会出现任何冲突,从而封装这些项。
The idea of namespaces in PHP is somewhat similar. In PHP, namespaces allow classes or functions or constants of same name be used in different contexts without any conflict, thereby encapsulating these items.
PHP 命名空间是根据它们的关联性对类/函数等进行的逻辑分组。正如同名的文件可以存在于两个不同的文件夹中一样,命名空间中也可以定义一个特定名称的类。此外,由于我们指定文件的完整路径来获取访问权限,因此我们需要指定类及其命名空间的完整名称。
A PHP namespace is logical grouping of classes/functions etc., depending on their relevance. Just as a file with same name can exist in two different folders, a class of a certain name can be defined in two namespaces. Further, as we specify the complete path of a file to gain access, we need to specify full name of class along with namespace.
随着应用程序规模的不断增大,包括许多类和函数定义,为每个类/函数提供一个唯一的名称可能会变得繁琐并且不够优雅。使用命名空间可让您以一种简洁的方式组织此类代码块。例如,如果我们需要声明一个 calculate() 函数来计算面积和税费,我们可以创建两个命名空间 area 和 tax,并在其中使用 calculate() 而不必将它们定义为类似 calculate_area() 和 calculate_tax() 的内容。
As your application size becomes bigger, involving many class and function definitions, giving give a unique name to each class/function may become tedious and not exactly elegant. Using namespaces lets you organize such code blocks in a neat manner. For example, if we need to declare a calculate() function to calculate area as well as tax, instead of defining them as something like calculate_area() and calculate_tax(), we can create two namespaces area and tax and use calculate() inside them.
Advantages of Namespace
下面列出了一些使用 PHP 中的命名空间的优点 −
Here are some of the advantages of using namespaces in PHP −
-
Namsepaces help in avoiding name collisions between classes/functions/constants defined by someone with third-party classes/functions/constants.
-
Namespaces provide the ability to alias (or shorten) Extra_Long_Names, thereby improving the readability of source code.
-
PHP Namespaces provide a way in which to group related classes, interfaces, functions and constants. Namespace names are case – insensitive.
Defining a Namespace
PHP 的名称空间关键字用于定义新的命名空间。
PHP’s namespace keyword is used to define a new namespace.
namespace myspace;
包含命名空间的 “.php” 文件必须在文件顶部声明命名空间,然后再声明任何其他内容(声明指令除外)。在命名空间中声明类、函数和常量会影响其访问权限。
A ".php" file containing a namespace must declare the namespace at the top of the file before any other (except the declare directive). Declaration of class, function and constants inside a namespace affects its access.
一个 PHP 脚本可以除了定义命名空间之外,还包含其他代码。为了加载在相同代码中定义的命名空间,PHP 具有“use”关键字。
A PHP script may contain other code apart from the definition of a namespace. To load the namespace defined in the same code, PHP has the "use" keyword.
use myspace;
Example
在以下 “hello.php” 脚本中,我们在 myspace 命名空间中定义了一个 hello() 函数,并在当前脚本中加载该命名空间之后对其进行调用。
In the following "hello.php" script, we define a hello() function inside myspace namespace, and call it after loading the namespace in the current script.
<?php
namespace myspace;
function hello() {
echo "Hello World";
}
use myspace;
myspace\hello();
?>
它将生成以下 output −
It will produce the following output −
Hello World
请注意,您必须用名称空间完整的名称限定 hello() 函数,其中包括名称空间 - myspace\hello()。
Note that you must qualify the hello() function with its full name that includes the namespace - myspace\hello().
Include Namespace
可以有一个脚本包含名称空间声明,以及在其中使用 include 语句加载该名称空间的另一个脚本。
You may have one script consisting of a declaration of a namespace, and the other script in which the namespace is loaded with include statement.
b.php
<?php
include 'a.php';
myspace\hello();
?>
它将生成以下 output −
It will produce the following output −
Hello World in myspace
在当前的脚本(如上所述的“b.php”)中,可能还有一个函数与 include 文件中函数名称相同。附加名称空间的完全限定函数,帮助解析器解决名称冲突。
There may be a case where the current script ("b.php" as above) also has a function of the same name as in the included file. The fully qualified function that prepends the namespace, helps the parser to resolve the name conflict.
Example
请看以下示例:
Take a look at the following example −
<?php
include 'a.php';
function hello() {
echo "Hello World from current namespace";
}
hello();
myspace\hello();
?>
它将生成以下 output −
It will produce the following output −
Hello World from current namespace
Hello World in myspace
Example
如上所述,名称空间声明必须在最上面,紧跟在 <?php 标签的后面。否则,解析器会抛出致命错误。
As mentioned above, the namespace declaration must be at the top, immediately after the opening <?php tag. Otherwise the parser throws a fatal error.
<?php
echo "hello"
namespace myspace;
function hello() {
echo "Hello World";
}
use myspace;
myspace\hello();
?>
它将生成以下 output −
It will produce the following output −
PHP Parse error: syntax error, unexpected token "namespace",
expecting "," or ";" in /home/cg/root/67771/main.php on line 4
上面的错误信息明确指出,在名称空间声明之前,只允许出现“declare 语句”。
The above error message makes it clear that only the "declare statement" is allowed to appear before the namespace declaration.
<?php
declare (strict_types=1);
namespace myspace;
function hello() {
echo "Hello World";
}
use myspace;
myspace\hello();
?>
Relative Namespace
可以通过相对于名称空间的路径引用当前名称空间中的对象,如函数、类和常量。
The objects such as functions, classes and constants may be accessed in the current namespace by referring the with relative namespace paths.
在下面的示例中,“b.php”包含一个带有 hello() 函数和 TEMP 常量的名称空间 space1\myspace。这些对象也在“a.php”中定义的名称空间 space1 中被定义。
In the following example, "b.php" contains a namespace space1\myspace with a hello() function and a TEMP constant. The same objects are also defined in namespace space1, present in "a.php".
显然,当“b.php”被包含在“a.php”时,“myspace”是“space1”的子空间。因此,通过对其相对名称空间(也是 TEMP 常量)加上前缀来调用来自“myspace”的 hello()。
Obviously, when "b.php" is included in "a.php", "myspace" is a subspace of "space1". Hence, hello() from "myspace" is called by prefixing its relative namespace (also the TEMP constant)
b.php
<?php
namespace space1\myspace;
const TEMP = 10;
function hello() {
echo "Hello from current namespace:" . __NAMESPACE__ . ;
}
?>
a.php
<?php
namespace space1;
include 'b.php';
function hello() {
echo "Hello from current namespace:" . __NAMESPACE__ . ;
}
const TEMP = 100;
hello(); // current namespace
myspace\hello(); // sub namespace
echo "TEMP : " . TEMP . " in " . __NAMESPACE__ . ;
echo "TEMP : " . myspace\TEMP . " \\in space1\\myspace\n";
?>
它将生成以下 output −
It will produce the following output −
Hello from current namespace:space1
Hello from current namespace:space1\myspace
TEMP : 100 in space1
TEMP : 10 in space1\myspace
Absolute Namespace
也可以通过附加绝对名称空间路径来访问任何名称空间中的函数/常量。例如,“b.php”中的 hello() 是“\space\myspace\hello()”。
You can also access the functions/constants from any namespace by prefixing the absolute namespace path. For example, hello() in "b.php" is "\space\myspace\hello()".
a.php
<?php
namespace space1;
include 'b.php';
function hello() {
echo "Hello from current namespace:" . __NAMESPACE__ . ;
}
const TEMP = 100;
\space1\hello(); //current namespace
\space1\myspace\hello(); //sub namespace
echo "TEMP: " . \space1\TEMP . " in " . __NAMESPACE__ . ;
echo "TEMP: " . \space1\myspace\TEMP . " in space1\\myspace\n";
?>
NAMESPACE 是 PHP 中的预定义常量,用于返回当前名称空间的名称。
The NAMESPACE is a predefined constant in PHP that returns the name of current namespace.
Namespace Rules
通过遵循以下规则,可以解决不同名称空间之间出现的函数/类/常量名称冲突:
Any conflict in the names of function/classes/constants appearing between different namespaces is resolved by following these rules −
-
A namespace identifier without namespace separator symbol (/) means it is referring to current namespace. This is an unqualified name.
-
If it contains separator symbol as in myspace\space1, it resolves to a subnamespace space1 under myspace. Such type of naming is relative namespace.
-
Name of fully qualified namespace starts with the "\" character. For example, "\myspace" or "\myspace\space1".
-
Fully qualified names resolve to absolute namespace. For example \myspace\space1 resolves to myspace\space1 namespace
-
If the name occurs in the global namespace, the "namespace\" prefix is removed. For example, "namespace\space1" resolves to space1.
-
However, if it occurs inside another namespace, it is treated differently. For example, if namespace\space1 is inside myspace, it is equivalent to "myspace\space1".
-
First segment of the name in qualified name is translated according to the current class/namespace import table.
-
If no import rule applies, the current namespace is prepended to the name.
-
class-like names are translated according to the class/namespace import table, function names according to the function import table and constants according to the constant import table.
-
For unqualified names, if no import rule applies and the name refers to a function or constant and the code is outside the global namespace, the name is resolved at runtime. First it looks for a function from the current namespace, then it tries to find and call the global function.
PHP - Object Iteration
一个 foreach 循环可用于遍历 PHP 类的对象的公开可见的所有成员。此功能在 PHP 5 及其以后版本中提供。您当然可以访问实例方法中的私有属性列表。PHP 还定义了可用于此目的的 Iterator 接口。
A foreach loop may be employed to iterate through all the publicly visible members of an object of a PHP class. This feature has been available in versions of PHP 5 onwards. You can of course access the list of private properties inside an instance method. PHP also defines Iterator interface which can be used for the purpose.
Using foreach Loop
在下面的示例中,类的公开属性使用 foreach 循环列出。
In the example below, the public properties of the class are listed with the use of foreach loop.
Example
<?php
class myclass {
private $var;
protected $var1;
public $x, $y, $z;
public function __construct() {
$this->var="Hello World";
$this->var1=array(1,2,3);
$this->x=100;
$this->y=200;
$this->z=300;
}
}
$obj = new myclass();
foreach($obj as $key => $value) {
print "$key => $value\n";
}
?>
它将生成以下 output −
It will produce the following output −
x => 100
y => 200
z => 300
请注意,只有公共成员才能在类外访问。如果类包含一个方法,则所有成员(公有、私有或受保护的)都可以从内部通过 foreach 循环进行遍历。
Note that only the public members are accessible outside the class. If the class includes a method, all the members (public, private or protected) can be traversed with a foreach loop from inside it.
让我们在上面的 myclass 中添加一个迭代方法。
Let us add an iterate method in the above myclass.
public function iterate() {
foreach ($this as $k=>$v) {
if (is_array($v)) {
var_dump($v);
echo PHP_EOL;
} else {
echo "$k : $v". PHP_EOL;
}
}
}
调用此实例方法以获取所有成员的列表。
Call this instance method to get the list of all the members.
它将生成以下 output −
It will produce the following output −
var : Hello World
array(3) {
[0]=>
int(1)
[1]=>
int(2)
[2]=>
int(3)
}
x : 100
y : 200
z : 300
Using Iterator Interface
PHP 提供 Iterator 接口,用于可自身在内部进行迭代的外部迭代器或对象。它定义了下面需要在用户定义类中实现的抽象方法。
PHP provides Iterator interface for external iterators or objects that can be iterated themselves internally. It defines following abstract methods which need to be implemented in the user defined class.
interface Iterator extends Traversable {
/* Methods */
public current(): mixed
public key(): mixed
public next(): void
public rewind(): void
public valid(): bool
}
-
The rewind() method rewinds the Iterator to the first element. This is the first method called when starting a foreach loop. It will not be executed after foreach loops.
-
The current() method returns the current element.
-
The key() method returns the key of the current element on each iteration of foreach loop.
-
The next() method is called after each iteration of foreach loop and moves forward to next element.
-
The valid() method checks if current position is valid.
Example
下面的示例通过实现 Iterator 接口演示对象迭代
The following example demonstrates object iteration by implementing Iterator interface
<?php
class myclass implements Iterator {
private $arr = array('a','b','c');
public function rewind():void {
echo "rewinding\n";
reset($this->arr);
}
public function current() {
$var = current($this->arr);
echo "current: $var\n";
return $var;
}
public function key() {
$var = key($this->arr);
echo "key: $var\n";
return $var;
}
public function next() : void {
$var = next($this->arr);
echo "next: $var\n";
# return $var;
}
public function valid() : bool {
$key = key($this->arr);
$var = ($key !== NULL && $key !== FALSE);
echo "valid: $var\n";
return $var;
}
}
$obj = new myclass();
foreach ($obj as $k => $v) {
print "$k: $v\n";
}
?>
它将生成以下 output −
It will produce the following output −
rewinding
valid: 1
current: a
key: 0
0: a
next: b
valid: 1
current: b
key: 1
1: b
next: c
valid: 1
current: c
key: 2
2: c
next:
PHP - Encapsulation
PHP 实现了 encapsulation ,这是 OOP 的一个重要原则,带有访问控制关键字 public, private 和 protected 。
PHP implements encapsulation, one of the important principles of OOP with access control keywords: public, private and protected.
封装是指将对象的数据成员或属性远离类外部环境获取的方式,仅允许通过类中可用的方法或函数进行受控访问。
Encapsulation refers to the mechanism of keeping the data members or properties of an object away from the reach of the environment outside the class, allowing controlled access only through the methods or functions available in the class.
下图展示了面向对象编程方法中封装的原则。
The following diagram illustrates the principle of encapsulation in object-oriented programming methodology.
PHP 的关键字列表包含以下关键字,这些关键字决定了 PHP 中对象实例的属性和方法的可访问性 −
PHP’s keywords list contains the following keywords that determine the accessibility of properties and methods of an object, which is an instance of a class in PHP −
-
Public − Class members are accessible from anywhere, even from outside the scope of the class, but only with the object reference.
-
Private − Class members can be accessed within the class itself. It prevents members from outside class access even with the reference of the class instance.
-
Protected − Members can be accessed within the class and its child class only, nowhere else.
这三个关键字“ public, private 和 protected ”通常称为访问修饰符。它们也被称为可见性模式,因为它们决定了某类成员可以使用的范围。
These three keywords "public, private and protected" are often called access modifiers. They are also referred as visibility modes, as they decide upto what extent a certain class member is available.
Public Members
在 PHP 中,类成员(成员变量和其他成员函数)默认情况下是公开的。
In PHP, the class members (both member variables as well as member functions) are public by default.
Example
在下面的程序中,对象的成员变量 title 和 price 在类的外部可以自由访问,因为它们默认情况下是公开的,除非另行指定。
In the following program, the member variables title and price of the object are freely accessible outside the class because they are public by default, if not otherwise specified.
<?php
class Person {
/* Member variables */
var $name;
var $age;
/*Constructor*/
function __construct(string $param1="Ravi", int $param2=28) {
$this->name = $param1;
$this->age = $param2;
}
function getName() {
echo "Name: $this->name" . PHP_EOL;;
}
function getAge() {
echo "Age: $this->age" . PHP_EOL;;
}
}
$b1 = new Person();
$b1->getName();
$b1->getAge();
echo "Name : $b1->name Age: $b1->age" . PHP_EOL;
?>
它将生成以下 output −
It will produce the following output −
Name: Ravi
Age: 28
Name : Ravi Age: 28
Note 由于默认情况下类成员的所有属性都为公有,因此如果需要,可以明确地宣告它们为公有。结果,可以从类的外围调用实例方法 getName() 和 getAge()。
Note that the properties all the class members are public by default, you can explicitly declare them as public if desired. As a result, the instance methods getName() and getAge() can be called from outside the class.
由于属性 name 和 age 也是公有的,因此也可以从类外访问它们,这与封装原则相违背。
Since properties name and age are also public, hence they can also be accessed outside the class, something which is not desired as per the principle of encapsulation.
Private Members
如上所述,封装原则要求不得直接访问成员变量。只有方法应具有对数据成员的访问权。因此,我们需要将成员变量设为私有,并将方法设为公有。
As mentioned above, the principle of encapsulation requires that the member variables should not be accessible directly. Only the methods should have the access to the data members. Hence, we need to make the member variables private and methods public.
Example
让我们将 name 和 age 属性的宣告改为私有,并运行以下 PHP 脚本:
Let us change the declaration of name and age properties to private and run the following PHP script −
<?php
class Person {
/* Member variables */
private $name;
private $age;
/*Constructor*/
function __construct(string $param1="Ravi", int $param2=28) {
$this->name = $param1;
$this->age = $param2;
}
public function getName() {
echo "Name: $this->name" . PHP_EOL;;
}
public function getAge(){
echo "Age: $this->age" . PHP_EOL;;
}
}
$b1 = new Person();
$b1->getName();
$b1->getAge();
echo "Name : $b1->name Age: $b1->age" . PHP_EOL;
?>
它将生成以下 output −
It will produce the following output −
Name: Ravi
Age: 28
PHP Fatal error: Uncaught Error: Cannot access private property Person::$name in person.php:27
该错误消息表明我们不能从公有范围访问私有属性。
The error message tells the reason that a private property cannot be accessed from a public scope.
Protected Members
针对类成员指定受保护的访问权在类继承的情况下有效。我们知道可以从类的外围访问公有成员,并且不能从类的外围访问私有成员。
The effect of specifying protected access to a class member is effective in case of class inheritance. We know that public members are accessible from anywhere outside the class, and private members are denied access from anywhere outside the class.
protected 关键字允许访问同一类的对象和继承类中的对象,但禁止访问其他环境。
The protected keyword grants access to an object of the same class and an object of its inherited class, denying it to any other environment.
Example
让我们继承 person 类并定义一个学生类。我们将 name 属性的私有级别更改为受保护级别。此学生类具有一个新的公有方法 getDetails(),它将打印 name 和 age 属性的值。
Let us inherit the person class and define a student class. We shall change the name property from private to protected. The student class has a new public method getDetails() that prints the values of name and age properties.
Person class
Person class
<?php
class Person {
/* Member variables */
protected $name;
private $age;
/*Constructor*/
function __construct(string $param1="Ravi", int $param2=28) {
$this->name = $param1;
$this->age = $param2;
}
public function getName(){
echo "Name: $this->name" . PHP_EOL;;
}
public function getAge() {
echo "Age: $this->age" . PHP_EOL;;
}
}
Student class
Student class
class student extends Person {
public function getDetails() {
echo "My Name: $this->name" . PHP_EOL;
echo "My age: $this->age" . PHP_EOL;
}
}
$s1 = new student();
$s1->getDetails();
?>
它将生成以下 output −
It will produce the following output −
My Name: Ravi
PHP Warning: Undefined property: student::$age in person.php on line 28
My age:
下表说明了 PHP 中类成员的可访问性规则:
The following table illustrates the rules of accessibility of class members in PHP −
PHP - The "Final" Keyword
PHP 中的 “final” 关键字用于类定义、类中的方法,以及类常量属性的定义。
The "final" keyword in PHP is used in the definition of a class, a method inside a class, as well as with the definition of a constant property of a class.
A Class with "final" Keyword
我们来看一下如何使用 “final” 关键字创建类 -
Let’s see how to create a class with the "final" keyword −
final class myclass {
/*class members*/
}
类定义中的 “final” 关键字防止此类被扩展。换句话说,您不能将 final 类用作父类。如果您尝试这样做,PHP 解析器会抛出一个错误
The "final" keyword in class definition prevents such a class from being extended. In other words, you cannot use a final class as a parent. If you try, PHP parser throws an error
<?php
final class myclass {
/* class body */
}
class newclass extends myclass {
/* class body */
}
?>
当您运行此代码时,它将显示 error -
When you run this code, it will show an error −
PHP Fatal error: Class newclass may not inherit from final class (myclass)
Method with "final" Keyword
下面是使用 “final” 关键字创建方法的方式 -
Here is how you can create a method with the "final" keyword −
class myclass {
final function myfunction() {
/* function body */
}
}
用 final 关键字给方法定义加上前缀,可以防止在子类中覆盖它。具有 final 方法的类可以扩展,但子类不能覆盖它。
Prefixing a method definition with the final keyword prevents it from being overridden in a child class. A class with final method can be extended, but the child class cannot override it.
Example
请看以下示例:
Take a look at the following example −
<?php
class myclass {
final public function hello() {
echo "Hello World!";
}
}
class newclass extends myclass {
public function hello() {
echo "Hello PHP!";
}
}
?>
当您运行此代码时,它将显示 error -
When you run this code, it will show an error −
PHP Fatal error: Cannot override final method myclass::hello() in hello.php
Constant with "final" Keyword
还可以使用 final 关键字在类中声明常量,从 PHP 8.1.0 开始。
You can also declare a constant in a class with the final keyword, starting from PHP 8.1.0 onwards.
final public const NAME = "My Class";
如果您尝试在子类中覆盖父类的最终常量,将会遇到错误。
If you try to override a final constant from parent class in a child class, an error is encountered.
<?php
class myclass {
final public const NAME = "My Class";
final public function hello() {
echo "Hello World!";
}
}
class newclass extends myclass {
public const NAME = "New Class";
}
?>
当您运行此代码时,它将显示 error -
When you run this code, it will show an error −
Fatal error: newclass::NAME cannot override final constant myclass::NAME
Example
以下 PHP 脚本包含父类 ellipse,其中 PI 常量和 area() 方法均声明为 final。它们由 circle 类继承。area() 函数计算圆的面积。
The following PHP script contains a parent class ellipse with a PI constant and area() method both declared as final. They are inherited by the circle class. The area() function calculates the area of circle.
<?php
class ellipse {
final public const PI=22/7;
private float $a, $b;
public function __construct($x, $y) {
$this->a = $x;
$this->b = $y;
}
final public function area() : float {
return self::PI*$this->a*$this->b;
}
}
class circle extends ellipse {
public function __construct(float $x) {
parent::__construct($x, $x);
}
}
$c1 = new circle(5);
echo "Area: " . $c1->area() . PHP_EOL;
?>
它将生成以下 output −
It will produce the following output −
Area: 78.571428571429
请注意,类的实例变量或属性不能声明为 final。
Note that the instance variables or properties of a class cannot be declared as final.
PHP - Overloading
在 C++ 或 Java 中,此术语表示一个类可以多次定义名称相同但参数和/或返回类型不同的类方法。在 PHP 中,“重载”一词有不同的解释。它是一个可以使用该术语动态创建属性和方法的功能。PHP 的魔术方法(以双下划线开头的名称方法)用于设置动态属性和方法。
In C++ or Java, the term means a class can a class method of same name more than once but with different arguments and/or return type. In PHP, the term overloading has a different interpretation. It is a feature with which properties and methods can be created dynamically. PHP’s magic methods (method names starting with double underscore) are used to set up dynamic properties and methods.
在处理未声明或在当前范围内不可见的属性或方法时,会调用用于重载目的的魔术方法。
The magic methods used for the purpose of overloading are invoked when interacting with properties or methods that have not been declared or are not visible in the current scope.
Property Overloading
PHP 的魔术方法示例包括 _construct()、_destruct()、__tostring() 等。PHP 使用下列魔术方法对属性进行重载。
The examples of PHP’s magic methods are _construct(), _destruct(), __tostring(), etc. PHP uses the following magic methods for overloading properties.
public __set ( string $name , mixed $value ) : void
public __get ( string $name ) : mixed
public __isset ( string $name ) : bool
public __unset ( string $name ) : void
在此,
Here,
-
__set() is run for writing data to inaccessible properties that are protected or private or non-existing.
-
__get() reads data from inaccessible properties.
-
__isset() calls isset() or empty() on inaccessible properties.
-
__unset() is invoked when unset() is called on inaccessible properties.
上面使用的 $name 参数是要设置或检索的属性的名称。__set() 方法的 $value 参数指定要分配给属性的值。
The $name argument used above is the name of the property to be set or retrieved. The $value argument of __set() method specifies the value to be assigned to the property.
_isset() 方法检查某个属性是否已经设置。 _unset() 方法移除属性。
The _isset() method checks if a certain property has been set or not. The _unset() method removes the property.
Property overloading 仅在 object context 中起作用。在任何 static context 中,这些魔术方法都不会被触发。因此它们不应该被声明为 static。
Property overloading works only in object context. In any static context, these magic methods will not be triggered. Hence they should not be declared static.
Example
在下面的代码中,设置和检索了一个名为 myprop 的动态属性,该属性未在类中声明。
In the following code, a dynamic property named myprop, which is not declared in the class, is set and retrieved.
<?php
class myclass {
public function __set($name, $value) {
echo "setting $name property to $value \n";
$this->$name = $value;
}
public function __get($name) {
echo "value of $name property is ";
return $this->$name;
}
}
$obj = new myclass();
# This calls __set() method
$obj->myproperty="Hello World!";
# This call __get() method
echo "Retrieving myproperty: " . $obj->myproperty . PHP_EOL;
?>
它将生成以下 output −
It will produce the following output −
setting myproperty property to Hello World!
Retrieving myproperty: Hello World!
_set() 和 _get() 魔术方法还设置和检索了一个声明为私有的属性。在 myclass 内部添加以下语句(在函数定义之前):
The _set() and _get() magical methods also set and retrieve a property which is declared as private. Add the following statement inside myclass (before the function definitions)
private $myproperty;
您可以在 myclass 中定义 __isset() 方法来检查属性 −
You can check if the property, define __isset() method in myclass −
public function __isset($name) {
return isset($this->$name);
}
使用此语句检查属性是否已设置 −
Check if the property is set with this statement −
var_dump (isset($obj->myproperty));
在这种情况下,它返回 true 。
Which in this case returns true.
使用在 myclass 中定义的 __unset() 方法取消动态创建的属性 −
To unset the dynamically created property with the __unset() method defined in myclass −
public function __unset($name) {
unset($this->$name);
}
以下代码会返回 false −
The following code would return false −
var_dump (isset($obj->myproperty));
Method Overloading
用于动态设置方法的两种魔术方法是 _call() 和 _callStatic() 。
Two magic methods used to set methods dynamically are _call() and _callStatic().
public __call (string $name , array $arguments) : mixed
public static __callStatic (string $name , array $arguments) : mixed
当在对象上下文中调用不可访问(未定义或私有)方法时,触发 _call()。另一方面,当在静态上下文中调用不可访问的方法时,触发 _callStatic()。
The _call() is triggered when invoking inaccessible (not defined or private) methods in an object context. On the other hand, the _callStatic() is triggered when invoking inaccessible methods in a static context.
Example
以下示例演示了 PHP 中的方法重载
The following example demonstrates method overloading in PHP
<?php
class myclass {
public function __call($name, $args) {
// Value of $name is case sensitive.
echo "Calling object method $name with " . implode(" ", $args). "\n";
}
public static function __callStatic($name, $args) {
echo "Calling static method $name with " . implode(" ", $args). "\n";
}
}
$obj = new myclass();
# This invokes __call() magic method
$obj->mymethod("Hello World!");
# This invokes __callStatic() method
myclass::mymethod("Hello World!");
?>
它将生成以下 output −
It will produce the following output −
Calling object method mymethod with Hello World!
Calling static method mymethod with Hello World!
请注意,“ → ”运算符的使用表示该方法是 instance method ,“::”运算符表示该方法是 static method 。
Note that the use of "→" operator implies that the method is an instance method, and "::" operator means that the method is a static method.
PHP - Cloning Objects
诸如“$obj1 = $obj2”的 PHP 语句只是创建对内存中同一对象的另一个引用。因此,属性的更改同时反映在原始对象和复制的对象中。PHP 中的 clone 关键字创建对象的浅拷贝。
A PHP statement such as "$obj1 = $obj2" merely creates another reference to the same object in memory. Hence, changes in attribute reflect both in original and duplicate object. The clone keyword in PHP creates a shallow copy of an object.
$obj2 = $obj1
原始对象中的更改不会反映在浅拷贝中。
Changes in the original object do not reflect in the shallow copy.
Example
请看以下示例:
Take a look at the following example −
<?php
class foo {
var $var1 = 'Hello';
}
$x = new foo();
$y = $x; # reference copy
echo $x->var1 . " " . $y->var1 . PHP_EOL;
$x->var1 = "Hello World";
echo $x->var1 . " " . $y->var1 . PHP_EOL;
?>
它将生成以下 output −
It will produce the following output −
Hello Hello
Hello World Hello World
在第一种情况下, $y 只是 $x 的引用副本。因此, var1 属性的任何更改都会反映在两者中。
In the first case, $y is just a reference copy of $x. Hence, any change in var1 property reflects in both.
但是,如果我们将 $y 声明为 $x 的克隆,则原始对象中的任何更改都不会反映在其浅拷贝中。
However, if we declare $y as a clone of $x, then any change in the original object is not reflected in its shallow copy.
Example
请看以下示例:
Take a look at the following example −
<?php
class foo {
var $var1 = 'Hello World';
}
$x = new foo();
# shallow copy
$y = clone $x;
echo $x->var1 . " " . $y->var1 . PHP_EOL;
$x->var1 = "Hello PHP";
echo $x->var1 . " " . $y->var1 . PHP_EOL;
?>
它将生成以下 output −
It will produce the following output −
Hello World Hello World
Hello PHP Hello World
Example
在以下代码中, myclass 的属性之一是作为地址类对象的属性。myclass 的一个对象被赋值复制。其嵌入地址对象的任何值的变化会反映在两个对象中,但是名称属性的变化不会体现在克隆对象中。
In the following code, myclass has one of attributes as object of address class. An object of myclass is duplicated by assignment. Any change in the value of its embedded address object is reflected in both the objects, but change in the name property is not effected in the cloned object.
<?php
class address {
var $city="Nanded";
var $pin="431601";
function setaddr($arg1, $arg2) {
$this->city=$arg1;
$this->pin=$arg2;
}
}
class myclass {
var $name="Raja";
var $obj;
function setname($arg) {
$this->name=$arg;
}
}
$obj1=new myclass();
$obj1->obj=new address();
echo "original object\n";
print_r($obj1);
echo "\n";
$obj2=$obj1; # reference copy
$obj1->setname("Ravi");
$obj1->obj->setaddr("Mumbai", "400001");
echo "after change: Original object\n";
print_r($obj1);
echo "\nCopied object\n";
print_r($obj2);
?>
它将生成以下 output −
It will produce the following output −
original object
myclass Object
(
[name] => Raja
[obj] => address Object
(
[city] => Nanded
[pin] => 431601
)
)
after change: Original object
myclass Object
(
[name] => Ravi
[obj] => address Object
(
[city] => Mumbai
[pin] => 400001
)
)
Copied object
myclass Object
(
[name] => Ravi
[obj] => address Object
(
[city] => Mumbai
[pin] => 400001
)
)
Using the "clone" Keyword
在浅表副本中,任何是对其他变量引用的原始对象的属性都将保持引用。clone 关键字不会复制复制对象的包含对象。
In a shallow copy, any properties of the original object that are references to other variables will remain references. The clone keyword does not copy the contained objects of the copied objects.
我们现在创建 myclass 对象的克隆,以便 $obj2 是 $obj1 的克隆。我们将 $obj1 的 name 属性从 Raja 更改为 Ravi ,然后修改嵌入地址对象。属性更改不会反映在其克隆中,但是引用的地址对象将更改。
We now create a clone of myclass object, so that $obj2 is the clone of $obj1. We change the name property of $obj1 from Raja to Ravi, and also modify the embedded address object. The property change will not reflect in its clone, but the referred address object will be changed.
Example
请看以下示例:
Take a look at the following example −
<?php
class address {
var $city="Nanded";
var $pin="431601";
function setaddr($arg1, $arg2) {
$this->city=$arg1;
$this->pin=$arg2;
}
}
class myclass {
var $name="Raja";
var $obj;
function setname($arg) {
$this->name=$arg;
}
}
$obj1=new myclass();
$obj1->obj=new address();
echo "original object\n";
print_r($obj1);
echo "\n";
$obj2=clone $obj1; # clone copy
$obj1->setname("Ravi");
$obj1->obj->setaddr("Mumbai", "400001");
echo "after change: Original object\n";
print_r($obj1);
echo "\nCopied object\n";
print_r($obj2);
?>
它将生成以下 output −
It will produce the following output −
original object
myclass Object
(
[name] => Raja
[obj] => address Object
(
[city] => Nanded
[pin] => 431601
)
)
after change: Original object
myclass Object
(
[name] => Ravi
[obj] => address Object
(
[city] => Mumbai
[pin] => 400001
)
)
Copied object
myclass Object
(
[name] => Raja
[obj] => address Object
(
[city] => Mumbai
[pin] => 400001
)
)
Using __clone() Method
clone 关键字创建对象的浅表副本。克隆对象后,PHP 将对所有对象的属性执行浅表副本。是对其他变量引用的任何属性都将保持引用。因此,对原始对象执行的任何更改也将出现在克隆对象中。
The clone keyword creates a shallow copy of the object. When an object is cloned, PHP will perform a shallow copy of all of the object’s properties. Any properties that are references to other variables will remain references. Hence, any changes done to the original object will also appear in the cloned object.
如果您希望防止复制的对象自动更新,则需要使用 __clone() 方法创建对象的深表副本。它是 PHP 中的魔术方法之一。
If you wish to prevent the copied object to update automatically, we need to create a deep copy of the object with the __clone() method. It is one of the magic methods in PHP.
一旦克隆完成,如果定义了 _clone() 方法,系统就会调用新创建对象的 _clone() 方法,以允许需要更改的任何必要属性。
Once the cloning is complete, if a _clone() method is defined, then the newly created object’s _clone() method will be called, to allow any necessary properties that need to be changed.
Example
在上述示例中,我们有一个 myclass 对象,其中一个属性 $obj 保存对地址类的对象的引用。要实现深表副本,我们会覆盖 myclass 中的 __clone() 魔术方法。
In the above example, we have an object of myclass, one of its attributes $obj holding the reference to an object of address class. To achieve a deep copy, we override the __clone() magic method in myclass.
<?php
class address {
var $city="Nanded";
var $pin="431601";
function setaddr($arg1, $arg2) {
$this->city=$arg1;
$this->pin=$arg2;
}
}
class myclass {
var $name="Raja";
var $obj;
function setname($arg) {
$this->name=$arg;
}
public function __clone() {
$this->obj = clone $this->obj ;
}
}
$obj1=new myclass();
$obj1->obj=new address();
echo "original object\n";
print_r($obj1);
echo "\n";
$obj2=clone $obj1; # cloned deep copy
$obj1->setname("Ravi");
$obj1->obj->setaddr("Mumbai", "400001");
echo "after change: Original object\n";
print_r($obj1);
echo "\nCloned object\n";
print_r($obj2);
?>
您现在会看到原始对象(我们更改地址属性)中的更改不会反映在克隆对象中,如下面的 output 所示。
You will now see that the changes in the original object (we change the address attributes) won’t reflect in the cloned object, as the following output shows −
original object
myclass Object
(
[name] => Raja
[obj] => address Object
(
[city] => Nanded
[pin] => 431601
)
)
after change: Original object
myclass Object
(
[name] => Ravi
[obj] => address Object
(
[city] => Mumbai
[pin] => 400001
)
)
Cloned object
myclass Object
(
[name] => Raja
[obj] => address Object
(
[city] => Nanded
[pin] => 431601
)
)
PHP - Anonymous Classes
版本 7.0 的发布是 PHP 语言演化中的一个重要里程碑,那时引入了许多新功能。PHP 7.0 版本中还提供了匿名类功能。
The release of version 7.0 is an important milestone in the evolution of PHP language, when a lot of new features were introduced. The feature of Anonymous class was also made available in PHP version 7.0.
正如术语“匿名”所暗示的那样,它是一个无(程序员声明的)名的类。通常的做法是用某个标识符定义一个类,以便可以重复使用它。另一方面,匿名类仅供一次性使用。
As the term "anonymous" suggests, it is a class without a (programmer declared) name. The usual practice is to define a class with a certain identifier, so that it can be used repeatedly. The anonymous class, on the other hand is for one-time use only.
$obj = new class() {
/* class body */
};
除了该类没有名称之外,它与普通命名类类似,因为它可以包含属性和方法。它的功能与命名类对象的功能没有区别。
Apart from this class not having a name, it is similar to a normal named class, in the sense it can contain properties and methods. Its functionality is no different from that of an object of a named class.
在类不需要文档时,以及类在执行期间仅使用一次时,匿名类可能会在一个已命名的类上使用。当需要创建简单的一次性对象时,匿名类非常有用。
An anonymous class might be used over a named class especially when the class does not need to be documented, and when the class is used only once during execution. Anonymous classes are useful when simple, one-off objects need to be created.
Example
在以下代码中,匿名类被实例化并存储在 $obj 对象中。该类包括 addition() 和 division() 方法的定义,这些方法用 $obj 对象调用。
In the following code, an anonymous class is instantiated and stored in $obj object. The class includes definitions of addition() and division() methods, which are called with the $obj object.
<?php
$obj = new class(10) {
private int $x;
function __construct($x) {
$this->x = $x;
}
public function addition($x) {
return $this->x+$x;
}
public function division($x) {
return $this->x/$x;
}
};
echo "Addition: " . $obj->addition(20) . PHP_EOL;
echo "Division: " . $obj->division(20) . PHP_EOL;
?>
它将生成以下 output −
It will produce the following output −
Addition: 30
Division: 0.5
Anonymous Class as a Child Class
匿名类可以执行普通类可以执行的所有操作。它可以扩展另一个类、实现一个接口甚至使用一个特性。
An anonymous class can do everything that a normal class can. It can extends another class, implement an interface or even use a trait.
Example
在以下示例中,匿名类是一个子类,它扩展了一个已经存在的父类。
In the example below, the anonymous class is a child class, extending a parent already available.
<?php
class myclass {
public function hello() {
echo "Hello World!" . PHP_EOL;
}
}
$obj = new class("Neena") extends myclass {
private string $nm;
function __construct($x) {
$this->nm = $x;
}
public function greeting() {
parent::hello();
echo "Welcome " . $this->nm . PHP_EOL;
}
};
$obj->greeting();
?>
它将生成以下 output −
It will produce the following output −
Hello World!
Welcome Neena
Example
尽管匿名类没有任何用户定义的名称,但 PHP 会为其分配内部名称,该名称可以使用内置的 get_class() 函数获得,如下所示:
Although the anonymous class doesn’t have any user defined name, PHP does assign it an internal name, which can be obtained with the built-in get_class() function as follows −
<?php
$obj = new class() {
function greeting() {
echo "Hello World" . PHP_EOL;
}
};
$obj->greeting();
echo "Name of class: " . get_class($obj);
?>
它将生成以下 output −
It will produce the following output −
Hello World
Name of class: class@anonymousC:\xampp\htdocs\hello.php:2$0
PHP 解析器随机分配内部名称。
PHP parser assigns the internal name randomly.
PHP - Web Concepts
PHP 是一种服务器端脚本语言,用于创建动态网页。它是 Web 开发中最流行的编程语言之一。本章旨在让您熟悉使用 PHP 进行 Web 应用程序开发的某些重要概念。
PHP is a server-side scripting language that is used to create dynamic webpages. It is one of the most popular programming languages for web development. This chapter aims to let you get familiarized with certain important concepts of web application development using PHP.
基于 Web 的应用程序是网页的集合。网页主要是用 HTML 标记创建的。HTML 包含用于定义页面元素(如文本、图像、表格等)外观的不同 HTML 标记。因此,HTML 本质上创建了一个静态网页。
A web-based application is a collection of webpages. A webpage is mainly created with HTML tags. HTML consists of different HTML tags which are required to define the appearance of page elements like text, image, table, etc. Hence, HTML essentially creates a static webpage.
Web 应用托管在安装了 PHP 模块的 HTTP 服务器上。浏览器作为 http 客户端,遵循 HTTP 协议来与服务器建立通信。
A Web application is hosted on a HTTP server with PHP module installed. The browser acts as a http client, to establish communication with the server, following HTTP protocol.
How to Add Dynamic Content on a Webpage?
要向网页添加动态内容,可以使用两种方式。
To add dynamic content io a webpage, there are two possibilities.
JavaScript 是一种客户端脚本语言,它可以访问 HTML 文档对象模型并在客户端浏览器上呈现动态内容。JavaScript 代码可以嵌入到 HTML 页面中。
JavaScript is a client-side scripting language, that can access the HTML document object model and render dynamic content on the client browser. JavaScript code can be embedded in HTML page.
浏览器可以通过 HTML 表单元素收集用户数据,然后将其发送到 HTTP 服务器进行处理。PHP 是一种广泛使用的服务器端处理语言。PHP 脚本也可以嵌入到 HTML 页面中。
The browser may collect data from the user in the form of HTML form elements and send it to a HTTP server for processing. PHP is a widely used Server-side processing language. PHP script can also be embedded inside HTML page.
Example
在下面的脚本中,嵌入到 HTML 中的 JavaScript 代码根据客户端浏览器呈现当前日期,而 PHP 代码则根据脚本托管的服务器显示当前日期。
In the following script, JavaScript code embedded in HTML renders the current date as per the client browser, and the PHP code displays the current date as per the server, where this script is hosted.
<!DOCTYPE html>
<html>
<body>
<script type="text/JavaScript">
document.write("Client's date :"+Date()+"\n");
</script>
<?php
date_default_timezone_set("Asia/Calcutta");
echo "server's date is " . date("Y-m-d") . "\n";
echo "The time is " . date("h:i:sa");
?>
</body>
</html>
PHP can intercept and process the data from HTML forms 。这使你可以收集你的用户的信息。下一章将讨论 PHP 表单处理。
PHP can intercept and process the data from HTML forms. This allows you to collect information from your users. The next chapter discusses PHP’s form handling.
PHP can be used to interact with databases ,例如 MySQL 和 PostgreSQL。这使你能够存储和检索你的数据库中的数据,并动态填充网页或为 Web 应用程序提供动力。PHP 包括用于数据库处理的 mysql、mysqli 和 PDO 扩展。
PHP can be used to interact with databases such as MySQL and PostgreSQL. This allows you to store and retrieve data from your database, and dynamically populate the web pages or to power the web applications. PHP includes mysql, mysqli and PDO extensions for database handling.
PHP can handle the data received from the client 使用 HTTP GET 和 POST 方法。我们将在后面的章节详细讨论 PHP 如何处理 GET/POST 方法。
PHP can handle the data received from the client with HTTP GET as well as POST methods. We shall discuss in detail, how PHP handles GET/POST methods in one of the latter chapters.
HTTP is a stateless protocol 。不过,它允许在服务器和客户端分别维护会话和 cookie。PHP 可用于创建和管理会话和 cookie。当用户浏览你的网站时,会话允许你跟踪各个用户,而 cookie 允许你将信息存储在用户的计算机上以供日后使用。在后续章节中,我们将学习 PHP 如何处理会话和 cookie。
HTTP is a stateless protocol. However, it allows Sessions and cookies to be maintained on server and client respectively. PHP can be used to create and manage sessions and cookies. Sessions allow you to track individual users as they navigate your website, while cookies allow you to store information on the user’s computer for later use. In of the subsequent chapters, we shall learn how PHP handles sessions and cookies.
PHP can be used to upload files to your web server 。这使你能够创建允许用户上传文件(例如图片、视频或文档)的 Web 应用程序。
PHP can be used to upload files to your web server. This allows you to create web applications that allow users to upload files, such as images, videos, or documents.
You can use PHP to create a login page for your website 。当用户输入他们的用户名和密码时,PHP 可以检查数据库以查看用户是否有效。如果用户有效,PHP 可以让用户登录并将其重定向到你的网站的主页。
You can use PHP to create a login page for your website. When the user enters their username and password, PHP can check the database to see if the user is valid. If the user is valid, PHP can log the user in and redirect them to the main page of your website.
Identifying Browser & Platform
PHP 创建了一些可以在用于设置 PHP 环境的 phpinfo.php 页面中看到的 environment variables 。
PHP creates some useful environment variables that can be seen in the phpinfo.php page that was used to setup the PHP environment.
PHP 设置的环境变量之一是 HTTP_USER_AGENT ,它标识用户的浏览器和操作系统。
One of the environment variables set by PHP is HTTP_USER_AGENT which identifies the user’s browser and operating system.
PHP 提供了一个 getenv() 函数来访问所有环境变量的值。HTTP_USER_AGENT 环境变量中包含的信息可用于根据浏览器创建适当的动态内容。
PHP provides a function getenv() to access the value of all the environment variables. The information contained in the HTTP_USER_AGENT environment variable can be used to create dynamic content appropriate to the browser.
Example
下面的示例展示了如何识别客户端浏览器和操作系统。
Following example demonstrates how you can identify a client browser and operating system.
NOTE − preg_match() 函数在 PHP Regular expression 会话中讨论。
NOTE − The function preg_match()is discussed in PHP Regular expression session.
<?php
function getBrowser() {
$u_agent = $_SERVER['HTTP_USER_AGENT'];
$bname = 'Unknown';
$platform = 'Unknown';
$version = "";
//First get the platform
if (preg_match('/linux/i', $u_agent)) {
$platform = 'linux';
} elseif (preg_match('/macintosh|mac os x/i', $u_agent)) {
$platform = 'mac';
} elseif (preg_match('/windows|win32/i', $u_agent)) {
$platform = 'windows';
}
// Next get the name of the useragent yes seperately and for good reason
if(preg_match('/MSIE/i',$u_agent) && !preg_match('/Opera/i',$u_agent)) {
$bname = 'Internet Explorer';
$ub = "MSIE";
} elseif(preg_match('/Firefox/i',$u_agent)) {
$bname = 'Mozilla Firefox';
$ub = "Firefox";
} elseif(preg_match('/Chrome/i',$u_agent)) {
$bname = 'Google Chrome';
$ub = "Chrome";
} elseif(preg_match('/Safari/i',$u_agent)) {
$bname = 'Apple Safari';
$ub = "Safari";
} elseif(preg_match('/Opera/i',$u_agent)) {
$bname = 'Opera';
$ub = "Opera";
} elseif(preg_match('/Netscape/i',$u_agent)) {
$bname = 'Netscape';
$ub = "Netscape";
}
// finally get the correct version number
$known = array('Version', $ub, 'other');
$pattern = '#(?<browser>' . join('|', $known) . ')
[/ ]+(?<version>[0-9.|a-zA-Z.]*)#';
if (!preg_match_all($pattern, $u_agent, $matches)) {
// we have no matching number just continue
}
// see how many we have
$i = count($matches['browser']);
if ($i != 1) {
//we will have two since we are not using 'other' argument yet
//see if version is before or after the name
if (strripos($u_agent,"Version") < strripos($u_agent,$ub)){
$version= $matches['version'][0];
} else {
$version= $matches['version'][1];
}
} else {
$version= $matches['version'][0];
}
// check if we have a number
if ($version == null || $version == "") {$version = "?";}
return array(
'userAgent' => $u_agent,
'name' => $bname,
'version' => $version,
'platform' => $platform,
'pattern' => $pattern
);
}
// now try it
$ua = getBrowser();
$yourbrowser = "Your browser: " . $ua['name'] . " " . $ua['version'] .
" on " .$ua['platform'] . " reports: <br >" . $ua['userAgent'];
print_r($yourbrowser);
?>
这在我的机器上产生以下结果。根据你使用的计算机,此结果可能有所不同。
This is producing following result on my machine. This result may be different for your computer depending on what you are using.
它将产生以下结果 −
It will produce the following result −
Your browser: Google Chrome 54.0.2840.99 on windows reports:
Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)
Chrome/54.0.2840.99 Safari/537.36
Display Images Randomly
PHP rand() 函数用于生成随机数。此函数可以生成指定范围内内的数。应设置随机数生成器的种子,以防止生成规则的数字模式。这是通过使用 srand() 函数来实现的,该函数指定种子号作为其参数。
The PHP rand() function is used to generate a random number.i This function can generate numbers with-in a given range. The random number generator should be seeded to prevent a regular pattern of numbers being generated. This is achieved using the srand() function that specifies the seed number as its argument.
Example
以下示例展示了如何每次从四张图片中显示不同的图片 −
Following example demonstrates how you can display different image each time out of four images −
<?php
srand( microtime() * 1000000 );
$num = rand( 1, 4 );
switch( $num ) {
case 1: $image_file = "/php/images/php_image_sample_1.jpg";
break;
case 2: $image_file = "/php/images/php_image_sample_2.jpg";
break;
case 3: $image_file = "/php/images/php_image_sample_3.jpg";
break;
case 4: $image_file = "/php/images/php_image_sample_4.jpg";
break;
}
echo "Random Image : <img src=$image_file />";
?>
它将产生以下结果 −
It will produce the following result −
Using HTML Forms
处理 HTML 表单和 PHP 时需要注意的最重要一点是 HTML 页面中的任何表单元素都将自动对你的 PHP 脚本可用。
The most important thing to notice when dealing with HTML forms and PHP is that any form element in an HTML page will automatically be available to your PHP scripts.
Example
通过将源代码放入 test.php 脚本中,尝试以下示例。
Try out following example by putting the source code in test.php script.
<?php
if( $_POST["name"] || $_POST["age"] ) {
if (preg_match("/[^A-Za-z'-]/",$_POST['name'] )) {
die ("invalid name and name should be alpha");
}
echo "Welcome ". $_POST['name']. "<br />";
echo "You are ". $_POST['age']. " years old.";
exit();
}
?>
<form action = "<?php <b>$_PHP_SELF</b> ?>" method = "POST">
Name: <input type = "text" name = "name" />
Age: <input type = "text" name = "age" />
<input type = "submit" />
</form>
它将产生以下结果 −
It will produce the following result −
-
The PHP default variable $_PHP_SELF is used for the PHP script name and when you click "submit" button then same PHP script will be called and will produce following result −
-
The method = "POST" is used to post user data to the server script. There are two methods of posting data to the server script which are discussed in PHP GET & POST chapter.
Browser Redirection
PHP header() 函数向浏览器提供原始的 HTTP 标头,可用于将其重定向到其他位置。重定向脚本应位于页面的最顶部,以防页面任何其他部分加载。
The PHP header() function supplies raw HTTP headers to the browser and can be used to redirect it to another location. The redirection script should be at the very top of the page to prevent any other part of the page from loading.
目标由 Location: 标头指定为 header() 函数的参数。在调用此函数后,可以使用 exit() 函数来停止解析代码的其余部分。
The target is specified by the Location: header as the argument to the header() function. After calling this function the exit() function can be used to halt parsing of rest of the code.
Example
以下示例演示如何将浏览器请求重定向到另一个网页。通过将源代码放入 test.php 脚本中,尝试此示例。
Following example demonstrates how you can redirect a browser request to another web page. Try out this example by putting the source code in test.php script.
<?php
if( $_POST["location"] ) {
$location = $_POST["location"];
header( "Location:$location" );
exit();
}
?>
<p>Choose a site to visit :</p>
<form action = "<?php <b>$_SERVER['PHP_SELF']</b> ?>" method ="POST">
<select name = "location">.
<option value = "http://www.tutorialspoint.com">
Tutorialspoint.com
</option>
<option value = "http://www.google.com">
Google Search Page
</option>
</select>
<input type = "submit" />
</form>
它将产生以下结果 −
It will produce the following result −
PHP - Form Handling
HTML Forms 在 PHP Web 应用程序中扮演着重要的角色。虽然纯粹由 HTML 编写的网页是一个静态网页,但 HTML 表单组件是一个重要的特性,有助于实现交互性和呈现动态内容。PHP 的表单处理功能可以在处理之前验证从用户收集的数据。
HTML Forms play an important role in PHP web applications. Although a webpage composed purely with HTML is a static webpage, the HTML form component is an important feature that helps in bringing interactivity and rendering dynamic content. PHP’s form handling functionality can validate data collected from the user, before processing.
HTML 表单是各种表单控件的集合,例如文本字段、复选框、单选按钮等,用户可以使用这些表单控件进行交互、输入或选择某些数据,这些数据可以由 JavaScript(客户端处理)在本地处理,也可以借助 PHP 等服务器端编程脚本发送到远程服务器进行处理。
An HTML Form is a collection various form controls such as text fields, checkboxes, radio buttons, etc., with which the user can interact, enter or choose certain data that may be either locally processed by JavaScript (client-side processing), or sent to a remote server for processing with the help of server-side programming scripts such as PHP.
一个或多个表单控件元素被放在 <form> 和 </form> 标记内。 form element 的特点是具有不同的属性,例如名称、操作和方法。
One or more form control elements are put inside <form> and </form> tags. The form element is characterized by different attributes such as name, action, and method.
<form [attributes]>
Form controls
</form>
Form Attributes
在 HTML 表单元素的众多属性中,经常需要和定义以下属性 −
Out of the many attributes of the HTML form element, the following attributes are often required and defined −
Action Attribute
表示处理表单提交的 URL 的字符串。例如, http://example.com/test.php 。若要将表单数据提交至定义 HTML 表单的相同 PHP 脚本,请使用 PHP_SELF 服务器变量 −
a string representing the URL that processes the form submission. For example, http://example.com/test.php. To submit the for-data to the same PHP script in which the HTML form is defined, use the PHP_SELF server variable −
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
Enctype Attribute
指定在将表单数据发送到服务器之前对其进行编码的方法。可能的值为 −
specifies the method using which the form-data should be encoded before sending it to the server. Possible values are −
-
application/x-www-form-urlencoded − The default value.
-
multipart/form-data − Use this if the form contains <input> elements with type=file.
-
text/plain − Useful for debugging purposes.
Method Attribute
表示提交表单所用 HTTP 方法的字符串。以下方法是 method 属性的可能值 −
a string representing the HTTP method to submit the form with. The following methods are the possible values of method attribute −
-
post − The POST method; form data sent as the request body.
-
get (default) − The GET; form data appended to the action URL with a "?" separator. Use this method when the form has no side effects.
-
dialog − When the form is inside a <dialog>, closes the dialog and causes a submit event to be fired on submission, without submitting data or clearing the form.
Name Attribute
表单的名称。该值不能是空字符串,且如果同一 HTML 文档中存在多个表单,则它必须是唯一的。
The name of the form. The value must not be the empty string, and must be unique if there are multiple forms in the same HTML document.
Target Attribute
一个指示提交表单后显示响应的位置的字符串。应为下列选项之一:
a string that indicates where to display the response after submitting the form. Should be one of the following −
-
_self (default) − Load into the same browsing context as the current one.
-
_blank − Load into a new unnamed browsing context.
-
_parent − Load into the parent browsing context of the current one.
-
_top − Load into the top-level browsing context (an ancestor of the current one and has no parent).
因此,在一个 PHP Web 应用程序中使用的典型 HTML 表单如下所示:
Hence, a typical HTML form, used in a PHP web application looks like −
<form name="form1" action="<?php echo $_SERVER['PHP_SELF'];?>" action="POST">
Form controls
</form>
Form Elements
HTML 表单设计为不同类型的控件或元素。用户可以与这些控件交互,以输入数据或从呈现的可用选项中进行选择。下面描述了一些元素:
A HTML form is designed with different types of controls or elements. The user can interact with these controls to enter data or choose from the available options presented. Some of the elements are described below −
Input Element
input 元素表示一个数据域,允许用户输入和/或编辑数据。
The input element represents a data field, which enables the user to enter and/or edit the data.
input 元素的 type 属性控制数据。input 元素可以是以下类型:
The type attribute of INPUT element controls the data. The INPUT element may be of the following types −
用于输入单行文本的文本域。
A text field to enter a single line text.
<input type="text" name="employee">
一个屏蔽输入字符的单行文本域。
A single line text filed that masks the entered characters.
<input type="password" name="pwd"><br>
一个可复选的矩形框,是从预定义列表中获取的一个或多个值。
A rectangular checkable box which is a set of zero or more values from a predefined list.
<input type="checkbox" id="s1" name="sport1" value="Cricket">
<label for="s1">I like Cricket</label><br>
<input type="checkbox" id="s2" name="sport2" value="Football">
<label for="s2">I like Football</label><br>
<input type="checkbox" id="s3" name="sport3" value="Tennis">
<label for="s3">I like Tennis</label><br><br>
此类型呈现一个带有两种状态(开或关)的可圆形单击按钮,通常是单选按钮组中的一个按钮。
This type renders a round clickable button with two states (ON or OFF), usually a part of multiple buttons in a radio group.
<input type="radio" id="g1" name="gender" value="Male">
<label for="g1">Male</label><br>
<input type="radio" id="g2" name="female" value="Female">
<label for="g2">Female</label><br>
input 类型呈现一个标有标题文件的按钮,允许用户从客户端文件系统选择一个文件,通常要上载到服务器。表单的 enctype 属性必须设置为“multipart/form-data”。
The input type renders a button captioned file and allows the user to select a file from the client filesystem, usually to be uploaded on the server. The form’s enctype attribute must be set to "multipart/form-data"
<input type="file" name="file">
一个单行文本域,经过自定义以接受一个符合有效电子邮件 ID 的字符串。
A single line text field, customized to accept a string conforming to valid email ID.
一个单行文本域,经过自定义以接受一个符合有效 URL 的字符串。
A single line text filed customized to accept a string conforming to valid URL.
此 input 元素呈现一个按钮,单击此按钮时,它会启动将表单数据提交到当前表单的 action 属性中指定的 URL。
This input element renders a button, which when clicked, initiates the the submission of form data to the URL specified in the action attribute of the current form.
<input type="submit" name="Submit">
Select Element
Select 元素表示用于在一组选项中进行选择的控件。每个选项都由 Select Control 的 option 属性定义。例如 −
The select element represents a control for selecting amongst a set of options. Each choice is defined with option attribute of Select Control. For example −
<select name="Subjects" id="subject">
<option value="Physics">Physics</option>
<option value="Chemistry">Chemistry</option>
<option value="Maths">Maths</option>
<option value="English">English</option>
</select>
Form Example
让我们使用这些表单元素来设计一个 HTML 表单并将其发送到 PHP_SELF 脚本
Let us use these form elements to design a HTML form and send it to a PHP_SELF script
<html>
<body>
<form method = "post" action = "<?php
echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<table>
<tr>
<td>Name:</td>
<td><input type = "text" name = "name"></td>
</tr>
<tr>
<td>E-mail: </td>
<td><input type = "email" name = "email"></td>
</tr>
<tr>
<td>Website:</td>
<td><input type = "url" name = "website"></td>
</tr>
<tr>
<td>Classes:</td>
<td><textarea name = "comment" rows = "5" cols = "40"></textarea></td>
</tr>
<tr>
<td>Gender:</td>
<td>
<input type = "radio" name = "gender" value = "female">Female
<input type = "radio" name = "gender" value = "male">Male
</td>
</tr>
<td>
<input type = "submit" name = "submit" value = "Submit">
</td>
</table>
</form>
<?php
$name = $email = $gender = $comment = $site = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = $_POST["name"];
$email = $_POST["email"];
$name = $_POST["name"];
$comment = $_POST["comment"];
$gender = $_POST["gender"];
$site = $_POST["website"];
}
echo "<h2>Your given values are as:</h2>";
echo $name;
echo "<br>";
echo $email;
echo "<br>";
echo $site;
echo "<br>";
echo $comment;
echo "<br>";
echo $gender;
?>
</body>
</html>
它将生成以下 output −
It will produce the following output −
PHP - Form Validation
术语“表单验证”指的是确定用户在各种表单元素中输入的数据是否可用于进一步处理的过程。在后续处理之前验证数据可避免可能的异常和运行时错误。
The term "Form Validation" refers to the process of ascertaining if the data entered by the user in various form elements is acceptable for further processing. Validation of data before its subsequent processing avoids possible exceptions and runtime errors.
验证可以在客户端和服务器端进行。当客户端提交表单时,表单数据会被服务器上运行的 PHP 脚本截取。可以使用 PHP 中的各种函数进行服务器端表单验证。
Validation can be done both on the client-side and on the server-side. When the client submits the form, the form data is intercepted by the PHP script running on the server. Using various functions available in PHP, the server-side form validation can be done.
Client-side Validation
根据 HTML5 规范,新的输入控件具有内置验证。例如,类型为“电子邮件”的输入元素,即使是文本字段,也经过了定制,以接受与电子邮件地址协议一致的字符串。
The new input controls as per the HTML5 specifications have in-built validation. For example an input element of the type ‘email’, even though is a text field, is customized to accept a string that is according to email address protocol.
在将数据提交至服务器之前验证。对于其他输入类型(如 URL、数字等)也是如此。
Validation takes place befor the data is submitted to the server. Same thing is true with other input types such as URL, number, etc.
Example
下面给出一个包含数字类型、电子邮件类型和 URL 类型的输入元素的 HTML 表单。如果您输入的不是按所需格式,那么在您尝试提交表单时会闪烁一个合适的错误信息。
Given below is an HTML form with input elements of number type, email type and URL type. If you enter data that is not as per the required format, a suitable error message is flashed as you try to submit the form.
<h1>Input Validation</h1>
<form>
<p><Label for "name">Enter your name</label>
<input type = "text" id="name" name="name"></p>
<p><label for="age">Enter age</label>
<input type = "text" id = "age" name="age"></p>
<p><label for="email">Enter your email:</label>
<input type="text" id="email" name="email"></p>
<p><label for="URL">Enter your website<label>
<input type = "text" id="URL" name="url"></p>
<input type="submit">
</form>
数字类型文本字段在右侧显示了向上/向下计数器箭头。只接受数字,可以增量或减量。
The number type text field shows up/down counter arrows on the right. Only number is accepted, and can be incremented or decremented.
如果电子邮件字段中的数据无效,您将收到如下错误信息。
If the data in email field is invalid, you get the error message flashed as below.
同样,任何不正确的 URL 格式也会显示如下所示的错误 −
Similarly, any incorrect format for the URL also flashes the error as shown −
Validation Functions
带有PHP的服务器端验证出现在两种情况下,一种是表单数据通过客户端验证,另一种是客户端根本没有验证。
The validation on the server-side with PHP comes into picture, either when the form data passes the client-side validation, or there’s no validation on the client side at all.
在上面示例中使用的 HTML 表单中,让我们删除所有特殊输入类型并使用文本类型的全部文本字段。通过 POST 方法将表单提交到服务器上的 hello.php。
In the HTML form used in the above example, let us remove all special input types and use all text fields of text type. The form is submitted with POST method to hello.php on the server.
<form action="hello.php" method="POST">
<p><Label for "name">Enter your name</label>
<input type = "text" id="name" name="name"></p>
<p><label for="age">Enter age</label>
<input type = "text" id = "age" name="age"></p>
<p><label for="email">Enter your email:</label>
<input type="text" id="email" name="email"></p>
<p><label for="URL">Enter your website<label>
<input type = "text" id="URL" name="url"></p>
<input type="submit">
</form>
Form is Empty
如果用户(可能是无意中)单击了提交按钮,你可以要求 PHP 重新显示表单。你需要检查 $_POST 数组是否已使用 isset() 函数初始化。如果没有,则 header() 函数会将控件重定向回表单。
If the user (may be inadvertently) clicks the submit button, you can ask PHP to display the form again. You need to check if the $_POST array has been initialized with isset() function. If not, the header() function redirects the control back to the form.
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (isset($_POST)) {
header("Location: hello.html", true, 301);
exit();
}
// form processing if the form is not empty
}
?>
Example
你还可以检查在提交表单时是否有任何字段为空。
You can also check if any of the fields is empty at the time of submitting the form.
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
foreach($_POST as $k=>$v) {
if (empty($v)==true) {
echo "One or more fields are empty \n";
echo "<a href = 'hello.html'>Click here to go back </a>";
exit;
}
else
echo "$k => $v \n";
}
}
?>
Age field is non-numeric
在 HTML 表单中,名称的输入字段为文本类型,因此它可以接受任何字符。然而,我们希望它具有数字性,这可以通过 is_numeric() 函数来保证。
In the HTML form the input field for name is of text type, hence it can accept any characters. However, we want it to be numeric. This can be ensured by is_numeric() function
<?php
if (is_numeric($_POST["age"])==false) {
echo "Age cannot be non-numeric \n";
echo "<a href = 'hello.html'>Click here to go back</a>";
}
?>
PHP 还有 is_string() 函数,用于检查字段是否包含字符串。另外两个函数 trim() 和 htmlspecialchars() 也对表单验证有用。
PHP also has is_string() function to check if a filed contains a string or not. Two other functions, trim() and htmlspecialchars() are also useful for form validation.
-
trim() − Removes whitespace from the beginning and end of a string
-
htmlspecialchars() − Converts special characters to HTML entities to prevent cross-site scripting (XSS) attacks.
PHP - Form Email/URL
PHP 为验证表单数据项(它们是字符串,但预期是电子邮件 ID 或 URL 的表示)提供了两种选择。检查表单元素是否包含电子邮件/URL 的一种方法是使用 RegEx (regular expressions) ,另一种更方便的方法是使用 filter_var() 函数。让我们应用这两种方法,并验证由表单提交给 PHP 脚本的电子邮件和 URL。
PHP provides two alternatives for validating the form data items which are strings but are expected to be a representation of Email ID or a URL. One way to check the form element contains email/URL is with the use of RegEx (regular expressions), and the other, more convenient approach is to use filter_var() function. Let us apply both these methods and validate email and URL submitted by a form to a PHP script.
本章中使用的 HTML 表单如下 −
The HTML Form used for this chapter is as follows −
<h1>Email and URL Validation</h1>
<form action="hello.php" method="POST">
<p><label for="email">Enter your email:</label>
<input type="text" id="email" name="email"></p>
<p><label for="URL">Enter your website<label>
<input type = "text" id="URL" name="url"></p>
<input type="submit">
</form>
Validation with Regex
PHP 的内置函数库包括执行正则表达式匹配的 preg_match() function 。
PHP’s built-in function library includes the preg_match() function that performs a regular expression match.
preg_match(
string $pattern,
string $subject,
array &$matches = null,
int $flags = 0,
int $offset = 0
): int|false
此函数在 subject 中搜索与 pattern 中给出的正则表达式匹配的部分。如果 pattern 与给定的 subject 匹配, preg_match() 返回 1;如果它不匹配,则返回 0;如果失败,则返回 false 。
This function searches subject for a match to the regular expression given in pattern. preg_match() returns 1 if the pattern matches given subject, 0 if it does not, or false on failure.
有效的电子邮件 ID 应满足以下正则表达式 −
A valid email ID should satisfy the following regular expression −
"/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix"
同样,有效的 URL 应满足以下正则表达式 −
Similarly, a valid URL should satisfy the following regular expression −
"/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i"
如果字符串是有效的电子邮件 ID,则以下函数返回“1”或“0”。
The following function returns "1" or "0" if the string is a valid email ID.
function checkemail($str) {
return (!preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $str)) ? FALSE : TRUE;
}
Example
让我们使用 checkmail() function ,借助以下 PHP 代码,来检查上面 HTML 中的电子邮件字段是否有效 −
Let us use the checkmail() function to check whether the email field in the above HTML is valid or not, with the help of following PHP code −
<?php
function checkemail($str) {
return (!preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@
([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $str)) ? FALSE : TRUE;
}
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$email = $_POST['email'];
if(!checkemail($email)){
echo "Invalid email address.";
} else {
echo "Valid email address.";
}
}
?>
HTML 表单如下呈现 −
The HTML form is rendered as below −
通过在电子邮件字段中输入有效/无效的电子邮件字符串来测试 PHP 代码。
Test the PHP code by entering valid/invalid email string in the email field.
以下 checkURL() function 检查字符串是否表示有效的 URL 或无效的 URL,并返回“1 或“0”。
The following checkURL() function checks if a string represents a valid or invalid URL, and returns "1 or "0".
function checkURL($str) {
return (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)
[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i", $str)) ? FALSE : TRUE;
}
Example
从 $_POST 数组中提取的 URL 字段作为参数提供给上述函数。
The URL field extracted from the $_POST array is given as argument to the above function.
<?php
function checkURL($str) {
return (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]
*[-a-z0-9+&@#\/%=~_|]/i", $str)) ? FALSE : TRUE;
}
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$url = $_POST['url'];
if(!checkURL($url)){
echo "Invalid URL.";
} else {
echo "Valid URL.";
}
}
?>
您可以通过在上面表单的 URL 字段中输入 URL 字符串来测试上面的代码。
You can test the above code by entering URL string in the URL field of the above form.
Using filter_var() function
内置的 filter_var() 函数使用指定的过滤器过滤变量。
The built-in filter_var() function filters a variable with a specified filter.
filter_var(mixed $value, int $filter = FILTER_DEFAULT, array|int $options = 0): mixed
根据 $filter 参数的值作为枚举过滤器 ID,将检查 $value 参数,如果过滤器失败,则该函数返回过滤后的数据或 false。
Depending on the enumerated filter ID as the value of $filter parameter, the $value parameter is checked and the function returns the filtered data, or false if the filter fails.
有各种 predefined filter ID constants 可用 −
There are various predefined filter ID constants available −
Sr.No |
ID & Description |
1 |
FILTER_VALIDATE_BOOL Returns true for "1", "true", "on" and "yes". Returns false otherwise. |
2 |
FILTER_VALIDATE_DOMAIN Validates whether the domain name label lengths are valid. |
3 |
FILTER_VALIDATE_EMAIL Validates whether the value is a valid e-mail address. |
4 |
FILTER_VALIDATE_IP Validates value as IP address |
5 |
FILTER_VALIDATE_URL Validates value as URL |
Example
以下 PHP 脚本验证了以上 HTML 提交的电子邮件和 URL 数据 −
The following PHP script validates the email and URL data submitted by the HTML for above −
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$email = $_POST['email'];
$url = $_POST['url'];
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
echo "Invalid email format and please re-enter valid email\n";
}
else
echo "Email entered is in valid format\n";
if (!filter_var($url, FILTER_VALIDATE_URL)) {
echo "Invalid URL format and please re-enter valid URL\n";
}
else
echo "URL entered is in valid format\n";
}
?>
你可以通过输入有效的/无效的电子邮件/URL 来测试以上脚本的性能。
You can test the performance of the above script by entering valid/invalid email/URL.
PHP - Complete Form
本章将表单验证和提取 HTML 表单数据的所有概念放入 PHP 代码中。下面给出的完整表单处理代码分三个部分:开始时的 PHP 代码部分,当表单提交时寻找任何验证错误;HTML 表单,带有各种元素,例如文本字段、单选按钮、选择控件、复选框等。第三部分又是一段 PHP 代码,它呈现用户输入的数据。
This chapter puts all the concepts of form validation and extraction of HTML form data into PHP code. The complete form handling code given below has three sections: A PHP code section in the beginning that looks for any validation errors when the form is submitted, the HTML form with various elements such as text fields, radio buttons, Select control, checkbox, etc. The third part is again a PHP code that renders the data entered by the user.
PHP Error Tracking
捕获错误的代码位于整个脚本的开头。显然,这将在每次加载页面时执行。如果在表单提交后加载,则以下片段检查每个元素是否为空,电子邮件字段是否格式化良好,以及复选框是否被单击(表示用户同意条款)。
The code that traps errors, is in the beginning of the entire script. Obviously, this will be executed every time the page is loaded. If it’s being loaded after the form is submitted, the following segment checks whether each element is empty, the email field is well-formed, and the checkbox is clicked (indicating that the user agrees to the terms).
<?php
// define variables and set to empty values
$nameErr = $emailErr = $genderErr = $websiteErr = "";
$name = $email = $gender = $class = $course = $subject = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["name"])) {
$nameErr = "Name is required";
} else {
$name = test_input($_POST["name"]);
}
if (empty($_POST["email"])) {
$emailErr = "Email is required";
} else {
$email = test_input($_POST["email"]);
// check if e-mail address is well-formed
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Invalid email format";
}
}
if (empty($_POST["course"])) {
$course = "";
} else {
$course = test_input($_POST["course"]);
}
if (empty($_POST["class"])) {
$class = "";
} else {
$class = test_input($_POST["class"]);
}
if (empty($_POST["gender"])) {
$genderErr = "Gender is required";
} else {
$gender = test_input($_POST["gender"]);
}
if (empty($_POST["subject"])) {
$subjectErr = "You must select one or more subjects";
} else {
$subject = $_POST["subject"];
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
HTML Form
呈现条目表单的 HTML 脚本遵循错误捕获代码。表单设计中采用了多种元素。
The HTML script that renders an entry form, follows the error trapping code. Various for elements have been employed in the form design.
<h2>Absolute Classes Registration Form</h2>
<p><span class = "error">* required field.</span></p>
<form method = "POST" action = "<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<table>
<tr>
<td>Name:</td>
<td>
<input type = "text" name = "name">
<span class = "error">* <?php echo $nameErr;?></span>
</td>
</tr>
<tr>
<td>E-mail: </td>
<td>
<input type = "text" name = "email">
<span class = "error">* <?php echo $emailErr;?></span>
</td>
</tr>
<tr>
<td>Time:</td>
<td>
<input type = "text" name = "course">
<span class = "error"><?php echo $websiteErr;?></span>
</td>
</tr>
<tr>
<td>Classes:</td>
<td><textarea name = "class" rows = "5" cols = "40"></textarea></td>
</tr>
<tr>
<td>Gender:</td>
<td>
<input type = "radio" name = "gender" value = "female">Female
<input type = "radio" name = "gender" value = "male">Male
<span class = "error">* <?php echo $genderErr;?></span>
</td>
</tr>
<tr>
<td>Select:</td>
<td>
<select name = "subject[]" size = "4" multiple>
<option value = "Android">C</option>
<option value = "Java">Java</option>
<option value = "C#">C#</option>
<option value = "Data Base">C++</option>
<option value = "Hadoop">PHP</option>
<option value = "VB script">Python</option>
</select>
</td>
</tr>
<tr>
<td>Agree</td>
<td><input type = "checkbox" name = "checked" value = "1"></td>
<?php if(!isset($_POST['checked'])){ ?>
<span class = "error">* <?php echo "You must agree to terms";?></span>
<?php } ?>
</tr>
<tr>
<td>
<input type = "submit" name = "submit" value = "Submit">
</td>
</tr>
</table>
</form>
请注意,表单数据已提交回同一个脚本,因此表单的 action 属性设置为 $_SERVER["PHP_SELF"] 超全局。此部分还包含某些内联 PHP 代码,这些代码在各个表单控件旁边闪烁错误消息——例如,如果名称字段在提交表单时为空,则在名称文本框旁边立即显示名称必需的消息。
Note that the form data is submitted back to the same script, hence the form’s action attribute is set to $_SERVER["PHP_SELF"] superglobal. This part also contains certain inline PHP code that flashes the error messages besides the respective form control – such as Name Required message just besides the Name text box, if the name field is empty while submitting the form.
Display Form Data
脚本的第三部分又是一段 PHP 代码,它回显用户提交的每个表单字段的值。
The third part of the script is again a PHP code that echoes the values of each of the form fields submitted by the user.
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
echo "<h2>Your given values are as :</h2>";
echo ("<p><b>Name</b> : $name</p>");
echo ("<p><b>Email address</b> : $email</p>");
echo ("<p><b>Preffered class time</b> : $course</p>");
echo ("<p><b>Class info</b> : $class </p>");
echo ("<p><b>Gender</b> : $gender</p>");
echo "<p><b>Subjcts Chosen:</b><p>";
if (!empty($subject)) {
echo "<ul>";
for($i = 0; $i < count($subject); $i++) {
echo "<li>$subject[$i]</u/li>";
}
echo "</ul>";
}
}
?>
当脚本从服务器的文档根目录运行时,此为表单中填写的示例数据 −
Here’s the sample data filled in the form when the script is run from the server’s document root folder −
当提交时, output 呈现如下所示 −
When submitted, the output is rendered as below −
Example
处理 HTML 表单的 PHP 的完整代码如下所示 −
The complete code of PHP’s handling HTML form is as follows −
<html>
<head>
<style>
.error {color: #FF0000;}
</style>
</head>
<body>
<?php
// define variables and set to empty values
$nameErr = $emailErr = $genderErr = $websiteErr = "";
$name = $email = $gender = $class = $course = $subject = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["name"])) {
$nameErr = "Name is required";
}else {
$name = test_input($_POST["name"]);
}
if (empty($_POST["email"])) {
$emailErr = "Email is required";
} else {
$email = test_input($_POST["email"]);
// check if e-mail address is well-formed
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Invalid email format";
}
}
if (empty($_POST["course"])) {
$course = "";
} else {
$course = test_input($_POST["course"]);
}
if (empty($_POST["class"])) {
$class = "";
} else {
$class = test_input($_POST["class"]);
}
if (empty($_POST["gender"])) {
$genderErr = "Gender is required";
} else {
$gender = test_input($_POST["gender"]);
}
if (empty($_POST["subject"])) {
$subjectErr = "You must select one or more subjects";
} else {
$subject = $_POST["subject"];
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<h2>Absolute Classes Registration Form</h2>
<p><span class = "error">* required field.</span></p>
<form method = "POST" action = "<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<table>
<tr>
<td>Name:</td>
<td>
<input type = "text" name = "name">
<span class = "error">* <?php echo $nameErr;?></span>
</td>
</tr>
<tr>
<td>E-mail: </td>
<td>
<input type = "text" name = "email">
<span class = "error">* <?php echo $emailErr;?></span>
</td>
</tr>
<tr>
<td>Time:</td>
<td>
<input type = "text" name = "course">
<span class = "error"><?php echo $websiteErr;?></span>
</td>
</tr>
<tr>
<td>Classes:</td>
<td><textarea name = "class" rows = "5" cols = "40"></textarea></td>
</tr>
<tr>
<td>Gender:</td>
<td>
<input type = "radio" name = "gender" value = "female">Female
<input type = "radio" name = "gender" value = "male">Male
<span class = "error">* <?php echo $genderErr;?></span>
</td>
</tr>
<tr>
<td>Select:</td>
<td>
<select name = "subject[]" size = "4" multiple>
<option value = "C">C</option>
<option value = "Java">Java</option>
<option value = "C#">C#</option>
<option value = "c++">C++</option>
<option value = "PHP">PHP</option>
<option value = "Python">Python</option>
</select>
</td>
</tr>
<tr>
<td>Agree</td>
<td><input type = "checkbox" name = "checked" value = "1"></td>
<?php if(!isset($_POST['checked'])){ ?>
<span class = "error">* <?php echo "You must agree to terms";?></span>
<?php } ?>
</tr>
<tr>
<td>
<input type = "submit" name = "submit" value = "Submit">
</td>
</tr>
</table>
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
echo "<h2>Your given values are as :</h2>";
echo ("<p><b>Name</b> : $name</p>");
echo ("<p><b>Email address</b> : $email</p>");
echo ("<p><b>Preffered class time</b> : $course</p>");
echo ("<p><b>Class info</b> : $class </p>");
echo ("<p><b>Gender</b> : $gender</p>");
echo "<p><b>Subjcts Chosen:</b><p>";
if (!empty($subject)) {
echo "<ul>";
for($i = 0; $i < count($subject); $i++) {
echo "<li>$subject[$i]</u/li>";
}
echo "</ul>";
}
}
?>
</body>
</html>
它将生成以下 output −
It will produce the following output −
PHP - File Inclusion
你可以在服务器执行之前将一个 PHP 文件的内容包含到另一个 PHP 文件中。有两个 PHP 函数可用于将一个 PHP 文件包含到另一个 PHP 文件中。
You can include the content of a PHP file into another PHP file before the server executes it. There are two PHP functions which can be used to included one PHP file into another PHP file.
-
The include() Function
-
The require() Function
这是 PHP 的一个优点,它有助于创建可以在多个页面中重复使用的函数、页眉、页脚或元素。这将有助于开发人员毫不费力地轻松更改整个网站的布局。如果有任何更改需求,则无需更改数千个文件,只需要更改包含的文件即可。
This is a strong point of PHP which helps in creating functions, headers, footers, or elements that can be reused on multiple pages. This will help developers to make it easy to change the layout of complete website with minimal effort. If there is any change required then instead of changing thousand of files just change included file.
The include() Function
include() 函数获取指定文件中所有的文本并且将其复制到使用 include 函数的文件中。如果加载文件时有任何问题,则 include() 函数会生成警告,但是脚本将继续执行。
The include() function takes all the text in a specified file and copies it into the file that uses the include function. If there is any problem in loading a file then the include() function generates a warning but the script will continue execution.
假设你要为网站创建一个通用菜单。然后创建一个包含以下内容的文件 menu.php。
Assume you want to create a common menu for your website. Then create a file menu.php with the following content.
<a href="http://www.tutorialspoint.com/index.htm">Home</a>
<a href="http://www.tutorialspoint.com/ebxml">ebXML</a>
<a href="http://www.tutorialspoint.com/ajax">AJAX</a>
<a href="http://www.tutorialspoint.com/perl">PERL</a>
现在创建尽可能多的页面,并添加此文件以创建页眉。例如,现在你的 test.php 文件可以包含以下内容。
Now create as many pages as you like and include this file to create header. For example now your test.php file can have following content.
<?php <b>include("menu.php");</b> ?>
<p>This is an example to show how to include PHP file!</p>
它将产生以下结果 −
It will produce the following result −
The require() Function
require() 函数获取指定文件中所有的文本并且将其复制到使用 include 函数的文件中。如果加载文件时有任何问题,则 require() 函数会生成致命错误并停止脚本执行。
The require() function takes all the text in a specified file and copies it into the file that uses the include function. If there is any problem in loading a file then the require() function generates a fatal error and halt the execution of the script.
因此 require() 和 include() 之间的区别在于它们如何处理错误条件。建议使用 require() 函数,而不要使用 include() 函数,因为如果缺少文件或文件命名错误,脚本不应继续执行。
So there is no difference in require() and include() except they handle error conditions. It is recommended to use the require() function instead of include(), because scripts should not continue executing if files are missing or misnamed.
你可以尝试使用以上示例结合 require() 函数,它会生成相同的结果。但是,如果将尝试以下两个示例,其中文件不存在,你将会得到不同的结果。
You can try using above example with require() function and it will generate same result. But if you will try following two examples where file does not exist then you will get different results.
<?php include("xxmenu.php"); ?>
<p>This is an example to show how to include wrong PHP file!</p>
这会产生以下结果 −
This will produce the following result −
This is an example to show how to include wrong PHP file!
现在我们尝试使用 require() 函数来执行相同的示例。
Now lets try same example with require() function.
<?php <b>require("xxmenu.php");</b> ?>
<p>This is an example to show how to include wrong PHP file!</p>
这次文件执行停止,没有显示任何东西。
This time file execution halts and nothing is displayed.
NOTE − 你可能会得到普通警告消息或致命错误消息,或者根本没有消息。这取决于你的 PHP 服务器配置。
NOTE − You may get plain warning messages or fatal error messages or nothing at all. This depends on your PHP Server configuration.
PHP - GET & POST
由于 PHP 主要用于 Web 应用程序开发,因此浏览器客户端发送的数据主要与 GET 和 POST 类型的 HTTP 请求方法有关。HTTP 协议还定义了向服务器发送请求的其他方法。除了 GET 和 POST 方法之外,还有 PUT、DELETE、HEAD 和 OPTIONS 方法。在本章中,我们主要研究 PHP 如何处理 GET 和 POST 方法。
Since PHP is mostly used for web application development, the data sent by the browser client is mainly with the GET and POST types of HTTP request methods. The HTTP protocol also defines other methods for sending the request to the server. They are PUT, DELETE, HEAD and OPTIONS (in addition to GET and POST methods). In this chapter, we shall concentrate on how PHP handles the GET and POST methods.
The GET Method
GET 方法发送编码的用户信息,并将其附加到页面请求。页面和编码信息由 ? 字符分隔。
The GET method sends the encoded user information appended to the page request. The page and the encoded information are separated by the ? character.
http://www.test.com/index.htm?name1=value1&name2=value2
-
The GET method produces a long string that appears in your server logs, in the browser’s Location: box.
-
The GET method is restricted to send upto 1024 characters only.
-
Never use GET method if you have password or other sensitive information to be sent to the server.
-
GET can’t be used to send binary data, like images or word documents, to the server.
-
The data sent by GET method can be accessed using QUERY_STRING environment variable.
-
The PHP provides $_GET associative array to access all the sent information using GET method.
通过将源代码放入 test.php 脚本中,尝试以下示例。
Try out following example by putting the source code in test.php script.
<?php
if( $_GET["name"] || $_GET["age"] ) {
echo "Welcome ". $_GET['name']. "<br />";
echo "You are ". $_GET['age']. " years old.";
exit();
}
?>
<form action = "<?php <b>$_PHP_SELF</b> ?>" method = "GET">
Name: <input type = "text" name = "name" />
Age: <input type = "text" name = "age" />
<input type = "submit" />
</form>
它将产生以下结果 −
It will produce the following result −
The POST Method
POST 方法通过 HTTP 标头传输信息。该信息已被编码,就像 GET 方法中所描述的那样,并放入名为 QUERY_STRING 的标头中。
The POST method transfers information via HTTP headers. The information is encoded as described in case of GET method and put into a header called QUERY_STRING.
-
The POST method does not have any restriction on data size to be sent.
-
The POST method can be used to send ASCII as well as binary data.
-
The data sent by POST method goes through HTTP header so security depends on HTTP protocol. By using Secure HTTP you can make sure that your information is secure.
-
The PHP provides $_POST associative array to access all the sent information using POST method.
通过将源代码放入 test.php 脚本中,尝试以下示例。
Try out following example by putting the source code in test.php script.
<?php
if( $_POST["name"] || $_POST["age"] ) {
if (preg_match("/[^A-Za-z'-]/",$_POST['name'] )) {
die ("invalid name and name should be alpha");
}
echo "Welcome ". $_POST['name']. "<br />";
echo "You are ". $_POST['age']. " years old.";
exit();
}
?>
<form action = "<?php <b>$_PHP_SELF</b> ?>" method = "POST">
Name: <input type = "text" name = "name" />
Age: <input type = "text" name = "age" />
<input type = "submit" />
</form>
它将产生以下结果 −
It will produce the following result −
Difference between GET and POST
GET 方法和 POST 方法之间的主要区别在于,虽然附加到 URL 的请求参数在浏览器的 URL 中公开出来,但 POST 数据包含在消息正文中,并且不会在 URL 中显示出来。因此,GET 方法不应用于将敏感数据发送到服务器。
The main difference between the GET and POST methods is that while the request parameters appended to the URL are exposed in the browser’s URL, the POST data is included in the message body, and not revealed in the URL. Hence, the GET method shouldn’t be used to send sensitive data to the server.
其次,GET 方法中的请求数据不能超过 2048 个字符,只能由 ASCII 字符组成,而使用 POST 方法时,对请求数据没有限制,也可以是二进制的(POST 数据的默认最大大小由 php.ini 文件中的 post_max_size 设置决定)
Secondly, the request data in GET method cannot exceed 2048 characters and can consist of ASCII characters only, while with POST method, there is no limit to the request data which can be in binary also (the default maximum size of POST data is determined by post_max_size setting in php.ini file)
PHP 提供了以下三个 superglobals 来检索和处理请求参数 −
PHP provides the following three superglobals to retrieve and process the request parameters −
-
$_GET − an associative array to access all the sent information using GET method.
-
$_POST − an associative array to access all the sent information using POST method.
-
$_REQUEST − an associative array that can be used to get the result from form data sent with both the GET and POST methods.
$_GET Array
你可以在查询字符串中以直接附加到 URL 的形式传递请求参数。
You can pass the request parameters in the form of query string directly appended to the URL.
将以下 PHP 脚本保存在文档根文件夹 ( htdocs ) 中,并将其命名为“hello.php” −
Save the following PHP script in the document root folder (htdocs) as "hello.php" −
<?php
echo "First name: " . $_REQUEST['first_name'] . " " .
"Last Name: " . $_REQUEST['last_name'] . "";
?>
在浏览器窗口中输入 http://localhost/hello.php?first_name=Amar&last_name=Sharma 作为 URL(确保 PHP 服务器正在运行)。
Enter http://localhost/hello.php?first_name=Amar&last_name=Sharma as the URL in a browser window (ensure that the PHP server is running).
$_GET 数组通过请求填充,输出显示如下 −
The $_GET array is populated from the request and the output is displayed as below −
First name: Amar Last Name: Sharma
如果 HTML 表单数据的方法属性为 GET,您也可以使用该数据填充 $_GET 数组。
You can also populate the $_GET array with the HTML form data if its method attribute is GET.
使用以下 HTML 表单收集数据并将其发送到“hello.php”。在文档根目录下,将以下脚本另存为“hello.html” −
Use the following HTML form to collect the data and send it to "hello.php". Under the document root, save the following script as "hello.html" −
<form action="hello.php" method="get">
First Name: <input type="text" name="first_name"/> <br/>
Last Name: <input type="text" name="last_name" />
<input type="submit" value="Submit" />
</form>
在浏览器中,输入 URL“http://localhost/hello.html” −
In your browser, enter the URL "http://localhost/hello.html" −
您应该会在浏览器窗口中获取类似 output 的内容。
You should get the similar output in the browser window.
$_POST Array
使用 POST 请求向服务器发送数据的最简单方法是将 HTML 表单的方法属性指定为 POST。假设浏览器中的 URL 为“http://localhost/hello.php”,那么在 HTML 表单“hello.html”中将 method=POST 设置为前面的示例 −
The easiest way to send data to a server with the POST request is specifying the method attribute of HTML form as POST. Assuming that the URL in the browser is "http://localhost/hello.php", method=POST is set in a HTML form "hello.html" as in the earlier example −
<form action="hello.php" method="post">
First Name: <input type="text" name="first_name"/> <br/>
Last Name: <input type="text" name="last_name" />
<input type="submit" value="Submit" />
</form>
“hello.php”脚本(位于文档根目录文件夹中)在 $_POST 数组中检索表单数据并将其作为 HTTP 响应呈现给浏览器 −
The "hello.php" script (in document root folder) retrieves the form data in the $_POST array and renders it as the HTTP response back to the browser −
<?php
echo "First name: " . $_POST['first_name'] . " " .
"Last Name: " . $_POST['last_name'] . "";
?>
在浏览器中打开 "http://localhost/hello.html" 。输入的数据将由服务器检索,然后像前面的示例一样呈现在客户端。
Open "http://localhost/hello.html" in your browser. The data entered is retrieved by the server, and rendered back to the client, as in the earlier example.
PHP - File Uploading
典型 PHP Web 应用程序所需的一个常见功能是允许用户上传文件。在 PHP 中从客户端上传文件非常容易。在本章中,我们将学习如何针对文件上传过程使用 PHP 脚本。
One of the common features required in a typical PHP web application is the provision of letting the user upload files. Uploading files from a client is very easy in PHP. In this chapter, we shall learn how to use PHP script for the file upload process.
上传文件的过程遵循以下步骤 −
The process of uploading a file follows these steps −
-
The user opens the page containing a HTML form featuring a text files, a browse button and a submit button.
-
The user clicks the browse button and selects a file to upload from the local PC.
-
The full path to the selected file appears in the text filed then the user clicks the submit button.
-
The selected file is sent to the temporary directory on the server.
-
The PHP script that was specified as the form handler in the form’s action attribute checks that the file has arrived and then copies the file into an intended directory.
-
The PHP script confirms the success to the user.
为了执行此活动,我们必须首先确保“php.ini”中启用了与文件上传相关的配置设置。
In order to perform this activity, we must first ensure that configuration settings related to file upload are enabled in "php.ini".
打开“php.ini”文件并确保以下设置已启用,方法是在 file_uploads、upload_tmp_dir、upload_max_filesize 和 max_file_uploads 参数中删除前导分号 (;) 符号 −
Open the "php.ini" file and ensure that the following settings are enabled by removing the leading semicolon (;) symbol in file_uploads, upload_tmp_dir, upload_max_filesize and max_file_uploads parameters −
;;;;;;;;;;;;;;;;
; File Uploads ;
;;;;;;;;;;;;;;;;
; Whether to allow HTTP file uploads.
; http://php.net/file-uploads
file_uploads=On
; Temporary directory for HTTP uploaded files (will use system
; default if not specified).
; http://php.net/upload-tmp-dir
upload_tmp_dir="C:\xampp\tmp"
; Maximum allowed size for uploaded files.
; http://php.net/upload-max-filesize
upload_max_filesize=40M
; Maximum number of files that can be uploaded via a single request
max_file_uploads=20
临时位置和最终位置的文件夹必须设置有允许文件写入的权限。如果任一位置设置为只读,那么进程将失败。
It is necessary that the folders for both temporary and final locations have permissions set that enable file writing. If either is set to be read-only then process will fail.
Creating a File Upload Form
下一步,我们需要设计一个用于文件上传的 HTML 表单。该表单的 method 属性必须为 POST,enctype 必须为 multipart/form-data。使用 file 作为 input 类型,以便让用户浏览并选择要上传的文件。
Next, we need to design a HTML form for file upload. The form’s method attribute must be POST and enctype must be multipart/form-data. Use the input type as file to let the user browse and select the file to be uploaded.
<h2>File Upload Form</h2>
<form method = "POST" action = "uploadfile.php" enctype="multipart/form-data">
<label for="file">File name:</label>
<input type="file" name="uploadfile" />
<input type="submit" name="submit" value="Upload" />
</form>
Creating an Upload Script
uploadfile.php 脚本接收已上传的文件。文件数据收集在超级全局变量 $_FILES 中。获取已上传文件的名称、文件类型、大小和 tmp_name 属性。
The uploadfile.php script receives the uploaded file. The file data is collected in a suparglobal variable $_FILES. Fetch the name, file type, size and the tmp_name attributes of the uploaded file.
move_uploaded_file() 函数将所选文件复制到文档文件夹中。
The move_uploaded_file() function copies the selected file to the document folder.
<?php
echo "<b>File to be uploaded: </b>" . $_FILES["uploadfile"]["name"] . "<br>";
echo "<b>Type: </b>" . $_FILES["uploadfile"]["type"] . "<br>";
echo "<b>File Size: </b>" . $_FILES["uploadfile"]["size"]/1024 . "<br>";
echo "<b>Store in: </b>" . $_FILES["uploadfile"]["tmp_name"] . "<br>";
if (file_exists($_FILES["uploadfile"]["name"])){
echo "<h3>The file already exists</h3>";
} else {
move_uploaded_file($_FILES["uploadfile"]["tmp_name"], $_FILES["uploadfile"]["name"]);
echo "<h3>File Successfully Uploaded</h3>";
}
?>
假设 myform.php 和 uploadfile.php 这两个文件都存储在文档文件夹中。
Assuming that both the files myform.php and uploadfile.php are stored in the document folder.
在浏览器中打开 “myform.php” - ([role="bare"]http://localhost/myform.php) -
Open "myform.php" in the browser ([role="bare"]http://localhost/myform.php) −
单击 File 按钮,浏览到待上传的目标文件,并单击 Upload 按钮。
Click the File button, browse to the desired file to be uploaded, and click the Upload button.
服务器返回以下消息 -
The server responds with the following message −
PHP - Cookies
万维网由 HTTP 协议驱动,该协议是一种无状态协议。Cookie 机制帮助服务器维护以前请求的信息。PHP 透明支持 HTTP cookie。
The worldwide web is powered by HTTP protocol, which is a stateless protocol. The mechanism of Cookies helps the server maintain the information of previous requests. PHP transparently supports HTTP cookies.
-
When a client first sends its request, the server includes a small piece of data along with its response as cookies. PHP provides the setcookie() method to inject cookies in the response.
-
This cookie data is stored in the client’s machine as text files. On subsequent visits of the same client, these cookies are included as a part of the request header.
-
The server populates the PHP superglobal variable "$_COOKIE" with all the cookies present in the client request.
本章将教您如何设置 cookie、如何访问 cookie 以及如何删除 cookie。
This chapter will teach you how to set cookies, how to access them and how to delete them.
The Anatomy of a Cookie
Cookie 通常设置在 HTTP 头中(尽管 JavaScript 也可以直接在浏览器上设置 cookie)。设置 cookie 的 PHP 脚本可能会发送类似以下内容的头:
Cookies are usually set in an HTTP header (although JavaScript can also set a cookie directly on a browser). A PHP script that sets a cookie might send headers that look something like this −
HTTP/1.1 200 OK
Date: Fri, 04 Feb 2000 21:03:38 GMT
Server: Apache/1.3.9 (UNIX) PHP/4.0b3
Set-Cookie: name=xyz; expires=Friday, 04-Feb-07 22:03:38 GMT;
path=/; domain=tutorialspoint.com
Connection: close
Content-Type: text/html
如您所见,Set-Cookie 标头包含一个名称值对、一个 GMT 日期、一个路径和一个域名。名称和值将经过 URL 编码。expires 字段是指示浏览器在给定时间和日期后“忘记”Cookie 的指令。
As you can see, the Set-Cookie header contains a name value pair, a GMT date, a path and a domain. The name and value will be URL encoded. The expires field is an instruction to the browser to "forget" the cookie after the given time and date.
如果浏览器配置为存储 cookie,它会将此信息保留至到期日期。如果用户将浏览器指向与 cookie 路径和域名匹配的任何页面,它将重新发送 cookie 至服务器浏览器头可能类似以下内容:
If the browser is configured to store cookies, it will then keep this information until the expiry date. If the user points the browser at any page that matches the path and domain of the cookie, it will resend the cookie to the server.The browser’s headers might look something like this −
GET / HTTP/1.0
Connection: Keep-Alive
User-Agent: Mozilla/4.6 (X11; I; Linux 2.2.6-15apmac ppc)
Host: zink.demon.co.uk:1126
Accept: image/gif, */*
Accept-Encoding: gzip
Accept-Language: en
Accept-Charset: iso-8859-1,*,utf-8
Cookie: name=xyz
随后,PHP 脚本可以访问环境变量 $_COOKIE 或 $HTTP_COOKIE_VARS[]中的 cookie,其中包含所有 cookie 名称和值。可以使用 $HTTP_COOKIE_VARS["name"] 访问上述 cookie。
A PHP script will then have access to the cookie in the environmental variables $_COOKIE or $HTTP_COOKIE_VARS[] which holds all cookie names and values. Above cookie can be accessed using $HTTP_COOKIE_VARS["name"].
How to Set a Cookie in PHP?
PHP 包含 setcookie 函数,用于创建要随 HTTP 响应一起发送给客户端的 cookie 对象。
PHP contains the setcookie function to create a cookie object to be sent to the client along with HTTP response.
setcookie(name, value, expire, path, domain, security);
Parameters
以下是所有参数的详细信息-
Here is the detail of all the arguments −
-
Name − This sets the name of the cookie and is stored in an environment variable called HTTP_COOKIE_VARS. This variable is used while accessing cookies.
-
Value − This sets the value of the named variable and is the content that you actually want to store.
-
Expiry − This specify a future time in seconds since 00:00:00 GMT on 1st Jan 1970. After this time cookie will become inaccessible. If this parameter is not set then cookie will automatically expire when the Web Browser is closed.
-
Path − This specifies the directories for which the cookie is valid. A single forward slash character permits the cookie to be valid for all directories.
-
Domain − This can be used to specify the domain name in very large domains and must contain at least two periods to be valid. All cookies are only valid for the host and domain which created them.
-
Security − This can be set to 1 to specify that the cookie should only be sent by secure transmission using HTTPS otherwise set to 0 which mean cookie can be sent by regular HTTP.
Example
下面给出的 PHP 脚本会检查 Cookie 名称为 username 已经设置,如果已被设置,则会取回其值。如果未设置,则会设置一个新的 Cookie username 。
The PHP script give below checks if the cookie named username is already set, and retrieves its value, if so. If not, a new cookie username is set.
<?php
if (isset($_COOKIE['username'])) {
echo "<h2>Cookie username already set:" . $_COOKIE['username'] . "</h2>";
} else {
setcookie("username", "MohanKumar");
echo "<h2>Cookie username is now set</h2>";
}
?>
从 Apache 服务器的文档根文件夹运行此脚本。您应会看到此消息 −
Run this script from the document root of the Apache server. You should see this message −
Cookie username is now set
如果重新执行此脚本,则现在已设置 Cookie。
If this script is re-executed, the cookie is now already set.
Cookie username already set: MohanKumar
浏览器的开发者工具是非常有用的工具。您可通过该工具设置、取回、删除 Cookie。由上述程序设置的 Cookie 可在浏览器的开发者工具的“应用程序”选项卡下查看。
Your browser’s developer tool is a very useful facility. You can set, retrieve and delete cookies with its help. The cookie set by the above program can be viewed under the Application tab of the browser’s developer tools.
如下面的 foreach 循环会取回所有 Cookie −
A foreach loop as below retrieves all the cookies −
<?php
$arr=$_COOKIE;
foreach ($arr as $key=>$val);
echo "<h2>$key=>$val </h2>";
?>
以下脚本包含一个 HTML 表单。它会将表单数据发送至 setcookie.php 脚本,该脚本会使用从 $_POST 数组取回的数据设置 Cookie。
The following script contains an HTML form. It sends the form data to setcookie.php script, that sets the cookies with the use of data retrieved from the $_POST array.
以下代码会呈现 HTML 表单 −
The HTML form is rendered by the following code −
<form action="setcookie.php" method="POST">
<input type="text" name="name">
<input type="text" name="age">
<input type="submit" name="Submit">
</form>
SetCookie.php 会读取表单数据并设置 Cookie。
SetCookie.php reads the form data and sets the cookies.
if (isset($_POST["submit"]) {
setcookie("name", $_POST["name"]);
setcookie("age", $_POST["age"]);
}
我们可通过另一个 getcookie.php 代码取回所设置的 Cookie。
With another getcookie.php code, we can retrieve the cookies set.
if (isset($_COOKIE["name"])
echo "Cookie: name => " . $_COOKIE["name"]. "<br>";
if (isset($_COOKIE["age"])
echo "Cookie: age => " . $_COOKIE["age"]. "<br>";
Accessing Cookies with PHP
PHP 提供了许多访问 Cookie 的方法。最简单的方法是使用 $_COOKIE 或 $HTTP_COOKIE_VARS 变量。以下示例将访问上述示例中设置的所有 Cookie。
PHP provides many ways to access cookies. Simplest way is to use either $_COOKIE or $HTTP_COOKIE_VARS variables. Following example will access all the cookies set in above example.
<?php
echo $_COOKIE["name"]. "<br />";
/* is equivalent to */
echo $HTTP_COOKIE_VARS["name"]. "<br />";
echo $_COOKIE["age"] . "<br />";
/* is equivalent to */
echo $HTTP_COOKIE_VARS["age"] . "<br />";
?>
您可以使用 isset() 函数检查 Cookie 是否已经设置。
You can use isset() function to check if a cookie is set or not.
<?php
if( isset($_COOKIE["name"]))
echo "Welcome " . $_COOKIE["name"] . "<br />";
else
echo "Sorry... Not recognized" . "<br />";
?>
Deleting the Cookies
要删除 Cookie,请使用在浏览器已经过期的日期设置 Cookie,以便浏览器触发 Cookie 删除机制。
To delete cookie set the cookie with a date that has already expired, so that the browser triggers cookie removal mechanism.
Example
请看以下示例:
Take a look at the following example −
<?php
setcookie("username", "", time() - 3600);
echo "<h2>Cookie username is now removed</h2>";
?>
浏览器会显示以下响应 −
The browser shows the following response −
Cookie username is now removed
您还可以通过在 Cookie 名称中使用数组符号来设置数组 Cookie。
You may also set array cookies by using array notation in the cookie name.
setcookie("user[three]", "Guest");
setcookie("user[two]", "user");
setcookie("user[one]", "admin");
如果 Cookie 名称包含句点 (.),则 PHP 会将它们替换为下划线 (_)。
If the cookie name contains dots (.), PHP replaces them with underscores (_).
虽然 Cookie 概念的主要目的是帮助 Web 开发者提供更为个性化、更为便捷的用户体验,但它可能会对您的隐私和个人信息构成风险。
Although the main purpose behind the concept of cookies is to help web developers provide a more personalized and convenient user experience, it may pose a risk to your privacy and personal information.
在某些情况下,如果你不接受应用的 cookies,应用可能会拒绝你完全访问。在这种情况下,建议定期清理浏览器缓存中的 cookie 相关数据。
In some cases, the application may deny you full access you don’t accept their cookies. In such cases, periodically clearing the cookie related data from your browser’s cache is advised.
PHP - Sessions
Web 会话是从用户与服务器建立连接到连接终止之间的时间。与 cookies 一起,会话变量使数据可以在整个网站的各个页面中访问。
A web session is the time duration between the time a user establishes connection with a server and the time the connection is terminated. Along with the cookies, the session variables make the data accessible across the various pages of an entire website.
在会话期间,网站维护有关用户操作和偏好的信息。会话数据填充在超全局关联数组 $_SESSION 中。
During a session, the website maintains information about the user’s actions and preferences. The session data is populated in a superglobal associative array $_SESSION.
要在 PHP 中启动一个新会话,你需要调用 session_start() 函数。
To start a new session in PHP, you need to call the session_start() function.
Starting a Session
为了启用对会话数据的访问,必须调用 session_start() 函数。 session_start() 基于通过 GET 或 POST 请求或通过 cookie 传递的会话标识符,创建一个会话或恢复当前会话。
In order to enable access to session data, session_start() function must be invoked. session_start() creates a session or resumes the current one based on a session identifier passed via a GET or POST request, or passed via a cookie.
session_start(array $options = []): bool
如果会话成功启动,此函数返回 true ;否则返回 false 。
This function returns true if a session was successfully started, otherwise false.
PHP 首先为该特定会话创建一个唯一标识符,该标识符是由 32 个十六进制数字组成的随机字符串。
PHP first creates a unique identifier for that particular session which is a random string of 32 hexadecimal numbers.
session_id() 函数设置或检索唯一会话 ID。
The session_id() function sets or retrieves a unique session ID.
session_id(?string $id = null): string|false
如果未给定 $id 参数,PHP 将生成一个随机会话 ID。你也可以指定自己的 ID。该函数返回当前会话的会话 ID,如果没有当前会话,则返回空字符串。如果失败,它将返回 false 。
PHP will generate a random session ID, if the $id parameter is not given. You may specify your own ID instead. The function returns the session id for the current session or the empty string if there is no current session. On failure, it returns false.
Example
请看以下示例:
Take a look at the following example −
<?php
// Starting the session
session_start();
$id = session_id();
echo "Session Id: ".$id ;
?>
浏览器会将一个随机字符串显示为 output −
The browser will show a random string as the output −
Session Id: mi3976f8ssethe9f04vq1ag6it
名为 PHPSESSID 的 cookie 会自动发送到用户的计算机,以存储唯一的会话标识符字符串。
A cookie called PHPSESSID is automatically sent to the user’s computer to store unique session identification string.
会话在服务器的临时目录中创建一个文件,其中存储已注册的会话变量及其值。此数据在该访问期间将对网站上的所有页面可用。
A session creates a file in a temporary directory on the server where registered session variables and their values are stored. This data will be available to all pages on the site during that visit.
临时文件的位置由“php.ini”文件中的一个名为“session.save_path”的设置确定。
The location of the temporary file is determined by a setting in the "php.ini" file called "session.save_path".
Handling Session Variables
会话变量存储在关联数组 $_SESSION[] 中。在会话的生命周期内可以访问这些变量。
Session variables are stored in associative array called $_SESSION[]. These variables can be accessed during lifetime of a session.
要创建新的会话变量,在 $_SESSION 数组中添加一个键值对 −
To create a new session variable, add a key-value pair in the $_SESSION array −
$_SESSION[ "var"]=value;
要回读会话变量的值,可以使用 echo/print 语句、或 var_dump() 或 print_r() 函数。
To read back the value of a session variable, you can use echo/print statements, or var_dump() or print_r() functions.
echo $_SESSION[ "var"];
要获取当前会话中所有会话变量的列表,可以使用 foreach 循环遍历 $_SESSION −
To obtain the list of all the session variables in the current session, you can use a foreach loop to traverse the $_SESSION −
foreach ($_SESSION as $key=>$val)
echo $key . "=>" . $val;
Example
以下示例启动一个会话,然后注册一个名为 counter 的变量,该变量在会话期间每次访问页面时都会递增。
The following example starts a session then register a variable called counter that is incremented each time the page is visited during the session.
使用 isset() function 检查会话变量是否已设置。
Use the isset() function to check if a session variable is already set or not.
以下 PHP 脚本在首次运行时启动会话,并设置一个名为 counter 的会话变量。当客户端再次访问同一 URL 时,由于会话变量已设置,因此计数器会增加。
The following PHP script starts a session when it runs for the first time, and sets a session variable named counter. When the client revisits the same URL again, since the session variable is already set, the counter is incremented.
<?php
session_start();
if( isset( $_SESSION['counter'] ) ) {
$_SESSION['counter'] += 1;
} else {
$_SESSION['counter'] = 1;
}
$msg = "Number of visits in this session: ". $_SESSION['counter'];
?>
<?php
echo "$msg";
?>
多次刷新浏览器以模拟重复访问。浏览器显示计数器 −
Refresh the browser multiple times to simulate repeated visits. The browser displays the counter −
Number of visits in this session: 5
Destroying a PHP Session
PHP 会话可以被 session_destroy() 函数销毁。此函数不需要任何参数,并且一次调用可以销毁所有会话变量。如果您想销毁单个会话变量,则可以使用 unset() 函数取消设置会话变量。
A PHP session can be destroyed by session_destroy() function. This function does not need any argument and a single call can destroy all the session variables. If you want to destroy a single session variable then you can use unset() function to unset a session variable.
以下是 unset a single variable 的示例 −
Here is an example to unset a single variable −
<?php
unset($_SESSION['counter']);
?>
以下是将会 destroy all the session variables 的调用 −
Here is the call which will destroy all the session variables −
<?php
session_destroy();
?>
如果您可以在 php.ini 文件中将 session.auto_start 变量设置为 1,则当用户访问您的网站时,您不需要调用 start_session() 函数来启动会话。
You don’t need to call start_session() function to start a session when a user visits your site if you can set session.auto_start variable to 1 in php.ini file.
Example
以下 PHP 脚本呈现一个 HTML 表单。表单数据用于创建三个会话变量。超链接将浏览器带到另一个页面,该页面读回会话变量。
The following PHP script renders a HTML form. The form data is used to create three session variables. A hyperlink takes the browser to another page, which reads back the session variables.
<html>
<body>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
<h3>User's ID: <input type="text" name="ID"/></h3>
<h3>User's Name: <input type="text" name="name"/></h3>
<h3>User Type: <input type="text" name="type"/></h3>
<input type="submit" value="Submit" />
</form>
<?php
session_start();
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$_SESSION['ID'] = $_POST['ID'];
$_SESSION['Name'] = $_POST['name'];
$_SESSION['type'] = $_POST['type'];
echo "<h2>Following Session variables Created</h2>";
foreach ($_SESSION as $key=>$val) {
echo "<h3>" . $key . "=>" . $val . "</h3>";
}
echo "<a href='test.php'><b>Click Here</b></a>";
}
?>
</body>
</html>
将此代码作为“hello.php”保存在文档根目录文件夹中,并在客户端浏览器中打开它。
Save this code as "hello.php" in the document root folder, and open it in a client browser.
按 Submit 按钮。浏览器将显示已创建的会话变量 −
Press the Submit button. The browser will show the session variables created −
浏览器通过遵循所示的链接导航到另一个页面。它读回会话变量。
The browser navigates to another page by following the link shown. It reads back the session variables.
PHP - Session Options
从 PHP 7 开始, session_start() 函数接受一个选项数组来覆盖在“php.ini”中设置的会话配置指令。在“php.ini”中的 [session] 会话定义了各种选项的默认值。
From PHP version 7 onwards, the session_start() function accepts an array of options to override the session configuration directives set in "php.ini". The [session] session in "php.ini" defines the default values of various options.
如果提供了选项,它们将采用将覆盖当前设置的会话配置指令的关联选项数组的形式。键不应包含“session.”前缀。
The options, if provided, are in the form of an associative array of options that will override the currently set session configuration directives. The keys should not include the "session." prefix.
Example
例如,您可以使用 session_start() 函数的参数定义的两个会话选项来启动 HTTP 会话 −
For example, you may start the HTTP session with the two session options defined as the parameters of session_start() function −
<?php
session_start([
'cache_limiter' => 'private',
'read_and_close' => true,
]);
?>
Configurable Options of an HTTP Session
PHP 中 HTTP 会话的一些可配置选项如下 −
Some of the configurable options of an HTTP session in PHP are as follows −
session.name
它指定用作 cookie 名称的会话名称。它应只包含字母数字字符。默认为 PHPSESSID。
It specifies the name of the session which is used as cookie name. It should only contain alphanumeric characters. Defaults to PHPSESSID.
session.save_handler
它定义用于存储和检索与会话关联的数据的处理程序的名称。默认为文件。
It defines the name of the handler which is used for storing and retrieving data associated with a session. Defaults to files.
session.auto_start
它指定会话模块是否在请求启动时自动启动会话。默认为 0(已禁用)。
It specifies whether the session module starts a session automatically on request startup. Defaults to 0 (disabled).
session.cookie_lifetime
它指定发送到浏览器的 cookie 的生命周期(以秒为单位)。值 0 表示“直到浏览器关闭”。默认为 0。
It specifies the lifetime of the cookie in seconds which is sent to the browser. The value 0 means "until the browser is closed." Defaults to 0.
session.cache_limiter
它指定会话页面使用的缓存控制方法。它可以是以下值之一:nocache、private、private_no_expire 或 public。默认为 nocache。
It specifies the cache control method used for session pages. It may be one of the following values: nocache, private, private_no_expire, or public. Defaults to nocache.
session.sid_length
它允许您指定会话 ID 字符串的长度。会话 ID 长度可在 22 到 256 之间。默认值为 32。
It allows you to specify the length of session ID string. Session ID length can be between 22 to 256. The default is 32.
PHP - Sending Emails
发送电子邮件是 PHP 提供支持的典型 Web 应用程序的常见需求功能之一。你会希望通过 PHP 应用程序本身(而非不同的邮件服务)向你的注册用户发送包含通知、更新和其他通信的电子邮件。你可以通过采用本章中描述的技术向你的 PHP 应用程序添加此功能。
The provision of sending emails is one the commonly required features of a typical PHP powered web application. You would like to send emails containing notifications, updates and other communications to your registered users, through your PHP application itself, instead of a different mail service. You can add this capability to your PHP application by adopting the techniques described in this chapter.
PHP 内置有一个 mail() 函数,可用于发送电子邮件。但是,你需要正确配置“php.ini”设置,才能使用它。首先,你必须知道你正在使用的网络托管平台的 SMTP 域名。例如,如果你的网站托管在 GoDaddy 托管服务上,则 SMTP 域名是“smtp.secureserver.net”,你应该在配置中使用它。
PHP has a built-in mail() function to send an email. However, you need configure properly the "php.ini" settings to be able to do so. First, you must know the SMTP domain of the web hosting platform that you are using. For example, if your website is being hosted on GoDaddy hosting service, the SMTP domain is "smtp.secureserver.net", which you should use in the configuration.
如果你使用 GoDaddy 的基于 Windows 的托管,你应该确保在 php.ini 文件中启用了两个指令。第一个指令称为 SMTP,它定义你的电子邮件服务器地址。第二个指令称为 sendmail_from,它定义你的自己电子邮件地址。
If you use Windows based hosting of GoDaddy, you should ensure that two directives are enabled in php.ini file. The first is called SMTP that defines your email server address. The second is called sendmail_from which defines your own email address.
用于 Windows 的配置看起来应该像这样:
The configuration for Windows should look something like this −
[mail function]
; For Win32 only.
SMTP = smtp.secureserver.net
; For win32 only
sendmail_from = webmaster@tutorialspoint.com
Linux 用户只需让 PHP 知道其 sendmail 应用程序的位置即可。应将路径和任何所需的 switch 指定给 sendmail_path 指令。
Linux users simply need to let PHP know the location of their sendmail application. The path and any desired switches should be specified to the sendmail_path directive.
用于 Linux 的配置看起来应该像这样:
The configuration for Linux should look something like this −
[mail function]
; For Win32 only.
SMTP =
; For win32 only
sendmail_from =
; For Unix only
sendmail_path = /usr/sbin/sendmail -t -i
PHP 中的 mail() 函数需要三个强制性参数,这些参数指定收件人的电子邮件地址、邮件主题和实际邮件,此外还有另外两个可选参数。
mail() function in PHP requires three mandatory arguments that specify the recipient’s email address, the subject of the message and the actual message additionally there are other two optional parameters.
mail( to, subject, message, headers, parameters );
Parameters
-
to − Required. Specifies the receiver / receivers of the email
-
subject − Required. Specifies the subject of the email. This parameter cannot contain any newline characters
-
message − Required. Defines the message to be sent. Each line should be separated with a LF (\n). Lines should not exceed 70 characters
-
headers − Optional. Specifies additional headers, like From, Cc, and Bcc. The additional headers should be separated with a CRLF (\r\n)
-
parameters − Optional. Specifies an additional parameter to the send mail program
多个收件人可指定为 mail() 函数的第一个参数,用逗号分隔。
Multiple recipients can be specified as the first argument to the mail() function in a comma separated list.
Sending HTML Email
当你使用 PHP 发送文本消息时,所有内容都将被视为纯文本。即使你将 HTML 标记包含在文本消息中,它也将显示为纯文本,并且 HTML 标记不会根据 HTML 语法进行格式化。但 PHP 提供了将 HTML 邮件作为实际 HTML 邮件发送的选项。
When you send a text message using PHP then all the content will be treated as simple text. Even if you will include HTML tags in a text message, it will be displayed as simple text and HTML tags will not be formatted according to HTML syntax. But PHP provides option to send an HTML message as actual HTML message.
发送邮件时,你可以指定一个 Mime 版本、内容类型和字符集来发送 HTML 邮件。
While sending an email message you can specify a Mime version, content type and character set to send an HTML email.
Example
以下示例显示了如何将 HTML 邮件发送至“ xyz@somedomain.com ”,并抄送给“ afgh@somedomain.com ”。你可以以这样的方式编写此程序,使其接收来自用户的全部内容,然后发送电子邮件。
The following example shows how to send an HTML email message to "xyz@somedomain.com" copying it to "afgh@somedomain.com". You can code this program in such a way that it should receive all content from the user and then it should send an email.
它应当接收来自用户的全部内容,然后发送一封电子邮件。
It should receive all content from the user and then it should send an email.
<?php
$to = "xyz@somedomain.com";
$subject = "This is subject";
$message = "<b>This is HTML message.</b>";
$message .= "<h1>This is headline.</h1>";
$header = "From:abc@somedomain.com \r\n";
$header .= "Cc:afgh@somedomain.com \r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-type: text/html\r\n";
$retval = mail ($to,$subject,$message,$header);
if( $retval == true ) {
echo "Message sent successfully...";
}else {
echo "Message could not be sent...";
}
?>
它将生成以下 output −
It will produce the following output −
Message could not be sent...
sh: 1: /usr/sbin/sendmail: not found
Sending Email from Localhost
上面的 PHP mail() 调用方法可能无法在你的 localhost 上运行。在这种情况中,有一种替代方案可用于发送电子邮件。你可以使用 PHPMailer 从 localhost 使用 SMTP 发送电子邮件。
The above method of calling PHP mail() may not work on your localhost. In that case, there is an alternate solution to sending email. You can use PHPMailer to send email using SMTP from localhost.
PHPMailer 是一个开源库,用于连接 SMTP 来发送电子邮件。你可以从 PEAR 或 Composer 存储库下载它,或从 https://github.com/PHPMailer/PHPMailer 下载它。从这里下载 ZIP 文件,并将 PHPMailer 文件夹的内容复制到 PHP 配置中指定的某个 include_path 目录中,并手动加载每个类文件。
PHPMailer is an open-source library to connect SMTP to send emails. You can download it from PEAR or Composer repositories, or download it from https://github.com/PHPMailer/PHPMailer. Download the ZIP file from here, and copy the contents of the PHPMailer folder into one of the include_path directories specified in your PHP configuration and load each class file manually.
Example
使用以下 PHP 脚本通过 PHPMailer 库发送电子邮件:
Use the following PHP script to send email with PHPMailer library −
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
require_once __DIR__ . '/vendor/phpmailer/src/Exception.php';
require_once __DIR__ . '/vendor/phpmailer/src/PHPMailer.php';
require_once __DIR__ . '/vendor/phpmailer/src/SMTP.php';
require 'vendor/autoload.php';
$mail = new PHPMailer;
if(isset($_POST['send'])){
// getting post values
$fname=$_POST['fname'];
$toemail=$_POST['toemail'];
$subject=$_POST['subject'];
$message=$_POST['message'];
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'myID@gmail.com'; // SMTP username
$mail->Password = 'mypassword'; // SMTP password
// Enable TLS encryption, 'ssl' also accepted
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->setFrom(myID@gmail.com', 'My_Name');
$mail->addReplyTo(myID@gmail.com', 'My_Name');
$mail->addAddress($toemail); // Add a recipient
$mail->isHTML(true); // Set email format to HTML
$bodyContent=$message;
$mail->Subject =$subject;
$body = 'Dear'.$fname;
$body .='<p>'.$message.'</p>';
$mail->Body = $body;
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
}
?>
使用以下 HTML 表单编写邮件消息。表单提交给上述 phpmail.php 脚本
Use the following HTML form to compose the mail message. The form is submitted to the above phpmail.php script
<h1>PHP - Sending Email</h1>
<form action="PHPmailer.php" method="post">
<label for="inputName">Name</label>
<input type="text" id="inputName" name="fname" required>
<label for="inputEmail">Email</label>
<input type="email" id="inputEmail" name="toemail" required>
<label for="inputSubject">Subject</label>
<input type="text" id="inputSubject" name="subject" required>
<label for="inputMessage">Message</label>
<textarea id="inputMessage" name="message" rows="5" required></textarea>
<button type="submit" name="send">Send</button>
</form>
Sending Attachments with Email
要发送具有混合内容的电子邮件,您应将 Content-type 头设为 multipart/mixed。然后,可以在边界中指定文本和附件部分。
To send an email with mixed content you should set Content-type header to multipart/mixed. Then text and attachment sections can be specified within boundaries.
一个边界以两个连字符开始,后跟一个唯一数字,该数字不能出现在电子邮件的消息部分中。PHP 函数 md5() 用于创建一个 32 位十六进制数以创建唯一数字。表示电子邮件最终部分的最终边界也必须以两个连字符结束。
A boundary is started with two hyphens followed by a unique number which can not appear in the message part of the email. A PHP function md5() is used to create a 32 digit hexadecimal number to create unique number. A final boundary denoting the email’s final section must also end with two hyphens.
Example
请看以下示例:
Take a look at the following example −
<?php
// request variables
$from = $_REQUEST["from"];
$emaila = $_REQUEST["emaila"];
$filea = $_REQUEST["filea"];
if ($filea) {
function mail_attachment ($from , $to, $subject, $message, $attachment){
$fileatt = $attachment; // Path to the file
$fileatt_type = "application/octet-stream"; // File Type
$start = strrpos($attachment, '/') == -1 ?
strrpos($attachment, '//') : strrpos($attachment, '/')+1;
// Filename that will be used for the file as the attachment
$fileatt_name = substr($attachment, $start,
strlen($attachment));
$email_from = $from; // Who the email is from
$subject = "New Attachment Message";
$email_subject = $subject; // The Subject of the email
$email_txt = $message; // Message that the email has in it
$email_to = $to; // Who the email is to
$headers = "From: ".$email_from;
$file = fopen($fileatt,'rb');
$data = fread($file,filesize($fileatt));
fclose($file);
$msg_txt="\n\n You have recieved a new attachment message from $from";
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . "
boundary=\"{$mime_boundary}\"";
$email_txt .= $msg_txt;
$email_message .= "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" . "Content-Type:text/html;
charset = \"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" .
$email_txt . "\n\n";
$data = chunk_split(base64_encode($data));
$email_message .= "--{$mime_boundary}\n" . "Content-Type: {$fileatt_type};\n" .
" name = \"{$fileatt_name}\"\n" . //"Content-Disposition: attachment;\n" .
//" filename = \"{$fileatt_name}\"\n" . "Content-Transfer-Encoding:
"base64\n\n" . $data . "\n\n" . "--{$mime_boundary}--\n";
$ok = mail($email_to, $email_subject, $email_message, $headers);
if($ok) {
echo "File Sent Successfully.";
// delete a file after attachment sent.
unlink($attachment);
} else {
die("Sorry but the email could not be sent. Please go back and try again!");
}
}
move_uploaded_file($_FILES["filea"]["tmp_name"],
'temp/'.basename($_FILES['filea']['name']));
mail_attachment("$from", "youremailaddress@gmail.com",
"subject", "message", ("temp/".$_FILES["filea"]["name"]));
}
?>
<html>
<head>
<script language = "javascript" type = "text/javascript">
function CheckData45() {
with(document.filepost) {
if(filea.value ! = "") {
document.getElementById('one').innerText = "Attaching File ... Please Wait";
}
}
}
</script>
</head>
<body>
<table width = "100%" height = "100%" border = "0"
cellpadding = "0" cellspacing = "0">
<tr>
<td align = "center">
<form name = "filepost" method = "post"
action = "file.php" enctype = "multipart/form-data" id = "file">
<table width = "300" border = "0" cellspacing = "0"
cellpadding = "0">
<tr valign = "bottom">
<td height = "20">Your Name:</td>
</tr>
<tr>
<td><input name = "from" type = "text" id = "from" size = "30"></td>
</tr>
<tr valign = "bottom">
<td height = "20">Your Email Address:</td>
</tr>
<tr>
<td class = "frmtxt2"><input name = "emaila" type = "text" id = "emaila" size = "30"></td>
</tr>
<tr>
<td height = "20" valign = "bottom">Attach File:</td>
</tr>
<tr valign = "bottom">
<td valign = "bottom"><input name = "filea" type = "file" id = "filea" size = "16"></td>
</tr>
<tr>
<td height = "40" valign = "middle">
<input name = "Reset2" type = "reset" id = "Reset2" value = "Reset">
<input name = "Submit2" type = "submit" value = "Submit" onClick = "return CheckData45()">
</td>
</tr>
</table>
</form>
<center>
<table width = "400">
<tr>
<td id = "one"></td>
</tr>
</table>
</center>
</td>
</tr>
</table>
</body>
</html>
它将生成以下 output −
It will produce the following output −
PHP - Sanitize Input
在 PHP 中,重要的是要确保在服务器端代码处理输入数据之前,通过删除任何不需要的字符来正确清理输入数据。通常,用户通过 HTML 表单向 PHP Web 应用程序输入其数据。如果表单数据包含任何不需要的字符,可能会造成损害,因此必须执行适当的清理操作。
In PHP, it is important to ensure that the input data is sanitized properly by removed any undesired characters before it is processed by the server side code. Usually, the users input their data to a PHP web application through a HTML form. If the form data consists of any undesired characters, it may prove to be harmful, hence an appropriate cleansing operation must be performed.
输入清理可以使用 PHP 中以下一个或多个函数的帮助进行。
Input sanitization can be done with the help of one or more of the following functions in PHP.
The htmlspecialchars() Function
此函数将特殊字符转换为 HTML 实体。
This function converts special characters to HTML entities.
htmlspecialchars(
string $string,
int $flags = ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401,
?string $encoding = null,
bool $double_encode = true
): string
在 HTML 中,某些字符有着特殊的意义。这个 htmlspecialchars() 函数用于对 HTML 实体中的特殊字符进行编码。当您希望将用户输入显示为 HTML 时,这将非常有用,而且希望防止脚本注入攻击。
In HTML, certain characters have special significance. This htmlspecialchars() function is used to encode special characters in HTML entities. This is useful when you want to display user input as HTML and want to prevent script injection attacks.
以下 special characters 按下述所示方式翻译 −
The following special characters are translated as shown −
Character |
Replaced by |
& (ampersand) |
& |
" (double quote) |
", unless ENT_NOQUOTES is set |
' (single quote) |
' (for ENT_HTML401) or ' (for ENT_XML1, ENT_XHTML or ENT_HTML5), but only when ENT_QUOTES is set |
< (less than) |
< |
> (greater than) |
> |
Flag Constants
flags 参数是一个或多个以下标志的位掩码,它们指定如何处理引号、无效的代码单元序列和使用的文档类型。
The flags parameter is a bitmask of one or more of the following flags, which specify how to handle quotes, invalid code unit sequences and the used document type.
Sr.No |
Constant & Description |
1 |
ENT_COMPAT Will convert double-quotes and leave single-quotes alone. |
2 |
ENT_QUOTES Will convert both double and single quotes. |
3 |
ENT_NOQUOTES Will leave both double and single quotes unconverted. |
4 |
ENT_IGNORE discard invalid code unit sequences instead of returning an empty string. |
5 |
ENT_SUBSTITUTE Replace invalid code unit sequences with a Unicode Replacement Character U+FFFD (UTF-8) or � |
6 |
ENT_DISALLOWED Replace invalid code points for the given document type with a Unicode Replacement Character U+FFFD (UTF-8) or � (otherwise) instead of leaving them as is. This may be useful. |
7 |
ENT_HTML401 Handle code as HTML 4.01. |
8 |
ENT_XML1 Handle code as XML 1. |
9 |
ENT_XHTML Handle code as XHTML. |
10 |
ENT_HTML5 Handle code as HTML 5. |
The strip_tags() Function
strip_tags() 函数从给定的字符串中删除所有 HTML 和 PHP 标记。
The strip_tags() function removes all the HTML and PHP tags from a given string.
strip_tags(string $string, array|string|null $allowed_tags = null): string
当您希望确保用户输入不包含任何潜在的恶意标记时,这个函数非常有用。
This function is very useful when you want ensure that the user input doesn’t contain any potentially malicious tags.
allowed_tags 参数是一个可选的第二个参数,用于指定不应删除的标记。这些标记可以以字符串的形式给出,也可以作为数组给出。
The allowed_tags parameter is an optional second parameter to specify tags which should not be stripped. These are either given as string, or as an array.
Example
请看以下示例:
Take a look at the following example −
<?php
$text = '<p>Hello World</p><!-- Comment -->
<a href="/test.html">Click Here</a>';
echo strip_tags($text);
echo "\n";
// Allow <p> and <a>
echo strip_tags($text, '<p><a>');
?>
它将生成以下 output −
It will produce the following output −
Hello World
Click Here
Hello World
Click Here
The addslashes() Function
addslashes() 函数向字符串添加反斜杠。
The addslashes() function adds backslashes to a string.
addslashes(string $string): string
此函数返回一个字符串,在需要转义的字符前面添加反斜杠。这些字符为 −
The function returns a string with backslashes added before characters that need to be escaped. These characters are −
-
Single Quote (')
-
Double Quote (")
-
Backslash (\)
-
NUL (The NUL Byte)
在向数据库中存储用户输入且希望防止 SQL 注入攻击时,请使用此函数。
Use this function when you are storing user input in a database and want to prevent SQL injection attacks.
The filter_var() Function
借助特定的过滤器标记,可以使用 filter_var() 函数来净化用户输入。
With the help of a specific filter flag, you can use filter_var() function to sanitize user input.
filter_var(mixed $value, int $filter =
FILTER_DEFAULT, array|int $options = 0): mixed
$value 参数是要净化其值的一个变量。$filter 参数是任何预定义的过滤器常量。
The $value parameter is a variable whose value needs to be sanitized. The $filter parameter is any of the predefined filter constants.
Sr.No |
ID & Description |
1 |
FILTER_SANITIZE_EMAIL Remove all characters except letters, digits and !#$%&'*+-=?^_`{ |
}~@.[]. |
2 |
FILTER_SANITIZE_ENCODED URL-encode string, optionally strip or encode special characters. |
3 |
FILTER_SANITIZE_ADD_SLASHES Apply addslashes(). (Available as of PHP 7.3.0). |
4 |
FILTER_SANITIZE_NUMBER_FLOAT Remove all characters except digits, +- and optionally .,eE. |
5 |
FILTER_SANITIZE_NUMBER_INT Remove all characters except digits, plus and minus sign. |
6 |
FILTER_SANITIZE_SPECIAL_CHARS HTML-encode '"<>& and characters with ASCII value less than 32, optionally strip or encode other special characters. |
7 |
FILTER_SANITIZE_FULL_SPECIAL_CHARS Equivalent to calling htmlspecialchars() with ENT_QUOTES set. Encoding quotes can be disabled by setting FILTER_FLAG_NO_ ENCODE_QUOTES. |
8 |
FILTER_SANITIZE_URL Remove all characters except letters, digits and $-_.+!*'(),{} |
\\^~[]`<>#%";/?:@&=. |
9 |
FILTER_UNSAFE_RAW |
PHP - Post-Redirect-Get (PRG)
在 PHP 中,PRG 代表“Post/Redirect/Get”。这是一种常用的技术,设计用于防止提交表单之后再次提交该表单。您可以在 PHP 中轻松实施此技术以避免重复提交表单。
In PHP, PRG stands for "Post/Redirect/Get". It is a commonly used technique that is designed to prevent the resubmission of a form after it’s been submitted. You can easily implement this technique in PHP to avoid duplicate form submissions.
通常,HTML 表单使用 POST 方法向服务器发送数据。服务器脚本获取数据以进行进一步处理,如在后端数据库中添加新记录或运行查询以获取数据。如果用户意外地刷新浏览器,则有可能再次重新提交相同的表单数据,可能导致数据完整性丢失。PHP 中的 PRG 方法帮助您避免这种陷阱。
Usually a HTML form sends data to the server with the POST method. The server script fetches the data for further processing like adding a new record in a backend database, or running a query to fetch data. If the user accidentally refreshes the browser, there is a possibility of the same form data being resubmitted again, possibly leading to loss of data integrity. The PRG approach in PHP helps you avoid this pitfall.
Example
首先,我们考虑以下 PHP 脚本,它渲染一个简单的 HTML 表单,并使用 POST 方法将其提交回自身。当用户填写数据并提交时,后端脚本会获取数据,渲染结果,然后返回以再次显示空白表单。
To start with, let us consider the following PHP script that renders a simple HTML form, and submits it back to itself with POST method. When the user fills the data and submits, the backend script fetches the data, renders the result, and comes back to show the blank form again.
<?php
if (isset($_POST["submit"])) {
if ($_SERVER["REQUEST_METHOD"] == "POST")
echo "First name: " . $_REQUEST['first_name'] . " " . "Last Name: " . $_REQUEST['last_name'] . "";
}
?>
<html>
<body>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
First Name: <input type="text" name="first_name"> <br/>
Last Name: <input type="text" name="last_name" />
<button type="submit" name="submit">Submit</button>
</form>
</body>
</html>
假设服务器正在运行,则将上述脚本放置在文档根文件夹中并在浏览器中访问。
Assuming that the server is running, the above script is placed in the document root folder and visited in the browser.
填写数据并提交。浏览器会回显结果,然后重新渲染表单。现在,如果您尝试刷新浏览器页面,将会弹出一个警告,如下所示 −
Fill the data and submit. The browser echoes the result, and re-renders the form. Now if you try to refresh the browser page, a warning pops up as shown below −
如果您按 Continue ,则会再次发布相同数据。
If you press Continue, the same data is posted again.
可以使用以下图形理解问题 −
The problem can be understood with the following figure −
在 PHP 脚本中执行以下步骤以避免问题 −
Following steps are taken in the PHP script to avoid the problem −
-
The PHP script before the HTML form starts a new session.
-
Check if the form has been submitted with POST method.
-
If so, store the form data in session variables
-
Redirect the browser to a result page. In our case, it is the same page. With the exit command, to terminate this script to make sure no more code gets executed.
-
If PHP finds that the REQUEST method is not POST, it checks if the session variables are set. If so, they are rendered along with the fresh copy of form.
-
Now even if the form is refreshed, you have successfully averted the possibility of resubmission.
Example
下面是使用 PRG 技术的 PHP 代码−
Here is the PHP code that uses the PRG technique −
<?php
session_start();
if (isset($_POST["submit"])) {
$_SESSION['fname'] = $_POST['first_name'];
$_SESSION['lname'] = $_POST['last_name'];
header("Location: hello.php");
exit;
}
if (isset($_SESSION["fname"])) {
echo "First name: " . $_SESSION['fname'] . " " . "Last Name: " . $_SESSION['lname'] . "";
unset($_SESSION["fname"]); unset($_SESSION["lname"]);
}
?>
<html>
<body>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
First Name: <input type="text" name="first_name"> <br />
Last Name: <input type="text" name="last_name" />
<button type="submit" name="submit">Submit</button>
</form>
</body>
</html>
PHP - Flash Messages
在 PHP Web 应用程序中, Message flashing 指的是使某些消息在浏览器窗口中弹出,以便用户接收应用程序反馈的技术。能够向用户提供与其交互的有意义的反馈是一项重要的设计原则,可以提供更好的用户体验。
Message flashing in a PHP web application refers to the technique that makes certain messages popup on the browser window for the user to receive application’s feedback. To be able to give the user a meaningful feedback to his interactions is an important design principle, that gives a better user experience.
在 PHP Web 应用程序中,我们可以使用会话数据不定期地针对特定操作、通知或警告等内容发出成功或失败的闪存消息,以便向用户随时通报相关信息。
In a PHP web application, we can use the session data to flash messages regarding success or failure of a certain action, notifications or warnings, etc., from time to time to keep the user informed.
flash message 使您能在一页上创建消息,然后在另一页上显示一次此消息。如要从一页向另一页传输消息,请使用 $_SESSION 超全局变量。
A flash message allows you to create a message on one page and display it once on another page. To transfer a message from one page to another, you use the $_SESSION superglobal variable.
首先,请按如下方式向 $_SESSION 数组添加一个变量 -
To start with, you add a variable to the $_SESSION array as follows −
<?php
session_start();
$_SESSION['flash_message'] = "Hello World";
?>
稍后,导航到另一页,然后从 $_SESSION 变量中检索闪存消息并将其分配给一个变量。随后,您可以显示消息,然后从 $_SESSION 中删除该消息 -
Later, navigate to another page, and retrieve the flashed message from the $_SESSION variable and assign it to a variable. Then, you can display the message and then delete the message from the $_SESSION −
<?php
session_start();
if(isset($_SESSION['flash_message'])) {
$message = $_SESSION['flash_message'];
unset($_SESSION['flash_message']);
echo $message;
}
?>
为了概括处理闪存消息的基本思路,我们将编写一个将消息添加到 $_SESSION 中的函数 -
To generalize the basic idea of handling the flashed messages, we shall write a function that adds a message to the $_SESSION −
session_start();
function create_flash_message(string $name, string $message): void {
// remove existing message with the name
if (isset($_SESSION[FLASH][$name])) {
unset($_SESSION[FLASH][$name]);
}
// add the message to the session
$_SESSION[FLASH][$name] = ['message' => $message];
}
让我们也编写另一个函数,该函数回读一条消息、在浏览器上闪烁并从 $_SESSION 中移除此消息。
Let us also have another function that reads back a message, flashes it on the browser, and removes it from the $_SESSION.
function display_flash_message(string $name): void {
if (!isset($_SESSION[FLASH][$name])) {
return;
}
// get message from the session
$flash_message = $_SESSION[FLASH][$name];
// delete the flash message
unset($_SESSION[FLASH][$name]);
// display the flash message
echo format_flash_message($flash_message);
}
format_flash_message() 函数通过适当的 CSS 规则对获得的字符串应用所需的格式设置。
The format_flash_message() function applies desired formatting to the obtained string with appropriate CSS rules.
如果应用程序闪现了多条消息,则可以使用以下示例检索并闪现所有这些消息 -
If there are more than messages that have been flashed by the application, all of them can be retrieved and flashed with the following example −
function display_all_flash_messages(): void {
if (!isset($_SESSION[FLASH])) {
return;
}
// get flash messages
$flash_messages = $_SESSION[FLASH];
// remove all the flash messages
unset($_SESSION[FLASH]);
// show all flash messages
foreach ($flash_messages as $flash_message) {
echo format_flash_message($flash_message);
}
}
使用以下 flash() function 创建、设置格式并闪现消息
Use the following flash() function to create, format and flash the messages
function flash(string $name = '', string $message = ''): void {
if ($name !== '' && $message !== '') {
create_flash_message($name, $message);
}
elseif ($name !== '' && $message === '') {
display_flash_message($name); // display a flash message
} elseif ($name === '' && $message === '' ) {
display_all_flash_messages(); // display all flash message
}
}
如要实现上述方法,请在第一页上调用 flash() function 。
To implement the above method, call the flash() function on the first page.
flash('first', 'Hello World');
导航到另一页并调用 flash() 函数来检索并显示消息 -
Navigate to another page and call the flash() function to retrieve and display the message −
flash('first');
闪存消息的使用机制通常用于注册页面,以便在用户注册后将他们重定向到登录页面并显示一条欢迎消息。
Mechanism of using the flash messages is usually employed on a signup page to redirect users to the login page with a welcome message after they sign up.
PHP - AJAX Introduction
由 PHP 提供支持的 Web 应用程序通常使用 AJAX,两者联合起来可用来创建动态且交互式的 Web 应用程序。AJAX 代表 Asynchronous Javascript and XML 。它允许网页异步更新,而无需重新加载整个页面。
PHP powered web applications often make use of AJAX, together they are useful to create dynamic and interactive web applications. AJAX stands for Asynchronous Javascript and XML. It allows webpages to be updated asynchronously without reloading the entire page.
在 AJAX 应用程序中,Web 浏览器与服务器端 PHP 脚本之间的数据交换是异步的。PHP 是一种服务器端脚本语言,可用于生成动态内容并处理数据。
In AJAX applications, the exchange of data between a web browser and the server-side PHP script is asynchronous. PHP is a server-side scripting language that can be used to generate dynamic content and process data.
由于我们可以使用 JavaScript 执行后台服务器调用并检索所需数据,并且可以在不强制页面完全重新加载的情况下更新网页的请求部分,所以 AJAX 在 Web 应用程序与 Web 服务器之间创建了一层附加层(称为 AJAX 引擎)。它缩短了页面刷新时间,并且为用户提供了迅速而灵敏的体验。
AJAX creates an additional layer known as AJAX engine in between the web application and web server due to which we can make background server calls using JavaScript and retrieve the required data, can update the requested portion of a web page without casing full reload of the page. It reduces the page refresh timing and provides a fast and responsive experience to the user.
What is Required to Run AJAX?
AJAX 使用的技术已经在所有现代浏览器中实现。因此,客户端不需要任何额外的模块来运行 AJAX 应用程序。AJAX 使用的技术有 −
The technologies that are used by AJAX are already implemented in all the Morden browsers. So the client does not require any extra module to run the AJAX application. The technologies used by AJAX are −
-
Javascript − It is an important part of AJAX. It allows you to create client-side functionality. Or we can say that it is used to create AJAX applications.
-
XML − It is used to exchange data between web server and client.
-
The XMLHttpRequest − It is used to perform asynchronous data exchange between a web browser and a web server.
-
HTML and CSS − It is used to provide markup and style to the webpage text.
-
DOM − It is used to interact with and alter the webpage layout and content dynamically.
在 PHP 中使用 AJAX,将需要使用 JavaScript 中的 XMLHttpRequest 对象向 PHP 服务器发送请求。然后 PHP 服务器将处理请求并返回响应,通常以 JSON 或 XML 的形式。随后 JavaScript 代码能够分析响应并相应地更新网页。
To use AJAX with PHP, you will need to use the XMLHttpRequest object in JavaScript to send requests to the PHP server. The PHP server will then process the request and return a response, typically in the form of JSON or XML. The JavaScript code can then parse the response and update the web page accordingly.
JavaScript 中的 XMLHttpRequest 对象是基于浏览器的 API,允许开发者向服务器发出 HTTP 请求而无需刷新该页面。这是 AJAX 编程的基础,这允许动态且互动的 Web 应用。
The XMLHttpRequest object in JavaScript is a browser-based API that allows developers to make HTTP requests to a server without reloading the page. This is the foundation of AJAX programming, which allows for dynamic and interactive web applications.
XMLHttpRequest 对象可以用来 −
The XMLHttpRequest object can be used to −
-
Retrieve data from a server, such as JSON, XML, or HTML.
-
Send data to a server, such as form data or file uploads.
-
Update a web page without reloading it.
-
Create chat applications and other interactive features.
要使用 XMLHttpRequest 对象,首先需要创建一个新实例。然后,可以使用 open() 方法指定 HTTP 方法和请求 URL。下一步,可以设置任何请求头(如果需要的话)。最后,可以使用 send() 方法发送请求。
To use the XMLHttpRequest object, you first need to create a new instance of it. Then, you can use the open() method to specify the HTTP method and request URL. Next, you can set any request headers, if needed. Finally, you can send the request using the send() method.
Example
以下是如何使用 XMLHttpRequest 对象从服务器检索数据的简单 JavaScript 代码 −
Here is a simple JavaScript code of how to use the XMLHttpRequest object to retrieve data from a server −
// Create a new XMLHttpRequest object
var xhr = new XMLHttpRequest();
// Set the HTTP method and request URL
xhr.open("GET", "test.php");
// Send the request
xhr.send();
// Listen for the onload event to be fired
xhr.onload = function() {
// Check the status code to ensure the request was successful
if (xhr.status === 200) {
// Get the response data.
var users = JSON.parse(xhr.responseText);
// Do something with the user data.
} else {
// Handle the error
}
};
服务器上的 PHP 脚本从 AJAX 请求中检索数据并发送回响应。
The PHP script on the server retrieves the data from AJAX request and sends back the response.
// Get the request data.
$name = $_GET["name"];
// Create a response object.
$response = new stdClass();
$response->message = "Hello, $name!";
// Send the response back to the client.
header("Content-Type: application/json");
echo json_encode($response);
PHP - AJAX Search
AJAX 是术语异步 JavaScript 和 XML 的缩写。Ajax 用于构建快速而动态的网页。下面的示例演示了如何使用 AJAX 函数与后端 PHP 脚本交互,以便在网页上提供一个搜索字段。
AJAX is a shortform of the term Asynchronous JavaScript and XML. Ajax is used to build fast and dynamic web pages. Below example demonstrates interaction with the backend PHP script with AJAX functions to provide a search field on the webpage.
Step 1
将以下脚本另存为“example.php”−
Save the following script as "example.php" −
<html>
<head>
<style>
span {
color: green;
}
</style>
<script>
function showHint(str) {
if (str.length == 0) {
document.getElementById("txtHint").innerHTML = "";
return;
} else {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("GET", "hello.php?q=" + str, true);
xmlhttp.send();
}
}
</script>
</head>
<body>
<p><b>Search your favourite tutorials:</b></p>
<form>
<input type = "text" onkeyup = "showHint(this.value)">
</form>
<p>Entered Course name: <span id="txtHint"></span></p>
</body>
</html>
此代码本质上是一个 HTML 脚本,该脚本呈现一个带有文本字段的 HTML 表单。在其 onkeyup 事件中,调用 showHint() JavaScript 函数。函数向服务器上的另一个 PHP 脚本发送 HTTP GET 请求。
This code is essentially a HTML script that renders a HTML form with a text field. On its onkeyup event, a showHint() JavaScript function is called. The function sends a HTTP GET request to another PHP script on the server.
Step 2
把以下脚本另存为“php_ajax.php”−
Save the following script as "php_ajax.php" −
<?php
// Array with names
$a[] = "Android";
$a[] = "B programming language";
$a[] = "C programming language";
$a[] = "D programming language";
$a[] = "euphoria";
$a[] = "F#";
$a[] = "GWT";
$a[] = "HTML5";
$a[] = "ibatis";
$a[] = "Java";
$a[] = "K programming language";
$a[] = "Lisp";
$a[] = "Microsoft technologies";
$a[] = "Networking";
$a[] = "Open Source";
$a[] = "Prototype";
$a[] = "QC";
$a[] = "Restful web services";
$a[] = "Scrum";
$a[] = "Testing";
$a[] = "UML";
$a[] = "VB Script";
$a[] = "Web Technologies";
$a[] = "Xerox Technology";
$a[] = "YQL";
$a[] = "ZOPL";
$q = $_REQUEST["q"];
$hint = "";
if ($q !== "") {
$q = strtolower($q);
$len = strlen($q);
foreach($a as $name) {
if (stristr($q, substr($name, 0, $len))) {
if ($hint === "") {
$hint = $name;
} else {
$hint .= ", $name";
}
}
}
}
echo $hint === "" ? "Please enter a valid course name" : $hint;
?>
Step 3
我们将通过输入网址 http://localhost/example.php 在浏览器中打开 example.php 来启动此应用程序
We will start this application by opening example.php in the browser by entering the URL http://localhost/example.php
在搜索字段中每次击键时,一个 GET 请求都会发送到服务器。服务器脚本从 $_REQUEST 数组中读取字符并搜索与其匹配的课程名称。匹配的值显示在文本字段下方的浏览器中。
On every keystroke in the search field, a GET request goes to the server. The server script reads the character from $_REQUEST array and searches for the course name that matches. The matched value is displayed below the text field in the browser.
PHP - AJAX XML Parser
使用 PHP 和 AJAX,我们可以解析本地目录和服务器上的 XML 文档。以下示例演示了如何用网络浏览器解析 XML。
Using PHP with AJAX, we can parse an XML document from local directory as well as on a server. The following example demonstrates how to parse XML with web browser.
客户端脚本呈现了一个 HTML 表单,并定义了一个 JavaScript 函数,用于通过 XMLHttpRequest 对象向服务器发送 HTTP 请求。
The client-end script renders a HTML form and defines a JavaScript function for sending a HTTP request to the server with XMLHttpRequest object.
在服务器上,一个 PHP 脚本从所需的 XML 文档加载 DOM 对象,从 $_REQUEST 变量中获取所选课程,并将已选课程的详细信息呈现为对客户端的响应。
On the server, a PHP script loads the DOM object from the required XML document, fetches the selected course from $_REQUEST variable, and renders the details of the course chosen as the response back to the client.
Step 1
以下 XML 文档存储在 XAMPP 服务器的文档根目录中。
The following XML document is stored on the document root of the XAMPP server.
<?xml version = "1.0" encoding = "utf-8"?>
<CATALOG>
<SUBJECT>
<COURSE>Android</COURSE>
<COUNTRY>India</COUNTRY>
<COMPANY>TutorialsPoint</COMPANY>
<PRICE>$10</PRICE>
<YEAR>2015</YEAR>
</SUBJECT>
<SUBJECT>
<COURSE>Html</COURSE>
<COUNTRY>India</COUNTRY>
<COMPANY>TutorialsPoint</COMPANY>
<PRICE>$15</PRICE>
<YEAR>2015</YEAR>
</SUBJECT>
<SUBJECT>
<COURSE>Java</COURSE>
<COUNTRY>India</COUNTRY>
<COMPANY>TutorialsPoint</COMPANY>
<PRICE>$20</PRICE>
<YEAR>2015</YEAR>
</SUBJECT>
<SUBJECT>
<COURSE>Microsoft</COURSE>
<COUNTRY>India</COUNTRY>
<COMPANY>TutorialsPoint</COMPANY>
<PRICE>$25</PRICE>
<YEAR>2015</YEAR>
</SUBJECT>
</CATALOG>
Step 2
下面的 AJAX 代码有一个 HTML 表单和一个 JavaScript 函数,通过 XMLHttpRequest 对象提出 HTTP 请求。
The AJAX code below has a HTML form and a JavaScript function to raise HTTP request through XMLHttpRequest object.
<html>
<head>
<script>
function showCD(str) {
if (str == "") {
document.getElementById("txtHint").innerHTML = "";
return;
}
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("GET","hello.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>
<form>
Select a Course:
<select name = "cds" onchange = "showCD(this.value)">
<option value = "">Select a course:</option>
<option value = "Android">Android </option>
<option value = "Html">HTML</option>
<option value = "Java">Java</option>
<option value = "Microsoft">MS technologies</option>
</select>
</form>
<div id = "txtHint"><b>Course info will be listed here...</b></div>
</body>
</html>
Step 3
搜索 XML 文档的服务器端 PHP 脚本如下 −
The server-side PHP script to search within the XML document is as follows −
<?php
$q = $_GET["q"];
$xmlDoc = new DOMDocument();
$xmlDoc->load("test.xml");
$x = $xmlDoc->getElementsByTagName('COURSE');
for ($i = 0; $i<=$x->length-1; $i++) {
if ($x->item($i)->nodeType == 1) {
if ($x->item($i)->childNodes->item(0)->nodeValue == $q) {
$y = ($x->item($i)->parentNode);
}
}
}
$cd = ($y->childNodes);
for ($i = 0;$i<$cd->length;$i++) {
if ($cd->item($i)->nodeType == 1) {
echo("<b>" . $cd->item($i)->nodeName . ":</b> ");
echo($cd->item($i)->childNodes->item(0)->nodeValue);
echo("<br>");
}
}
?>
访问“http://localhost/example.php”来让用户选择课程。在选择后,相关详细信息从服务器中获取并显示如下 −
Visit "http://localhost/example.php" to let the user select a course. Upon selection, the relevant details are fetched from the server and displayed as below −
PHP - AJAX Auto Complete Search
自动完成功能是一种预测机制,当用户在提供的搜索框中输入数据时显示输入建议。它也被称为实时搜索,因为它对用户的输入作出反应。在本例中,我们将使用 PHP 中的 AJAX 和 XML 解析器来演示自动完成文本框的使用。
Autocomplete feature is a typeahead mechanism to show input suggestion as the user enters data in the search box provided. It is also called live search because it reacts to the users' input. In this example, we shall use AJAX and XML parser in PHP to demonstrate the use of auto complete text box.
此应用程序有三个主要部分 −
This application has three main constituents −
-
The XML Document
-
JavaScript Code
-
XML Parser in PHP
现在让我们详细讨论这三个部分 −
Let us now discuss these three constituents in detail −
The XML Document
将以下 XML 脚本另存为文档根目录中的“ autocomplete.xml ”
Save the following XML script as "autocomplete.xml" in the document root folder
<?xml version = "1.0" encoding = "utf-8"?>
<pages>
<link>
<title>android</title>
<url>https://www.tutorialspoint.com/android/index.htm</url>
</link>
<link>
<title>Java</title>
<url>https://www.tutorialspoint.com/java/index.htm</url>
</link>
<link>
<title>CSS </title>
<url>https://www.tutorialspoint.com/css/index.htm</url>
</link>
<link>
<title>angularjs</title>
<url>https://www.tutorialspoint.com/angularjs/index.htm </url>
</link>
<link>
<title>hadoop</title>
<url>https://www.tutorialspoint.com/hadoop/index.htm </url>
</link>
<link>
<title>swift</title>
<url>https://www.tutorialspoint.com/swift/index.htm </url>
</link>
<link>
<title>ruby</title>
<url>https://www.tutorialspoint.com/ruby/index.htm </url>
</link>
<link>
<title>nodejs</title>
<url>https://www.tutorialspoint.com/nodejs/index.htm </url>
</link>
</pages>
JavaScript Code
以下脚本为用户呈现一个文本字段,用于输入他选择的课程名称。每次击键时,都会调用一个 JavaScript 函数,并将输入值以 GET 方法传给服务器端 PHP 脚本。服务器响应将异步呈现。
The following script renders a text field for the user to enter a course name of his choice. On every keystroke a JavaScript function is called, and the input value is passed to the server-side PHP script with GET method. The server’s response is asynchronously rendered.
将此代码另存为“ index.php ”。
Save this code as "index.php".
<html>
<head>
<script>
function showResult(str) {
if (str.length == 0) {
document.getElementById("livesearch").innerHTML = "";
document.getElementById("livesearch").style.border = "0px";
return;
}
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
} else {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("livesearch").innerHTML = xmlhttp.responseText;
document.getElementById("livesearch").style.border = "1px solid #A5ACB2";
}
}
xmlhttp.open("GET","livesearch.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>
<form>
<h2>Enter Course Name</h2>
<input type = "text" size = "30" onkeyup = "showResult(this.value)">
<div id = "livesearch"></div>
<a href = "https://www.tutorialspoint.com">More Details</a>
</form>
</body>
</html>
XML Parser in PHP
这是服务器上的 PHP 脚本。它解析给定的 XML 源文档,读取输入字段中输入的字符,在已解析的 XNL 对象中进行搜索,然后将响应发送回。
This the PHP script on the server. It parses the given XML source document, reads the characters entered in the input field, searches for it in the parsed XNL object, and sends back the response.
将以下代码另存为“livesearch.php”。
Save the following code as "livesearch.php".
<?php
$xml_doc = new DOMDocument();
$xml_doc->load('autocomplete.xml');
$x=$xml_doc->getElementsByTagName('link');
$q = $_GET['q'];
$result = '';
foreach($x as $node) {
if (stripos("{$node->nodeValue}", $q) !== false) {
$result .= "{$node->nodeValue}";
}
}
// Set $response to "No records found." in case no hint was found
// or the values of the matching values
if ($result == '')
$result = 'No records found.';
// show the results or "No records found."
echo $result;
?>
在 XAMPP 服务器运行的情况下,访问“http://localhost/index.php”,浏览器会显示一个输入文本字段。对于在其中键入的每个字符,相关的建议都会出现在其下面。
With the XAMPP server running, visit "http://localhost/index.php" and the browser displays a input text field. For each character typed in it, the relevant suggestions appear below it.
PHP - AJAX RSS Feed Example
Really Simple Syndication (RSS)
RSS(Really Simple Syndication 的缩写)可用于发布网站的经常更新信息,如音频、视频、图像等。我们可以使用 AJAX 和 PHP 将 RSS 提要集成到网站中。此代码演示了如何在我们的站点中显示 RSS 提要。
RSS, which stands for Really Simple Syndication, is used to publish often updated information from website like audio, video, images, etc. We can integrate RSS feeds to a website by using AJAX and PHP. This code demonstrates how to show RSS feeds in our site.
Index.html
索引页应如下所述 −
The index page should be as follows −
<html>
<head>
<script>
function showRSS(str) {
if (str.length == 0) {
document.getElementById("output").innerHTML = "";
return;
}
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
} else {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("output").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("GET","rss.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>
<p>Please Select an option to get RSS:</p>
<form>
<select onchange = "showRSS(this.value)">
<option value = "">Select an RSS-feed:</option>
<option value = "cnn">CNN</option>
<option value = "bbc">BBC News</option>
<option value = "pc">PC World</option>
</select>
</form>
<br>
<div id = "output">RSS-feeds</div>
</body>
</html>
rss.php
“rss.php”中包含有关如何访问 RSS Feeds RSS 提要并将 RSS 提要返回到网页的语法。
"rss.php" has contained syntax about how to get access to RSS Feeds RSS Feeds and return RSS Feeds to the webpages.
<?php
$q = $_GET["q"];
if($q == "cnn") {
$xml = ("http://rss.cnn.com/rss/cnn_topstories.rss");
} elseif($q == "bbc") {
$xml = ("http://newsrss.bbc.co.uk/rss/newsonline_world_edition/americas/rss.xml");
} elseif($q = "pcw"){
$xml = ("http://www.pcworld.com/index.rss");
}
$xmlDoc = new DOMDocument();
$xmlDoc->load($xml);
$channel = $xmlDoc->getElementsByTagName('channel')->item(0);
$channel_title = $channel->getElementsByTagName('title')
->item(0)->childNodes->item(0)->nodeValue;
$channel_link = $channel->getElementsByTagName('link')
->item(0)->childNodes->item(0)->nodeValue;
$channel_desc = $channel->getElementsByTagName('description')
->item(0)->childNodes->item(0)->nodeValue;
echo("<p><a href = '" . $channel_link . "'>" .
$channel_title . "</a>");
echo("<br>");
echo($channel_desc . "</p>");
$x = $xmlDoc->getElementsByTagName('item');
for ($i = 0; $i<=2; $i++) {
$item_title = $x->item($i)->getElementsByTagName('title')
->item(0)->childNodes->item(0)->nodeValue;
$item_link = $x->item($i)->getElementsByTagName('link')
->item(0)->childNodes->item(0)->nodeValue;
$item_desc = $x->item($i)->getElementsByTagName('description')
->item(0)->childNodes->item(0)->nodeValue;
echo ("<p><a href = '" . $item_link . "'>" . $item_title . "</a>");
echo ("<br>");
echo ($item_desc . "</p>");
}
?>
它将生成如下输出:
It will produce the following output −
PHP - XML Introduction
借助 PHP 的内置函数和库,我们可以处理 XML 数据的处理。XML(eXtensible Markup Language 的缩写)是一种用于结构化文档交换的数据格式,特别是在网络上。
With the help of PHP’s built-in functions and libraries, we can handle manipulation of XML data. XML, which stands for eXtensible Markup Language, is a data format for structured document interchange, especially on the Web.
XML 是一种流行的文件格式,用于存储数据的序列化,将其传输到另一个位置并在目的地重建它。
XML is a popular file format used for serialization of data storing the data, transmitting it to another location, and reconstructing it at the destination.
在本章中,我们将学习使用 PHP 进行 XML 处理的基础知识。
In this chapter, we shall learn about the basics of XML processing with PHP.
Features of XML
XML 的一个特点是它既可读,又可机器读。XML 的规范由万维网联盟定义和标准化。PHP 解析器可以在 XML 数据上执行读/写操作。
One of the features of XML is that it is both human readable and machine readable. The specifications of XML are defined and standardized by The World Wide Web Consortium. PHP parser can perform read/write operations on XML data.
XML Tags
与 HTML 类似,XML 文档也是借助 tags 组成的。然而,你可以定义自己的标签,这不像 HTML,在其中你需要使用预定义的标签才能编写 HTML 文档。
Like HTML, XML document is also composed with the help of tags. However, you can define your own tags, which is unlike HTML where you need to use predefined tags to compose a HTML document.
HTML 标签本质上应用于文本、图像、多媒体资源等的格式化属性。XML 标签为数据元素定义用户指定的属性。
The HTML tags essentially apply formatting attributes over text, image, multimedia resources etc. The XML tags define user specified attributes to the data elements.
XML Document
XML 文档具有标签的层次结构,这些标签定义文档中数据的元素和属性。每个 XML 文档都包含一个根元素,它封装了其他元素。元素可以具有属性,这些属性提供有关元素的其他信息或特性。元素中的数据通过开始和结束标签来封装。
An XML document has a hierarchical structure of tags that define the elements and attributes of data within a document. Each XML document consists of a root element that encloses other elements. Elements can have attributes, which provide additional information or properties about the element. The data within elements are enclosed by opening and closing tags.
Types of XML Parsers
在 PHP 中,有两种类型的 XML 解析器 −
In PHP, there are two types of XML parsers available −
-
Tree based parsers
-
Event based parsers
Tree-based Parsers
使用这种类型的解析器,PHP 将在内存中加载整个 XML 文档,并将 XML 文档转换为树结构。它分析整个文档,并为树元素提供访问权限。
With this type of a parser, PHP loads the entire XML document in the memory and transforms the XML document into a Tree structure. It analyzes the whole document, and provides access to the Tree elements.
For smaller documents, tree-based parser works well ,但对于大型 XML 文档,它会导致重大的性能问题。 SimpleXML parser 和 DOM XML parser 是基于树的解析器的示例
For smaller documents, tree-based parser works well, but for large XML document, it causes major performance issues. SimpleXML parser and DOM XML parser are the examples of tree-based parsers
Simple XML Parser
简单 XML 解析器也称为基于树的 XML 解析器,它将解析简单的 XML 文件。简单 XML 解析器将调用 simplexml_load_file() 方法以从特定路径获取 xml 的访问权限。
The Simple XML parser also called as tree-based XML parser and it will parse the simple XML file. Simple XML parse will call simplexml_load_file() method to get access to the xml from specific path.
DOM Parser
DOM 解析器也称为复杂节点解析器,用于解析高度复杂的 XML 文件。它用作修改 XML 文件的界面。DOM 解析器已使用 UTF-8 字符编码进行编码。
DOM Parser also called as a complex node parser, Which is used to parse highly complex XML file. It is used as interface to modify the XML file. DOM parser has encoded with UTF-8 character encoding.
Event-based Parsers
基于事件的解析器不会在内存中加载整个 XML 文档。相反,它一次读取一个节点。解析器允许你实时交互。一旦你移到下一个节点,以前的那个节点就会从内存中移除。
An event-based parser doesn’t load the entire XML document in the memory. instead, it reads in one node at a time. The parser allows you to interact with in real time. Once you move onto the next node, the old one is removed from the memory.
由于没有涉及内存过载,因此这种类型的解析器适用于大型 XML 文档,并且解析文档的速度比任何基于树的解析器都要快。XMLReader 和 XML Expat Parser 是基于事件的解析器的示例。
As there is no memory overload involved, this type of parser is suitable for large XML documents, and the document is parsed faster than any tree-based parser. XMLReader and XML Expat Parser are the examples of event-based parsers.
XML Parser
XML 解析基于 SAX 解析。它比所有上述解析器都要快。它将创建 XML 文件并解析 XML。XML 解析器已由 ISO-8859-1、US-ASCII 和 UTF-8 字符编码编码。
XML parsing is based on SAX parse. It is more faster the all above parsers. It will create the XML file and parse the XML. XML parser has encoded by ISO-8859-1, US-ASCII and UTF-8 character encoding.
PHP - Simple XML Parser
PHP 的 SimpleXML 扩展提供了非常简单易用的工具集,用于将 XML 转换为可以使用普通属性选择器和数组迭代器进行处理的对象。它是一个基于树的解析器,适用于简单的 XML 文件,但是在处理更大且复杂的 XML 文档时可能会遇到问题。
The SimpleXML extension of PHP provides a very simple and easy to use toolset to convert XML to an object that can be processed with normal property selectors and array iterators. It is a tree_based parser, and works well with simple XML files, but may face issues when working with larger and complex XML documents.
SimpleXML 扩展中定义了以下函数:
The following functions are defined in SimpleXML extension −
simplexml_load_file
simplexml_load_file() 函数将 XML 文件解释为一个对象:
The simplexml_load_file() function interprets an XML file into an object −
simplexml_load_file(
string $filename,
?string $class_name = SimpleXMLElement::class,
int $options = 0,
string $namespace_or_prefix = "",
bool $is_prefix = false
): SimpleXMLElement|false
给定文件中的格式良好的 XML 文档将转换为一个对象。
A well-formed XML document in the given file is converted into an object.
filename 参数是一个表示要解析的 XML 文件的字符串。class_name 是一个可选参数。它指定函数将返回其对象的类。该函数返回一个 SimpleXMLElement 类对象,其中属性包含 XML 文档中保存的数据,或者在失败时返回 false 。
The filename parameter is a string representing the XML file to be parsed. class_name is the optional parameter. It specifies the class whose object will be returned by the function. The function returns an object of class SimpleXMLElement with properties containing the data held within the XML document, or false on failure.
Example
请看以下示例:
Take a look at the following example −
<?php
$xml = simplexml_load_file("test.xml") or die("Error: Cannot create object");
print_r($xml);
?>
它将生成以下 output −
It will produce the following output −
SimpleXMLElement Object
(
[Course] => Android
[Subject] => Android
[Company] => TutorialsPoint
[Price] => $10
)
simplexml_load_string
simplexml_load_string() 函数将 XML 文件解释为一个对象。
The simplexml_load_string() function interprets an XML file into an object.
simplexml_load_string(
string $filename,
?string $class_name = SimpleXMLElement::class,
int $options = 0,
string $namespace_or_prefix = "",
bool $is_prefix = false
): SimpleXMLElement|false
给定字符串中的格式良好的 XML 文档将转换为一个对象。
A well-formed XML document in the given string is converted into an object.
$data 参数是一个表示要解析的 XML 文档的字符串。 class_name 是一个可选参数。它指定函数将返回其对象的类。该函数返回一个 SimpleXMLElement 类对象,其中属性包含 XML 文档中保存的数据,或者在失败时返回 false 。
The $data parameter is a string representing the XML document to be parsed. class_name is the optional parameter. It specifies the class whose object will be returned by the function. The function returns an object of class SimpleXMLElement with properties containing the data held within the XML document, or false on failure.
Example
请看以下示例:
Take a look at the following example −
<?php
$data = "<?xml version = '1.0' encoding = 'UTF-8'?>
<note>
<Course>Android</Course>
<Subject>Android</Subject>
<Company>TutorialsPoint</Company>
<Price>$10</Price>
</note>";
$xml = simplexml_load_string($data) or die("Error: Cannot create object");
print_r($xml);
?>
它将生成以下 output −
It will produce the following output −
SimpleXMLElement Object
(
[Course] => Android
[Subject] => Android
[Company] => TutorialsPoint
[Price] => $10
)
simplexml_import_dom
simplexml_import_dom() 函数从 DOM 节点构造一个 SimpleXMLElement 对象。
The simplexml_import_dom() function constructs a SimpleXMLElement object from a DOM node.
simplexml_import_dom(SimpleXMLElement|DOMNode $node, ?string
$class_name = SimpleXMLElement::class): ?SimpleXMLElement
该函数获取 DOM 文档的一个节点,并将其转化为一个 SimpleXML 节点。然后,这个新对象可以用作原生 SimpleXML 元素。
This function takes a node of a DOM document and makes it into a SimpleXML node. This new object can then be used as a native SimpleXML element.
node 参数是一个 DOM Element 节点。可以给出可选的 class_name,以便 simplexml_import_dom() 返回 SimpleXMLElement 类的指定子类的对象。该函数返回的值是一个 SimpleXMLElement 或 (在失败时) null。
The node parameter is a DOM Element node. The optional class_name may be given so that simplexml_import_dom() will return an object of the specified sub class of the SimpleXMLElement class. The value returned by this function is a SimpleXMLElement or null on failure.
Example
请看以下示例:
Take a look at the following example −
<?php
$dom = new DOMDocument;
$dom->loadXML('<books><book><title>PHP Handbook</title></book></books>');
if (!$dom) {
echo 'Error while parsing the document';
exit;
}
$s = simplexml_import_dom($dom);
echo $s->book[0]->title;
?>
它将生成以下 output −
It will produce the following output −
PHP Handbook
Get the Node Values
以下代码演示了如何从 XML 文件获取节点值,XML 应如下所示:
The following code shows how to get the node values from an XML file and the XML should be as follows −
<?xml version = "1.0" encoding = "utf-8"?>
<tutorialspoint>
<course category = "JAVA">
<title lang = "en">Java</title>
<tutor>Gopal</tutor>
<duration></duration>
<price>$30</price>
</course>
<course category = "HADOOP">
<title lang = "en">Hadoop</title>.
<tutor>Satish</tutor>
<duration>3</duration>
<price>$50</price>
</course>
<course category = "HTML">
<title lang = "en">html</title>
<tutor>raju</tutor>
<duration>5</duration>
<price>$50</price>
</course>
<course category = "WEB">
<title lang = "en">Web Technologies</title>
<tutor>Javed</tutor>
<duration>10</duration>
<price>$60</price>
</course>
</tutorialspoint>
Example
PHP 代码应如下所示:
PHP code should be as follows −
<?php
$xml = simplexml_load_file("books.xml") or die("Error: Cannot create object");
foreach($xml->children() as $books) {
echo $books->title . "<br> ";
echo $books->tutor . "<br> ";
echo $books->duration . "<br> ";
echo $books->price . "<hr>";
}
?>
它将生成以下 output −
It will produce the following output −
Java
Gopal
$30
________________________________________
Hadoop
Satish
3
$50
________________________________________
html
raju
5
$50
________________________________________
Web Technologies
Javed
10
$60
________________________________________
PHP - SAX Parser Example
PHP 在 php.ini 设置文件中默认启用了 XML 解析器扩展。该解析器实现了 SAX API,这是一种基于事件的解析算法。
PHP has the XML parser extension enabled by default in the php.ini settings file. This parser implements SAX API, which is an event-based parsing algorithm.
基于事件的解析器不会在内存中加载整个 XML 文档。相反,它一次读取一个节点。解析器允许你实时交互。一旦你移到下一个节点,以前的那个节点就会从内存中移除。
An event-based parser doesn’t load the entire XML document in the memory. instead, it reads in one node at a time. The parser allows you to interact with in real time. Once you move onto the next node, the old one is removed from the memory.
基于 SAX 的解析机制比基于树的解析器更快。PHP 库包含用于处理 XML 事件的函数,如本章所述。
SAX based parsing mechanism is faster than the tree based parsers. PHP library includes functions to handle the XML events, as explained in this chapter.
解析 XML 文档的第一步是让一个解析器对象使用 xml_parse_create() 函数。
The first step in parsing a XML document is to have a parser object, with xml_parse_create() function
xml_parser_create(?string $encoding = null): XMLParser
此函数创建一个新的 XML 解析器,并返回一个 XMLParser 对象供其他 XML 函数使用。
This function creates a new XML parser and returns an object of XMLParser to be used by the other XML functions.
xml_parse() 函数开始解析 XML 文档
The xml_parse() function starts parsing an XML document
xml_parse(XMLParser $parser, string $data, bool $is_final = false): int
xml_parse() 解析 XML 文档。已配置事件的处理程序将按需要多次调用。
xml_parse() parses an XML document. The handlers for the configured events are called as many times as necessary.
XMLParser 扩展提供不同的事件处理程序函数。
The XMLParser extension provides different event handler functions.
xml_set_element_handler()
此函数设置 XML 解析器的元素处理程序函数。每当 XML 解析器遇到开始或结束标记时,就会发出元素事件。有单独的处理程序来处理开始标记和结束标记。
This function sets the element handler functions for the XML parser. Element events are issued whenever the XML parser encounters start or end tags. There are separate handlers for start tags and end tags.
xml_set_element_handler(XMLParser $parser, callable $start_handler,
callable $end_handler): true
当打开新的 XML 元素时,调用 start_handler() 函数。当关闭 XML 元素时,调用 end_handler() 函数。
The start_handler() function is called when a new XML element is opened. end_handler() function is called when an XML element is closed.
xml_set_character_data_handler()
此函数设置 XML 解析器 parser 的字符数据处理程序函数。字符数据大概包括 XML 文档中所有非标记内容,包括标记之间的空格。
This function sets the character data handler function for the XML parser parser. Character data is roughly all the non-markup contents of XML documents, including whitespace between tags.
xml_set_character_data_handler(XMLParser $parser, callable $handler): true
xml_set_processing_instruction_handler()
此函数设置 XML 解析器 parser 的处理指令 (PI) 处理程序函数。<?php ?> 是一个处理指令,其中 php 称为“PI 目标”。它们的处理是特定于应用程序的。
This function sets the processing instruction (PI) handler function for the XML parser parser. <?php ?> is a processing instruction, where php is called the "PI target". The handling of these are application-specific.
xml_set_processing_instruction_handler(XMLParser $parser, callable $handler): true
processing instruction 具有以下格式 -
A processing instruction has the following format −
<?target
data
?>
xml_set_default_handler()
此函数设置 XML 解析器 parser 的默认处理程序函数。未转交给其他处理程序的所有内容都转交给默认处理程序。您将在默认处理程序中获得 XML 和文档类型声明等内容。
This function sets the default handler function for the XML parser parser. What goes not to another handler goes to the default handler. You will get things like the XML and document type declarations in the default handler.
xml_set_default_handler(XMLParser $parser, callable $handler): true
Example
以下示例演示了用于解析 XML 文档的 SAX API 的使用。我们将使用 SAX.xml 如下所示 -
The following example demonstrates the use of SAX API for parsing the XML document. We shall use the SAX.xml as below −
<?xml version = "1.0" encoding = "utf-8"?>
<tutors>
<course>
<name>Android</name>
<country>India</country>
<email>contact@tutorialspoint.com</email>
<phone>123456789</phone>
</course>
<course>
<name>Java</name>
<country>India</country>
<email>contact@tutorialspoint.com</email>
<phone>123456789</phone>
</course>
<course>
<name>HTML</name>
<country>India</country>
<email>contact@tutorialspoint.com</email>
<phone>123456789</phone>
</course>
</tutors>
Example
以下是解析上述文档的 PHP 代码。它打开 XML 文件并调用 xml_parse() 函数,直至文件结束。事件处理程序将数据存储在 tutors 数组中。然后,按元素顺序回显该数组。
The PHP code to parse the above document is given below. It opens the XML file and calls xml_parse() function till its end of file is reached. The event handlers store the data in tutors array. Then the array is echoed element wise.
<?php
// Reading XML using the SAX(Simple API for XML) parser
$tutors = array();
$elements = null;
// Called to this function when tags are opened
function startElements($parser, $name, $attrs) {
global $tutors, $elements;
if(!empty($name)) {
if ($name == 'COURSE') {
// creating an array to store information
$tutors []= array();
}
$elements = $name;
}
}
// Called to this function when tags are closed
function endElements($parser, $name) {
global $elements;
if(!empty($name)) {
$elements = null;
}
}
// Called on the text between the start and end of the tags
function characterData($parser, $data) {
global $tutors, $elements;
if(!empty($data)) {
if ($elements == 'NAME' || $elements == 'COUNTRY' || $elements == 'EMAIL' || $elements == 'PHONE') {
$tutors[count($tutors)-1][$elements] = trim($data);
}
}
}
$parser = xml_parser_create();
xml_set_element_handler($parser, "startElements", "endElements");
xml_set_character_data_handler($parser, "characterData");
// open xml file
if (!($handle = fopen('sax.xml', "r"))) {
die("could not open XML input");
}
while($data = fread($handle, 4096)) {
xml_parse($parser, $data);
}
xml_parser_free($parser);
$i = 1;
foreach($tutors as $course) {
echo "course No - ".$i. '<br/>';
echo "course Name - ".$course['NAME'].'<br/>';
echo "Country - ".$course['COUNTRY'].'<br/>';
echo "Email - ".$course['EMAIL'].'<br/>';
echo "Phone - ".$course['PHONE'].'<hr/>';
$i++;
}
?>
上述代码给出了以下输出 -
The above code gives the following output −
course No - 1
course Name - Android
Country - India
Email - contact@tutorialspoint.com
Phone - 123456789
________________________________________
course No - 2
course Name - Java
Country - India
Email - contact@tutorialspoint.com
Phone - 123456789
________________________________________
course No - 3
course Name - HTML
Country - India
Email - contact@tutorialspoint.com
Phone - 123456789
________________________________________
PHP - DOM Parser Example
PHP 中的 DOM 扩展提供广泛的功能,我们可借此对 XML 和 HTML 文档执行各种操作。我们可以动态构造一个 DOM 对象,从一个 HTML 文件或包含 HTML 标记树的字符串加载一个 DOM 文档。我们还可以将 DOM 文档保存到 XML 文件中,或者从 XML 文档中提取 DOM 树。
The DOM extension in PHP comes with extensive functionality with which we can perform various operations on XML and HTML documents. We can dynamically construct a DOM object, load a DOM document from a HTML file or a string with HTML tag tree. We can also save the DOM document to a XML file, or extract the DOM tree from a XML document.
DOMDocument 类是 DOM 扩展中定义的最重要类之一。
The DOMDocument class is one the most important classes defined in the DOM extension.
$obj = new DOMDocument($version = "1.0", $encoding = "")
它表示整个 HTML 或 XML 文档;用作文档树的根。DOMDocument 类包括许多静态方法的定义,其中一些如下 -
It represents an entire HTML or XML document; serves as the root of the document tree. The DOMDocument class includes definitions of a number of static methods, some of which are introduced here −
Sr.No |
Methods & Description |
1 |
createElement Create new element node |
2 |
createAttribute Create new attribute |
3 |
createTextNode Create new text node |
4 |
getElementById Searches for an element with a certain id |
5 |
getElementsByTagName Searches for all elements with given local tag name |
6 |
load Load XML from a file |
7 |
loadHTML Load HTML from a string |
8 |
loadHTMLFile Load HTML from a file |
9 |
loadXML Load XML from a string |
10 |
save Dumps the internal XML tree back into a file |
11 |
saveHTML Dumps the internal document into a string using HTML formatting |
12 |
saveHTMLFile Dumps the internal document into a file using HTML formatting |
13 |
saveXML Dumps the internal XML tree back into a string |
Example
让我们对以下 HTML 文件使用此示例 −
Let us use the following HTML file for this example −
<html>
<head>
<title>Tutorialspoint</title>
</head>
<body>
<h2>Course details</h2>
<table border = "0">
<tbody>
<tr>
<td>Android</td>
<td>Gopal</td>
<td>Sairam</td>
</tr>
<tr>
<td>Hadoop</td>
<td>Gopal</td>
<td>Satish</td>
</tr>
<tr>
<td>HTML</td>
<td>Gopal</td>
<td>Raju</td>
</tr>
<tr>
<td>Web technologies</td>
<td>Gopal</td>
<td>Javed</td>
</tr>
<tr>
<td>Graphic</td>
<td>Gopal</td>
<td>Satish</td>
</tr>
<tr>
<td>Writer</td>
<td>Kiran</td>
<td>Amith</td>
</tr>
<tr>
<td>Writer</td>
<td>Kiran</td>
<td>Vineeth</td>
</tr>
</tbody>
</table>
</body>
</html>
我们现在将通过在以下 PHP 代码中调用 loadHTMLFile() 方法从上述 HTML 文件中提取文档对象模型 −
We shall now extract the Document Object Model from the above HTML file by calling the loadHTMLFile() method in the following PHP code −
<?php
/*** a new dom object ***/
$dom = new domDocument;
/*** load the html into the object ***/
$dom->loadHTMLFile("hello.html");
/*** discard white space ***/
$dom->preserveWhiteSpace = false;
/*** the table by its tag name ***/
$tables = $dom->getElementsByTagName('table');
/*** get all rows from the table ***/
$rows = $tables[0]->getElementsByTagName('tr');
/*** loop over the table rows ***/
foreach ($rows as $row) {
/*** get each column by tag name ***/
$cols = $row->getElementsByTagName('td');
/*** echo the values ***/
echo 'Designation: '.$cols->item(0)->nodeValue.'<br />';
echo 'Manager: '.$cols->item(1)->nodeValue.'<br />';
echo 'Team: '.$cols->item(2)->nodeValue;
echo '<hr />';
}
?>
它将生成以下 output −
It will produce the following output −
Designation: Android
Manager: Gopal
Team: Sairam
________________________________________
Designation: Hadoop
Manager: Gopal
Team: Satish
________________________________________
Designation: HTML
Manager: Gopal
Team: Raju
________________________________________
Designation: Web technologies
Manager: Gopal
Team: Javed
________________________________________
Designation: Graphic
Manager: Gopal
Team: Satish
________________________________________
Designation: Writer
Manager: Kiran
Team: Amith
________________________________________
Designation: Writer
Manager: Kiran
Team: Vineeth
________________________________________
PHP - Login Example
一个典型的 PHP 网络应用程序在登录之前将对用户进行认证,通过询问他的凭据,诸如 username 和 password 。然后根据服务器上可用的用户数据对凭据进行检查。在这个例子中,用户数据是用关联数组的形式提供的。以下 PHP 登录脚本在下面进行了说明 −
A typical PHP web application authenticates the user before logging in, by asking his credentials such as username and password. The credentials are then checked against the user data available with the server. In this example, the user data is available in the form of an associative array. The following PHP Login script is explained below −
HTML Form
该代码的 HTML 部分给出了一个简单的 HTML 表单,它接受用户名和密码,并且将数据发布到自身。
The HTML part of the code presents a simple HTML form, that accepts username and password, and posts the data to itself.
<form action = "<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="post">
<div>
<label for="username">Username:</label>
<input type="text" name="username" id="name">
</div>
<div>
<label for="password">Password:</label>
<input type="password" name="password" id="password">
</div>
<section style="margin-left:2rem;">
<button type="submit" name="login">Login</button>
</section>
</form>
PHP Authentication
PHP 脚本解析 POST 数据,并且检查用户名是否存在于用户数组中。如果存在,则它进一步检查密码是否与数组中已注册的用户相符
The PHP script parses the POST data, and checks if the username is present in the users array. If found, it further checks whether the password corresponds to the registered user in the array
<?php
if (array_key_exists($user, $users)) {
if ($users[$_POST['username']]==$_POST['password']) {
$_SESSION['valid'] = true;
$_SESSION['timeout'] = time();
$_SESSION['username'] = $_POST['username'];
$msg = "You have entered correct username and password";
} else {
$msg = "You have entered wrong Password";
}
} else {
$msg = "You have entered wrong user name";
}
?>
将用户名和相应的消息添加到 $_SESSION 数组中。用户将看到相应的消息,提示他输入的凭据是否正确。
The username and the appropriate message is added to the $_SESSION array. The user is prompted with a respective message, whether the credentials entered by him are correct or not.
The Complete Code
以下是完整代码——
Here is the complete code −
Login.php
<?php
ob_start();
session_start();
?>
<html lang = "en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="loginstyle.css">
<title>Login</title>
</head>
<body>
<h2 style="margin-left:10rem; margin-top:5rem;">Enter Username and Password</h2>
<?php
$msg = '';
$users = ['user'=>"test", "manager"=>"secret", "guest"=>"abc123"];
if (isset($_POST['login']) && !empty($_POST['username'])
&& !empty($_POST['password'])) {
$user=$_POST['username'];
if (array_key_exists($user, $users)){
if ($users[$_POST['username']]==$_POST['password']){
$_SESSION['valid'] = true;
$_SESSION['timeout'] = time();
$_SESSION['username'] = $_POST['username'];
$msg = "You have entered correct username and password";
}
else {
$msg = "You have entered wrong Password";
}
}
else {
$msg = "You have entered wrong user name";
}
}
?>
<h4 style="margin-left:10rem; color:red;"><?php echo $msg; ?></h4>
<br/><br/>
<form action = "<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="post">
<div>
<label for="username">Username:</label>
<input type="text" name="username" id="name">
</div>
<div>
<label for="password">Password:</label>
<input type="password" name="password" id="password">
</div>
<section style="margin-left:2rem;">
<button type="submit" name="login">Login</button>
</section>
</form>
<p style="margin-left: 2rem;">
<a href = "logout.php" tite = "Logout">Click here to clean Session.</a>
</p>
</div>
</body>
</html>
Logout.php
要注销,用户点击 logout.php 的链接
To logout, the user clicks on the link to logout.php
<?php
session_start();
unset($_SESSION["username"]);
unset($_SESSION["password"]);
echo '<h4>You have cleaned session</h4>';
header('Refresh: 2; URL = login.php');
?>
通过输入 “http://localhost/login.php” 启动应用程序。以下是 different scenarios −
Start the application by entering "http://localhost/login.php". Here are the different scenarios −
Correct Username and Password
Correct Username and Password
Incorrect Password
Incorrect Password
Incorrect Username
Incorrect Username
当用户单击底部的链接时,会话变量将被删除,且登录屏幕将重新出现。
When the user clicks the link at the bottom, the session variables are removed, and the login screen reappears.
PHP - Facebook Login
可以使用社交媒体登录(也称为 SSO)要求用户登录 Web 应用程序。这样用户无需创建一个新帐户。相反,用户可以使用其现有的社交媒体帐户信息登录。一些社交媒体登录的示例包括:谷歌、Facebook、LinkedIn、Apple。
Users can be asked to log into a web application with the help of Social media login, also called SSO. This way users need not create a new account. Instead, users can use their existing social media account information to log in. Some examples of social media login include: Google, Facebook, LinkedIn, Apple.
在本章中,我们将说明如何使用 Facebook 证书激活登录 PHP 应用程序。
In this chapter, we shall explain how to activate logging into a PHP application with Facebook credentials.
添加 Facebook 登录功能的第一步是创建一个 Facebook 应用程序。访问 https://developers.facebook.com/apps/creation/ 并使用您的 Facebook 帐户登录。
The first step to add Facebook login feature is to create a Facebook app. Visit https://developers.facebook.com/apps/creation/ and sign in with your Facebook account.
接下来,输入要创建的 Facebook 应用程序的名称 −
Next, enter the name of the Facebook app you want to create −
进入应用程序设置并获取应用程序 ID 和密钥 −
Go in the App settings and obtain Application ID and secret code −
将平台选为网站 −
Select platform as website −
接下来,您需要在 PHP 中设置 Facebook SDK。从“https://packagist.org/packages/facebook/php-sdk”下载适用于 PHP 的 Facebook SDK 或使用 composer : composer require “facebook/graph-sdk-v5”。将 SDK 文件解压到您的 PHP 应用程序可访问的目录中。
Next, you need to set Up Facebook SDK in PHP. Download the Facebook SDK for PHP from "https://packagist.org/packages/facebook/php-sdk" or use composer: composer require "facebook/graph-sdk-v5". Extract the SDK files to a directory accessible by your PHP application.
要在 PHP 代码中配置 Facebook SDK,请在您的 PHP 文件中包含 Facebook SDK 自动加载器:require_once DIR . '/vendor/autoload.php';
To configure Facebook SDK in PHP Code, include the Facebook SDK autoloader in your PHP file: require_once DIR . '/vendor/autoload.php';
设置您的应用程序访问令牌和应用程序密钥 −
Set up your app’s access token and app secret −
$app_id = 'YOUR_APP_ID';
$app_secret = 'YOUR_APP_SECRET';
接下来,创建 Facebook 登录按钮。创建 HTML 按钮并添加 Facebook 登录 JavaScript SDK 以触发登录流程 −
Next, create Facebook Login Button. Create an HTML button and add the Facebook login JavaScript SDK to trigger the login flow −
<button id="facebook-login-button">Login with Facebook</button>
包含 Facebook JavaScript SDK −
Include the Facebook JavaScript SDK −
<script src="https://connect.facebook.net/en_US/sdk.js#xfbml=1&version=v13.0&appId=YOUR_APP_ID&autoLogApp=true" async defer></script>
创建一个 PHP 脚本来处理 Facebook 登录回调 −
Create a PHP script to handle the Facebook login callback −
<?php
session_start();
$fb = new Facebook\Facebook([
'app_id' => $app_id,
'app_secret' => $app_secret,
'default_graph_version' => 'v13.0',
]);
$helper = $fb->getRedirectLoginHelper();
$accessToken = $helper->getAccessToken();
if ($accessToken) {
// User is logged in, handle their data
$user = $fb->get('/me', ['fields' => 'id,name,email']);
$_SESSION['user_data'] = $user;
header('Location: profile.php');
} else {
// User is not logged in, redirect to login page
$loginUrl = $helper->getLoginUrl(['scope' => 'public_profile,email']);
header('Location: ' . $loginUrl);
}
?>
成功登录后,将用户数据存储在会话中,并重定向到受保护页面。在受保护页面上,查看会话中的用户数据以验证访问。
After successful login, store user data in the session and redirect to a protected page. On protected pages, check the session for user data to verify access.
PHP - Paypal Integration
PayPal 是一个付款处理系统。我们可以使用 PHP 来将 PayPal 与网站集成。
PayPal is a payment processing system. We can integrate PayPal with websites by using with PHP.
PayPal Integration File System
PayPal 集成文件系统包含四个文件,如下所示 -
PayPal integration file system included four files as shown below −
-
constants.php − This file includes the API user name, password and signature.
-
CallerService.php − This file includes the PayPal Services, which are used to call PayPal services.
-
confirmation.php − This file includes a form with minimum fields required to make payment process and it will return payment success or failure.
-
PayPal_entry.php − This page has used to send the user the data to PayPal. It acts as an adapter between PayPal and user form.
用户需要从这里下载一个 PayPal SDK 文件并具体解压一个 zip 文件。zip 文件包含四个 PHP 文件。我们不需要更改“constants.php”以外的任何文件。
The user has to download a PayPal SDK file from here and exact a zip file. The zip file contains four PHP files. We don’t need to change any file except "constants.php".
constants.php
“constants.php”文件包含以下代码 -
The "constants.php" file contains code as shown below −
<?php
define('API_USERNAME', 'YOUR USER NAME HERE');
define('API_PASSWORD', 'YOUR PASSWORD HERE');
define('API_SIGNATURE', 'YOUR API SIGNATURE HERE');
define('API_ENDPOINT', 'https://api-3t.paypal.com/nvp');
define('USE_PROXY',FALSE);
define('PROXY_HOST', '127.0.0.1');
define('PROXY_PORT', '808');
define('PAYPAL_URL', 'https://www.PayPal.com/webscr&cmd=_express-checkout&token=');
define('VERSION', '53.0');
?>
用户将在上方语法中声明放置在“constants.php”中的用户名、密码和签名。
The user will declare the username, password and signature in the above syntax which are placed in "constants.php".
这是一个实验示例,所以最后金额将被添加到沙盒账户。
This is an experimental example so the last amount will be added to sandbox’s account.
PHP - MySQL Login
MySQL 是 PHP 支持的 Web 应用程序的后端数据库的热门选择。在本章中,我们将学习如何为 PHP 应用程序开发一个登录页面,该页面验证给定的用户名和密码。
MySQL is a popular choice as a backend database for PHP powered web applications. In this chapter, we shall learn to develop a login page for a PHP application that authenticates the given username and password.
您应该有一个安装了 PHP 和 MySQL 的 Web 服务器,以便对本章中讨论的示例进行实验。可以轻松安装适用于您操作系统的 Apache、PHP 和 MySQL(MariaDB)的捆绑二进制文件 XAMPP。
You should have a web server having PHP and MySQL installed for experimenting with the example discussed in this chapter. The bundled binaries of Apache, PHP and MySQL (MariaDB) in the form of XAMPP for your operating system can be easily installed.
在运行示例代码之前,您应该有一个名为 mydb 的 MySQL 数据库,其中必须有一个名为 admin 的表。您可以使用以下 SQL 脚本创建表并插入测试数据
Before running the example code, you should have a MySQL database called mydb in which there must be a table called admin. You can use following SQL script to create the table and insert a test data
use mydb;
CREATE TABLE `admin` (
`username` varchar(10) NOT NULL,
`passcode` varchar(10) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
INSERT INTO `admin` (`username`, `passcode`) VALUES
('guest', 'abc123'),
('manager', 'secret'),
('user', 'test');
ALTER TABLE `admin`
ADD PRIMARY KEY (`username`);
COMMIT;
PHP 登录应用程序的第一部分是建立数据库连接对象。我们使用 myqli API 来获得连接对象。将以下代码保存为 “config.php”
The first part of PHP login application is to establish database connection object. We use myqli API to obtain connection object. Save following code as "config.php"
Config.php
<?php
define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', '');
define('DB_DATABASE', 'mydb');
$db = mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
?>
此 PHP 脚本在登录脚本中调用。它向用户提供一个 HTML 表单,用于输入用户名和密码。如果提交了表单,PHP 会运行一个 SELECT 查询,以检索 admin 表中用户名和密码与用户输入匹配的行。
This PHP script is called inside the login script. It presents the user with a HTML form to enter username and password. In case the form is submitted, PHP runs a SELECT query to retrieve a row in the admin table where the username and passcode matches with the user inputs.
$myusername = mysqli_real_escape_string($db,$_POST['username']);
$mypassword = mysqli_real_escape_string($db,$_POST['password']);
$sql = "SELECT * FROM admin WHERE username = '$myusername' and passcode = '$mypassword'";
$result = mysqli_query($db,$sql);
$row = mysqli_num_rows($result);
如果行数为 1,则表示输入的用户名和密码匹配。用户名保存到 $_SESSION 变量,浏览器会定向到 welcome.php 脚本。
If the row count is one, it indicates that the username and the password entered matches. The username is save to the $_SESSION variable and the browser is directed to welcome.php script.
Login.php
将以下代码保存为 “login.php” −
Save the following code as "login.php" −
<?php
include("config.php");
session_start();
$error='';
if($_SERVER["REQUEST_METHOD"] == "POST") {
// username and password sent from form
$myusername = mysqli_real_escape_string($db,$_POST['username']);
$mypassword = mysqli_real_escape_string($db,$_POST['password']);
$sql = "SELECT * FROM admin WHERE username = '$myusername' and passcode = '$mypassword'";
$result = mysqli_query($db,$sql);
$row = mysqli_num_rows($result);
$count = mysqli_num_rows($result);
if($count == 1) {
// session_register("myusername");
$_SESSION['login_user'] = $myusername;
header("location: welcome.php");
} else {
$error = "Your Login Name or Password is invalid";
}
}
?>
<html>
<head>
<title>Login Page</title>
<style type = "text/css">
body {
font-family:Arial, Helvetica, sans-serif;
font-size:14px;
}
label {
font-weight:bold;
width:100px;
font-size:14px;
}
.box {
border:#666666 solid 1px;
}
</style>
</head>
<body bgcolor = "#FFFFFF">
<div align = "center">
<div style = "width:300px; border: solid 1px #333333; " align = "left">
<div style = "background-color:#333333; color:#FFFFFF; padding:3px;"><b>Login</b></div>
<div style = "margin:30px">
<form action = "" method = "post">
<label>UserName :</label><input type = "text" name = "username" class = "box"/><br /><br />
<label>Password :</label><input type = "password" name = "password" class = "box" /><br/><br />
<input type = "submit" value = " Submit "/><br />
</form>
<div style = "font-size:11px; color:#cc0000; margin-top:10px"><?php echo $error; ?></div>
</div>
</div>
</div>
</body>
</html>
Welcome.php
当用户通过身份验证时,会调用 “welcome.php” 脚本。它读取会话变量以显示欢迎消息。
The "welcome.php" script gets invoked when the user is authenticated. It reads the session variable to display a welcome message.
<?php
include('session.php');
?>
<html>
<head>
<title>Welcome </title>
</head>
<body>
<h1>Welcome <?php echo $login_session; ?></h1>
<h2><a href = "logout.php">Sign Out</a></h2>
</body>
</html>
Logout.php
最后,退出脚本删除并销毁会话,并将用户重定向到登录页面。
Finally, the logout script removes the destroys the session and redirects the user to the login page.
<?php
session_start();
if(session_destroy()) {
header("Location: login.php");
}
?>
要启动登录应用程序,请访问 “http://localhost/login.php”
To start the login application, visit "http://localhost/login.php"
输入用户名和密码。在按下提交按钮后,这些输入与 admin 表中的行进行比对。成功后,您会收到以下消息 −
Enter the username and password. On pressing the submit button, these inputs are checked against the rows in admin table. On success, you get the following message −
如果查询没有提取到任何匹配的行,则会显示以下错误消息 −
If the query doesn’t fetch any matching row, the error message is displayed as follows −
PHP & MySQL
PHP 几乎可以与所有数据库软件一起使用,包括 Oracle 和 Sybase,但最常用的是免费提供的 MySQL 数据库。
PHP will work with virtually all database software, including Oracle and Sybase but most commonly used is freely available MySQL database.
What you should already have ?
-
You have gone through MySQL tutorial to understand MySQL Basics.
-
Downloaded and installed a latest version of MySQL.
-
Created database user guest with password guest123.
-
If you have not created a database then you would need root user and its password to create a database.
我们已将本章分为以下几个部分 −
We have divided this chapter in the following sections −
-
Connecting to MySQL database − Learn how to use PHP to open and close a MySQL database connection.
-
Create MySQL Database Using PHP − This part explains how to create MySQL database and tables using PHP.
-
Delete MySQL Database Using PHP − This part explains how to delete MySQL database and tables using PHP.
-
Insert Data To MySQL Database − Once you have created your database and tables then you would like to insert your data into created tables. This session will take you through real example on data insert.
-
Retrieve Data From MySQL Database − Learn how to fetch records from MySQL database using PHP.
-
Using Paging through PHP − This one explains how to show your query result into multiple pages and how to create the navigation link.
-
Updating Data Into MySQL Database − This part explains how to update existing records into MySQL database using PHP.
-
Deleting Data From MySQL Database − This part explains how to delete or purge existing records from MySQL database using PHP.
-
Using PHP To Backup MySQL Database − Learn different ways to take backup of your MySQL database for safety purpose.
PHP.INI File Configuration
在计算机上安装 PHP 软件后,就会在安装目录中创建 php.ini。对于 XAMPP,php.ini 位于 c:\xamm\php 文件夹中。这是一个重要的配置文件,它控制着性能并设置所有与 PHP 相关的参数。
On installing PHP software on your machine, php.ini is created in the installation directory. In case of XAMPP, php.ini is found in c:\xamm\php folder. It is an important configuration file that controls the performance and sets all the PHP related parameters.
phpinfo() 函数显示一个不同的参数列表及其 PHP、Aache、MySQL 和 Web 服务器安装的其他部分的当前值。
The phpinfo() function displays a list of different parameters and their current values of PHP, Aache, MySQL and other parts of the web server installation.
运行以下代码以显示设置,其中一个设置显示“php.ini”文件的路径:
Run the following code to display the settings, one of which shows the path to the "php.ini" file:
<?php
echo phpinfo();
?>
Loaded Configuration File
查找指明php.ini 文件所在位置的已加载配置文件设置
Locate the Loaded Configuration File setting that displays the location of php.ini file
C:\xampp\php\php.ini
PHP 行为的不同方面由大量的参数(称为指令)配置。“php.ini”文件以分号(;)符号开头的多数行开头 - 表示该行已注释。未注释的行实际上是有效的指令及其值。换句话说,要激活并为特定指令指定值,请删除前导分号。
Different aspects of PHP’s behaviour are configured by a large number of parameters (called directives). The "php.ini" file comes with most of the lines starting with semicolon (;) symbol – indicating that the line is commented. The uncommented line is actually the effective directive and its value. In other words, to activate and assign a value to a particular directive, remove the leading semicolon.
directive = value
指令名称区分大小写。指令是在变量中用于配置 PHP 或 PHP 扩展。请注意,没有名称验证,因此如果没有找到预期的指令,将使用默认值,它可以是字符串、数字、PHP 常量(例如 E_ALL 或 M_PI)、其中一个 INI 常量(开、关、真、假、是、否和无)。
Directive names are *case sensitive. Directives are variables used to configure PHP or PHP extensions. Note that there is no name validation, so if an expected directive is not found a default value will be used, which can be a string, a number, a PHP constant (e.g. E_ALL or M_PI), one of the INI constants (On, Off, True, False, Yes, No and None).
实际上,C:\XAMPP\PHP 文件夹包含两个 INI 文件,一个用于生产环境,另一个用于开发环境。
Actually, the C:\XAMPP\PHP folder contains two INI files, one to be used in production environment and other in development environment.
php.ini-development.ini 与它的生产版本非常相似,除了在错误方面更加详细。在开发阶段,将其复制为 php.ini 以便能够跟踪代码中的错误。一旦代码准备就绪,可以使用 php.ini-production.ini 文件作为有效的 php.ini 文件,这在很大程度上抑制了错误消息。
The php.ini-development.ini is very similar to its production variant, except it is much more verbose when it comes to errors. In development stage, copy this as php.ini to be able to trace the bugs in the code. Once the code is ready for deployment, use php.ini-production.ini file as the effective php.ini file, which essentially supress the error messages to a large extent.
php.ini 中的指令分为不同的类别,如错误处理、数据处理、路径和目录、文件上传、PHP 扩展和模块设置。
The directives in php.ini are divided in different categories, like Error handling, data handling, path and directories, file uploads, PHP extensions and module settings.
以下是“php.ini”文件中一些重要指令的列表:
Here is a list of some of the important directives in "php.ini" file:
short_open_tag = Off
简写标签看起来像这样:<? ?>。如果您想要使用 XML 功能,则必须将此选项设置为“关”。
Short open tags look like this: <? ?>. This option must be set to Off if you want to use XML functions.
safe_mode = Off
如果设置为开,您可能使用 --enable-safe-mode 标志编译了 PHP。安全模式最适用于 CGI 用途。请参见本章前面的“CGI 编译时选项”部分中的说明。
If this is set to On, you probably compiled PHP with the --enable-safe-mode flag. Safe mode is most relevant to CGI use. See the explanation in the section "CGI compile-time options". earlier in this chapter.
safe_mode_exec_dir = [DIR]
此选项仅在安全模式开启时才相关;它也可以在 Unix 构建过程中使用 --with-exec-dir 标志设置。处于安全模式的 PHP 仅会从该目录中执行外部二进制文件。默认值为 /usr/local/bin。这与提供正常的 PHP/HTML 网页无关。
This option is relevant only if safe mode is on; it can also be set with the --with-exec-dir flag during the Unix build process. PHP in safe mode only executes external binaries out of this directory. The default is /usr/local/bin. This has nothing to do with serving up a normal PHP/HTML Web page.
safe_mode_allowed_env_vars = [PHP_]
此选项设置用户在安全模式下可以更改的环境变量。默认值仅为以“PHP_”为前缀的变量。如果此指令为空,则大部分变量均可更改。
This option sets which environment variables users can change in safe mode. The default is only those variables prepended with "PHP_". If this directive is empty, most variables are alterable.
safe_mode_protected_env_vars = [LD_LIBRARY_PATH]
此选项设置用户在安全模式下不可更改的环境变量,即使 safe_mode_allowed_env_vars 设置为宽松的模式。
This option sets which environment variables users can’t change in safe mode, even if safe_mode_allowed_env_vars is set permissively
disable_functions = [function1, function2…]
PHP4 配置的一个受欢迎的补充,也是在 PHP5 中延续的一个补充,就是出于安全性考虑禁用选定功能的能力。以前,这需要手动编辑 PHP 所基于的 C 代码。文件系统、系统和网络功能可能应该首先启用,因为允许通过 HTTP 编写文件和更改系统从来都不是一个安全的想法。
A welcome addition to PHP4 configuration and one perpetuated in PHP5 is the ability to disable selected functions for security reasons. Previously, this necessitated hand-editing the C code from which PHP was made. Filesystem, system, and network functions should probably be the first to go because allowing the capability to write files and alter the system over HTTP is never such a safe idea.
max_execution_time = 30
函数 set_time_limit() 在安全模式下不可用,因此这是在安全模式下使脚本超时的主要方法。在 Windows 中,您必须根据消耗的最大内存而非时间中止执行。如果您使用 Apache,也可以使用 Apache 超时设置来超时,但这也会应用于网站上的非 PHP 文件。
The function set_time_limit() won.t work in safe mode, so this is the main way to make a script time out in safe mode. In Windows, you have to abort based on maximum memory consumed rather than time. You can also use the Apache timeout setting to timeout if you use Apache, but that will apply to non-PHP files on the site too.
error_reporting = E_ALL & ~E_NOTICE
默认值为 E_ALL & ~E_NOTICE,所有错误,不包括通知。开发服务器应至少设置为默认值;仅正式服务器才能考虑使用较低的值。
The default value is E_ALL & ~E_NOTICE, all errors except notices. Development servers should be set to at least the default; only production servers should even consider a lesser value
error_prepend_string = [""]
与它的配套设置 error_append_string 一起使用时,此设置允许您使错误消息与其他文本的不同颜色,或满足您的其他要求。
With its bookend, error_append_string, this setting allows you to make error messages a different color than other text, or what have you.
warn_plus_overloading = Off
此设置在将 + 运算符用于字符串(比如表单值)时会发出警告。
This setting issues a warning if the + operator is used with strings, as in a form value.
variables_order = EGPCS
此配置设置取代了 gpc_order。这两个设置现在与其 register_globals 一起弃用。它设置了不同变量的顺序:环境、GET、POST、COOKIE 和 SERVER(也称为内置)。您可以更改此顺序。
This configuration setting supersedes gpc_order. Both are now deprecated along with register_globals. It sets the order of the different variables: Environment, GET, POST, COOKIE, and SERVER (aka Built-in). You can change this order around.
变量将按照从左到右的顺序逐个覆盖,最右边的每次都赢得比赛。这意味着,如果您保留默认设置并碰巧为环境变量、POST 变量和 COOKIE 变量使用了相同名称,那么在进程结束时,COOKIE 变量将拥有该名称。实际生活中,这种情况很少发生。
Variables will be overwritten successively in left-to-right order, with the rightmost one winning the hand every time. This means if you left the default setting and happened to use the same name for an environment variable, a POST variable, and a COOKIE variable, the COOKIE variable would own that name at the end of the process. In real life, this doesn’t happen much.
register_globals = Off
此设置允许您决定是否将 EGPCS 变量注册为全局变量。此设置现在已弃用,从 PHP4.2 开始,此标志默认设置为关。请改用超级全局数组。本书中的所有主要代码列表都使用超级全局数组。
This setting allows you to decide whether you wish to register EGPCS variables as global. This is now deprecated, and as of PHP4.2, this flag is set to Off by default. Use superglobal arrays instead. All the major code listings in this book use superglobal arrays.
magic_quotes_gpc = On
此设置转义传入 GET/POST/COOKIE 数据中的引号。如果您使用许多可能会提交给自己或其他表单并在表单值中显示的表单,您可能需要将此指令设置为开,或者准备对字符串类型数据使用 addslashes()。
This setting escapes quotes in incoming GET/POST/COOKIE data. If you use a lot of forms which possibly submit to themselves or other forms and display form values, you may need to set this directive to On or prepare to use addslashes() on string-type data.
magic_quotes_runtime = Off
此设置转义传入数据库和文本字符串中的引号。记住,SQL 在存储字符串时会对单引号和撇号添加斜杠,在返回时不会去除。如果此设置关闭,您需要在从 SQL 数据库输出任何类型的字符串数据时使用 stripslashes()。如果将 magic_quotes_sybase 设置为开,则此设置必须关闭。
This setting escapes quotes in incoming database and text strings. Remember that SQL adds slashes to single quotes and apostrophes when storing strings and does not strip them off when returning them. If this setting is Off, you will need to use stripslashes() when outputting any type of string data from a SQL database. If magic_quotes_sybase is set to On, this must be Off.
magic_quotes_sybase = Off
此设置不对传入数据库和文本字符串中的单引号使用反斜杠,而是使用 Sybase 样式的单引号。如果将 magic_quotes_runtime 设置为开,则此设置必须关闭。
This setting escapes single quotes in incoming database and text strings with Sybase-style single quotes rather than backslashes. If magic_quotes_runtime is set to On, this must be Off.
auto-prepend-file = [path/to/file]
如果此处指定了路径,PHP 必须在每个 PHP 文件的开头自动对其 include()。包含路径限制适用。
If a path is specified here, PHP must automatically include() it at the beginning of every PHP file. Include path restrictions do apply.
auto-append-file = [path/to/file]
如果此处指定了路径,PHP 必须在每个 PHP 文件的末尾对其 include(),除非您使用 exit() 函数转义。包括路径限制适用。
If a path is specified here, PHP must automatically include() it at the end of every PHP file.unless you escape by using the exit() function. Include path restrictions do apply.
include_path = [DIR]
如果您设置此值,您将只能从这些目录包含或需要文件。包含目录通常位于您的文档根目录下;如果您在安全模式下运行,则这是强制性的。将其设置为。,以便包含与您的脚本所在的相同目录中的文件。多个目录使用冒号分隔:.:/usr/local/apache/htdocs:/usr/local/lib。
If you set this value, you will only be allowed to include or require files from these directories. The include directory is generally under your document root; this is mandatory if you.re running in safe mode. Set this to . in order to include files from the same directory your script is in. Multiple directories are separated by colons: .:/usr/local/apache/htdocs:/usr/local/lib.
doc_root = [DIR]
如果您使用 Apache,您已经在 httpd.conf 中为该服务器或虚拟主机设置了文档根目录。如果您在使用安全模式或希望仅启用您网站的一部分的 PHP(例如,仅在 Web 根目录的一个子目录中),请在此处设置此值。
If you are using Apache, you have already set a document root for this server or virtual host in httpd.conf. Set this value here if you.re using safe mode or if you want to enable PHP only on a portion of your site (for example, only in one subdirectory of your Web root).
file_uploads = [on/off]
如果您要使用 PHP 脚本上传文件,请打开此标志。
Turn on this flag if you will upload files using PHP script.
upload_tmp_dir = [DIR]
除非您了解 HTTP 上传的影响,否则不要对这一行进行注释!
Do not comment this line unless you understand the implications of HTTP uploads!
session.save-handler = files
除了极少数情况下,您一般不会希望更改此设置。因此请不要动它。
Except in rare circumstances, you will not want to change this setting. So don’t touch it.
ignore_user_abort = [On/Off]
如果网站访问者单击浏览器的停止按钮,此设置将控制会发生的事情。默认设置为“开”,这意味着该脚本将继续运行到完成或超时。如果该设置被更改为“关”,该脚本将中止。此设置只在模块模式下工作,而不在 CGI 中工作。
This setting controls what happens if a site visitor clicks the browser’s Stop button. The default is On, which means that the script continues to run to completion or timeout. If the setting is changed to Off, the script will abort. This setting only works in module mode, not CGI.
mysql.default_host = hostname
如果未指定其他主机,则在连接到数据库服务器时要使用的默认服务器主机。
The default server host to use when connecting to the database server if no other host is specified.
PHP - Array Destructuring
在 PHP 中,数组解构一词指的是将数组元素提取到单个变量中的机制。它也可以称为解包数组。PHP 的 list() 构造函数用来解构给定的数组,并在一条语句中将其项目分配给变量列表。
In PHP, the term Array destructuring refers to the mechanism of extracting the array elements into individual variables. It can also be called unpacking of array. PHP’s list() construct is used to destrucrure the given array assign its items to a list of variables in one statement.
list($var1, $var2, $var3, . . . ) = array(val1, val2, val3, . . .);
结果, val1 被分配到 $var1, val2 到 $var2 等等。即使因为括号,您可能认为 list() 是一个函数,但它不是,因为它没有返回值。PHP 将字符串视为一个数组,但是它无法用 list() 解包。此外,list() 中的括号不能是空的。
As a result, val1 is assigned to $var1, val2 to $var2 and so on. Even though because of the parentheses, you may think list() is a function, but it’s not as it doesn’t have return value. PHP treats a string as an array, however it cannot be unpacked with list(). Moreover, the parenthesis in list() cannot be empty.
除了 list(),您还可以使用方括号 [] 作为解构数组的快捷方式。
Instead of list(), you can also use the square brackets [] as a shortcut for destructuring the array.
[$var1, $var2, $var3, . . . ] = array(val1, val2, val3, . . .);
Example
请看以下示例:
Take a look at the following example −
<?php
$marks = array(50, 56, 70);
list($p, $c, $m) = $marks;
echo "Physics: $p Chemistry: $c Maths: $m" . PHP_EOL;
# shortcut notation
[$p, $c, $m] = $marks;
echo "Physics: $p Chemistry: $c Maths: $m" . PHP_EOL;
?>
它将生成以下 output −
It will produce the following output −
Physics: 50 Chemistry: 56 Maths: 70
Physics: 50 Chemistry: 56 Maths: 70
Destructuring an Associative Array
在 PHP 7.1.0 之前,list() 只在从 0 开始的数字索引的数字数组上工作。PHP 7.1 数组解构也适用于关联数组。
Before PHP 7.1.0, list() only worked on numerical arrays with numerical indices start at 0. PHP 7.1, array destructuring works with associative arrays as well.
让我们尝试对以下关联数组进行解构(或拆包),一个具有非数字索引的数组。
Let us try to destructure (or unpack) the following associative array, an array with non-numeric indices.
$marks = array('p'=>50, 'c'=>56, 'm'=>70);
要对这个数组进行解构,list() 语句将每个数组键与一个独立变量相关联。
To destructure this array the list() statement associates each array key with a independent variable.
list('p'=>$p, 'c'=>$c, 'm'=>$m) = $marks;
此外,您还可以使用 [] 替代解构符号。
Instead, you can also use the [] alternative destructuring notation.
['p'=>$p, 'c'=>$c, 'm'=>$m] = $marks;
尝试并执行以下 PHP 脚本 −
Try and execute the following PHP script −
<?php
$marks = array('p'=>50, 'c'=>56, 'm'=>70);
list('p'=>$p, 'c'=>$c, 'm'=>$m) = $marks;
echo "Physics: $p Chemistry: $c Maths: $m" . PHP_EOL;
# shortcut notation
['p'=>$p, 'c'=>$c, 'm'=>$m] = $marks;
echo "Physics: $p Chemistry: $c Maths: $m" . PHP_EOL;
?>
Skipping Array Elements
对于索引数组,您可以跳过其某些元素,只将其他元素分配给必需的变量
In case of an indexed array, you can skip some of its elements in assign only others to the required variables
<?php
$marks = array(50, 56, 70);
list($p, , $m) = $marks;
echo "Physics: $p Maths: $m" . PHP_EOL;
# shortcut notation
[$p, , $m] = $marks;
echo "Physics: $p Maths: $m" . PHP_EOL;
?>
对于关联数组,由于索引不是从 0 开始递增的,因此在分配时不必遵循元素的顺序。
In case of an associative array, since the indices are not incremental starting from 0, it is not necessary to follow the order of elements while assigning.
<?php
$marks = array('p'=>50, 'c'=>56, 'm'=>70);
list('c'=>$c, 'p'=>$p, 'm'=>$m) = $marks;
echo "Physics: $p Chemistry: $c Maths: $m" . PHP_EOL;
['c'=>$c, 'm'=>$m, 'p'=>$p] = $marks; # shortcut notation
echo "Physics: $p Chemistry: $c Maths: $m" . PHP_EOL;
?>
Destructuring a Nested Array
您还可以将数组解构的概念扩展到嵌套数组。在以下示例中,嵌套在其内部的子数组是一个索引数组。
You can extend the concept of array destructuring to nested arrays as well. In the following example, the subarray nested inside is an indexed array.
<?php
$marks = ['marks' => [50, 60, 70]];
['marks' => [$p, $c, $m]] = $marks;
echo "Physics: $p Chemistry: $c Maths: $m" . PHP_EOL;
?>
即使嵌套数组也是关联数组,解构也同样有效。
Destructuring works well even if the nested array is also an associative array.
<?php
$marks = ['marks' => ['p'=>50, 'c'=>60, 'm'=>70]];
['marks' => ['p'=>$p, 'c'=>$c, 'm'=>$m]] = $marks;
echo "Physics: $p Chemistry: $c Maths: $m" . PHP_EOL;
?>
PHP - Coding Standard
每家公司都基于其最佳实践遵循自己的编码规范。编码规范是必需的,因为可能会有许多开发者在研究不同的模块,因此,如果他们开始发明自己的规范,那么源代码将变得非常难以管理,并且在未来难以维护该源代码。
Every company follows its own coding standard based on its best practices. Coding standard is required because there may be many developers working on different modules so if they will start inventing their own standards then source will become very un-manageable and it will become difficult to maintain that source code in future.
以下是人们应该使用编码规范的部分原因:
Here are some reasons why one should use coding specifications −
-
Your peer programmers have to understand the code you produce. A coding standard acts as the blueprint for all the team to decipher the code.
-
Simplicity and clarity achieved by consistent coding saves you from common mistakes.
-
If you revise your code after some time then it becomes easy to understand that code.
-
Following a uniform coding standard brings more quality in software.
在使用 PHP 编码时可以遵循一些准则。
There are few guidelines which can be followed while coding in PHP.
Indenting and Line Length
使用 4 个空格的缩进,不要使用任何制表符,因为不同的计算机对制表符使用不同的设置。建议将行长度保持在大约 75-85 个字符以内,以便于更好地阅读代码。
Use an indent of 4 spaces and don’t use any tab because different computers use different setting for tab. It is recommended to keep lines at approximately 75-85 characters long for better code readability.
Control Structures
这些内容包括 if、for、while、switch 等。控制语句在控制关键字和开括号之间应该有一个空格,以区别于函数调用。强烈建议你始终使用大括号,即使在技术上它们是可选项的情况下也是如此。
These include if, for, while, switch, etc. Control statements should have one space between the control keyword and opening parenthesis, to distinguish them from function calls. You are strongly encouraged to always use curly braces even in situations where they are technically optional.
Examples
if ((condition1) || (condition2)) {
action1;
} elseif ((condition3) && (condition4)) {
action2;
} else {
default action;
}
你可以按如下方式编写 switch 语句:
You can write the switch statements as follows:
switch (condition) {
case 1:
action1;
break;
case 2:
action2;
break;
default:
defaultaction;
break;
}
Function Calls
函数调用时不应该在函数名称、开括号和第一个参数之间使用空格;在逗号和每个参数之间使用空格;在最后一个参数、闭括号和分号之间不使用空格。以下是一个示例:
Functions should be called with no spaces between the function name, the opening parenthesis, and the first parameter; spaces between commas and each parameter, and no space between the last parameter, the closing parenthesis, and the semicolon. Here’s an example −
$var = foo($bar, $baz, $quux);
Function Definitions
函数声明遵循“BSD/Allman 样式”:
Function declarations follow the "BSD/Allman style" −
function fooFunction($arg1, $arg2 = '') {
if (condition) {
statement;
}
return $val;
}
Comments
C 风格注释 (/* */) 和标准 C++ 注释 (//) 都可以。允许使用 Perl/shell 风格注释 (#),但不鼓励这样做。
C style comments (/* */) and standard C++ comments (//) are both fine. Use of Perl/shell style comments (#) is allowed but discouraged.
PHP Code Tags
始终使用 <?php ?> 来分隔 PHP 代码,而不是 <? ?> 简写。这是 PHP 合规性要求,也是在不同的操作系统和设置上包含 PHP 代码的最可移植方式。
Always use <?php ?> to delimit PHP code, not the <? ?> shorthand. This is required for PHP compliance and is also the most portable way to include PHP code on differing operating systems and setups.
Variable Names
-
Use all lower case letters
-
Use '_' as the word separator.
-
Global variables should be prepended with a 'g'.
-
Global constants should be all caps with '_' separators.
-
Static variables may be prepended with 's'.
Make Functions Reentrant
函数不应保留防止函数重入的静态变量。
Functions should not keep static variables that prevent a function from being reentrant.
One Statement Per Line
除了语句紧密相关之外,每行只能有一个语句。
There should be only one statement per line unless the statements are very closely related.
Short Methods or Functions
方法应限制在单个代码页中。
Methods should limit themselves to a single page of code.
在编写 PHP 程序时,应考虑很多要点。总的来说,意图应始终一致,并且仅在遵循编码标准时才有可能。如果你喜欢不同的东西,你可以设计自己的标准。
There could be many more points which should be considered while writing your PHP program. Over all intention should be to be consistent throughout of the code programming and it will be possible only when you will follow any coding standard. You can device your own standard if you like something different.
PHP - Regular Expressions
正则表达式本身不过是字符的序列或模式。它们为模式匹配功能提供基础。
Regular expressions are nothing more than a sequence or pattern of characters itself. They provide the foundation for pattern-matching functionality.
使用正则表达式,可以在另一个字符串中搜索特定字符串,可以用另一个字符串替换一个字符串,还可以将一个字符串拆分成许多块。
Using regular expression you can search a particular string inside a another string, you can replace one string by another string and you can split a string into many chunks.
PHP 提供特定于两组正则表达式函数的函数,每个函数都对应一定类型的正则表达式。您可以根据您的喜好使用其中任何一个。
PHP offers functions specific to two sets of regular expression functions, each corresponding to a certain type of regular expression. You can use any of them based on your comfort.
-
POSIX Regular Expressions
-
PERL Style Regular Expressions
POSIX Regular Expressions
POSIX 正则表达式的结构与典型算术表达式的结构并无不同:将各种元素(运算符)组合起来以形成更复杂的表达式。
The structure of a POSIX regular expression is not dissimilar to that of a typical arithmetic expression: various elements (operators) are combined to form more complex expressions.
最简单的正则表达式是匹配单个字符的正则表达式,如 g,在诸如 g、haggle 或 bag 等字符串中。
The simplest regular expression is one that matches a single character, such as g, inside strings such as g, haggle, or bag.
让我们对 POSIX 正则表达式中使用的几个概念进行解释。之后,我们将向您介绍正则表达式相关函数。
Lets give explanation for few concepts being used in POSIX regular expression. After that we will introduce you with regular expression related functions.
Brackets
括号 ([]) 在正则表达式中使用时具有特殊的含义。它们用于查找字符范围。
Brackets ([]) have a special meaning when used in the context of regular expressions. They are used to find a range of characters.
Sr.No |
Expression & Description |
1 |
[0-9] It matches any decimal digit from 0 through 9. |
2 |
[a-z] It matches any character from lower-case a through lowercase z. |
3 |
[A-Z] It matches any character from uppercase A through uppercase Z. |
4 |
[a-Z] It matches any character from lowercase a through uppercase Z. |
上面显示的范围是通用的;您还可以使用范围 [0-3] 以匹配从 0 到 3 的任何十进制数字,或使用范围 [b-v] 以匹配从 b 到 v 的任何小写字母。
The ranges shown above are general; you could also use the range [0-3] to match any decimal digit ranging from 0 through 3, or the range [b-v] to match any lowercase character ranging from b through v.
Quantifiers
用方括号括起来的字符序列和单个字符出现的频率或位置可以用特殊字符表示。每个特殊字符都有一个特定含义。+、*、?、{int. range} 和 $ 标志都出现在字符序列之后。
The frequency or position of bracketed character sequences and single characters can be denoted by a special character. Each special character having a specific connotation. The +, *, ?, {int. range}, and $ flags all follow a character sequence.
Sr.No |
Expression & Description |
1 |
p+ It matches any string containing at least one p. |
2 |
p* It matches any string containing zero or more p’s. |
3 |
p? It matches any string containing zero or one p’s. |
4 |
p{*N}* It matches any string containing a sequence of N p’s |
5 |
p{2,3} It matches any string containing a sequence of two or three p’s. |
6 |
p{2, } It matches any string containing a sequence of at least two p’s. |
7 |
p$ It matches any string with p at the end of it. |
8 |
*^*p It matches any string with p at the beginning of it. |
Examples
以下示例将阐明您对匹配字符的概念。
Following examples will clear your concepts about matching characters.
Sr.No |
Expression & Description |
1 |
[^a-zA-Z] It matches any string not containing any of the characters ranging from a through z and A through Z. |
2 |
p.p It matches any string containing p, followed by any character, in turn followed by another p. |
3 |
^.{2}$ It matches any string containing exactly two characters. |
4 |
<b>(.)</b>* It matches any string enclosed within <b> and </b>. |
5 |
p(hp)* It matches any string containing a p followed by zero or more instances of the sequence php. |
Predefined Character Ranges
为了方便你的编程,提供了几个预定义的字符范围,也称为字符类。字符类指定了一整个字符范围,例如,字母表或整数集 -
For your programming convenience several predefined character ranges, also known as character classes, are available. Character classes specify an entire range of characters, for example, the alphabet or an integer set −
PHP’s Regexp POSIX Functions
PHP 目前提供七个函数用于使用 POSIX 风格的正则表达式搜索字符串 -
PHP currently offers seven functions for searching strings using POSIX-style regular expressions −
Sr.No |
Function & Description |
1 |
ereg()The ereg() function searches a string specified by string for a string specified by pattern, returning true if the pattern is found, and false otherwise. |
2 |
ereg_replace()The ereg_replace() function searches for string specified by pattern and replaces pattern with replacement if found. |
3 |
eregi()The eregi() function searches throughout a string specified by pattern for a string specified by string. The search is not case sensitive. |
4 |
eregi_replace()The eregi_replace() function operates exactly like ereg_replace(), except that the search for pattern in string is not case sensitive. |
5 |
split()The split() function will divide a string into various elements, the boundaries of each element based on the occurrence of pattern in string. |
6 |
spliti()The spliti() function operates exactly in the same manner as its sibling split(), except that it is not case sensitive. |
7 |
sql_regcase()The sql_regcase() function can be thought of as a utility function, converting each character in the input parameter string into a bracketed expression containing two characters. |
PERL Style Regular Expressions
Perl 风格的正则表达式类似于 POSIX 对应表达式。POSIX 语法几乎可以与 Perl 风格的正则表达式函数互换使用。事实上,你可以使用先前 POSIX 部分中介绍的任何限定符。
Perl-style regular expressions are similar to their POSIX counterparts. The POSIX syntax can be used almost interchangeably with the Perl-style regular expression functions. In fact, you can use any of the quantifiers introduced in the previous POSIX section.
让我们对 PERL 正则表达式中使用的一些概念进行说明。之后,我们会向你介绍与正则表达式相关的函数。
Lets give explanation for few concepts being used in PERL regular expressions. After that we will introduce you wih regular expression related functions.
Meta characters
元字符只是一个字母字符,前置反斜杠,作用是给组合赋予一个特殊含义。
A meta character is simply an alphabetical character preceded by a backslash that acts to give the combination a special meaning.
例如,你可以使用“\d”元字符搜索大额资金: /([\d]+)000/ ,此处 \d 将搜索任何数字字符的字符串。
For instance, you can search for large money sums using the '\d' meta character: /([\d]+)000/, Here \d will search for any string of numerical character.
以下是可以用于 PERL 风格正则表达式中的元字符列表。
Following is the list of meta characters which can be used in PERL Style Regular Expressions.
Character Description
. a single character
\s a whitespace character (space, tab, newline)
\S non-whitespace character
\d a digit (0-9)
\D a non-digit
\w a word character (a-z, A-Z, 0-9, _)
\W a non-word character
[aeiou] matches a single character in the given set
[^aeiou] matches a single character outside the given set
(foo|bar|baz) matches any of the alternatives specified
Modifiers
有几个修饰符可用,它们可以使你和正则表达式的合作更容易,比如区分大小写、在多行中搜索等。
Several modifiers are available that can make your work with regexps much easier, like case sensitivity, searching in multiple lines etc.
Modifier Description
i Makes the match case insensitive
m Specifies that if the string has newline or carriage
return characters, the ^ and $ operators will now
match against a newline boundary, instead of a
string boundary
o Evaluates the expression only once
s Allows use of . to match a newline character
x Allows you to use white space in the expression for clarity
g Globally finds all matches
cg Allows a search to continue even after a global match fails
PHP’s Regexp PERL Compatible Functions
PHP 提供以下函数,用于使用 Perl 兼容正则表达式搜索字符串 −
PHP offers following functions for searching strings using Perl-compatible regular expressions −
Sr.No |
Function & Description |
1 |
preg_match()The preg_match() function searches string for pattern, returning true if pattern exists, and false otherwise. |
2 |
preg_match_all()The preg_match_all() function matches all occurrences of pattern in string. |
3 |
preg_replace()The preg_replace() function operates just like ereg_replace(), except that regular expressions can be used in the pattern and replacement input parameters. |
4 |
preg_split()The preg_split() function operates exactly like split(), except that regular expressions are accepted as input parameters for pattern. |
5 |
preg_grep()The preg_grep() function searches all elements of input_array, returning all elements matching the regexp pattern. |
6 |
preg_ quote()Quote regular expression characters |
PHP - Error Handling
在 PHP 中处理错误是指在 PHP 代码中做出规定来有效识别和恢复程序可能遇到的运行时错误。在 PHP 中,错误的处理方法有 -
Error handling in PHP refers to the making a provision in PHP code to effectively identifying and recovering from runtime errors that the program might come across. In PHP, the errors are handled with the help of −
-
The die() function
-
The Error Handler Function
The die() Function
die() 函数是 PHP 中 exit() 的别名。当遇到它们时,这两个函数都会导致当前 PHP 脚本终止。如果在括号中指定了一个可选字符串,它将在程序终止前输出。
The die() function is an alias of exit() in PHP. Both result in termination of the current PHP script when encountered. An optional string if specified in the parenthesis, will be output before the program terminates.
die("message");
Example
以下代码是 die() 在 PHP 脚本中的典型用法。如果 PHP 找不到文件,它将显示“File not found”消息,否则它会继续将其打开以进行后续处理。
The following code is a typical usage of die() in a PHP script. It displays the File not found message if PHP doesn’t find a file, otherwise proceeds to open it for subsequent processing.
<?php
if(!file_exists("nosuchfile.txt")) {
die("File not found");
} else {
$file = fopen("nosuchfile","r");
print "Opend file sucessfully";
// Rest of the code here.
fclose($file);
}
?>
它将生成以下 output −
It will produce the following output −
File not found
使用上述技术,当您的程序发生错误时,您可以随时停止程序并显示更具意义且用户友好的消息,而不是让 PHP 生成致命错误消息。
Using above technique, you can stop your program whenever it errors out and display more meaningful and user friendly message, rather than letting PHP generate fatal error message.
The Error Handler Function
使用 die() 处理错误被认为是一种笨拙且糟糕的程序设计,因为它会导致网站用户的体验很差。PHP 提供了一个更优雅的替代方法,您可以使用它定义一个自定义函数并指定它来处理错误。
Using die() for error handling is considered an ungainly and poor program design, as it results in an ugly experience for site users. PHP offers a more elegant alternative with which you can define a custom function and nominate it for handling the errors.
set_error_handler() 函数具有以下参数 -
The set_error_handler() function has the following parameters −
set_error_handler(?callable $callback, int $error_levels = E_ALL): ?callable
第一个参数是用户定义的函数,每当遇到错误时都会自动调用该函数。
The first parameter is a user defined function which is called automatically whenever an error is encountered.
自定义错误处理回调函数应具有以下参数 -
The custom error handler callback function should have the following parameters −
handler(
int $errno,
string $errstr,
string $errfile = ?,
int $errline = ?,
array $errcontext = ?
): bool
Parameters
Parameter |
Importance |
Description |
errno |
Required |
It specifies the error level for the user-defined error. It must be numerical value. |
errstr |
Required |
It specifies the error message for the user-defined error. |
errfile |
Optional |
It specifies the filename in which the error occurred. |
errline |
Optional |
It specifies the line number at which the error occurred. |
errcontext |
Optional |
It specifies an array containing variables and their values in use when the error occurred. |
如果回调函数返回 false,将调用默认错误。
If the callback function returns false, the default error will be called.
$errno 是一个对应于预定义错误级别的整数。
The $errno is an integer corresponding to the predefined error levels.
Sr.No |
Constant & Description |
Value |
1 |
E_ERROR (int) Fatal run-time errors that can not be recovered from. Execution of the script is halted. |
1 |
2 |
E_WARNING (int) Run-time warnings (non-fatal errors). Execution of the script is not halted. |
2 |
3 |
E_PARSE (int) Compile-time parse errors. Parse errors should only be generated by the parser. |
4 |
4 |
E_NOTICE (int) Run-time notices. Something that could indicate an error, but could also happen in the normal course of running a script. |
8 |
5 |
E_CORE_ERROR (int) Fatal errors that occur during PHP’s initial startup. This is like an E_ERROR |
16 |
6 |
E_CORE_WARNING (int) Warnings (non-fatal errors) that occur during PHP’s initial startup. This is like an E_WARNING, |
32 |
7 |
E_COMPILE_ERROR (int) Fatal compile-time errors. This is like an E_ERROR. |
64 |
8 |
E_COMPILE_WARNING (int) Compile-time warnings (non-fatal errors). This is like an E_WARNING. |
128 |
9 |
E_USER_ERROR (int) User-generated error message. This is like an E_ERROR, generated in PHP code by using the PHP function trigger_error(). |
256 |
10 |
E_USER_WARNING (int) User-generated warning message. This is like an E_WARNING, generated in PHP code by using the function trigger_error(). |
512 |
11 |
E_USER_NOTICE (int) User-generated notice message. This is like an E_NOTICE generated in PHP code by using the function trigger_error(). |
1024 |
12 |
E_STRICT (int) Enable to have PHP suggest changes to your code which will ensure the best interoperability and forward compatibility of your code. |
2048 |
13 |
E_RECOVERABLE_ERROR (int) Catchable fatal error. If the error is not caught by a user defined handler, the application aborts as it was an E_ERROR. |
4096 |
14 |
E_DEPRECATED (int) Run-time notices. Enable this to receive warnings about code that will not work in future versions. |
8192 |
15 |
E_USER_DEPRECATED (int) User-generated warning message. This is like an E_DEPRECATED, generated in PHP code by using the function trigger_error(). |
16384 |
16 |
E_ALL (int) All errors, warnings, and notices. |
32767 |
Example
请看以下示例:
Take a look at the following example −
<?php
error_reporting(E_ERROR);
function myerrorhandler($errno, $errstr) {
echo "error No: $errno Error message: $errstr" . PHP_EOL;
echo "Terminating PHP script";
die();
}
set_error_handler("myerrorhandler");
$f = fopen("nosuchfile.txt", "r");
echo "file opened successfully";
// rest of the code
fclose($f);
?>
它将生成以下 output −
It will produce the following output −
error No: 2 Error message: fopen(nosuchfile.txt): Failed to open stream: No
such file or directory
Terminating PHP script
PHP 的错误类层次结构始于 throwable 接口。PHP 中所有预定义的 Error 类都继承自 Error 类。
PHP’s error class hierarchy starts from throwable interface. All the predefined Error classes in PHP are inherited from Error class.
The ArithmeticError Class
ArithmeticError class 继承自 Error class 。在执行某些数学运算(例如以负数执行按位移位运算)时,可能会发生这种类型的错误。
The ArithmeticError class is inherited from the Error class. This type of error may occur while performing certain mathematical operations such as performing bitwise shift operation by negative amount.
Example
请看以下示例:
Take a look at the following example −
<?php
try {
$a = 10;
$b = -3;
$result = $a << $b;
}
catch (ArithmeticError $e) {
echo $e->getMessage();
}
?>
它将生成以下 output −
It will produce the following output −
Bit shift by negative number
当调用 intdiv() 函数导致的值超出整数的合法边界时,也会引发此错误。
This error is also thrown when call to intdiv() function results in value such that it is beyond the legitimate boundaries of integer.
DivisionByZeroError
DivisionByZeroError 类是 ArithmeticError 类的子类。当除法运算中的分母值为零时,会发生此类型的错误。
DivisionByZeroError class is a subclass of ArithmeticError class. This type of error occurs when value of denominator is zero in the division operation.
Example: Modulo by Zero
来看一下以下示例:
Take a look at the following example:
<?php
try {
$a = 10;
$b = 0;
$result = $a%$b;
echo $result;
}
catch (DivisionByZeroError $e) {
echo $e->getMessage();
}
?>
它将生成以下 output −
It will produce the following output −
Modulo by zero
当模运算符(%)的第二个运算符为 0,且 intdiv() 函数的第二个参数为 0 时,也会发生这种情况。
This can also occur when a modulo operator (%) has 0 as second operator, and intdiv() function having second argument as 0.
ArgumentCountError
当传递给用户定义函数或方法的参数少于其定义中的参数时,PHP 解析器会引发 ArgumentCountError。
PHP parser throws ArgumentCountError when arguments passed to a user defined function or method are less than those in its definition.
Example
请看以下示例:
Take a look at the following example −
<?php
function add($x, $y) {
return $x+$y;
}
try {
echo add(10);
}
catch (ArgumentCountError $e) {
echo $e->getMessage();
}
?>
它将生成以下 output −
It will produce the following output −
Too few arguments to function add(), 1 passed in C:\xampp\php\test.php on line 9 and exactly 2 expected
TypeError
当实际参数类型和形式参数类型不匹配、返回类型不匹配声明的返回类型时,会引发此错误。
This error is raised when actual and formal argument types don’t match, return type doesn’t match the declared returned type.
Example
请看以下示例:
Take a look at the following example −
<?php
function add(int $first, int $second) {
echo "addition: " . $first + second;
}
try {
add('first', 'second');
}
catch (TypeError $e) {
echo $e->getMessage(), "";
}
?>
它将生成以下 output −
It will produce the following output −
add(): Argument #1 ($first) must be of type int, string given,
called in /home/cg/root/63814/main.php on line 7
当 PHP 的内置函数传递的参数数量不正确时,也会引发 TypeError。但是,必须一开始就设置“strict_types=1”指令。
TypeError is also thrown when PHP’s built-in function is passed incorrect number of arguments. However, the "strict_types=1" directive must be set in the beginning.
Exceptions Handling in PHP
PHP 具有一个类似于其他编程语言的异常模型。异常很重要,并提供了对错误处理的更好控制。
PHP has an exception model similar to that of other programming languages. Exceptions are important and provides a better control over error handling.
让我们解释一下与异常相关的新的关键字。
Lets explain there new keyword related to exceptions.
-
Try − A function using an exception should be in a "try" block. If the exception does not trigger, the code will continue as normal. However if the exception triggers, an exception is "thrown".
-
Throw − This is how you trigger an exception. Each "throw" must have at least one "catch".
-
Catch − A "catch" block retrieves an exception and creates an object containing the exception information.
当抛出异常时,语句后面的代码不会执行,并且 PHP 将尝试查找第一个匹配的 catch 块。如果未捕获异常,则会使用“未捕获异常”发出 PHP 致命错误……
When an exception is thrown, code following the statement will not be executed, and PHP will attempt to find the first matching catch block. If an exception is not caught, a PHP Fatal Error will be issued with an "Uncaught Exception …
-
An exception can be thrown, and caught ("catched") within PHP. Code may be surrounded in a try block.
-
Each try must have at least one corresponding catch block. Multiple catch blocks can be used to catch different classes of exceptions.
-
Exceptions can be thrown (or re-thrown) within a catch block.
Example
以下是代码片,复制并将此代码粘贴到文件中并验证结果。
Following is the piece of code, copy and paste this code into a file and verify the result.
<?php
try {
$error = 'Always throw this error';
throw new Exception($error);
// Code following an exception is not executed.
echo 'Never executed';
}catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "";
}
// Continue execution
echo 'Hello World';
?>
在上面的示例中,$e→getMessage 函数用于获取错误消息。有以下函数可以从 Exception 类中使用。
In the above example $e→getMessage function is used to get error message. There are following functions which can be used from Exception class.
-
getMessage() − message of exception
-
getCode() − code of exception
-
getFile() − source filename
-
getLine() − source line
-
getTrace() − n array of the backtrace()
-
getTraceAsString() − formated string of trace
Creating Custom Exception Handler
您可以定义您自己的自定义异常处理程序。使用以下函数设置用户定义的异常处理程序函数。
You can define your own custom exception handler. Use following function to set a user-defined exception handler function.
string set_exception_handler ( callback $exception_handler )
这里 exception_handler 是在发生未捕获异常时要调用的函数的名称。必须在调用 set_exception_handler() 之前定义此函数。
Here exception_handler is the name of the function to be called when an uncaught exception occurs. This function must be defined before calling set_exception_handler().
Example
请看以下示例:
Take a look at the following example −
<?php
function exception_handler($exception) {
echo "Uncaught exception: " , $exception->getMessage(), "\n";
}
set_exception_handler('exception_handler');
throw new Exception('Uncaught Exception');
echo "Not Executed";
?>
在 PHP Error Handling Functions 查看错误处理函数的完整列表
Check complete set of error handling functions at PHP Error Handling Functions
PHP Try…Catch
在 PHP 中,提供了关键字 try, catch, throw 和 finally 来处理异常。如果错误是程序自身无法处理的意外程序结果,则必须使用 die() 终止程序或设置自定义错误处理程序。
In PHP, the keywords try, catch, throw and finally are provided to deal with exceptions. Whereas an Error is an unexpected program result, which cannot be handled by the program itself and the program has to be terminated with die() or setting a custom error handler.
另一方面,异常是指可以通过某种方式处理的意外情况,在异常从正常流中抛出后,程序可以继续运行。
On the other hand, an exception refers to an unexpected situation which can be handled in such a way that the program may keep running after throwing the exception out of its normal flow.
可以通过 PHP 代码中的 catch 关键字抛出异常,并使用该关键字捕获异常。可能容易出现异常的代码块被 try 块包围。每个 try 至少必须有一个对应的 catch 或 finally 块。
An exception can be thrown, and caught with the catch keyword within PHP code. A code block which is potentially prone to exception is surrounded by a try block. Each try must have at least one corresponding catch or finally block.
Try, Throw, Catch, and Finally
与异常相关的四个关键字具有以下作用 −
The four exception related keywords have the following role to play −
-
Try − A block of code where some exception is likely to occur is placed in "try" block. If exception is not triggered, the code continues execution. However, if exception does occur, it is "thrown". The execution is halted and PHP looks for matching "catch" block. If the exception is not caught, PHP issues a Fatal Error.
-
Throw − Here is how you trigger an exception. Each "throw" must have at least one "catch" or "finally" block.
-
Catch − a block that retrieves an exception and creates an object containing the exception information. Multiple catch blocks can be used to catch different exceptions.
-
Finally − Code within a finally block is always executed after throw or catch block.
Example
以下是一个异常处理技术的示例。该代码在浏览器上渲染两个文本字段,并要求用户输入两个数字以执行它们的除法。如果第二个数字(分母)为 0,则会抛出异常,程序进入 catch 块并打印异常消息。否则,将显示除法的结果。
Here is an example of exception handling technique. The code renders two text fields on the browser and asks the user to enter two numbers for their division to be performed. If the second number (denominator) is 0, an exception is thrown and the program enters the catch block and prints the exception message. Otherwise the result of division is displayed.
<html>
<body>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
<h3>First No: <input type="text" name="first"/></h3>
<h3>Second No: <input type="text" name="second"/></h3>
<input type="submit" value="Submit" />
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$x = $_POST['first'];
$y = $_POST['second'];
echo "$x $y";
try {
if ($y == 0) {
throw new Exception("Division by Zero");
}
$z = $x/$y;
echo "<h3>x = $x y = $y Division = $z<br>";
}
catch (Exception $e) {
echo "<h3> Exception: " . $e->getMessage();
}
}
?>
</body>
</html>
它将生成以下 output −
It will produce the following output −
Case 1: x = 10 y = 5 Division = 2
Case 2: x = 10 y = 0
Exception: Division by Zero
The Exception Class
PHP 抛出一个 Exception class 对象。在 PHP 中,Exception 类是用户异常的基础。它实现可抛出的接口。
PHP throws an object of Exception class. In PHP, Exception class is the base for user exceptions. It implements throwable interface.
该类定义以下方法 −
This class defines the following methods −
getMessage()
此函数将异常消息作为字符串返回 −
This function returns the Exception message as a string −
final public Exception::getMessage(): string
getCode()
此函数返回异常代码作为 Exception 中的 int
This function returns the exception code as int in Exception −
final public Exception::getCode(): int
看看以下 example −
Take a look at the following example −
try {
throw new Exception("Some error message", 30);
}
catch(Exception $e) {
echo "The exception code is: " . $e->getCode();
}
getFile()
此函数返回创建异常的文件名
This function returns the filename in which the exception was created −
final public Exception::getFile(): string
看看以下 example −
Take a look at the following example −
try {
if ($y == 0) {
throw new Exception("Division by Zero");
}
$z = $x/$y;
echo "<h3>x = $x y = $y Division = $z<br>";
}
catch (Exception $e) {
echo "<h3> Exception: " . $e->getMessage(). " in " . $e->getFile();
}
它将生成以下 output −
It will produce the following output −
Exception: Division by Zero in C:\xampp\htdocs\hello.php
getLine()
此函数返回创建异常的行号
This function returns the line number where the exception was created −
final public Exception::getLine(): int
Example
请看以下示例:
Take a look at the following example −
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$x = $_POST['first'];
$y = $_POST['second'];
echo "$x $y";
try {
if ($y == 0) {
throw new Exception("Division by Zero");
}
$z = $x/$y;
echo "<h3>x = $x y = $y Division = $z<br>";
}
catch (Exception $e) {
echo "<h3> Exception: " . $e->getMessage(). " in " . $e->getLine() . " of " . $e->getFile();
}
}
?>
它将生成以下 output −
It will produce the following output −
Exception: Division by Zero in 21 of C:\xampp\htdocs\hello.php
Multiple Catch Blocks
PHP 允许一系列 catch 块跟随 try 块来处理不同的异常情况。可以采用多个 catch 块来处理预定义的异常和错误,以及用户定义的异常。
PHP allows a series of catch blocks following a try block to handle different exception cases. Multiple catch blocks may be employed to handle predefined exceptions and errors as well as user defined exceptions.
Example
以下示例使用 catch 块来处理 DivisioByZeroError、TypeError、ArgumentCountError 和 InvalidArgumentException 条件。还有一个 catch 块来处理常规的 Exception。
The following example uses catch blocks to process DivisioByZeroError, TypeError, ArgumentCountError and InvalidArgumentException conditions. There is also a catch block to handle general Exception.
<?php
declare(strict_types=1);
function divide(int $a, int $b) : int {
return $a / $b;
}
$a=10;
$b=0;
try {
if (!$b) {
throw new DivisionByZeroError('Division by zero.');
if (is_int($a)==FALSE || is_int($b)==FALSE)
throw new InvalidArgumentException("Invalid type of arguments");
$result=divide($a, $b);
echo $result;
}
// if argument types not matching
catch (TypeError $x) {
echo $x->getMessage();
}
// if denominator is 0
catch (DivisionByZeroError $y) {
echo $y->getMessage();
}
// if number of arguments not equal to 2
catch (ArgumentCountError $z) {
echo $z->getMessage();
}
// if argument types not matching
catch (InvalidArgumentException $i) {
echo $i->getMessage();
}
// any uncaught exception
catch (Exception $ex) {
echo $ex->getMessage();
}
?>
首先,由于分母为 0,因此将显示“除以 0”错误 −
To begin with, since denominator is 0, "divide by 0" error will be displayed −
Division by 0
设置 $b=3 ,它将导致 TypeError,因为期望除数函数返回整数,但是除法结果为 float 。
Set $b=3 which will cause TypeError because divide function is expected to return integer but division results in float.
divide(): Return value must be of type int, float returned
如果仅将一个变量传递给除数函数,通过更改 $res=divide($a); ,这将导致 ArgumentCountError −
If just one variable is passed to divide function by changing $res=divide($a); this will result in an ArgumentCountError −
Too few arguments to function divide(), 1 passed in C:\xampp\htdocs\hello.php on line 16 and exactly 2 expected
如果其中一个参数不是整数,则是 InvalidArgumentException 的情况。将 $b 更改为字符串 −
If one of arguments is not integer, it is a case of InvalidArgumentException. Change $b to a string −
Invalid type of arguments
The Finally Block
finally 块也可能在 catch 块后面或代替 catch 块指定。无论是否抛出异常, finally 块内的代码都将在 try 和 catch 块之后始终执行,并且在恢复正常执行之前执行。
A finally block may also be specified after or instead of catch blocks. Code within the finally block will always be executed after the try and catch blocks, regardless of whether an exception has been thrown, and before normal execution resumes.
try {
if ($y == 0) {
throw new Exception("Division by Zero");
}
$z = $x/$y;
echo "<h3>x = $x y = $y Division = $z </h3><br>";
}
catch (Exception $e) {
echo "<h3> Exception: " . $e->getMessage(). "</h3>";
}
finally {
echo "<h3>End of try - catch - finally</h3>";
}
它将生成如下输出:
It will produce the following output −
Case 1 −
Case 1 −
x = 10 y = 5 Division = 2
End of try - catch – finally
Case 2 −
Case 2 −
X=10 y=0
Exception: Division by Zero
End of try - catch – finally
Finally With Return
try 块或 catch 块(或两者)包含 return 语句时,有 finally 块的特殊行为。通常, return 语句会导致程序控制权返回到调用位置。但是,对于具有 return 的 try/catch 块的函数, finally 块中的语句将在返回之前首先执行。
There is a peculiar behaviour of finally block when either try block or catch block (or both) contain a return statement. Normally a return statement causes the control of program to go back to the calling position. However, in case of a function with try/catch block with return, the statements in finally block are executed first before returning.
Example
在以下示例中,div() 函数有一个“try-catch-finally”构造。没有异常的 try 块返回除法结果。如果发生异常,则 catch 块返回错误消息。但是,无论哪种情况, finally 块中的语句都将首先执行。
In the following example, the div() function has a "try-catch-finally" construct. The try block without exception returns result of division. In case of exception, the catch block returns an error message. However, in either case, the statement in the finally block is executed first.
<?php
function div($x, $y) {
try {
if ($y==0)
throw new Exception("Division by 0");
else
$res=$x/$y;;
return $res;
}
catch (Exception $e) {
return $e->getMessage();
}
finally {
echo "This block is always executed\n";
}
}
$x=10;
$y=0;
echo div($x,$y);
?>
它将生成以下 output −
It will produce the following output −
This block is always executed
Division by 0
PHP - Bugs Debugging
PHP 代码中的故障是指程序中导致意外结果或崩溃的错误。在用户这样做之前寻找错误的过程中的系统方法称为调试。本章给出了在 PHP 代码中跟踪错误的一些重要提示。
A bug in a PHP code refers to an error in the program that leads to unexpected results or crash. A systematic approach towards the process of finding bugs before users do is called debugging. In this chapter, some important tips to trace bugs in a PHP code are given.
程序很少在第一次工作时就正确运行。您的程序中会出现很多问题,导致 PHP 解释器生成错误消息。您可以选择这些错误消息的去向。消息可以连同其他程序输出一起发送到网络浏览器。它们还可以包含在“Web 服务器错误日志”中。
Programs rarely work correctly the first time. Many things can go wrong in your program that can cause the PHP interpreter to generate an error message. You have a choice about where those error messages go. The messages can be sent along with other program output to the web browser. They can also be included in the "web server error log".
要使错误消息显示在浏览器中,将“display_errors”配置指令设置为 ON。确保在“php.ini”文件中启用了以下设置。
To make error messages display in the browser, set the "display_errors" configuration directive to ON. Ensure that the following settings are enabled in the "php.ini" file.
display_errors=On
display_startup_errors=On
您还可以使用 ini_set() function 覆盖“pnp.ini”配置 −
You can also use the ini_set() function to override the "pnp.ini" configuration −
ini_set('display_errors', 1)
ini_set('display_startup_errors', 1)
要将错误发送到 Web 服务器错误日志中,请将“log_errors”设置为 ON。如果您同时想要这两个地方都有错误消息,则可以将两者都设置为 On。
To send errors to the web server error log, set "log_errors" to ON. You can set them both to On if you want error messages in both places.
PHP 定义了一些 constants ,可用于设置 error_reporting 的值,以只报告特定类型的错误 −
PHP defines some constants that you can use to set the value of error_reporting such that only errors of certain types get reported −
-
E_ALL (for all errors except strict notices)
-
E_PARSE (parse errors)
-
E_ERROR (fatal errors)
-
E_WARNING (warnings)
-
E_NOTICE (notices)
-
E_STRICT (strict notices)
在编写 PHP 程序时,最好使用 PHP 感知编辑器,例如 BBEdit 或 Emacs。这些编辑器的一个特色功能是语法高亮显示。它会根据程序中不同部分的内容更改这些部分的颜色。例如,字符串为粉色,if 和 while 等关键词为蓝色,注释为灰色,变量为黑色。
While writing your PHP program, it is a good idea to use PHP-aware editors like BBEdit or Emacs. One of the special features of these editors is syntax highlighting. It changes the color of different parts of your program based on what those parts are. For example, strings are pink, keywords such as if and while are blue, comments are grey, and variables are black.
Microsoft 的 VS Code 也是编辑 PHP 代码的不错选择。如果您安装 VS Code 扩展 Intelephense,在编辑器窗口中输入 PHP 语句时,将获得类型提示和错误消息。
VS Code from Microsoft is also a good choice for editing PHP code. If you install VS Code extension Intelephense, you will get type hints and error message as you enter PHP statements in the editor window.
另一个功能是引号和括号匹配,它有助于确保您的引号和括号处于平衡状态。当您键入“}”之类的闭合定界符时,编辑器将高亮显示它匹配的开“{”。
Another feature is quote and bracket matching, which helps to make sure that your quotes and brackets are balanced. When you type a closing delimiter such as "}", the editor highlights the opening "{" that it matches.
Points to Check while Debugging a Code
在调试程序代码时,需要验证以下几点 −
One needs to verfity the following points while debugging a program code −
Missing Semicolons
每条 PHP 语句都以分号 (;) 结束。PHP 不会停止读取语句,直到它到达分号。如果您在行的末尾省略分号,PHP 将继续在下一行读取该语句。
Every PHP statement ends with a semicolon (;). PHP doesn’t stop reading a statement until it reaches a semicolon. If you leave out the semicolon at the end of a line, PHP continues reading the statement on the following line.
Not Enough Equal Signs
当您询问比较语句中的两个值是否相等时,您需要两个等号 (==)。使用一个等号是一个常见的错误。
When you ask whether two values are equal in a comparison statement, you need two equal signs (==). Using one equal sign is a common mistake.
Misspelled Variable Names
如果您拼写错误了一个变量,那么 PHP 将其理解为一个新变量。请记住:对于 PHP 来说,$test 并不是与 $Test 相同的变量。
If you misspelled a variable then PHP understands it as a new variable. Remember: To PHP, $test is not the same variable as $Test.
Missing Dollar Signs
变量名中缺少美元符号确实很难看出来,但至少它通常会导致一条错误消息,以便您知道在哪里查找问题。
A missing dollar sign in a variable name is really hard to see, but at least it usually results in an error message so that you know where to look for the problem.
Troubling Quotes
引号可能太多、太少或类型错误。因此,检查引号是否数量平衡。
You can have too many, too few, or the wrong kind of quotes. So check for a balanced number of quotes.
Array Index
PHP 中的数组是一个项的集合,每个项都分配了一个从 0 开始的增量索引。
An array in PHP is a collection of items, each item assigned an incrementing index starting with 0.
此外,正确处理所有错误并将所有跟踪消息定向到系统日志文件中,以便在发生任何问题时将其记录到系统日志文件中,这样您就可以调试该问题。
Moreover, handle all the errors properly and direct all trace messages into system log file so that if any problem happens then it will be logged into system log file and you will be able to debug that problem.
PHP For C Developers
如果您有 C 编程的先验知识,学习 PHP 会变得容易得多,尤其是在基础方面。尽管 PHP 与 C 非常相似,但它捆绑了许多特定于 Web 的库,所有这些库都直接连接到您偏爱的 Web 服务器。
If you have a prior knowledge of C programming, learning PHP becomes a lot easier, especially the basics. Although PHP is a lot like C, it is bundled with a whole lot of Web-specific libraries, with everything hooked up directly to your favorite Web server.
考虑 PHP 的最简单方法是将其视为可以嵌入 HTML 文档中经过解释的 C。PHP 脚本也可以从命令行执行,很像 C 程序。
The simplest way to think of PHP is as interpreted C that you can embed in HTML documents. PHP script can also be executed from the command line, much like a C program.
语句和函数定义的语法应该很熟悉,但变量总是以 $ 作为前缀,函数不需要单独的原型。
The syntax of statements and function definitions should be familiar, except that variables are always preceded by $, and functions do not require separate prototypes.
让我们看一下 PHP 和 C 中的一些异同 −
Let us take a look at some of the similarities and differences in PHP and C −
Similarities Between C and PHP
与 C 类似,PHP 代码不区分空格,语句以分号终止。
Similar to C, PHP Code is blank insensitive, statements are terminated with semicolons.
函数调用具有相同的结构
function calls have the same structure
my_function(expression1, expression2) {
Statements;
}
大括号用于将多个语句放入块中。
Curly brackets are used to put multiple statements into blocks.
PHP 支持 C 和 C++ 样式的注释 (/* */ 和 //),还支持 Perl 和 shell 脚本样式 (#)。
PHP supports C and C++-style comments (/* */ as well as //), and also Perl and shell-script style (#).
Operators − 赋值运算符 (=, =, *= 等等)、布尔运算符 (&&, ||, !) 、比较运算符 (<,>, ⇐, >=, ==, !=) 以及基本算术运算符(, -, *, /, %)在 PHP 中的行为与 C 中的行为相同。
Operators − The assignment operators (=, =, *=, and so on), the Boolean operators (&&, ||, !), the comparison operators (<,>, ⇐, >=, ==, !=), and the basic arithmetic operators (, -, *, /, %) all behave in PHP as they do in C.
Control Structures − 基本控制结构 (if, switch, while, for) 的行为如同在 C 中,包括 break 和 continue。一个值得注意的区别是,PHP 中的 switch 可以接受字符串作为 case 标识符。
Control Structures − The basic control structures (if, switch, while, for) behave as they do in C, including supporting break and continue. One notable difference is that switch in PHP can accept strings as case identifiers.
PHP 还具有 foreach 循环结构,用于遍历诸如数组的集合。
PHP also has foreach looping construct that traverses the collections such as arrays.
Function Names − 在您浏览文档时,您会看到许多看似与 C 函数相同的函数名称。
Function Names − As you peruse the documentation, you.ll see many function names that seem identical to C functions.
Differences Between C and PHP
Dollar Sign − 所有变量名称均以 $ 为前缀。无需在赋值前声明变量,并且它们没有固有类型。PHP 是一种动态类型语言,而 C 是一种静态类型语言。
Dollar Sign − All variable names are prefixed with a leading $. Variables do not need to be declared in advance of assignment, and they have no intrinsic type. PHP is a dynamically typed language, as against C being a statically typed language.
Types − PHP 只有两种数值类型:整数(对应于 C 中的 long)和 double(对应于 C 中的 double)。在 PHP 中,float 等同于 double。字符串的长度是任意的。PHP 中没有单独的 char 类型,如在 C 中的情况。
Types − PHP has only two numerical types: integer (corresponding to a long in C) and double (corresponding to a double in C). In PHP, float is synonymous to double. Strings are of arbitrary length. There is no separate char type in PHP, as is the case in C.
Type Conversion − C 是一种强类型语言,因为变量的类型必须在使用前声明,并且在编译时检查类型。而 PHP 是一种弱类型语言,在编译时不检查类型,类型错误通常不会在运行时发生。相反,变量和值会根据需要自动跨类型转换。
Type Conversion − C is a strongly typed language, as type of a variable must be declared before using, and the types are checked at compile time. PHP on the other hand, is a weakly typed language, types are not checked at compile time, and type errors do not typically occur at runtime either. Instead, variables and values are automatically converted across types as needed.
Arrays − 数组具有与 C 数组语法表面上相似的语法,但它们的实现方式完全不同。在 C 中,数组是相似数据类型的集合。在 PHP 数组中,项可以是不同类型的。PHP 数组实际上是关联数组或哈希,索引可以是数字或字符串。它们无需预先声明或分配。
Arrays − Arrays have a syntax superficially similar to C’s array syntax, but they are implemented completely differently. In C, an array is a collection of similar data types. In a PHP array, the items may be of different types. PHP arrays are actually associative arrays or hashes, and the index can be either a number or a string. They do not need to be declared or allocated in advance.
No Struct Type − C 中的 struct 关键字用于定义新的数据类型。PHP 中没有 struct 关键字或其等效项,部分原因是数组和对象类型一起使它变得不必要。PHP 数组的元素不必是一致的类型。
No Struct Type − The struct keyword in C is used to define a new data type. There is no struct keyword or its equivalent in PHP, partly because the array and object types together make it unnecessary. The elements of a PHP array need not be of a consistent type.
No Pointers − 指针是 C 中的一个重要概念。PHP 中没有指针可用,尽管无类型变量扮演着类似的角色。与 C 不同,PHP 支持变量引用。您还可以在某种程度上模拟函数指针,方法是函数名称可以存储在变量中并使用变量而不是文字名称进行调用。
No Pointers − Pointers are an important concept in C. There are no pointers available in PHP, although the tapeless variables play a similar role. Unlike C, PHP does support variable references. You can also emulate function pointers to some extent, in that function names can be stored in variables and called by using the variable rather than a literal name.
No Prototypes − 无需在定义其实现之前声明函数,只要可在当前代码文件或包含的文件的某个位置找到该定义。相反,C 函数必须在使用之前定义。
No Prototypes − Functions do not need to be declared before their implementation is defined, as long as the definition can be found somewhere in the current code file or included files. On the contrary, a C function must defined before it is used.
No main() − 在 C 程序中,main() 函数是入口点,无论它出现在代码的哪个位置。而 PHP 程序从脚本中的第一个语句开始执行
No main() − In a C program, the main() function is the entry point, irrespective of where it is present in the code. A PHP program on the other hand starts execution from the first statement in the script
Memory Management − PHP 引擎实际上是一个垃圾回收环境(引用计数),并且在小型脚本中无需执行任何取消分配。您应该自由分配新结构——例如新的字符串和对象实例。在 PHP5 中,可以为对象定义析构函数,但 C/C++ 中没有 free 或 delete 关键字。在释放内存之前,当对对象的最后一个引用消失时,调用析构函数。
Memory Management − The PHP engine is effectively a garbage-collected environment (reference-counted), and in small scripts there is no need to do any deallocation. You should freely allocate new structures - such as new strings and object instances. IN PHP5, it is possible to define destructor for objects, but there is are no free or delete keywords as in C/C++. Destructor are called when the last reference to an object goes away, before the memory is reclaimed.
Compilation and Linking − PHP 是解释性语言。因此,不会创建 PHP 脚本的编译版本。C 程序先被编译以获取目标代码,然后链接到必需的库以构建一个可执行文件。PHP 脚本没有单独的编译步骤。PHP 脚本无法变成自执行文件。
Compilation and Linking − PHP is an interpreted language. Hence, the compiled version of PHP script is not created. A C program is first compiled to obtain the object code, which is then linked to the required libraries to build an executable. There is no separate compilation step for PHP scripts. A PHP script cannot be turned into a self executable.
Permissiveness − 总的来说,PHP 比 C 更宽容(尤其是在其类型系统中),因此它会让您摆脱新的错误类型。意外结果比错误更常见。
Permissiveness − As a general matter, PHP is more forgiving than C (especially in its type system) and so will let you get away with new kinds of mistakes. Unexpected results are more common than errors.
PHP For PERL Developers
PERL 是一种动态类型、高级且通用的编程语言。通常认为 PERL 是实用抽取和报告语言的缩写。而 PHP 也是一种通用脚本语言。最初 PHP 曾是“个人主页”的缩写,但如今它已被公认为一个递归缩写“PHP:超文本预处理器”。
PERL is a dynamically typed, high level and general-purpose programming language. It is normally believed that PERL is an acronym for Practical Extraction and Reporting Language. PHP on the other hand is also a general-purpose scripting language. Initially PHP used to be a short for Personal Home Page’, but these days it has now been recognized as a recursive acronym ‘PHP: Hypertext Preprocessor’.
本章节概述了 PHP 与 Perl 之间的主要相似性和差异。这将帮助 Perl 开发人员非常快速地了解 PHP 并避免常见错误。
In this chapter, certain major similarities and differences in between PHP and PERL are listed. This will help PERL developers to understand PHP very quickly and avoid common mistakes.
Similarities between PERL and PHP
Perl 和 PHP 均为脚本语言。它们不用于提前在执行之前构建本机独立可执行文件。
Both Perl and PHP are scripting languages. They are not used to build native standalone executables in advance of execution.
早期的 PHP 版本受到 Perl 的启发。PHP 的基本语法与 Perl 非常相似。两者都与 C 共享许多语法特性。它们的代码对空白不敏感,每条语句都以分号结尾。
Early versions of PHP were inspired by PERL. PHP’s basic syntax is very similar to PERL. Both share a lot of syntactic features with C. Their code is insensitive to whitespace, Each statement is terminated by semicolons.
PHP 和 Perl 都使用大括号将多个语句组织成一个单一代码块。函数调用以函数名开头,后跟用括号括起来并用逗号分隔的实际参数,在两种情况下都一样。
Both PHP and PERL use curly braces to organize multiple statements into a single block. Function calls start with the name of the function, followed by the actual arguments enclosed in parentheses and separated by commas, in both the cases.
-
All variables in PHP look like scalar variables in PERL: a name with a dollar sign ($) in front of it.
-
Since both the languages are dynamically typed, you don’t need to declare the type of a PHP as well as a PERL variable before using it.
-
In PHP, as in PERL, variables have no intrinsic type other than the value they currently hold. You can store either number or string in same type of variable.
-
Both PHP and Perl do more interpretation of double-quoted strings ("string") than of single-quoted strings ('string').
Differences between PERL and PHP
PHP 可以嵌入 HTML 中。虽然可以从命令行运行 PHP 脚本,但它更常用作 Web 服务器上的服务器端脚本语言,并用于生成网页。
PHP can be embedded inside HTML. Although it is possible to run a PHP script from the command line, it is more popularly used as a server-side scripting language on a Web server and used for producing Web pages.
如果你习惯了用 Perl 编写 CGI 脚本,则 PHP 中的主要区别在于你不再需要使用 print 或 heredoc 语句显式打印大块静态 HTML,而只需在 PHP 代码块之外编写 HTML 本身。
If you are used to writing CGI scripts in PERL, the main difference in PHP is that you no longer need to explicitly print large blocks of static HTML using print or heredoc statements and instead can simply write the HTML itself outside of the PHP code block.
No @ or % variables − PHP 仅有一种变量,它以美元符号 ($) 开头。该语言中的任何数据类型都可以存储在(标量或复合)这些变量中。在 Perl 中,数组变量以 @ 符号为前缀。此外,哈希变量以 % 符号为前缀。
No @ or % variables − PHP has one only kind of variable, which starts with a dollar sign ($). Any of the datatypes in the language can be stored in such variables, whether scalar or compound. In PERL, the array variable is prefixed with @ symbol. Also, the hash variable is prefixed by % symbol.
与 Perl 不同,PHP 有一种称为数组的单一数据类型,该类型可以是 indexed array 或 associative array ,与 hash in PERL 类似。
Unlike PERL, PHP has a single datatype called an array which can be an indexed array or associative array, which is similar to hash in PERL.
Function calls in PHP 看起来很像 subroutine calls in PERL 。另一方面,PHP 中的函数定义通常需要某种形式的正式参数列表,就像 C 或 Java 中一样,这与 Perl 不同。
Function calls in PHP look pretty much like subroutine calls in PERL. Function definitions in PHP, on the other hand, typically require some kind of list of formal arguments as in C or Java which is not the case in PERL.
Perl 中的 Scope of variables 默认情况下是全局的。这意味着顶级变量在子例程中是可见的。通常,这会导致在函数中滥用全局变量。在 PHP 中,函数定义中变量的范围默认情况下是局部的。
Scope of variables in PERL is global by default. This means that top-level variables are visible inside subroutines. Often, this leads to promiscuous use of globals across functions. In PHP, the scope of variables within function definitions is local by default.
PHP 中的 No module system 亦是如此。在 PHP 中,普通代码文件与用作导入库的代码文件之间没有真正区别。
No module system in PHP as such. In PHP there is no real distinction between normal code files and code files used as imported libraries.
Break and continue 而不是 next and last − PHP 更像是 C 语言,并且使用 break 和 continue,而不是像 Perl 中的 next 和 last 语句。
Break and continue rather than next and last − PHP is more like C language and uses break and continue instead of next and last statement as in PERL.
No elsif − 一个次要拼写差异:Perl 的 elsif 是 PHP 的 elseif。
No elsif − A minor spelling difference: Perl’s elsif is PHP’s elseif.
除了 Perl 样式 (#) single-line comments 之外,PHP 还提供 C 样式 multiline comments (/* comment */)和 Java 样式单行注释(// comment)。
In addition to Perl-style (#) single-line comments, PHP offers C-style multiline comments (/* comment */) and Java-style single-line comments (// comment).
Regular expressions − PHP 没有针对正则表达式特定于内置语法,但在其“Perl 兼容”正则表达式函数中具有相同的大部分功能。
Regular expressions − PHP does not have a built-in syntax specific to regular expressions, but has most of the same functionality in its "Perl-compatible" regular expression functions.
PHP – Frameworks
PHP生态系统中有很多Web框架。一些流行的PHP Web框架包括Laravel、Yii、CakePHP 等。尽管可以通过核心PHP构建Web应用程序,但开发人员越来越倾向于使用Web框架来快速开发应用程序。
The PHP ecosystem has a number of web frameworks. Some of the popular PHP web frameworks are Laravel, Yii, CakePHP etc. While one can build a web application with the help of core PHP, developers are increasingly preferring web frameworks for rapid application development.
What are Software Frameworks?
在计算机编程中, software framework 是库和类的集合,它提供了通用的功能,使开发人员能够更多地专注于应用程序逻辑,而不是为例行程序但繁琐的低级流程编写代码。
In computer programming, a software framework is a collection of libraries and classes that provide a generic functionality that allows the developer to concentrate more on the application logic, rather than writing code for routine but tedious low-level processes.
框架提供了一个可重复使用的软件环境,该环境能快速构建一个最小的工作模板应用程序。然后,开发人员可以修改这些模块以获得额外功能。
A framework provides a reusable software environment that quickly builds a minimal working template application. Developer can then modify these blocks for additional functionality.
每个框架都是为了帮助开发者构建一定类型应用程序而构建的。例如, web frameworks (有时也被称为“Web应用程序框架”)用于开发包括Web服务、Web资源和Web API在内的Web应用程序。
Each framework is built to help the developer build an application of a certain type. For example, web frameworks (sometimes also called "web application framework") are used for the development of web applications including web services, web resources, and web APIs.
在本章中,我们简要概述了其中一些流行的PHP frmeworks。
In this chapter, let us take a brief overview of some of the popular PHP frmeworks.
FuelPHP
FuelPHP ( https://fuelphp.com/ ) 基于模型视图控制器,并拥有创新插件。FuelPHP 支持基于路由的理论,您可以在其中直接路由到较近的输入uri,使闭包成为控制器并使其控制进一步执行。
FuelPHP (https://fuelphp.com/) works based on Model View Control and having innovative plug ins. FuelPHP supports router-based theory where you might route directly to a nearer the input uri, making the closure the controller and giving it control of further execution.
CakePHP
CakePHP ( https://cakephp.org/ ) 是以简单的方式构建简单而强大的Web应用程序的极好来源。PHP中内置的一些强大功能包括输入验证、SQL注入预防,它们可以使您的应用程序保持安全。
CakePHP (https://cakephp.org/) is a great source to build up simple and great web application in an easy way. Some great features which are inbuilt in PHP are input validation, SQL injection prevention that keeps you application safe and secure.
FlightPHP
FlightPHP ( https://flightphp.com/ ) 对于创建RESTful Web服务非常有帮助,并且它在MIT许可证下。
FlightPHP (https://flightphp.com/) is very helpful to make RESTful web services and it is under MIT licence.
Symfony
Symfony适用于专业开发人员,他们可以利用PHP组件(如Drupal、PHPBB、laravel、eX、OROCRM和piwik)来构建网站。
Symfony is for highly professional developers to build websites with PHP components such as Drupal, PHPBB, laravel, eX, OROCRM and piwik.
yiiFramework
YiiFramework ( https://www.yiiframework.com/ ) 基于具有高端安全性的Web 2.0。它包括输入验证、输出过滤和SQL注入。
YiiFramework (https://www.yiiframework.com/) works based on web 2.0 with high end security. It included input Validation, output filtering, and SQL injection.
Laravel
Laravel ( https://laravel.com/ ) 最适用于RESRful路由和轻量级blading引擎。Laravel已与某些经过良好测试且可靠的代码模块集成在一起。
Laravel (https://laravel.com/) is most useful for RESRful Routing and light weight bled tempting engine. Laravel has integrated with some of great components of well tested and reliable code.
Zend
Zend ( https://framework.zend.com/ ) 是一个现代框架,用于执行高端Web应用程序。它基于加密和安全编码工具工作。Zend Framework是专业PHP包的集合,安装量超过5.7亿次。
Zend (https://framework.zend.com/) is Modern frame work for performing high end web applications. This works based on Cryptographic and secure coding tools. Zend Framework is a collection of professional PHP packages with more than 570 million installations.
它可用于使用PHP 5.6+开发Web应用程序和服务,并使用广泛的语言功能提供100%面向对象代码。
It can be used to develop web applications and services using PHP 5.6+, and provides 100% object-oriented code using a broad spectrum of language features.
Codeigniter
对于需要简单而优雅的工具包来创建创新的Web应用程序的开发人员来说,Codeigniter可以轻松开发较小的占用空间。
Codeigiter is simple to develop small footprint for developer who need simple and elegant tool kit to create innovative web applications.
Phalcon PHP
Pholcon ( https://phalcon.io/en-us ) 是一个基于MVC并与创新架构集成以提高执行速度的PHP框架。
Pholcon (https://phalcon.io/en-us) is a PHP framework that works based on MVC and integrated with innovative architecture to do perform faster.
PHPixie
PHPixie ( https://phpixie.com/ ) 基于MVC,专为快速和可靠地开发网站而设计。
PHPixie (https://phpixie.com/) works based on MVC and designed for fast and reliability to develop web sites.
Core PHP vs Frameworks
PHP是目前最流行的用于Web应用程序开发的服务器端编程语言,近75%的网站使用PHP(以其核心形式或可用的各种PHP框架之一)构建。为了在使用“核心PHP”或框架进行Web开发之间做出选择,我们需要了解两者之间的优缺点。
PHP is by far the most popular server-side programming language for web application development, with nearly 75% of websites using PHP either in its core form or one of the PHP frameworks available. To make a choice between using "core PHP" or frameworks for web development, we need to understand the pros and cons of both.
简单来说,用核心PHP开发Web应用程序就像通过在纸上写下每个步骤来手动解决数学问题一样。另一方面,使用框架类似于使用计算器等工具来解决问题。正如计算器一样,框架是用于快速开发应用程序的有用工具。
To give a simple analogy, developing a web application purely with core PHP is like solving a mathematical problem manually by writing down each step on the paper. On the other hand, using a framework is similar to using tools such as a calculator to solve a problem. Just as a calculator, the frameworks are useful tools for rapid application development.
Core PHP vs Frameworks – Pros and Cons
Web 框架(尤其是 PHP 框架)是一组一个或多个 PHP 库和类。它提供通用功能,从而允许开发人员更加专注于应用程序逻辑,而不是编写草稿代码。它提供可重用的软件环境,可以快速构建一个最小的工作模板应用程序。
A web framework, especially a PHP framework is a collection of one or more PHP libraries and classes. It provides a generic functionality that allows the developer to concentrate more on the application logic, rather than writing code scratch. It provides a reusable software environment that quickly builds a minimal working template application.
纯粹使用核心 PHP 开发 Web 应用程序具有自己的优点和缺点−
Developing a web application purely in core PHP has its own advantages and disadvantages −
-
It gives the developer better control and flexibility.
-
At the same time, for a larger application developed with core PHP only can become unwieldy, difficult to manage and maintain.
现在,让我们来看看使用 PHP 框架的优缺点−
Now, let’s turn to pros and cons of using PHP Frameworks −
-
A PHP framework such as Symfony, Laravel or Yii offers a more standardized approach towards web application development. With most of the routine and repetitive part handled by the framework, the developer can concentrate more on the application logic. Hence, there is lesser time wasted in debugging.
-
On the other hand, a framework is not as flexible compared to core PHP. The skeleton template of the application is readily made available by the framework, leaving the developer to customize the functionality only within the scope defined by the framework.
The MVC Architecture
大多数 Web 应用程序框架使用 MVC (模型、视图和控制器) architecture ,这通过将逻辑与样式分离,极大程度地简化了编写高质量、强大的代码。
Most of the web application frameworks employ the MVC (Model, View and Controller) architecture, which makes it a lot easier to write quality, robust code, by separating the logic from the style.
如果您希望为应用程序开发使用核心 PHP 特性,您可以在对象导向方法或模块化方法中自由选择,这取决于哪种方法更适合您。
If you wish to use the core PHP features for your application development, you are free to adopt an object oriented approach or a modular approach, whichever suits you.
Built-in Security Measures
PHP 框架提供 built-in security measures 以纳入 Web 应用程序中。
PHP frameworks offer built-in security measures to be incorporated in a web applications.
-
If you choose to develop an application with core PHP, you will have to provide the security measures explicitly.
-
Most of the frameworks however have a few external dependencies, which may leave the application rather vulnerable, as compared to a core PHP application which is a self-contained solution.
框架基于应用程序 may be a little slow 在性能方面,与核心 PHP 应用程序相比,特别是对于较小的应用程序。
A framework-based application may be a little slow when it comes to performance, as compared to core PHP application, especially for a smaller application.
Comparison: Core PHP vs Frameworks
两者的比较可以总结如下−
The comparison between the two can be summarized as follows −
-
For smaller applications, core PHP is preferrable over framework.
-
Framework offers rapid development and code reusability.
-
Frameworks are less flexible.
-
Using core PHP features, the developer has complete control.
-
For large applications, the MVC architecture is helpful.
-
Frameworks offer integrated authorization and authentication support. In a core PHP application, the security rules need to be explicitly defined.
PHP – Design Patterns
在软件工程理论中,“设计模式”一词通常是指可重复使用的解决方案,该解决方案可用作模板,以便开发应用来解决常见问题。你可以将软件设计模式视为开发软件解决方案时的正规最佳实践。
In the theory of software engineering, the term "Design patterns" generally refers to a reusable solution that can be used as a template for developing applications to address commonly occurring problems. You can consider the software design patterns as formalized best practices when developing software solutions.
大多数标准设计模式在使用 PHP 开发应用时可以非常有效地实现。在本章中,我们将了解如何在开发 PHP 应用时应用一些流行设计模式。
Most of the standard design patterns can be very effectively implemented in developing applications in PHP. In this chapter, we shall learn how to apply some of the popular design patterns in developing PHP applications.
Singleton Pattern
当你希望将某个类的对象实例化限制为仅一个实例时,单例设计模式很有用。名称“单例模式”源自数学中的单例概念。单例模式确保只有一个实例,在整个应用中可以全局访问它。
The singleton design pattern is useful when you want to restrict the instantiation of an object of a certain class to only one instance. The name "singleton pattern" comes from the concept of singleton in Mathematics. Singleton pattern ensures that there will be only one instance, having a global access to it throughout the application.
单例模式的典型应用是创建数据库连接对象,该对象必须在应用的生命周期中创建一次。
Typical application of singleton pattern is creation of a database connection object, which must be created once in the lifetime of an application.
Example
在以下代码中,DataBaseConnector 类只能实例化一次,否则将发出不允许重复对象的消息。
In the following code, the DataBaseConnector class can be instantiated only once, otherwise a message that disallows duplicate object will be issued.
<?php
class DataBaseConnector {
private static $obj;
private final function __construct() {
echo __CLASS__ . " object created for first time ". PHP_EOL;
}
public static function getConnect() {
if (!isset(self::$obj)) {
self::$obj = new DataBaseConnector();
return self::$obj;
} else {
echo "connection object could not be created again" . PHP_EOL;
}
}
}
$obj1 = DataBaseConnector::getConnect();
$obj2 = DataBaseConnector::getConnect();
var_dump($obj1 == $obj2);
?>
它将生成如下输出:
It will produce the following output −
DataBaseConnector object created for first time
connection object could not be created again
bool(false)
Factory Pattern
这是最常用的设计模式之一。在此模式中,你并不直接声明所需类的对象,而是提供了另一个类,其静态方法创建所需对象。
This is one of the most commonly used design patterns. In this pattern, you don’t declare the object of the desired class directly, but another class is provided whose static method creates the required object.
Example
以下示例演示了工厂设计模式的工作原理 −
The following example demonstrates how factory design pattern works −
<?php
class Automobile {
private $bikeMake;
private $bikeModel;
public function __construct($make, $model) {
$this->bikeMake = $make;
$this->bikeModel = $model;
}
public function getMakeAndModel() {
return $this->bikeMake . ' ' . $this->bikeModel;
}
}
class AutomobileFactory {
public static function create($make, $model) {
return new Automobile($make, $model);
}
}
$pulsar = AutomobileFactory::create('ktm', 'Pulsar');
print_r($pulsar->getMakeAndModel());
?>
它将生成如下输出:
It will produce the following output −
ktm Pulsar
Strategy Pattern
策略模式推荐使用以下方法:封装特定的算法族,允许负责实例化特定算法的客户端类。实现该模式的类不了解实际的实现。
The strategy pattern recommends an approach where you encapsulate specific families of algorithms allowing the client class responsible for instantiating a particular algorithm. The class that implements the pattern has no knowledge of the actual implementation.
Example
以下代码演示了策略模式的使用。我们有一个接口,其 case() 方法由两个不同的类以不同的方式实现。testdata 类的对象通过自己的 process() 方法间接调用各自的 case() 方法。
Here is a code that demonstrates the use of strategy pattern. We have an interface whose case() method is implemented differently by two different classes. The object of testdata class calls the respective case() methods indirectly through its own process() method.
<?php
interface example {
public function case($str);
}
class ucase implements example {
public function case($str) {
return strtoupper($str);
}
}
class lcase implements example {
public function case($str) {
return strtolower($str);
}
}
class testdata {
private $data;
public function __construct($input) {
$this->data = $input;
}
public function process(example $type) {
return $this->data = $type->case($this->data);
}
}
$str = "hello";
$obj = new testdata($str);
echo $obj->process(new ucase) . PHP_EOL;
$str = "HELLO";
echo $obj->process(new lcase);
?>
它将生成以下 output −
It will produce the following output −
HELLO
Hello
MVC Design Pattern
MVC(代表模型、视图和控制器)是一种非常流行的软件架构模式。大多数 PHP 网络(例如 Laravel、Symfony 等)都实现了 MVC 架构。
MVC, which stands for Model, View and Controller, is a very popular softeware architecture pattern. Most of the PHP networks such as Laravel, Symfony etc. implement the MVC architecture.
应用中每个层的角色分离如下 −
The separation of the role of each layer in an application is as follows −
-
Model − Refers to the data structure. In this case, the database.
-
View − Refers to the user interface. The HTML and CSS.
-
Controller − The "middleman" doing the processing. Accepts input from the view, and works with the model. Self-explanatory, the PHP scripts and libraries themselves.
视图充当 GUI,模型充当后端,而控制器则充当适配器。在这里,三个部分相互连接。它会传递数据并在彼此之间访问数据。
The View acts as the GUI, the Model acts as the back-end and the Control acts as an adapter. Here, three parts are interconnected with each other. It will pass the data and access the data between each other.
Example
让我们在以下示例中用纯 PHP、JavaScript 和 HTML 实现 MVC 设计模式 −
Let us implement the MVC design pattern in pure PHP, JavaScript and HTML in the example below −
应用的表示层是 view.php,它呈现 HTML 表单。用户将数据提交给控制器脚本。控制器返回的结果使用一些 JavaScript 在网页上呈现。
The presentation layer of the application is view.php, which renders a HTML form. The user submits the data to a controller script. The result returned by the controller is rendered on the web page with a bit of JavaScript
<!DOCTYPE html>
<html>
<head>
<title>View (User Interface)</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<form id="mysearch" action="controller.php" method="POST">
<input type="text" id = "nm" name="search" required>
<input type="submit" value="Search">
</form>
<div id="results"></div>
<script>
let results = document.getElementById("results");
results.innerHTML = "";
</script>
<?php
session_start();
if (isset($_SESSION['result'])) {
$arr=$_SESSION['result'];
foreach ($arr as $obj) {?>
<script>
results.innerHTML += "<div><?php echo $obj['id'] . "-" .
$obj['name'] . "</div>"; ?>";
</script>
<?php
}
}
?>
</body>
</html>
控制器脚本需要 model.php,并使用数据库对象,调用 select 方法从数据库中获取数据。结果存储在当前会话中,以便在视图页面上访问它。
The controller script requires model.php, and uses the database object, calls the select method to fetch data from the database. The result is stored in the current session so that it can be accessed on the view page.
<?php
session_start();
require "model.php";
$results = $_DB->select(
"SELECT * FROM `users` WHERE `name` LIKE ?",
["%{$_POST["search"]}%"]
);
$_SESSION['search'] = $_POST['search'];
$_SESSION['result'] = $results;
Header("Location: view.php", true);
?>
应用的模型层在“model.php”中编码。它使用 PDO 扩展与名为 mydb 的 mysql 数据库建立连接。
The model layer of the application is coded in "model.php". It establishes connection with mysql database named mydb, using PDO extension.
<?php
class DB {
public $error = "";
private $pdo = null;
private $stmt = null;
var $dsn="localhost";
var $dbName="myDB";
var $username="root";
var $password="";
function __construct () {
$this->pdo = new PDO("mysql:host=$this->dsn;dbname=$this->
dbName",$this->username,$this->password);
}
function __destruct () {
if ($this->stmt!==null) { $this->stmt = null; }
if ($this->pdo!==null) { $this->pdo = null; }
}
function select ($sql, $data=null) {
$this->stmt = $this->pdo->prepare($sql);
$this->stmt->execute($data);
return $this->stmt->fetchAll();
}
}
$_DB = new DB();
?>
后端 mydb 数据库必须有一个包含 ID 和 NAME 字段的 users 表。
The backend mydb database must have a users table with ID and NAME fields.
-- Table structure for table `users`
--
CREATE TABLE `users` (
`id` bigint(20) NOT NULL,
`name` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
--
-- Dumping data for table `users`
--
INSERT INTO `users` (`id`, `name`) VALUES
(21, 'Ahmad Shaikh'),
(24, 'Akshay Wadkar'),
(26, 'Bridget Wooten'),
(10, 'Coby Kelleigh'),
(20, 'Dashan Shah'),
(12, 'Elizabeth Taylor'),
(41, 'Issac Newton'),
(34, 'Julia Roberts'),
(31, 'Junior Mahmood'),
(32, 'Kailas Bende'),
(47, 'Karan Sharma'),
(16, 'Kenneth Sanders'),
(28, 'Kirstie Thomas'),
(33, 'Lawrence Murphy'),
(14, 'Leah Shan'),
(51, 'Marcus Best'),
(29, 'Maya Pande'),
(50, 'Nathaniel Khan'),
(6, 'Richard Breann'),
(54, 'Rowan Avalos'),
(3, 'Rusty Terry'),
(37, 'Sacha Gross'),
(27, 'Sally Castillo'),
(11, 'Sarah Sanders'),
(18, 'Seth Sonnel'),
(38, 'Shannon Peterson'),
(25, 'Shayan Clements'),
(49, 'Shoaib Vickers'),
(43, 'Simran Kaur'),
(35, 'Sulaiman Gilmour'),
(44, 'Taran Morin'),
(48, 'Taran Morin'),
(22, 'Thelma Kim'),
(8, 'Tillie Sharalyn'),
(36, 'Virgil Collier');
在浏览器中访问 "http://localhost/view.php" 以启动应用程序。输入与包含必需字母的名称相对应的搜索词。
Start the application by visiting "http://localhost/view.php" in the browser. Enter a search term corresponding to the names having required letters.
PHP – Filters
重要的是,在 PHP 应用程序中处理之前,验证以客户端请求的形式接收的输入数据。为了执行输入验证,PHP 中的 filter 扩展程序提供了一些 filter 函数,并由预定义的 filter 常量和标志支持。PHP 库的 filter 扩展程序还有助于通过 GET 或 POST 方法净化接收的输入。
It is important that the input data received in the form of client request is validated before processing in a PHP application. To perform input validation, the filter extension in PHP provides a number of filter functions, backed up by predefined filter constants and flags. The filter extension of PHP library also helps in sanitizing the input received by either GET or POST methods.
filter 扩展程序是一项强大的功能,有助于防止安全漏洞,例如 SQL 注入和跨站脚本攻击。扩展程序有两种类型的过滤器 −
The filter extension is a powerful feature that helps prevention of security vulnerabilities, such as SQL injection and cross-site scripting. The extension has two types of filters −
Validation Filters
验证过滤器检查数据是否符合某些标准。例如,您想确保用户正确地输入了 HTML 表单中的电子邮件字段。FILTER_VALIDATE_EMAIL 将确定数据是否有效的电子邮件地址。但是,验证过滤器不会更改数据本身。
Validation filters check if the data meets certain criteria. For example, you want to ensure that the user has correctly input an email field in the HTML form. The FILTER_VALIDATE_EMAIL will determine if the data is a valid email address. The validation filters, however, will not change the data itself.
Sanitization Filters
清理是指从输入中删除不需要的字符的过程。因此,它可以通过删除不需要的字符来更改数据。例如,传入 FILTER_SANITIZE_EMAIL 将删除不适合包含在电子邮件地址中的字符,而不会执行验证。
Sanitization refers to the process of removing undesired characters from the input. Hence, it may alter the data by removing undesired characters. For example, passing in FILTER_SANITIZE_EMAIL will remove characters that are inappropriate for an email address to contain, without performing validation.
Filter Flags
PHP 中的 filter 扩展程序定义了许多 filter flags 如下所示 −
The filter extension in PHP defines a number of filter flags as follows −
Sr.No |
ID & Description |
1 |
FILTER_FLAG_STRIP_LOW Strips characters that have a numerical value <32. |
2 |
FILTER_FLAG_STRIP_HIGH Strips characters that have a numerical value >127. |
3 |
FILTER_FLAG_STRIP_BACKTICK Strips backtick characters. |
4 |
FILTER_FLAG_ALLOW_FRACTION Allows a period (.) as a fractional separator in numbers. |
5 |
FILTER_FLAG_ALLOW_THOUSAND Allows a comma (,) as a thousands separator in numbers. |
6 |
FILTER_FLAG_ALLOW_SCIENTIFIC Allows an e or E for scientific notation in numbers. |
7 |
FILTER_FLAG_NO_ENCODE_QUOTES If this flag is present, single (') and double (") quotes will not be encoded. |
8 |
FILTER_FLAG_ENCODE_LOW Encodes all characters with a numerical value <32. |
9 |
FILTER_FLAG_ENCODE_HIGH Encodes all characters with a numerical value >127. |
10 |
FILTER_FLAG_ENCODE_AMP Encodes ampersands (&). |
11 |
FILTER_NULL_ON_FAILURE Returns null for unrecognized values. |
12 |
FILTER_FLAG_ALLOW_OCTAL Regards inputs starting with a zero (0) as octal numbers. |
13 |
FILTER_FLAG_ALLOW_HEX Regards inputs starting with 0x or 0X as hexadecimal numbers. |
14 |
FILTER_FLAG_EMAIL_UNICODE Allows the local part of the email address to contain Unicode characters. |
15 |
FILTER_FLAG_IPV4 Allows the IP address to be in IPv4 format. |
16 |
FILTER_FLAG_IPV6 Allows the IP address to be in IPv6 format. |
17 |
FILTER_FLAG_NO_PRIV_RANGE Fails validation for the following private IPv4 ranges: 10.0.0.0/8, 172.16.0.0/12 and 192.168.0.0/16. |
18 |
FILTER_FLAG_NO_RES_RANGE Fails validation for the following reserved IPv4 ranges: 0.0.0.0/8, 169.254.0.0/16, 127.0.0.0/8 and 240.0.0.0/4. Fails validation for the following reserved IPv6 ranges: ::1/128, ::/128, ::ffff:0:0/96 and fe80::/10. |
19 |
FILTER_FLAG_GLOBAL_RANGE Fails validation for non global IPv4/IPv6 ranges |
20 |
FILTER_FLAG_SCHEME_REQUIRED Requires the URL to contain a scheme part. |
21 |
FILTER_FLAG_HOST_REQUIRED Requires the URL to contain a host part. |
22 |
FILTER_FLAG_PATH_REQUIRED Requires the URL to contain a path part. |
23 |
FILTER_FLAG_QUERY_REQUIRED Requires the URL to contain a query string. |
24 |
FILTER_REQUIRE_SCALAR Requires the value to be scalar. |
25 |
FILTER_REQUIRE_ARRAY Requires the value to be an array. |
26 |
FILTER_FORCE_ARRAY If the value is a scalar, it is treated as array with the scalar value as only element. |
Filter Functions
过滤器扩展包括以下内容: filter functions −
The filter extension includes the following filter functions −
Sr.No |
ID & Description |
1 |
filter_has_var() Checks if variable of specified type exists |
2 |
filter_id() Returns the filter ID belonging to a named filter |
3 |
filter_input_array() Gets external variables and optionally filters them |
4 |
filter_input () Gets a specific external variable by name and filters it |
5 |
filter_list() Returns a list of all supported filters |
6 |
filter_var_array() Gets multiple variables and optionally filters them |
7 |
filter_var() Filters a variable with a specified filter |
Predefined Constants
以上函数使用一个名为 input_type 的参数,它是用来表示向 PHP 脚本提供输入用于筛选目的的预定义枚举常量之一。
The above functions use one parameter called input_type which is one of the predefined enumerated constants representing how the input has been provided to the PHP script for filtering purpose.
Constant |
Types |
INPUT_POST (int) |
POST Variables |
INPUT_GET (int) |
GET Variables |
INPUT_COOKIE (int) |
COOKIE Variables |
INPUT_ENV (int) |
ENV Variables |
INPUT_SERVER (int) |
SERVER Variables |
INPUT_SESSION (int) |
SESSION Variables |
INPUT_REQUEST (int) |
REQUEST Variables |
filter_has_var() function
filter_has_var() 函数检查指定类型变量是否存在。
The filter_has_var() function checks if variable of specified type exists.
filter_has_var(int $input_type, string $var_name): bool
input_type 是预定义常量之一,如 INPUT_GET、INPUT_POST、INPUT_COOKIE、INPUT_SERVER 或 INPUT_ENV;var_name 参数是用于检查的变量名称。该函数在成功时返回 true,失败时返回 false。
The input_type is one of predefined constants INPUT_GET, INPUT_POST, INPUT_COOKIE, INPUT_SERVER, or INPUT_ENV; where as the var_name parameter is the name of a variable to check. The function returns true on success or false on failure.
Example
访问 XAMPP 服务器上的以下 PHP 脚本。
Visit the following PHP script on the XAMPP server.
<?php
if (!filter_has_var(INPUT_GET, "email")) {
echo("Email not found");
} else {
echo("Email found");
}
?>
它将生成以下 output −
It will produce the following output −
Visit abc@example.com
Email found
filter_input() function
filter_input() 函数通过名称获取特定外部变量并根据应用的过滤器常量过滤它
The filter_input() function gets a specific external variable by name and filters it accorfing to the applied filter constant
filter_input(
int $type,
string $var_name,
int $filter = FILTER_DEFAULT,
array|int $options = 0
): mixed
type 参数是常量之一,如 INPUT_GET、INPUT_POST、INPUT_COOKIE、INPUT_SERVER 或 INPUT_ENV。第二个参数是 var_name,这是用于获取的变量名称。你可以使用要应用的过滤器。使用任何预定义筛选标志。如果省略,则使用 FILTER_DEFAULT
The type parameter is one of the constants INPUT_GET, INPUT_POST, INPUT_COOKIE, INPUT_SERVER, or INPUT_ENV. Second parameter is var_name, the name of a variable to get. You can use the filter to be applied. Use any of the predefined filter flags. If omitted, FILTER_DEFAULT will be used
该函数在成功时返回所请求变量的值,如果过滤器失败则返回 false,如果未设置 var_name 变量则返回 null。
The function returns the value of the requested variable on success, false if the filter fails, or null if the var_name variable is not set.
Example
请看以下示例:
Take a look at the following example −
<?php
if (!filter_input(INPUT_GET, "email", FILTER_VALIDATE_EMAIL)) {
echo("Email is not valid");
} else {
echo("Email is valid");
}
?>
它将生成以下 output −
It will produce the following output −
如果你使用 URL http://localhost/hello.php?email= abc@example.com ,
If you use the URL abc@example.com,
Email is valid
如果 URL 为 http://localhost/hello.php?email=a b c@example.com ,
If the URL is http://localhost/hello.php?email=a b c@example.com,
Email is not valid
你还可以针对通过 POST 方法接收的输入使用 INPUT_POST 类型 −
You can also use INPUT_POST type for validating the input received through the POST method −
<?php
if (!filter_input(INPUT_POST, "email", FILTER_VALIDATE_EMAIL)) {
echo("Email is not valid");
} else {
echo("Email is valid");
}
?>
要使用 POST 请求传递数据,打开命令提示符,并使用以下 CURL 命令
To pass data with POST request, open the command prompt, and use the following CURL command
curl -X POST -d "{\"email\": \"a@b.com\"}" http://localhost/hello.php
filter_list() function
filter_list() 函数返回所有受支持的过滤器的列表
The filter_list() function returns a list of all supported filters
filter_list(): array
Example
该函数返回所有受支持的过滤器的名称数组,如果没有这样的过滤器,则返回空数组。
The function returns an array of names of all supported filters, empty array if there are no such filters.
<?php
print_r(filter_list());
?>
它将生成以下 output −
It will produce the following output −
Array
(
[0] => int
[1] => boolean
[2] => float
[3] => validate_regexp
[4] => validate_domain
[5] => validate_url
[6] => validate_email
[7] => validate_ip
[8] => validate_mac
[9] => string
[10] => stripped
[11] => encoded
[12] => special_chars
[13] => full_special_chars
[14] => unsafe_raw
[15] => email
[16] => url
[17] => number_int
[18] => number_float
[19] => add_slashes
[20] => callback
)
filter_input_array() function
filter_input_array() 获取外部变量,并可选地对其进行筛选。
The filter_input_array() gets external variables and optionally filters them.
filter_input_array(int $type, array|int $options = FILTER_DEFAULT,
bool $add_empty = true): array|false|null
此函数用于在不重复调用 filter_input() 的情况下检索多个值。
This function is useful for retrieving many values without repetitively calling filter_input().
type 参数是 INPUT_GET、INPUT_POST、INPUT_COOKIE、INPUT_SERVER 或 INPUT_ENV 之一。
The type parameter is one of INPUT_GET, INPUT_POST, INPUT_COOKIE, INPUT_SERVER, or INPUT_ENV.
options 参数是用于定义参数的数组。有效键是包含变量名称的字符串,有效值是过滤器类型或选择性指定过滤器、标志和选项的数组。此参数也可以是用于保存过滤器常量的整数。然后输入数组中的所有值都将通过该过滤器进行筛选。
The options parameter is an array defining the arguments. A valid key is a string containing a variable name and a valid value is either a filter type, or an array optionally specifying the filter, flags and options. This parameter can be also an integer holding a filter constant. Then all values in the input array are filtered by this filter.
此函数在成功时返回一个包含请求变量值数组。如果由 type 指定的输入数组未填充,则在未给出 FILTER_NULL_ON_FAILURE 标志的情况下函数返回 null,否则返回 false。对于其他失败,则返回 false。
The function returns an array containing the values of the requested variables on success. If the input array designated by type is not populated, the function returns null if the FILTER_NULL_ON_FAILURE flag is not given, or false otherwise. For other failures, false is returned.
Example
要在 HTTP 请求中包含数组,我们在“hello.html”中使用以下 HTML 表单,并通过 POST 方法发送。
To include an array in the HTTP request, we use the following HTML form in "hello.html", and send it by POST method.
<!DOCTYPE html>
<html>
<body>
<h1>Filter Input Array</h1>
<form action="hello.php" method="POST">
<p><label for="email">Enter your email:</label>
<input type="text" id="email" name="email"></p>
<p><label for="age">Enter your age<label>
<input type = "text" id="age" name="age"></p>
<input type="submit">
</form>
</body>
</html>
验证输入数组的 PHP 脚本如下所示 −
The PHP script to validate the input array is as follows −
<?php
$filters = array (
"age" => array ("filter"=>FILTER_VALIDATE_INT,
"options"=>array("min_range"=>20,"max_range"=>40) ),
"email" => FILTER_VALIDATE_EMAIL
);
print_r(filter_input_array(INPUT_POST, $filters));
?>
打开 HTML 表单,并将年龄填为 30, * abc@example.com* 作为电子邮件,结果将是一个数组,验证这两个输入 −
Open the HTML form and enter 30 as age, abc@example.com as email, the result will be an array, validating both the inputs −
Array ( [age] => 30 [email] => abc@example.com )
尝试给出无效的输入,如“age=15”。输出数组将显示 age 密钥的 null 值
Try giving invalid inputs such as "age=15". The output array will show a null value for age key
Array ( [age] => [email] => abc@example.com )
PHP – JSON
PHP 的标准发行版默认启用 JSON 支持。PHP 扩展实现 JavaScript 对象表示法 (JSON) 数据交换格式。PHP 解析器中的 JSON 扩展处理 JSON 数据。
Standard distributions of PHP have the JSON support enabled by default. The PHP extension implements the JavaScript Object Notation (JSON) data interchange format. The JSON extension in PHP parser handles the JSON data.
JSON(JavaScript 对象表示法)是一种轻量级、基于文本、与语言无关的数据交换格式。JSON 为便携式表示结构化数据定义了一组较小的格式化规则。它是一种基于文本的数据格式,对于人类和机器来说都很容易读取。
JSON (JavaScript Object Notation) is a lightweight, text-based, language-independent data interchange format. JSON defines a small set of formatting rules for the portable representation of structured data. It is a text based data format that is easy for the humans as well as machines to read.
PHP 版本 5.2 及更高版本中的 JSON 扩展提供了许多预定义的常量、JSON 相关函数以及 JsonException 类。
The JSON extension in PHP version 5.2 onwards provides a number of predefined constants, JSON related functions, and also a JsonException class.
PHP JSON Functions
PHP 具有以下 JSON 函数 −
PHP has the following JSON functions −
json_encode()
此函数返回一个字符串,其中包含所提供值的 JSON 表示形式。如果参数是数组或对象,则会递归序列化。
This function returns a string containing the JSON representation of the supplied value. If the parameter is an array or object, it will be serialized recursively.
json_encode(mixed $value, int $flags = 0, int $depth = 512): string|false
json_decode()
此函数采用 JSON 编码的字符串,并将其转换为 PHP 值。
This function takes a JSON encoded string and converts it into a PHP value.
json_decode(
string $json,
?bool $associative = null,
int $depth = 512,
int $flags = 0
): mixed
当此函数的关联参数为 true 时,JSON 对象将作为关联数组返回;当为 false 时,JSON 对象将作为对象返回。
When the associative parameter of this function is true, JSON objects will be returned as associative arrays; when false, JSON objects will be returned as objects.
编码/解码操作受所提供标志的影响。预定义常量及其整数值如下 −
The encode/decode operations are affected by the supplied flags. The predefined constants and their integer values are as below −
Predefined Constant |
Values |
JSON_HEX_TAG |
1 |
JSON_HEX_AMP |
2 |
JSON_HEX_APOS |
4 |
JSON_HEX_QUOT |
8 |
JSON_FORCE_OBJECT |
16 |
JSON_NUMERIC_CHECK |
32 |
JSON_UNESCAPED_SLASHES |
64 |
JSON_PRETTY_PRINT |
128 |
JSON_UNESCAPED_UNICODE |
256 |
json_last_error_msg()
此函数返回最近 json_encode() 或 json_decode() 调用的错误字符串。
This function returns the error string of the last json_encode() or json_decode() call.
json_last_error_msg(): string
如果没有发生错误,则返回“No error”消息。
"No error" message is returned if no error has occurred.
json_last_error()
此函数返回一个整数。
This function returns an integer.
json_last_error(): int
该函数返回一个整数,对应于以下常量之一 −
The function returns an integer corresponding to one of the following constants −
Sr.No |
Constant & Meaning |
1 |
JSON_ERROR_NONE No error has occurred |
2 |
JSON_ERROR_DEPTH The maximum stack depth has been exceeded |
3 |
JSON_ERROR_STATE_MISMATCH Invalid or malformed JSON |
4 |
JSON_ERROR_CTRL_CHAR Control character error, possibly incorrectly encoded |
5 |
JSON_ERROR_SYNTAX Syntax error |
6 |
JSON_ERROR_UTF8 Malformed UTF-8 characters, possibly incorrectly encoded |
7 |
JSON_ERROR_RECURSION One or more recursive references in the value to be encoded |
8 |
JSON_ERROR_INF_OR_NAN One or more NAN or INF values in the value to be encoded |
9 |
*JSON_ERROR_UNSUPPORTED_TYPE*A value of a type that cannot be encoded was given |
10 |
JSON_ERROR_INVALID_PROPERTY_NAME A property name that cannot be encoded was given |
11 |
JSON_ERROR_UTF16 Malformed UTF-16 characters, possibly incorrectly encoded |
Example
以下 PHP 代码将给定的数组编码为 JSON 表示,并将 JSON 字符串解码回 PHP 数组。
The following PHP code encodes a given array to JSON representation, and decodes the JSON string back to PHP array.
<?php
$arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);
$encoded = json_encode($arr);
echo "The initial array: " . PHP_EOL;
var_dump($arr);
echo "Encoded JSON: $encoded" . PHP_EOL;
$decoded = json_decode($encoded);
echo "Array obtained after decoding: " . PHP_EOL;
var_dump($decoded);
?>
它将生成以下 output −
It will produce the following output −
The initial array:
array(5) {
["a"]=>
int(1)
["b"]=>
int(2)
["c"]=>
int(3)
["d"]=>
int(4)
["e"]=>
int(5)
}
Encoded JSON: {"a":1,"b":2,"c":3,"d":4,"e":5}
Array obtained after decoding:
object(stdClass)#1 (5) {
["a"]=>
int(1)
["b"]=>
int(2)
["c"]=>
int(3)
["d"]=>
int(4)
["e"]=>
int(5)
}
PHP – Exceptions
在 7 之前的版本中,PHP 解析器在响应各种条件时曾经报告错误。每个错误曾经都是一个预定义类型。PHP7 已经更改了错误报告的机制。与传统的错误报告不同,现在大多数错误都是通过抛出错误异常来报告的。
Prior to version 7, PHP parser used to report errors in response to various conditions. Each error used to be of a certain predefined type. PHP7 has changed the mechanism of error reporting. Instead of traditional error reporting, most errors are now reported by throwing error exceptions.
PHP 中的异常处理机制与许多其他语言类似,并且使用 try, catch, throw 和 finally 关键字实现。
The exception handling mechanism in PHP is similar to many other languages, and is implemented with the try, catch, throw and finally keywords.
The Throwable Interface
PHP 中的异常实现了 Throwable interface 。Throwable 接口充当任何可通过 throw 语句引发对象的基类,包括 Error 和 Exception 对象。
Exceptions in PHP implements the Throwable interface. The Throwable interface acts as the base for any object that can be thrown via throw statement, including Error and Exception objects.
用户定义的类不能直接实现 Throwable 接口。相反,要声明一个用户定义的异常类,必须扩展 Exception class 。
A user defined class cannot implement Throwable interface directly. Instead, to declare a user defined exception class, it must extend the Exception class.
带有潜在异常的 PHP 代码被 try 块包围。如果找到异常对象,则抛出该对象,以方便捕获潜在的异常。每个 try 必须至少有一个对应的 catch 或 finally 块。此外,对应于 try 块可能有多个 catch/finally 块。
PHP code with potential exceptions is surrounded in a try block. An exception object is thrown if it is found, to facilitate the catching of potential exceptions. Each try must have at least one corresponding catch or finally block. Moreover, there may be more than one catch/finally blocks corresponding to a try block.
try {
// throw errors in the try-block
// if an error occurs we can throw an exception
throw new Exception('this is an error.');
}
catch(Exception $e) {
// catch the throws in the catch-block
// do something with the exception object, eg.
// display its message
echo 'Error message: ' .$e->getMessage();
}
如果抛出了异常并且没有 catch 块,那么异常会“冒泡”出现,直到找到一个匹配的 catch 块。如果调用堆栈一直向下绕回到全局范围而没有遇到一个匹配的 catch 块,那么将调用一个全局异常处理程序(如果已设置),否则程序将以一个致命错误终止。
If an exception is thrown and there is no catch block, the exception will "bubble up" until it finds a matching catch block. If the call stack is unwound all the way to the global scope without encountering a matching catch block, a global exception handler will be called (if it is set) otherwise the program will terminate with a fatal error.
set_exception_handler
如果异常没有在 try/catch 块中被捕获,则此函数将设置默认异常处理程序。在回调函数执行之后,程序执行将停止。
This function sets the default exception handler if an exception is not caught within a try/catch block. After the callback is executed, the program execution will stop.
set_exception_handler(?callable $callback): ?callable
当一个未捕获的异常发生时,$callback 参数是要被调用的函数的名称。在调用 set_exception_handler() 之前必须定义此函数。此处理程序函数需要接受一个参数,该参数将是抛出的异常对象。
The $callback parameter is the name of the function to be called when an uncaught exception occurs. This function must be defined before calling set_exception_handler(). This handler function needs to accept one parameter, which will be the exception object that was thrown.
此函数将返回以前定义的异常处理程序的名称,或者在出错时返回 NULL。如果之前没有定义处理程序,那么也将返回 NULL。
The function returns the name of the previously defined exception handler, or NULL on error. If no previous handler was defined, NULL is also returned.
Example
请看以下示例:
Take a look at the following example −
<?php
function handler($ex) {
echo "Uncaught exception is : " , $ex->getMessage(), "\n";
}
set_exception_handler('handler');
throw new Exception('Not Found Exception');
echo "not included Executed\n";
?>
它将生成以下 output −
It will produce the following output −
Uncaught exception is : Not Found Exception
SPL Exceptions
标准 PHP 库包含预定义的异常 −
Standard PHP library contains predefined exceptions −
Sr.No |
Predefined Exceptions |
1 |
LogicException Exception that represents error in the program logic. |
2 |
BadFunctionCallException Exception thrown if a callback refers to an undefined function or if some arguments are missing. |
3 |
BadMethodCallException Exception thrown if a callback refers to an undefined method or if some arguments are missing. |
4 |
DomainException Exception thrown if a value does not adhere to a defined valid data domain. |
5 |
InvalidArgumentException Exception thrown if an argument is not of the expected type. |
6 |
LengthException Exception thrown if a length is invalid. |
7 |
OutOfRangeException Exception thrown when an illegal index was requested. |
8 |
RuntimeException Exception thrown if an error which can only be found on runtime occurs. |
9 |
OutOfBoundsException Exception thrown if a value is not a valid key. |
10 |
OverflowException Exception thrown when adding an element to a full container. |
11 |
RangeException Exception thrown to indicate range errors during program execution. An arithmetic error other than under/overflow. |
12 |
UnderflowException Exception thrown when performing an invalid operation on an empty container, such as removing an element. |
13 |
UnexpectedValueException Exception thrown if a value does not match with a set of values. |
User-defined Exception
你可以定义一个扩展了基本 Exception 类的自定义异常类。下面的脚本定义了一个称为 myException 的自定义异常类。如果 $num 的值小于 0 或者大于 100,则会抛出此类型的异常。
You can define a custom exception class that extends the base Exception class. Following script defines a custom exception class called myException. This type of exception is thrown if value of $num is less than 0 or greater than 100.
Example
Exception 类的 getMessage() 方法返回错误消息,而 getLine() 方法返回出现异常的代码行。
The getMessage() method of Exception class returns the error message and getLine() method returns line of code in which exception appears.
<?php
class myException extends Exception {
function message() {
return "error : ". $this->getMessage(). "in line no". $this->getLine();
}
}
$num=125;
try {
if ($num>100 || $num<0)
throw new myException("$num is invalid number");
else
echo "$num is a valid number";
}
catch (myException $m) {
echo $m->message();
}
?>
使用 $num=125 和 $num=90 运行上述代码,可获得一条错误消息和一条有效的数字消息 −
Run the above code with $num=125 and $num=90 to get an error message and a message of valid number −
error : 125 is invalid number in line no 10
PHP – Special Types
PHP 的两种数据类型 resource 和 NULL - 被归类为 special types 。资源类型的对象是指外部资源,例如数据库连接、文件流等。另一方面,NULL 数据类型是一个未分配任何数据的变量。在本章中,我们将详细了解这些类型。
PHP’s two data types – resource and NULL – are classified as special types. An object of resource type refers to external resources like database connection, file streams etc. On the other hand, a NULL data type is a variable without any data assigned to it. In this chapter, we shall learn more about these types.
Resource Type
PHP 程序经常需要与外部环境交互,例如数据库或磁盘文件等。这些在 PHP 中被视为资源。资源是一种特殊的数据类型,它指向任何此类外部资源。PHP 使用相关函数来创建这些资源。例如,fopen() 函数会打开一个磁盘文件,并将其引用存储在资源变量中。
A PHP program often needs to interact with an external environment such as a database, or a disk file etc. These are treated as resources in PHP. Resource is a special data type that refers to any such external resource. PHP uses relevant functions to create these resources. For example, fopen() function opens a disk file and its reference is stored in a resource variable.
PHP 的 Zend 引擎使用引用计数系统。因此,垃圾回收器会自动销毁引用计数为零的资源,并且无需手动释放资源数据类型所使用的内存。
PHP’s Zend engine uses reference counting system. Hence, a resource with zero reference count is destroyed automatically by garbage collector and the memory used by resource data type need not be freed manually.
不同的 PHP 内置函数返回各自的资源变量。随后,PHP 使用它们与相应的外部环境进行交互。例如,fopen() 函数返回一个文件资源,该资源用作文件句柄,并且通过此资源变量来促进对文件的读/写操作。
Different built-in PHP functions return respective resource variables. Subsequently, PHP uses them for interacting with the corresponding external environment. For example, the fopen() function returns a file resource, which acts as a file handle and the read/write operations on the file are facilitated by this resource variable.
下表总结了返回资源变量的不同函数 −
The following table summarizes different functions that return resource variables −
Resource Type |
Built-in functions |
Definition |
Produced |
Sold |
bzip2 |
bzopen() |
bzclose() |
Bzip2 file |
curl |
curl_init() |
curl_close() |
Curl session |
ftp |
ftp_connect(), |
ftp_close() |
FTP stream |
mssql link |
mssql_connect() |
mssql_close() |
Link to Microsoft SQL Server database |
mysql link |
mysql_connect() |
mysql_close() |
Link to MySQL database |
mysql result |
mysql_db_query(), |
mysql_free_result() |
MySQL result |
oci8 connection |
oci_connect() |
oci_close() |
Connection to Oracle Database |
ODBC link |
odbc_connect() |
odbc_close() |
Link to ODBC database |
pdf document |
pdf_new() |
pdf_close() |
PDF document |
stream |
opendir() |
closedir() |
Dir handle |
stream |
fopen(), tmpfile() |
fclose() |
File handle |
socket |
socket_create() |
Socket_close() |
Socket handle |
xml |
xml_parser_create() |
xml_parser_free() |
XML parser |
zlib |
gzopen() |
gzclose() |
gz-compressed file |
zlib.deflate |
deflate_init() |
None() |
incremental deflate context |
zlib.inflate |
inflate_init() |
None() |
incremental inflate context |
PHP 具有 get_resource_type() 函数,该函数返回变量的资源类型。
PHP has get_resource_type() function that returns resource type of a variable.
get_resource_type ( resource $handle ) : string
其中 $handle 是要获取其类型的资源变量。此函数返回一个对应于资源类型的字符串。
where $handle is the resource variable whose type is to be obtained. This function returns a string corresponding to resource type.
还有一个 get_resource_id() 函数,它为给定的资源提供一个整数标识符。
There is also get_resource_id() function an integer identifier for the given resource.
get_resource_id(resource $resource): int
Example
此函数提供了一种类型安全的方式来为给定的资源生成整数标识符。
This function provides a type-safe way for generating the integer identifier for a given resource.
<?php
$fp = fopen("hello.php", "r");
$resource = get_resource_type($fp);
$id = get_resource_id($fp);
echo "The resource type is : $resource The resource ID is : $id";
?>
它将生成以下 output −
It will produce the following output −
The resource type is : stream The resource ID is : 5
NULL type
在 PHP 中,没有值的变量称为 null 数据类型。这样的变量有一个值被定义为 NULL。变量可以显式分配 NULL 或使用 unset() 函数将其值设置为 null。
In PHP, a variable with no value is said to be of null data type. Such a variable has a value defined as NULL. A variable can be explicitly assigned NULL or its value been set to null by using unset() function.
$var=NULL;
可以将其他类型的变量强制转换为 null,尽管从 PHP 7.2 开始已弃用将 null 强制转换为其他类型。在早期版本中,使用 (unset)$var 语法完成强制转换
It is possible to cast variable of other type to null, although casting null to other type has been deprecated from PHP 7.2. In earlier versions, casting was done using (unset)$var syntax
Example
以下示例显示如何将 NULL 分配给变量
The following example shows how to assign NULL to a variable
<?php
$var=NULL;
var_dump($var);
?>
它将生成以下 output −
It will produce the following output −
NULL
Example
以下示例将 null 变量执行到其他主变量 −
The following example performs null variable to other primary variables −
<?php
$var = NULL;
var_dump( (int) $var);
var_dump((float)$var);
var_dump((bool) $var) ;
var_dump( (boolean) $var);
?>
它将生成以下 output −
It will produce the following output −
int(0)
float(0)
bool(false)
bool(false)
PHP – Hashing
“哈希” 一词表示一种对数据(特别是文本)进行加密以获得固定长度值的技术。PHP 库包含许多函数,可以通过应用不同的哈希算法(例如 md5、SHA2、HMAC 等)对数据执行哈希。获得的加密值称为原始密钥的哈希。
The term "hashing" represents a technique of encrypting data (specially a text) to obtain a fixed-length value. PHP library includes a number of functions that can perform hashing on data by applying different hashing algorithms such as md5, SHA2, HMAC etc. The encrypted value obtained is called as the hash of the original key.
哈希处理是一个单向过程,从某种意义上说,无法对哈希进行反转,因而无法获取原始键。
Processing of hashing is a one-way process, in the sense, it is not possible to reverse the hash so as to obtain the original key.
Applications of Hashing
哈希技术被有效用于以下目的:
The hashing technique is effectively used for the following purposes −
Password Authentication
我们通常会注册各种在线应用程序,例如 gmail、Facebook 等。您需要填写一个表格,您在其中为在线帐户创建密码。服务器会对您的密码进行哈希,哈希值存储在数据库中。当您登录时,提交的密码会被哈希,并与数据库中的密码进行比较。这让你的密码不会被盗用。
We often register for various online applications such as gmail, Facebook etc. You are required to fill up a form wherein you create a password for an online account. The server hashes your password and the hashed value is stored in the database. At the time of logging in, the password submitted is hashed and compared with the one in the database. This protects your password from being stolen.
Data Integrity
哈希的一个重要用途是验证数据没有被篡改。当您从互联网下载文件时,它会显示给您哈希值,您可以用此哈希值来与下载的文件进行对比,确保文件没有被破坏。
One of the important uses of hashing is to verify if the data has not been tampered with. When a file is downloaded from the internet, you are shown its hash value, which you can compare with the downloaded to make sure that the file has not been corrupted.
The Process of Hashing
可以由下图查看哈希处理过程:
The process of hashing can be represented by the following figure −
Hashing Algorithms in PHP
PHP 支持多个哈希算法:
PHP supports a number of hashing algorithms −
-
MD5 − MD5 is a 128-bit hash function that is widely used in software to verify the integrity of transferred files. The 128-bit hash value is typically represented as a 32-digit hexadecimal number. For example, the word "frog" always generates the hash "8b1a9953c4611296a827abf8c47804d7"
-
SHA − SHA stands for Secure Hash Algorithm. It’s a family of standards developed by the National Institute of Standards and Technology (NIST). SHA is a modified version of MD5 and is used for hashing data and certificates. SHA-1 and SHA-2 are two different versions of that algorithm. SHA-1 is a 160-bit hash. SHA-2 is actually a “family” of hashes and comes in a variety of lengths, the most popular being 256-bit.
-
HMAC − HMAC (Hash-Based Message Authentication Code) is a cryptographic authentication technique that uses a hash function and a secret key.
-
HKDF − HKDF is a simple Key Derivation Function (KDF) based on the HMAC message authentication code.
-
PBKDF2 − PBKDF2 (Password-Based Key Derivation Function 2) is a hashing algorithm that creates cryptographic keys from passwords.
Hash Functions in PHP
PHP 库包含多个哈希函数:
The PHP library includes several hash functions −
The hash_algos Function
此函数返回一个数字索引数组,其中包含受支持哈希算法的列表。
This function returns a numerically indexed array containing the list of supported hashing algorithms.
hash_algos(): array
The hash_file Function
该函数返回一个字符串,其中包含计算出的消息摘要,小写十六进制。
The function returns a string containing the calculated message digest as lowercase hexits.
hash_file(
string $algo,
string $filename,
bool $binary = false,
array $options = []
): string|false
algo 参数是所选哈希算法的类型(即 “md5”、“sha256”、“haval160,4”等)。 filename 是描述待哈希文件位置的 URL;支持 fopen 包装器。
The algo parameter is the type of selected hashing algorithm (i.e. "md5", "sha256", "haval160,4", etc.). The filename is the URL describing location of file to be hashed; supports fopen wrappers.
Example
请看以下示例:
Take a look at the following example −
<?php
/* Create a file to calculate hash of */
$fp=fopen("Hello.txt", "w");
$bytes = fputs($fp, "The quick brown fox jumped over the lazy dog.");
fclose($fp);
echo hash_file('md5', "Hello.txt");
?>
它将生成以下 output −
It will produce the following output −
5c6ffbdd40d9556b73a21e63c3e0e904
The hash() Function
hash() 函数生成哈希值(消息摘要):
The hash() function generates a hash value (message digest) −
hash(
string $algo,
string $data,
bool $binary = false,
array $options = []
): string
algo 参数是所选哈希算法的类型(即 “md5”、“sha256”、“haval160,4”等)。 data 参数是要进行哈希处理的消息。如果 binary 参数为 true ,它将输出原始二进制数据;“false” 输出小写十六进制。
The algo parameter is the type of selected hashing algorithm (i.e. "md5", "sha256", "haval160,4", etc..). The data parameter is the message to be hashed. If the binary parameter is "true", it outputs raw binary data; "false" outputs lowercase hexits.
Example
该函数返回一个字符串,其中包含计算出的消息摘要,小写十六进制。
The function returns a string containing the calculated message digest as lowercase hexits.
<?php
echo "Using SHA256 algorithm:" . hash('sha256', 'The quick brown fox jumped over the lazy dog.'). PHP_EOL;
echo "Using MD5 algorithm:",hash('md5', 'The quick brown fox jumped over the lazy dog.'), PHP_EOL;
echo "Using SHA1 algorithm:" . hash('sha1', 'The quick brown fox jumped over the lazy dog.');
?>
它将生成以下 output −
It will produce the following output −
Using SHA256 algorithm:68b1282b91de2c054c36629cb8dd447f12f096d3e3c587978dc2248444633483
Using MD5 algorithm:5c6ffbdd40d9556b73a21e63c3e0e904
Using SHA1 algorithm:c0854fb9fb03c41cce3802cb0d220529e6eef94e
PHP – Encryption
PHP 的早期版本包含 mcrypt 扩展,它提供了加密/解密功能。由于缺乏维护,mycrypt 扩展已从 PHP 7.2 版本中弃用并删除。PHP 现在包含 OpenSSL 库,该库具有广泛的功能来支持加密和解密功能。
Early versions of PHP included mcrypt extension, that provided encryption/decryption capabilities. Due to lack of maintenance, the mycrypt extension has been deprecated and removed from PHP 7.2 version onwards. PHP now includes OpenSSL library that has an extensive functionality to support encryption and decryption features.
OpenSSL 支持各种加密算法,例如 AES(高级加密标准)。可通过调用 openssl_get_cipher_methods() 函数获取所有受支持的算法。
OpenSSL supports various encryption algorithms such as AES (Advanced Encryption Standard). All the supported algorithms can be obtained by invoking openssl_get_cipher_methods() function.
OpenSSL 扩展中的两个重要函数为 -
The two important functions in OpenSSL extension are −
-
openssl_encrypt() − Encrypts data
-
openssl_decrypt() − Decrypts data
The openssl_encrypt() Function
此函数使用给定的方法和密钥加密给定数据,并返回原始或 base64 编码的字符串 -
This function encrypts the given data with given method and key, and returns a raw or base64 encoded string −
openssl_encrypt(
string $data,
string $cipher_algo,
string $passphrase,
int $options = 0,
string $iv = "",
string &$tag = null,
string $aad = "",
int $tag_length = 16
): string|false
该函数具有以下 parameters -
The function has the following parameters −
Sr.No |
Parameter & Description |
1 |
data The plaintext message data to be encrypted. |
2 |
cipher_algo The cipher method. |
3 |
passphrase The passphrase. If the passphrase is shorter than expected, padded with NULL characters; if the passphrase is longer than expected, it is truncated. |
4 |
options options is a bitwise disjunction of the flags OPENSSL_RAW_DATA and OPENSSL_ZERO_PADDING. |
5 |
iv A non-NULL Initialization Vector. |
6 |
tag The authentication tag passed by reference when using AEAD cipher mode (GCM or CCM). |
7 |
aad Additional authenticated data. |
8 |
tag_length The length of the authentication tag. Its value can be between 4 and 16 for GCM mode. |
该函数在成功时返回加密字符串,或在失败时返回 false 。
The function returns the encrypted string on success or false on failure.
The openssl_decrypt() Function
此函数获取原始或 base64 编码的字符串并使用给定的方法和密钥对其解密。
This function takes a raw or base64 encoded string and decrypts it using a given method and key.
openssl_decrypt(
string $data,
string $cipher_algo,
string $passphrase,
int $options = 0,
string $iv = "",
?string $tag = null,
string $aad = ""
): string|false
openssl_decrypt() 函数使用与 openssl_encrypt 函数相同的参数。
The openssl_decrypt() function uses the same parameters as the openssl_encrypt function.
此函数在成功时返回解密字符串,或在失败时返回 false。
This function returns the decrypted string on success or false on failure.
Example
请看以下示例:
Take a look at the following example −
<?php
function sslencrypt($source, $algo, $key, $opt, $iv) {
$encstring = openssl_encrypt($source, $algo, $key, $opt, $iv);
return $encstring;
}
function ssldecrypt($encstring, $algo, $key, $opt, $iv) {
$decrstring = openssl_decrypt($encstring, $algo, $key, $opt, $iv);
return $decrstring;
}
// string to be encrypted
$source = "PHP: Hypertext Preprocessor";
// Display the original string
echo "Before encryption: " . $source . "\n";
$algo = "BF-CBC";
$opt=0;
$ivlength = openssl_cipher_iv_length($algo);
$iv = random_bytes($ivlength);
$key = "abcABC123!@#";
// Encryption process
$encstring = sslencrypt($source, $algo, $key, $opt, $iv);
// Display the encrypted string
echo "Encrypted String: " . $encstring . "\n";
// Decryption process
$decrstring = ssldecrypt($encstring, $algo, $key, $opt, $iv);
// Display the decrypted string
echo "Decrypted String: " . $decrstring;
?>
它将生成以下 output −
It will produce the following output −
Before encryption: PHP: Hypertext Preprocessor
Encrypted String:
Decrypted String:
PHP is_null() Function
PHP 将 NULL 定义为其一种特殊数据类型。它表示某个变量尚未分配任何特定数据类型的任何值。它是 PHP 中的一个内置常量,用于指示任何对象或值的故意缺失。可以明确地将变量分配为 NULL,或使用 unset() 函数将值设置为 null。
PHP defines NULL as one of its special data types. It indicates that a certain variable has not been assigned a value any specific data type. It is a built-in constant in PHP and is used to indicate the intentional absence of any object or value. A variable can be explicitly assigned NULL or its value been set to null by using the unset() function.
The is_null() Function
PHP 提供了一个布尔函数 is_null() 来检查变量是否确实为 NULL 类型。
PHP provides a Boolean function is_null() to check if a variable is indeed of NULL type.
is_null(mixed $value): bool
Example 1
如果任何变量明确地分配为 NULL,则显然 is_null() 函数返回 true 。
If any variable is explicitly assigned NULL, obviously the is_null() function returns true.
<?php
$x = NULL;
echo "Variable \$x is null? ";
var_dump(is_null($x));
?>
它将生成以下 output −
It will produce the following output −
Variable $x is null? bool(true)
Example 2
如果 unset 具有特定值的变量,则 is_null() 函数也返回 true,但带有警告
If a variable with a certain value is unset, then too the is_null() function returns true, but with a warning
<?php
$x = "Hello";
unset($x);
echo "Variable \$x is null?\n";
var_dump(is_null($x));
?>
它将生成以下 output −
It will produce the following output −
Variable $x is null?
bool(true)
PHP Warning: Undefined variable $x in /home/cg/root/89262/main.php on line 5
Example 3
同样,如果您仅声明一个变量而不为其分配任何值,则 is_null() 函数会返回 true 并带有警告 -
Similarly, if you just declare a variable, without assigning any value to it, the is_null() function returns true with a warning −
<?php
$y;
echo "Variable \$y is null?\n";
var_dump(is_null($y));
?>
它将生成以下 output −
It will produce the following output −
Variable $y is null?
bool(true)
Warning: Undefined variable $y in hello.php on line 9
Example 4
您还可以使用相等操作符 (==) 检查变量是否为 NULL。
You can also use the equality operator (==) to check if a variable is NULL.
<?php
$x = NULL;
if ($x === NULL) {
echo '$x is NULL';
} else {
echo '$x is not NULL';
}
?>
它将生成以下 output −
It will produce the following output −
$x is NULL
Example 5
空字符串 "" 不认为等同于 NULL。因此,is_null() 函数以及 =="" 操作符返回 false 。请看以下示例 −
A null string "" is not considered equal to NULL. Hence, the is_null() function as well as the "==" operator return false. Take a look at the following example −
<?php
$y = "";
if ($y === NULL) {
echo '$y is NULL';
} else {
echo '$y is not NULL';
}
echo "$y is null?\n";
var_dump(is_null($y));
?>
它将生成以下 output −
It will produce the following output −
$y is not NULL is null?
bool(false)
PHP 中其他两个与 is_null() 函数相关的函数是 isset() 函数和 empty() 函数。
Two other functions in PHP that are relevant to is_null() function are the isset() function and the empty() function.
The isset() Function
isset() 函数确定变量是否已声明且与 NULL 不同。
The isset() function determines if a variable is declared and is different than NULL.
isset(mixed $var, mixed ...$vars): bool
Example
分配了 NULL 的变量被认为是未设置的。
A variable that is assigned NULL is considered as unset.
<?php
$x = NULL;
echo '$x is set? ';
var_dump(isset($x));
?>
它将生成以下 output −
It will produce the following output −
$x is set? bool(false)
请注意,空字符 ("\0") 不等同于 PHP null 常量。
Note that a null character ("\0") is not equivalent to the PHP null constant.
The empty() Function
empty() 函数检查一个变量是否被认为是空的。如果变量不存在或其值为 NULL,则该变量被认为是空的。如果变量不存在,empty() 不会生成警告。
The empty() function checks if a variable is considered to be empty. A variable is considered empty if it does not exist or if its value is NULL. empty() does not generate a warning if the variable does not exist.
Example 1
请看以下示例:
Take a look at the following example −
<?php
$x = NULL;
echo '$x is empty? ';
var_dump(empty($x));
$y;
echo '$y is empty? ';
var_dump(empty($y));
?>
它将生成以下 output −
It will produce the following output −
$x is empty? bool(true)
$y is empty? bool(true)
Example 2
如果变量设置为 "0"、NULL 或根本未设置,empty() 函数返回 true 。
The empty() function returns true if a variable is set to "0", NULL, or is not set at all.
<?php
$var = 0;
if (empty($var)) {
echo '$var is either 0, empty, or not set at all';
}
?>
它将生成以下 output −
It will produce the following output −
$var is either 0, empty, or not set at all
PHP – System Calls
PHP 内置函数库包括一类函数,用于在 PHP 代码中调用操作系统实用程序和外部程序。在本章中,我们将讨论用于执行系统调用的 PHP 函数。
PHP’s library of built-in function includes a category of functions that deal with invoking operating system utilities and external programs from within the PHP code. In this chapter, we shall discuss the PHP functions used to perform system calls.
The system() Function
system() 函数类似于 C 语言中的 system() 函数,可执行给定的命令并输出结果。
The system() function is similar to the system() function in C that it executes the given command and outputs the result.
system(string $command, int &$result_code = null): string|false
如果 PHP 作为服务器模块运行,system() 调用会尝试在每行输出之后自动刷新 Web 服务器的输出缓冲区。成功时返回命令输出的最后一行,失败时返回 false。
The system() call tries to automatically flush the web server’s output buffer after each line of output if PHP is running as a server module. It returns the last line of the command output on success, and false on failure.
Example
下面的 PHP 代码调用 Windows 操作系统的 DIR 命令,并显示当前目录中的文件列表。
The following PHP snippet invokes DIR command of Windows OS and displays the list of files in the current directory.
<?php
echo '<pre>';
// Outputs all the result of DOS command "dir", and returns
// the last output line into $last_line. Stores the return value
// of the shell command in $retval.
$last_line = system('dir/w', $retval);
// Printing additional info
echo '
</pre>
<hr />Last line of the output: ' . $last_line . '
<hr />Return value: ' . $retval;
?>
它将生成以下 output −
It will produce the following output −
Volume in drive C has no label.
Volume Serial Number is 7EE4-E492
Directory of C:\xampp\htdocs
[.] [..] applications.html bitnami.css
[dashboard] employee.csv favicon.ico hello.csv
hello.html hello.php homepage.php [img]
index.php [Langi] menu.php myform.php
myname.php new.png new.txt test.php
test.zip [TPcodes] uploadfile.php [webalizer]
welcome.png [xampp]
18 File(s) 123,694 bytes
8 Dir(s) 168,514,232,320 bytes free
Last line of the output: 8 Dir(s) 168,514,232,320 bytes free
Return value: 0
The shell_exec() Function
shell_exec() 函数等同于 PHP 的反引号操作符。它通过 shell 执行给定命令,并以字符串的形式返回完整的输出。
The shell_exec() function is identical to PHP’s backtick operator. It executes the given command via shell and return the complete output as a string
shell_exec(string $command): string|false|null
函数返回一个包含已执行命令输出的字符串,如果管道不能建立,则返回 false;如果发生错误或命令没有产生输出,则返回 null。
The function returns a string containing the output from the executed command, false if the pipe cannot be established or null if an error occurs or the command produces no output.
Example
在下面的代码中,我们使用 shell_exec() 函数在当前目录中获取扩展名为 ".php" 的文件列表 −
In the following code, we use shell_exec() function to obtain a list of files with ".php" as the extension in the current directory −
<?php
$output = shell_exec('dir *.php');
echo "<pre>$output</pre>";
?>
它将生成以下 output −
It will produce the following output −
Volume in drive C has no label.
Volume Serial Number is 7EE4-E492
Directory of C:\xampp\htdocs
10/26/2023 08:27 PM 73 hello.php
10/12/2023 10:40 AM 61 homepage.php
07/16/2015 09:02 PM 260 index.php
10/12/2023 10:39 AM 49 menu.php
09/25/2023 01:43 PM 338 myform.php
10/12/2023 10:49 AM 51 myname.php
10/26/2023 02:00 PM 369 test.php
09/25/2023 01:42 PM 555 uploadfile.php
8 File(s) 1,756 bytes
0 Dir(s) 168,517,771,264 bytes free
The exec() Function
exec() 函数以字符串参数的形式执行给定的命令。
The exec() function executes the given command as a string argument.
exec(string $command, array &$output = null,
int &$result_code = null):string|false
如果指定 $output 参数,它将是一个数组,其中填充了命令的每行输出。
The $output parameter, if specified, is an array that will be filled with every line of output from the command.
Example
在此情况下,我们使用 exec() 函数从程序内部调用 whoami 命令。whoami 命令返回用户名。
In this case, we use exec() function to call whoami command from inside the program. The whoami command returns the username.
<?php
// outputs the username that owns the running php/httpd process
// (on a system with the "whoami" executable in the path)
$output=null;
$retval=null;
exec('whoami', $output, $retval);
echo "Returned with status $retval and output:\n";
var_dump($output);
?>
它将生成以下 output −
It will produce the following output −
Returned with status 0 and output: array(1)
{ [0]=> string(13) "gnvbgl3\mlath" }
The passthru() Function
passthru() 函数执行外部程序并显示原始输出。虽然 passthru() 函数类似于 exec() 或 system() 函数,因为它执行一个命令,但是当操作系统命令的输出是需要直接传回浏览器的二进制数据时,它应该用在它们的地方。
The passthru() function executes an external program and display raw output. Though the passthru() function is similar to the exec() or system() function in that it executes a command, it should be used in their place when the output from the OS command is binary data which needs to be passed directly back to the browser.
Example
一个使用 passthu() 函数显示系统 PATH 环境变量内容的 PHP 程序
A PHP program that uses passthu() function to display the contents of system PATH environment variable
passthru(string $command, int &$result_code = null): ?false
<?php
passthru ('PATH');
?>
它将生成以下 output −
It will produce the following output −
PATH=C:\Python311\Scripts\;C:\Python311\;C:\WINDOWS\system32;C:\WINDOWS;
C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;
C:\WINDOWS\System32\OpenSSH\;C:\xampp\php;C:\Users\mlath\AppData\Local
\Microsoft\WindowsApps;C:\VSCode\Microsoft VS Code\bin
Backtick Operator
PHP 支持一个执行运算符:反引号 (``)。(它不是单引号!) PHP 会尝试将反引号的内容作为外壳命令执行;将返回输出。反引号运算符的使用与 shell_exec() 相同。
PHP supports one execution operator: backticks (``). (they are not single-quotes!) PHP will attempt to execute the contents of the backticks as a shell command; the output will be returned. Use of the backtick operator is identical to shell_exec().
Example
请看以下示例:
Take a look at the following example −
<?php
$output = `dir *.php`;
echo "<pre>$output</pre>";
?>
它将生成以下 output −
It will produce the following output −
Volume in drive C has no label.
Volume Serial Number is 7EE4-E492
Directory of C:\xampp\htdocs
10/26/2023 08:42 PM 61 hello.php
10/12/2023 10:40 AM 61 homepage.php
07/16/2015 09:02 PM 260 index.php
10/12/2023 10:39 AM 49 menu.php
09/25/2023 01:43 PM 338 myform.php
10/12/2023 10:49 AM 51 myname.php
10/26/2023 02:00 PM 369 test.php
09/25/2023 01:42 PM 555 uploadfile.php
8 File(s) 1,744 bytes
0 Dir(s) 168,471,289,856 bytes free
当 shell_exec() 被禁用时,反引号运算符将被禁用。
The backtick operator is disabled when shell_exec() is disabled.
PHP – HTTP Authentication
在 PHP 中,header() 函数用于向客户端浏览器发送一个“需要验证”的消息,导致出现一个用户名/密码输入窗口。事实上,header() 允许你发送任何原始 HTTP 标头。
In PHP, the header() function is used to send an "Authentication Required" message to the client browser causing it to pop up a Username/Password input window. In fact header() allows you to send any raw HTTP header.
header(string $header, bool $replace = true, int $response_code = 0): void
字符串参数将传递给 header() 函数。例如
The string parameter is passed to the header() function. For example
header("HTTP/1.1 404 Not Found");
它用于找出要发送的 HTTP 状态代码。
It is used to figure out the HTTP status code to send.
还可以使用 header() 函数将浏览器重定向到另一个 URL。
You can also use header() function to redirect the browser to another URL.
一旦用户输入了用户名和密码,包含 PHP 脚本的 URL 将再次被调用,其中预定义变量 PHP_AUTH_USER、PHP_AUTH_PW 和 AUTH_TYPE 分别设置为用户名、密码和验证类型。这些预定义变量在 $_SERVER 数组中找到。仅支持“Basic”和“Digest”身份验证方法。
Once the user has filled in a username and a password, the URL containing the PHP script will be called again with the predefined variables PHP_AUTH_USER, PHP_AUTH_PW, and AUTH_TYPE set to the user name, password and authentication type respectively. These predefined variables are found in the $_SERVER array. Only "Basic" and "Digest" authentication methods are supported.
<?php
/* Redirect browser */
header("Location: http://www.example.com/");
/* Make sure that code below does not get executed when we redirect. */
exit;
?>
可选的替换参数表明标头是否应该替换先前的类似标头或添加第二个相同类型的标头,并且响应代码参数将 HTTP 响应代码强制为指定的值。
The optional replace parameter indicates whether the header should replace a previous similar header, or add a second header of the same type, and response_code parameter forces the HTTP response code to the specified value.
为了能够强制客户端身份验证,需要在文档根文件夹中使用 .htaccess 文件。打开一个新文本文档,将以下文本放入其中,并以 .htaccess 为其名称保存。
To be able to force he client authentication, you need a .htaccess file in document root folder. Open a new text file, put the following text in it, and save it with .htaccess as its name.
CGIPassAuth On
Example
强制页面上客户端身份验证的一个示例脚本片段如下所示:
An example script fragment which would force client authentication on a page is as follows −
<?php
if (!isset($_SERVER['PHP_AUTH_USER'])) {
header('WWW-Authenticate: Basic realm="My Realm"');
header('HTTP/1.0 401 Unauthorized');
echo 'User hits Cancel button';7
exit;
} else {
echo "<p>Hello {$_SERVER['PHP_AUTH_USER']}.</p>";
echo "<p>You entered {$_SERVER['PHP_AUTH_PW']} as your password.</p>";
}
?>
Output
当你在浏览器中访问脚本时,就会弹出一个对话框,如下所示:
When you visit the script in a browser, it pops up a dialog box as shown −
一旦你点击了登录按钮,可能会有一个后端脚本来验证登录凭据。一旦身份验证通过,将创建两个服务器变量,其键为 PHP_AUTH_USER 和 PHP_AUTH_PW,可以使用 phpinfo() 函数的输出对其进行验证。
Once you click on the sign in button, there may be a backend script to authenticate the login credentials. Once authenticated, two server variables will be created with the keys PHP_AUTH_USER and PHP_AUTH_PW, which can be verified with the output of phpinfo() function.
PHP – Swapping Variables
PHP 没有提供任何内置函数用于交换或互换两个变量的值。然而,有几种技术可以用来完成交换。
PHP doesn’t provide any built-in function with which you can swap or interchange values of two variables. However, there are a few techniques which you can use to perform the swap.
最直接的方法之一是使用第三个变量作为临时的占位符来实现交换。按照特定顺序使用算术运算符也很有效。你也可以用二进制异或操作符用于交换。在本教程中,我们将用 PHP 来实现这些交换技术
One of the most straightforward approaches is to use a third variable as a temporary place holder to facilitate swapping. Using the arithmetic operators in a specific order also is very effective. You can also use the binary XOR operator for swapping purpose. In this chapter, we shall implement these swapping techniques in PHP
Temporary Variable
从逻辑上来说,这是最明显、最简单的方法。要交换 “a” 和 “b” 的值,请使用第三个变量 “c”。将 “a” 的值赋给 “c”,使用 “b” 的现有值覆盖 “a”,然后将 “b” 设置为存储在 “c” 中的 “a” 的早期值。
This is logically the most obvious and the simplest approach. To swap values of "a" and "b", use a third variable "c". Assign the value of "a" to "c", overwrite "a" with existing value of "b" and then set "b" to the earlier value of "a" that was stored in "c".
Example
请看以下示例:
Take a look at the following example −
<?php
$a = 10;
$b = 20;
echo "Before swapping - \$a = $a, \$b = $b". PHP_EOL;
$c = $a;
$a = $b;
$b = $c;
echo "After swapping - \$a = $a, \$b = $b". PHP_EOL;
?>
它将生成以下 output −
It will produce the following output −
Before swapping - $a = 10, $b = 20
After swapping - $a = 20, $b = 10
Using addition (+) Operator
此解决方案利用了这样一个事实:从两个数字的和中减去一个数字会得到第二个数字。换句话说,“sum(a+b) – a” 等于 “b”,反之亦然。
This solution takes the advantage of the fact that subtracting a number from the sum of two numbers gives back the second number. In other words, "sum(a+b) – a" is equal to "b" and vice versa.
Example
让我们利用该属性来交换 “a” 和 “b” −
Let us take advantage of this property to swap "a" and "b" −
<?php
$a = 10;
$b = 20;
echo "Before swapping - \$a = $a, \$b = $b". PHP_EOL;
$a = $a + $b;
$b = $a - $b;
$a = $a - $b;
echo "After swapping - \$a = $a, \$b = $b". PHP_EOL;
?>
它将生成以下 output −
It will produce the following output −
Before swapping - $a = 10, $b = 20
After swapping - $a = 20, $b = 10
你也可以像这样使用其他算术运算符来执行交换:减法 (-)、乘法 (*) 和除法 (/)。
You can also use the other arithmetic operators – subtraction (-), multiplication (*) and division (/) in a similar manner to perform swapping.
Using list() Function
PHP 中的 list() 函数将数组解压为单独的变量。这有助于我们实现两个变量之间的交换目标。为此,生成一个包含 “a” 和 “b” 的数组,然后将其解压到 “b” 和 “a” 变量中,以获取具有交换值的方法获取 “a” 和 “b”。
The list() function in PHP unpacks the array in separate variables. This helps in our objective of performing swap between two variables. To do that, build an array of "a" and "b", and then unpack it to "b" and "a" variables to obtain "a" and "b" with interchanged values.
Example
请看以下示例:
Take a look at the following example −
<?php
$a = 10;
$b = 20;
echo "Before swapping - \$a = $a, \$b = $b". PHP_EOL;
$arr = [$a, $b];
list($b, $a) = $arr;
echo "After swapping - \$a = $a, \$b = $b". PHP_EOL;
?>
它将生成以下 output −
It will produce the following output −
Before swapping - $a = 10, $b = 20
After swapping - $a = 20, $b = 10
Bitwise XOR
按位异或 (^) 运算符也可以用于交换两个变量 “x” 和 “y” 的值。如果两个操作数在相同位置上的一个位为 1,则返回 1,否则返回 0。
The bitwise XOR (^) operator can also be used to swap the value of two variables "x" and "y". It returns 1 when one of two bits at same position in both operands is 1, otherwise returns 0.
Example
请看以下示例:
Take a look at the following example −
<?php
$a = 10;
$b = 20;
echo "Before swapping - \$a = $a, \$b = $b". PHP_EOL;
$a = $a ^ $b;
$b = $a ^ $b;
$a = $a ^ $b;
echo "After swapping - \$a = $a, \$b = $b". PHP_EOL;
?>
它将生成以下 output −
It will produce the following output −
Before swapping - $a = 10, $b = 20
After swapping - $a = 20, $b = 10
PHP – Closure::call()
在 PHP 中, closure 是一个匿名函数,可以访问其创建的范围内的变量,即使该范围已经关闭。你需要在其中指定 use 关键字。
In PHP, a closure is an anonymous function that has access to the variables in the scope in which it was created, even after that scope has closed. You need to specify use keyword in it.
闭包将函数代码与创建它们的范围封装在对象中。在 PHP 7 中,引入了一个新的 closure::call() 方法来将对象范围绑定到闭包并调用它。
Closures are objects that encapsulate the function code and the scope in which they were created. With PHP 7, a new closure::call() method was introduced to bind an object scope to a closure and invoke it.
Methods in the Closure Class
闭包类具有以下方法,包括 call() 方法 −
The Closure class has the following methods including the call() method −
final class Closure {
/* Methods */
private __construct()
public static bind(Closure $closure, ?object $newThis, object|string|null $newScope = "static"): ?Closure
public bindTo(?object $newThis, object|string|null $newScope = "static"): ?Closure
public call(object $newThis, mixed ...$args): mixed
public static fromCallable(callable $callback): Closure
}
call() method 是闭包类的静态方法。它被引入为 bind() 或 bindTo() 方法的快捷方式。
The call() method is a static method of Closure class. It has been introduced as a shortcut the bind() or bindTo() methods.
bind() method 根据特定的 bound 对象和类范围复制闭包,而 bindTo() 方法则使用新的 bound 对象和类范围复制闭包。
The bind() method Duplicates a closure with a specific bound object and class scope while the bindTo() method duplicates the closure with a new bound object and class scope.
call() 方法具有以下 signature −
The call() method has the following signature −
public Closure::call(object $newThis, mixed ...$args): mixed
call() 方法将闭包临时绑定到 newThis,并使用任何给定的参数调用它。
The call() method temporarily binds the closure to newThis, and calls it with any given parameters.
在 PHP 7 之前的版本中,bindTo() 方法可以使用如下方式:
With version prior to PHP 7, the bindTo() method can be used as follows −
<?php
class A {
private $x = 1;
}
// Define a closure Pre PHP 7 code
$getValue = function() {
return $this->x;
};
// Bind a clousure
$value = $getValue->bindTo(new A, 'A');
print($value());
?>
该程序将 $getValue (它是一个闭包对象)绑定到 A 类的对象,并打印其私有变量 $x 的值 - 它为 1。
The program binds the $getValue which is a closure object, to the object of A class and prints the value of its private variable $x – it is 1.
在 PHP 7 中,绑定通过 call() 方法实现,如下所示 −
With PHP 7, the binding is achieved by call() method as shown below −
<?php
class A {
private $x = 1;
}
// PHP 7+ code, Define
$value = function() {
return $this->x;
};
print($value->call(new A));
?>
PHP – Filtered unserialize()
在 PHP 中,内置函数 unserialize() 可从 PHP 版本 4 开始使用。在 PHP 7 中,添加了一个传递允许类列表的条款。这允许过滤不受信任的源。unserialze() 函数仅反序列化来自可信类的 data。
In PHP, the built-in function unserialize() is available from PHP version 4 onwards. With PHP 7, a provision to pass a list of allowed classes has been added. This allows the untrusted source to be filtered out. The unserialze() function unserializes the data from only the trusted classes.
在 PHP 中,序列化表示生成值的存储表示。这对于存储或传递 PHP 值非常有用,而不会丢失它们的类型和结构。内置 serialize() 函数用于此目的。
In PHP, serialization means generation of a storable representation of a value. This is useful for storing or passing PHP values around without losing their type and structure. The built-in serialize() function is used for this purpose.
serialize(mixed $value): string
unserialize() 函数从序列化的表示中给出一个 PHP 值。从 PHP 7 开始,unserialize() 函数遵循以下格式 -
The unserialze() function gives a PHP value from the serialized representation. From PHP 7 onwards, the unserialize() function follows the format below −
unserialize(string $data, array $options = [ ]): mixed
$data 参数是你想要反序列化的序列化字符串。
The $data parameter is the serialized string which you want to unserialize.
$options 参数已新引入。它是一个关联数组,具有以下键 -
The $options parameter has been newly introduced. It is an associative array of following keys −
Sr.No |
Name & Description |
1 |
allowed_classes an array of class names which should be accepted, or false to accept no classes, or true to accept all classes. Omitting this option is the same as defining it as true |
2 |
max_depth The maximum depth of structures permitted during unserialization. |
Example
请看以下示例:
Take a look at the following example −
<?php
class MyClass {
var int $x;
function __construct(int $x) {
$this->x = $x;
}
}
class NewClass {
var int $y;
function __construct(int $y) {
$this->y = $y;
}
}
$obj1 = new MyClass(10);
$obj2 = new NewClass(20);
$sob1 = serialize($obj1);
$sob2 = serialize($obj2);
// default behaviour that accepts all classes
// second argument can be ommited.
// if allowed_classes is passed as false, unserialize converts all objects into __PHP_Incomplete_Class object
$usob1 = unserialize($sob1 , ["allowed_classes" => true]);
// converts all objects into __PHP_Incomplete_Class object except those of MyClass and NewClass
$usob2 = unserialize($sob2 , ["allowed_classes" => ["MyClass", "NewClass"]]);
echo $usob1->x . PHP_EOL;
echo $usob2->y . PHP_EOL;
?>
它将生成以下 output −
It will produce the following output −
10
20
PHP – IntlChar
在 PHP7 中,引入了新的 IntlChar 类。该类提供对可用于访问 Unicode 字符相关信息的大量实用工具方法的访问权限。Intl 类中有很多静态方法和常量。它们紧密遵守底层 ICU(Unicode 的国际组件)库所使用的名称和行为。
In PHP7, a new IntlChar class has been introduced. It provides access to a number of utility methods that can be used to access information about Unicode characters. There are a number of static methods and constants in Intl class. They adhere closely to the names and behavior used by the underlying ICU (International Components for Unicode) library.
Note 您需要在系统中的 PHP 程序中启用 Intl 扩展。要启用它,请打开 php.ini 文件并取消注释(从该行删除前导分号)。
Note that you need to enable the Intl extension in the PHP installation in your system. To enable, open php.ini file and uncomment (remove the leading semicolon from the line)
extension=intl
以下内容通过示例展示 Intl 类中的一些静态函数:
Some static functions from Intl class are explained with examples as below −
IntlChar::charAge
此函数获取码点的“年代”
This function gets the "age" of the code point
public static IntlChar::charAge(int|string $codepoint): ?array
“年代”表示将码点首次指定(作为非字符或私用)或分配字符后的 Unicode 版本。
The "age" is the Unicode version when the code point was first designated (as a non-character or for Private Use) or assigned a character.
IntlChar::charFromName
charFromName() 函数通过名称查找 Unicode 字符并返回其码点值。
The charFromName() function finds Unicode character by name and return its code point value
public static IntlChar::charFromName(string $name,
int $type = IntlChar::UNICODE_CHAR_NAME): ?int
使用查找时要用的名称类型参数组。可以是以下任何一个常量 -
The type parameter sets of names to use for the lookup. Can be any of these constants −
-
IntlChar::UNICODE_CHAR_NAME (default)
-
IntlChar::UNICODE_10_CHAR_NAME
-
IntlChar::EXTENDED_CHAR_NAME
-
IntlChar::CHAR_NAME_ALIAS
-
IntlChar::CHAR_NAME_CHOICE_COUNT
IntlChar::charName
charName() 函数检索 Unicode 字符的名称
The charName() function retrieves the name of a Unicode character
public static IntlChar::charName(int|string $codepoint,
int $type = IntlChar::UNICODE_CHAR_NAME): ?string
IntlChar::isalpha
isalpha() 函数确定指定代码点是否是字母字符。对于通用类别“L”(字母)返回 true。
The isalpha() function determines whether the specified code point is a letter character. true for general categories "L" (letters).
public static IntlChar::isalpha(int|string $codepoint): ?bool
Example
请看以下示例:
Take a look at the following example −
<?php
var_dump(IntlChar::isalpha("A"));
var_dump(IntlChar::isalpha("1"));
?>
它将生成以下 output −
It will produce the following output −
bool(true)
bool(false)
Intl 类定义了类似的静态方法,例如 isdigit()、isalnum()、isblank() 等等。
The Intl class defines similar static methods such as isdigit(), isalnum(), isblank(), etc.
IntlChar::islower
islower() 函数确定指定代码点是否具有通用类别“Ll”(小写字母)。
The islower() function determines whether the specified code point has the general category "Ll" (lowercase letter).
public static IntlChar::islower(int|string $codepoint): ?bool
Example
请看以下示例:
Take a look at the following example −
<?php
var_dump(IntlChar::islower("A"));
var_dump(IntlChar::islower("a"));
?>
它将生成以下 output −
It will produce the following output −
bool(false)
bool(true)
同样,还有如下函数:isupper()、istitle()、iswhitespace() 等等。
Similarly, there are functions such as isupper(), istitle(), iswhitespace() etc.
PHP – CSPRNG
CSPRNG 的缩写代表密码安全伪随机数生成器。PHP 函数库包含了许多用于生成随机数的函数。例如:
The acronym CSPRNG stands for Cryptographically Secure Pseudorandom Number Generator. PHP function library includes many functions that generate random numbers. For example −
-
mt_rand() − Generate a random value via the Mersenne Twister Random Number Generator
-
mt_srand() − Seeds the Mersenne Twister Random Number Generator
-
rand() − Generate a random integer.
Example
以下代码演示了如何使用函数 mt_rand() 来生成随机数:
The following code shows how you can use the function mt_rand() to generate random numbers −
<?php
# Generates random integer between the range
echo "Random integer: " . rand(1,100) . PHP_EOL;
# Generate a random value via the Mersenne Twister Random Number Generator
echo "Random number: " . mt_rand(1,100);
?>
它将生成以下 output −
It will produce the following output −
Random integer: 45
Random number: 86
请注意,每次执行代码时输出可能不同。但是由这些函数生成的随机数并非密码安全,因为可能会猜出其结果。PHP 7 引入了几个生成安全随机数的函数。
Note that the output may vary every time the code is executed. However, random numbers generated by these functions are not cryptographically safe, as it is possible to guess their outcome. PHP 7, introduced a couple of functions that generate secure random numbers.
以下函数具有加密安全性,是新添加的 -
The following functions which are cryptographically secure, are newly added −
-
random_bytes() − Generates cryptographically secure pseudo-random bytes.
-
random_int() − Generates cryptographically secure pseudo-random integers.
The random_bytes() Function
random_bytes() 生成任意长度的加密随机比特串,该串适合用于加密用途,例如生成哈希盐、密钥或初始化向量时。
random_bytes() generates an arbitrary-length string of cryptographic random bytes that are suitable for cryptographic use, such as when generating salts, keys or initialization vectors.
string random_bytes ( int $length )
Parameters
-
length − The length of the random string that should be returned in bytes.
该函数返回一个包含请求数量的加密安全随机比特串的字符串。
The function returns a string containing the requested number of cryptographically secure random bytes.
如果找不到适当的随机性来源,将抛出一个 Exception。如果给定无效的参数,将抛出一个 TypeError。如果给定了无效的字节长度,将抛出一个 Error。
If an appropriate source of randomness cannot be found, an Exception will be thrown. If invalid parameters are given, a TypeError will be thrown. If an invalid length of bytes is given, an Error will be thrown.
The random_int() Function
random_int() 生成加密随机整数,适用于在公正结果极其重要的场景下使用。
random_int() generates cryptographic random integers that are suitable for use where unbiased results are critical.
int random_int ( int $min , int $max )
Parameters
-
min − The lowest value to be returned, which must be PHP_INT_MIN or higher.
-
max − The highest value to be returned, which must be less than or equal to PHP_INT_MAX.
该函数返回一个在 min 到 max 范围内的加密安全随机整数(包含边界值)。
The function returns a cryptographically secure random integer in the range min to max, inclusive.
如果找不到适当的随机性来源,将抛出一个 Exception。如果给定无效的参数,将抛出一个 TypeError。如果 max 小于 min,将抛出一个 Error。
If an appropriate source of randomness cannot be found, an Exception will be thrown. If invalid parameters are given, a TypeError will be thrown. If max is less than min, an Error will be thrown.
PHP – Expectations
预期是与旧的 assert() 函数向后兼容的增强功能。预期允许在生产代码中进行零成本的断言,并提供在断言失败时抛出自定义异常的能力。
Expectations are a backwards compatible enhancement to the older assert() function. Expectation allows for zero-cost assertions in production code, and provides the ability to throw custom exceptions when the assertion fails.
assert() 现在是一个语言构造,其中第一个参数与要测试的字符串或布尔值相反,是一个表达式。
assert() is now a language construct, where the first parameter is an expression as compared to being a string or Boolean to be tested.
Configuration Directives for assert()
下表列出了 assert() 函数的配置指令:
The following table lists down the configuration directives for the assert() function −
Directive |
Default value |
Possible values |
zend.assertions |
1 |
1 − generate and execute code (development mode) 0 − generate code but jump around it at runtime -1 − do not generate code (production mode) |
assert.exception |
0 |
1 − throw, when the assertion fails, either by throwing the object provided as the exception or by throwing a new AssertionError object if exception was not provided. 0 − use or generate a Throwable as described above, but only generates a warning based on that object rather than throwing it (compatible with PHP 5 behaviour) |
Parameters
-
Assertion − The assertion. In PHP 5, this must be either a string to be evaluated or a Boolean to be tested. In PHP 7, this may also be any expression that returns a value, which will be executed and the result is used to indicate whether the assertion succeeded or failed.
-
Description − An optional description that will be included in the failure message, if the assertion fails.
-
Exception − In PHP 7, the second parameter can be a Throwable object instead of a descriptive string, in which case this is the object that will be thrown, if the assertion fails and the assert.exception configuration directive is enabled.
Example
请看以下示例:
Take a look at the following example −
<?php
ini_set('assert.exception', 1);
class CustomError extends AssertionError {}
assert(false, new CustomError('Custom Error Message!'));
?>
它将生成以下 output −
It will produce the following output −
PHP Fatal error: Uncaught CustomError: Custom Error Message! In test.php:6
PHP – The "use" Statement
PHP 中的“use”关键字被认为与多种用途相关联,例如别名引用、插入特性以及在闭包中继承变量。
The "use" keyword in PHP is found to be associated with multiple purposes, such as aliasing, inserting traits and inheriting variables in closures.
Aliasing
别名引用是通过使用操作符完成的。它允许你用别名或其他名称引用一个外部全限定名称。
Aliasing is accomplished with the use operator. It allows you to refer to an external fully qualified name with an alias or alternate name.
Example
请看以下示例:
Take a look at the following example −
use My\namespace\myclass as Another;
$obj = new Another;
你还可以按如下所示进行分组使用声明 -
You can also have groupped use declaration as follows −
use some\namespace\{ClassA, ClassB, ClassC as C};
use function some\namespace\{fn_a, fn_b, fn_c};
use const some\namespace\{ConstA, ConstB, ConstC};
Traits
借助“use”关键字,你可以向类中插入一个特性。特性类似于类,但只打算以细粒度和一致的方式对功能进行分组。不可能自己实例化特性。
With the help of use keyword, you can insert a trait into a class. A Trait is similar to a class, but only intended to group functionality in a fine-grained and consistent way. It is not possible to instantiate a Trait on its own.
Closures
闭包也是一个匿名函数,它可以在“use”关键字的帮助下访问其作用域之外的变量。
Closure is also an anonymous function that can access variables outside its scope with the help of the "use" keyword.
Example
请看以下示例:
Take a look at the following example −
<?php
$maxmarks=300;
$percent=function ($marks) use ($maxmarks) {
return $marks*100/$maxmarks;
};
$m = 250;
echo "marks=$m percentage=". $percent($m);
?>
它将生成以下 output −
It will produce the following output −
marks=250 percentage=83.333333333333
PHP – Integer Division
PHP 引入了一个新函数 intdiv(),它执行其操作数的整数除法,并返回除法结果作为 int。
PHP has introduced a new function intdiv(), which performs integer division of its operands and return the division as int.
intdiv() 函数返回两个整数参数的整数商。如果 “a/b” 除法的结果是 “c”,余数是 “r” −
The intdiv() function returns integer quotient of two integer parameters. If "a/b" results in "c" as division and "r" as remainder such that −
a=b*c+r
在这种情况下, intdiv(a,b) 返回 r −
In this case, intdiv(a,b) returns r −
intdiv ( int $x , int $y ) : int
$x 和 $y 是除法表达式的分子和分母部分。intdiv() 函数返回一个整数。如果两个参数都是正数或负数,则返回值为正数。
The $x and $y are the numerator and denominator parts of the division expression. The intdiv() function returns an integer. The return value is positive if both parameters are positive or both parameters are negative.
Example 1
如果分子 < 分母,则 intdiv() 函数返回 “0”,如下所示 −
If numerator is < denominator, the intdiv() function returns "0", as shown below −
<?php
$x=10;
$y=3;
$r=intdiv($x, $y);
echo "intdiv(" . $x . "," . $y . ") = " . $r . "\n";
$r=intdiv($y, $x);
echo "intdiv(" . $y . "," . $x . ") = " . $r;
?>
它将生成以下 output −
It will produce the following output −
intdiv(10,3) = 3
intdiv(3,10) = 0
Example 2
在以下示例中,intdiv() 函数返回负整数,因为分子或分母是负数。
In the following example, the intdiv() function returns negative integer because either the numerator or denominator is negative.
<?php
$x=10;
$y=-3;
$r=intdiv($x, $y);
echo "intdiv(" . $x . "," . $y . ") = " . $r . "\n";
$x=-10;
$y=3;
$r=intdiv($x, $y);
echo "intdiv(" . $x . "," . $y . ") = " . $r . "\n";
?>
它将生成以下 output −
It will produce the following output −
intdiv(10,-3) = -3
intdiv(-10,3) = -3
Example 3
分子和分母都是正数或都是负数的情况下,intdiv() 函数返回正整数。
The intdiv() function returns a positive integer in the case of numerator and denominator both being positive or both being negative.
<?php
$x=10;
$y=3;
$r=intdiv($x, $y);
echo "intdiv(" . $x . "," . $y . ") = " . $r . "\n";
$x=-10;
$y=-3;
$r=intdiv($x, $y);
echo "intdiv(" . $x . "," . $y . ") = " . $r ;
?>
它将生成以下 output −
It will produce the following output −
intdiv(10,3) = 3
intdiv(-10,-3) = 3
Example 4
在以下示例中,分母为 “0”。它导致 DivisionByZeroError 异常。
In the following example, the denominator is "0". It results in DivisionByZeroError exception.
<?php
$x=10;
$y=0;
$r=intdiv($x, $y);
echo "intdiv(" . $x . "," . $y . ") = " . $r . "\n";
?>
它将生成以下 output −
It will produce the following output −
PHP Fatal error: Uncaught DivisionByZeroError: Division by zero in hello.php:4
PHP – Deprecated Features
随着每个新版本的添加一些新功能,一些功能也会被移除,因为它们被认为已过时。在本章中,我们将了解 PHP 版本 5 之后的弃用功能。
As some new features are added with each new version, some features are also removed as they are deemed to be obsolete. In this chapter, we have a look at deprecated features after PHP version 5.
Deprecated in PHP Ver 7
PHP 4 Style Constructors
PHP 4 样式的构造函数是与它们定义所在的类同名的函数,现在已弃用,并且将在未来移除。如果 PHP 4 构造函数是类中定义的唯一构造函数,则 PHP 7 将发出 E_DEPRECATED。实现 __construct() 函数的类不受影响。
PHP 4 style Constructors are methods having same name as the class they are defined in, are now deprecated, and will be removed in the future. PHP 7 will emit E_DEPRECATED if a PHP 4 constructor is the only constructor defined within a class. Classes implementing a __construct() method are unaffected.
Example
请看以下示例:
Take a look at the following example −
<?php
class A {
function A() {
print('Style Constructor');
}
}
?>
它在浏览器中生成以下 output −
It produces the following output on the browser −
Deprecated: Methods with the same name as their class will not be
constructors in a future version of PHP; A has a deprecated constructor in...
Static Calls to Non-static Methods
对非静态方法的静态调用已弃用,并且将来可能会被移除。
Static calls to non-static methods are deprecated, and may be removed in the future.
Example
请看以下示例:
Take a look at the following example −
<?php
class A {
function b() {
print('Non-static call');
}
}
A::b();
?>
它在浏览器中生成以下 output −
It produces the following output on the browser −
Deprecated: Non-static method A::b() should not be called statically in...
Non-static call
password_hash() salt option
password_hash() 函数的 salt 选项已弃用,以便开发人员不会生成他们自己的(通常不安全的)salt。当开发人员不提供 salt 时,函数本身会生成一个加密安全的 salt - 因此不再需要自定义 salt 生成了。
The salt option for the password_hash() function has been deprecated so that the developers do not generate their own (usually insecure) salts. The function itself generates a cryptographically secure salt, when no salt is provided by the developer - thus custom salt generation is not required any more.
capture_session_meta SSL context option
capture_session_meta SSL 上下文选项已弃用。SSL 元数据现在通过 stream_get_meta_data() 函数使用。
The capture_session_meta SSL context option has been deprecated. SSL metadata is now used through the stream_get_meta_data() function.
ext/mcrypt
mcrypt 扩展已弃用,取而代之的是 OpenSSL。
The mcrypt extension has been deprecated in favour of OpenSSL.
Unquoted Strings
不存在全局常量的未引用的字符串将被视为它们自身的字符串。此行为过去会发出 E_NOTICE,但现在会发出 E_WARNING。在下一个 PHP 主要版本中,将转而引发 Error 异常。
Unquoted strings that are non-existent global constants are taken to be strings of themselves. This behaviour used to emit an E_NOTICE, but will now emit an E_WARNING. In the next major version of PHP, an Error exception will be thrown instead.
The __autoload() Method
__autoload() 函数已弃用,因为它不如 spl_autoload_register() (因为它无法链接自动加载器),并且这两种自动加载样式之间没有互操作性。
The __autoload() method has been deprecated because it is inferior to spl_autoload_register() (due to it not being able to chain autoloaders), and there is no interoperability between the two autoloading styles.
The create_function() Function
鉴于此函数存在安全问题,现在已弃用。首选的替代方法是使用匿名函数。
Given the security issues of this function has now been deprecated. The preferred alternative is to use anonymous functions.
The each() Function
此函数会给一些语言更改带来实现问题。因此已将其弃用。
This function causes implementation issues for some language changes. It has therefore been deprecated.
Case-Insensitive Constants
声明不区分大小写的常数已被弃用。现在传递 true 作为 define() 的第三个参数将生成弃用警告。
The declaration of case-insensitive constants has been deprecated. Passing true as the third argument to define() will now generate a deprecation warning.
Deprecated in PHP Ver 8
如果具有默认值的参数后面跟随一个必需参数,则默认值无效。自 PHP 8.0.0 起,则此操作已被弃用,并且通常可以通过删除默认值来解决,而无需更改功能 −
If a parameter with a default value is followed by a required parameter, the default value has no effect. This is deprecated as of PHP 8.0.0 and can generally be resolved by dropping the default value, without a change in functionality −
<?php
function test($a = [], $b) {} // Before
function test($a, $b) {} // After
?>
此规则的一个例外是形式为 Type $param = null 的参数,其中 null 默认值使类型隐式可为 null。这种用法仍然允许,但建议改为使用显式可为 null 的类型 −
One exception to this rule are parameters of the form Type $param = null, where the null default makes the type implicitly nullable. This usage remains allowed, but it is recommended to use an explicit nullable type instead −
<?php
function test(A $a = null, $b) {} // Still allowed
function test(?A $a, $b) {} // Recommended
?>
显式将 exclude_disabled 设置为 false 调用 get_defined_functions() 已被弃用,并且不再有效。get_defined_functions() 永远不会包含已禁用的函数。
Calling get_defined_functions() with exclude_disabled explicitly set to false is deprecated and no longer has an effect. get_defined_functions() will never include disabled functions.
现在,返回 true 或 false 的排序比较函数将抛出弃用警告,并且应该用返回小于、等于或大于零的整数的实现来替换。
Sort comparison functions that return true or false will now throw a deprecation warning, and should be replaced with an implementation that returns an integer less than, equal to, or greater than zero.
<?php
// Replace
usort($array, fn($a, $b) => $a > $b);
// With
usort($array, fn($a, $b) => $a <=> $b);
?>
Implicit Incompatible float to int Conversions
导致精度损失的浮点数到整数的隐式转换现在已弃用。这会影响数组键、强制模式下的 int 类型声明以及对整数进行操作的操作符。
The implicit conversion of float to int which leads to a loss in precision is now deprecated. This affects array keys, int type declarations in coercive mode, and operators working on ints.
Calling a Static Element on a Trait
直接在特性上调用静态方法或访问静态属性已弃用。只有在使用特性的类上才应访问静态方法和属性。
Calling a static method, or accessing a static property directly on a trait is deprecated. Static methods and properties should only be accessed on a class using the trait.
Date Functions
已弃用 date_sunrise() 和 date_sunset()。请改为使用 date_sun_info()。
date_sunrise() and date_sunset() have been deprecated. Use date_sun_info() instead.
已弃用 strptime()。请改为使用 date_parse_from_format()(用于与区域设置无关的解析)或 IntlDateFormatter::parse()(用于与区域设置相关的解析)。
strptime() has been deprecated. Use date_parse_from_format() instead (for locale-independent parsing), or IntlDateFormatter::parse() (for locale-dependent parsing).
已弃用 strftime() 和 gmstrftime()。您可以改为使用 date()(用于与区域设置无关的格式化)或 IntlDateFormatter::format()(用于与区域设置相关的格式化)。
strftime() and gmstrftime() have been deprecated. You can use date() instead (for locale-independent formatting), or IntlDateFormatter::format() (for locale-dependent formatting).
PHP – Removed Extensions & SAPIs
在每个 PHP 新版本中,都会添加新功能,同时删除某些过时的功能。PHP 版本 7 是一个主要版本,其中一些 PHP 扩展和 SAPI(服务器端应用程序编程接口)被删除。在随后的 PHP 8 版本中,还删除了一些其他扩展。
With each new version of PHP, new functionality is added and at the same time certain obsolete functionality is removed. PHP version 7 is a major version when a number of PHP extensions and SAPIs (Server-side Application Programming Interface) were removed. In the subsequent PHP 8 version also, a few more extensions have been removed.
在 PHP 中,扩展是使用 C/C++ 编写的库或插件,并编译到共享库中,以便可以加载到 PHP 解释器中。一旦 PHP 解释器启动,扩展中的函数就会对 PHP 脚本可用。
In PHP, an extension is a library or plugin, written in C/C++, and compiled into shared libraries so that can be loaded into the PHP interpreter. Once the PHP interpreter starts, the functions in the extension are available to PHP scripts.
定期删除扩展是因为它们不再维护或已被更现代的替代方法所取代。例如,与 PHP 7 同时,ereg 扩展被 preg 扩展替换,mssql 扩展被 PDO_MSSQL 扩展替换。
The extensions are periodically removed because they are either no longer maintained or have been replaced with more modern alternatives. Coinciding with PHP 7 for example, the ereg extension was replaced with the preg extension, and the mssql extension was replaced with the PDO_MSSQL extension.
Removed Extensions
以下扩展已随着 PHP 7 的生效而删除 −
The following extensions have been removed with effect from PHP 7 −
-
ereg extension replaced by preg
-
mssql extension replaced by pdo_mssql
-
mysql extension mysqli
-
sybase_ct replaced by pdo_sybase
从 PHP 8 起,已移除以下扩展:
The following extensions have been removed from PHP 8 onwards −
-
Mcrypt − The Mcrypt extension was used for encryption and decryption, but it has been deprecated since PHP 7.1 and removed in PHP 8 due to security vulnerabilities.
-
MDB2 − The MDB2 extension, earlier used for accessing MDB database files, is removed in PHP 8 due to lack of maintenance.
-
Ming − As Flash is not popular nowadays, the Ming extension, used for generating flash content, has been deprecated since PHP 5.5 and removed in PHP 8.
-
Phar Data − The Phar Data extension was used for accessing data within PHAR archives, but it has been removed in PHP 8 as there are other methods for accessing PHAR data.
-
SNMP − Because it is not being maintained, the SNMP extension has been removed in PHP 8.
-
Tidy − Since new libraries for HTML validation have been added, the Tidy extension was removed in PHP.
-
Tokenizer − The Tokenizer extension was also removed in PHP 8 for the same reason.
-
cURL − The cURL extension was removed in PHP 8.1, as it was no longer maintained.
Removed SAPIs
SAPI 是 PHP 中服务器端应用程序编程接口的缩写。SAPI 负责将 PHP 代码转换为 Web 服务器可以理解的内容。它解析 PHP 代码并调用适当的 Web 服务器函数。然后,Web 服务器会生成一个 HTTP 响应,将其发回客户端。
SAPI stands for Server-side Application Programming Interface in PHP. The SAPI is responsible for translating PHP code into something that the web server can understand. It parses the PHP code and calls the appropriate web server functions. The web server then generates an HTTP response that is sent back to the client.
从 PHP 7 起,已移除以下 SAPI(服务器端应用程序编程接口):
The following SAPIs (Server-side Application Programming Interfaces) have been removed from PHP 7 onwards −
-
aolserver
-
apache
-
apache_hooks
-
apache2filter
-
caudium
-
cgi
-
cgi-fcgi
-
fastcgi
-
isapi
-
litespeed
-
nsapi
-
pwsapi
-
router
-
thttpd
-
uwsgi
-
webserver
-
apache2filter
-
continuity
-
isapi
-
milter
-
nsapi
-
pi3web
-
roxen
-
thttpd
-
tux
-
webjames
PHP – PEAR
PEAR 是 PHP Extension and Application Repository 的首字母缩写。它是一个 PHP 包或扩展的存储库。您可以自由地在您的代码中加入任何这些来自 PEAR 的扩展。PEAR 项目是由 Stig S. Bakken 于 1999 年成立的。
PEAR is an acronym for PHP Extension and Application Repository. It is a repository of PHP packages or extensions. You can freely incorporate any of these extensions from PEAR in your code. The PEAR project was established by Stig S. Bakken in 1999.
大多数 PHP 预编译发行版(如 XAMPP)已经将 PEAR 捆绑在其中。如果没有,您可以通过从 https://pear.php.net/go-pear.phar 下载 go-pear.phar 文件并运行来安装 PEAR:
Most of the precompiled distributions of PHP such as XAMPP already have PEAR bundled with it. If not, you can install PEAR by downloading go-pear.phar file from https://pear.php.net/go-pear.phar and run
php go-pear.phar
在 Windows 命令提示符中,以开始安装。
In a Windows Command Prompt to start the installation.
根据您对安装步骤的响应,PEAR 包管理器将安装在您在安装期间指定的路径中。
Based on your responses to the setup steps, the PEAR Package Manager will be installed in the path, specified during installation.
然后,您可以将该安装路径添加到您的 PATH 环境中。可以手动完成此操作(开始 > 控制面板 > 系统 > 环境),或运行(双击)新生成的 PEAR_ENV.reg,它现在位于 PHP 源代码目录中。
You can then add that installation path to your PATH environment. Either do this manually (Start > Control Panel > System > Environment) or run (double-click) the newly generated PEAR_ENV.reg that’s now found in the PHP source directory.
您现在可以通过运行该命令来访问 PEAR 包管理器:
You can now access the PEAR Package Manager by running the command −
C:\xampp\php>pear
在 Windows 命令提示符中。
In a Windows Command Prompt.
您将按照以下方式获取 PEAR 命令的列表:
You will get the list of PEAR commands as follows −
C:\xampp\php>pear
Commands:
build Build an Extension From C Source
bundle Unpacks a Pecl Package
channel-add Add a Channel
channel-alias Specify an alias to a channel name
channel-delete Remove a Channel From the List
channel-discover Initialize a Channel from its server
channel-info Retrieve Information on a Channel
channel-login Connects and authenticates to remote channel server
channel-logout Logs out from the remote channel server
channel-update Update an Existing Channel
clear-cache Clear Web Services Cache
config-create Create a Default configuration file
config-get Show One Setting
config-help Show Information About Setting
config-set Change Setting
config-show Show All Settings
convert Convert a package.xml 1.0 to package.xml 2.0 format
cvsdiff Run a "cvs diff" for all files in a package
cvstag Set CVS Release Tag
download Download Package
download-all Downloads each available package from the default channel
info Display information about a package
install Install Package
list List Installed Packages In The Default Channel
list-all List All Packages
list-channels List Available Channels
list-files List Files In Installed Package
list-upgrades List Available Upgrades
login Connects and authenticates to remote server [Deprecated in favor of channel-login]
logout Logs out from the remote server [Deprecated in favor of channel-logout]
makerpm Builds an RPM spec file from a PEAR package
package Build Package
package-dependencies Show package dependencies
package-validate Validate Package Consistency
pickle Build PECL Package
remote-info Information About Remote Packages
remote-list List Remote Packages
run-scripts Run Post-Install Scripts bundled with a package
run-tests Run Regression Tests
search Search remote package database
shell-test Shell Script Test
sign Sign a package distribution file
svntag Set SVN Release Tag
uninstall Un-install Package
update-channels Update the Channel List
upgrade Upgrade Package
upgrade-all Upgrade All Packages [Deprecated in favor of calling upgrade with no parameters]
使用 PEAR 安装软件包非常容易。寻找软件包的一种方法是使用官方 PEAR 站点 https://pear.php.net/packages.php ,然后运行
Installing packages with PEAR is so easy. One way to find packages, is using the official PEAR site https://pear.php.net/packages.php and then run
pear install <package-name>
下一步是在你的代码中使用 PEAR 软件包。为此,你应该在你的程序中使用 include、require、include_once 或 require_once 语句包含软件包的主要 PHP 脚本。
The next step is to use the PEAR package in your code. To do that, you should include the main PHP script of the package in your program with include, require, include_once or require_once statements.
<?php
include "PEARPACKAGE.php";
. . . . .
// rest of the code
. . . . .
?>
Composer 是一款较新的 PHP 软件包管理器,是管理 PHP 项目软件包的替代可用管理器。Composer 也支持 PEAR 软件包的安装。对于 PHP 软件包分发,许多人更喜欢 Composer 而非 PEAR。
A newer PHP package manager called Composer is an alternative available for managing packages for a PHP project. Composer also supports the installation of PEAR packages. Composer is preferred by many instead of PEAR for PHP package distribution.
PHP – CSRF
缩写词“CSRF”代表跨站点请求伪造。CSRF 是一种 Internet 漏洞,涉及受信任网站用户的发出未经授权的命令。通过采取本章中说明的措施,可以为 PHP Web 应用程序提供充足的保护来防御此类攻击。
The acronym "CSRF" stands for Cross-Site Request Forgery. CSRF is an Internet exploit that involves a trusted website user issuing unauthorized commands. Providing adequate protection to a PHP web application against this attack can be achieved by taking the measures explained in this chapter.
默认情况下,浏览器使用“GET”请求方法来发送数据。这通常用作 CSRF 中的利用点。为了将命令注入到特定网站中,攻击者会使用诸如“IMG”之类的 HTML 标签。例如,Web 应用程序的 URL 端点,如“/delete.php?empcode=1234”,会删除从 GET 请求的 empcode 参数中传递的帐户。现在,如果经过身份验证的用户在任何其他应用程序中遇到以下脚本。
By default, the browser uses the "GET" request method to send data. This is commonly used as the exploit point in a CSRF. To inject commands into a specific website, the attacker employs HTML tags like "IMG." For example, the url endpoint of a web application such as "/delete.php?empcode=1234" deletes account as passed from empcode parameter of a GET request. Now, if an authenticated user come across the following script in any other application.
<img src="http://example.com/delete.php?empcode=1234"
width="0" height="0" border="0">
会不经意地导致与 empcode=1234 相关的数据被删除。
Inadvertently causes the data related to empcode=1234 to be deleted.
此问题的常见解决方法是使用 CSRF 令牌。CSRF 令牌是一串随机字符,嵌入到请求中,以便 Web 应用程序可以相信已从预期来源接收请求(按照正常工作流程)。
A common workaround for this problem is the use of CSRF tokens. A CSRF token is a string of random characters embedded into requests so that a web application can trust that a request has been received from an expected source as per the normal workflow.
Steps to Implement CSRF
在 PHP 中实现 CSRF 令牌保护的步骤如下 -
The steps to implement CSRF token protection in PHP are as follows −
-
Begin the script by starting a new session.
-
Generate a token of random characters. You can use any of the several built-in function that PHP provides for generation of random string. Let use md5() function to obtain the hash value of uniqueid() function that generates a unique randome string.
-
Inside the HTML form to be provided for the user to submit the data, include a hidden file with its value as the random token generated in the above step.
-
The token can is then validated by the server against the user session after form submission to eliminate malicious requests.
-
You can also add another session variable whose value is the current time, and send an expiry time for the validation purpose.
Example
以下 PHP 代码实现了 CSRF 令牌验证机制。以下脚本生成一个令牌并将其嵌入到 HTML 表单中。
Here is the PHP code that implements CSRF token verification mechanism. The following script generates a token and embeds in a HTML form.
<?php
session_start();
if(!isset($_SESSION["csrf_token"])) {
// No token present, generate a new one
$token = md5(uniqid(rand(), true));
$_SESSION["csrf_token"] = $token;
} else {
// Reuse the token
$token = $_SESSION["csrf_token"];
}
?>
<html>
<body>
<form method="get" action="test.php">
<input type="text" name="empcode" placeholder="empcode" />
<input type="hidden" name="csrf_token" value="<?php echo $token;?>" />
<input type="submit" />
</form>
</body>
</html>
该表单提交到 "test.php" 脚本,如下所示 -
The form is submitted to "test.php" script as below −
<?php
session_start();
echo "hello";
if ($_GET["csrf_token"] == $_SESSION["csrf_token"]) {
// Reset token
echo $_GET["csrf_token"] . "<br>";
echo $_SESSION["csrf_token"] . "<br>";
echo "<h3>CSRF token validation successful. Proceed to further action</h3>";
} else {
echo "<h3>CSRF token validation failed</h3>";
}
?>
它将生成以下 output −
It will produce the following output −
要模拟 CSRF 验证失败,请打开浏览器的检查工具,手动编辑隐藏字段中的值,然后提交表单以查看令牌不匹配,从而导致验证失败。
To simulate the failure of CSRF validation, open the inspect tool of the browser, edit the value in the hidden field manually and submit the form to see that the tokens don’t match leading to the validation failure.
PHP – FastCGI Process
PHP FastCGI 进程管理器 (PHP-FPM) 是一个有效的替代传统基于 CGI 的方法,它用于处理 PHP 请求,特别是在高流量环境中。PHP-FPM 有一些重要的特性。这些特性如下所列 −
PHP FastCGI Process Manager (PHP-FPM) is an efficient alternative to traditional CGI-based methods for handling PHP requests, particularly in high-traffic environments. PHP-FPM has a number of important features. These features are as follows −
Reduced Memory Consumption
借助处理请求的工作进程池,与为每个请求产生一个新进程的传统 CGI 方法相比,PHP-FPM 大大降低了内存开销。
With the help of a pool of worker processes to handle requests PHP-FPM significantly reduces memory overhead compared to traditional CGI methods that spawn a new process for each request.
Improved Performance
PHP-FPM 的工作进程是持久的。它允许它们处理多个请求。它不需要重复创建和销毁进程。这提高了响应时间,并改善了高并发处理。
PHP-FPM’s worker processes are persistent. It allows them to handle multiple requests. It doesn’t need ti repeatedly create and destroy processes. This leads to faster response times and improved handling of high concurrency.
Enhanced Scalability
PHP-FPM 的工作进程池可以根据流量需求进行动态调整,从而使它能够有效地扩展以处理不同的工作负载。
PHP-FPM’s pool of worker processes can be dynamically adjusted based on traffic demands, allowing it to scale effectively to handle varying workloads.
Advanced Process Management
PHP-FPM 提供平滑的启动和关闭。它还对进程管理有细颗粒度控制,包括紧急重启和对工作进程的监控。
PHP-FPM offers graceful startup and shutdown. It also has granular control over process management, including, emergency restarts, and monitoring of worker processes.
Environment Isolation
PHP-FPM 能够为不同的应用程序或用户组创建不同的池,以便为每个环境提供更好的隔离和安全性。
PHP-FPM enables the creation of separate pools for different applications or user groups, so that better isolation and security can be provided for each environment.
Customizable Configuration
PHP-FPM 使用基于 php.ini 的配置选项。通过这些丰富的选项,可以微调其行为,以满足特定的应用程序要求。
PHP-FPM uses php.ini based configuration options. With these extensive options, fine-tuning of its behavior is possible to match specific application requirements.
Supports multiple PHP Versions
PHP-FPM 可同时管理多个 PHP 版本,从而可以在一台服务器上部署不同的 PHP 应用程序。
PHP-FPM can manage multiple PHP versions simultaneously, enabling the deployment of different PHP applications on a single server.
PHP-FPM 通常与 Nginx 或 Apache 等 Web 服务器一起使用。它用作处理 PHP 请求的后端处理器。由于其性能、可扩展性和可靠性,它已成为在生产环境中管理 PHP 应用程序的首选方法。
PHP-FPM is commonly used with web servers like Nginx or Apache. It acts as a backend processor for handling PHP requests. It has become the preferred method for managing PHP applications in production environments due to its performance, scalability, and reliability.
PHP – PDO Extension
PDO 是 PHP 数据对象 (PHP Data Objects) 的缩写。PHP 可以与大多数关系型和 NOSQL 数据库进行交互。默认的 PHP 安装附带已安装和启用的特定供应商的数据库扩展。除了适用于特定类型数据库的此类数据库驱动程序(例如适用于 MySQL 的 mysqli 扩展)之外,PHP 还支持抽象层,例如 PDO 和 ODBC。
PDO is an acronym for PHP Data Objects. PHP can interact with most of the relational as well as NOSQL databases. The default PHP installation comes with vendor-specific database extensions already installed and enabled. In addition to such database drivers specific to a certain type of database, such as the mysqli extension for MySQL, PHP also supports abstraction layers such as PDO and ODBC.
PDO 扩展定义了一个轻量级的、一致的接口,用于在 PHP 中访问数据库。每个特定供应商扩展的功能不同于其他扩展。因此,如果你打算更改某个 PHP 应用程序的后端数据库,例如从 PostGreSql 更改为 MySQL,你需要对代码进行许多更改。另一方面,PDO API 除了指定要使用的数据库的新 URL 和凭据之外,不需要任何更改。
The PDO extension defines a lightweight, consistent interface for accessing databases in PHP. The functionality of each vendor-specific extension varies from the other. As a result, if you intend to change the backend database of a certain PHP application, say from PostGreSql to MySQL, you need to make a lot of changes to the code. The PDO API on the other hand doesn’t require any changes apart from specifying the URL and the credentials of the new database to be used.
你当前的 PHP 安装必须具有相应的 PDO 驱动程序才能正常工作。目前使用相应的 PDO 接口支持以下数据库 −
Your current PHP installation must have the corresponding PDO driver available to be able to work with. Currently the following databases are supported with the corresponding PDO interfaces −
Driver Name |
Supported Databases |
PDO_CUBRID |
Cubrid |
PDO_DBLIB |
FreeTDS / Microsoft SQL Server / Sybase |
PDO_FIREBIRD |
Firebird |
PDO_IBM |
IBM DB2 |
PDO_INFORMIX |
IBM Informix Dynamic Server |
PDO_MYSQL |
MySQL 3.x/4.x/5.x/8.x |
PDO_OCI |
Oracle Call Interface |
PDO_ODBC |
ODBC v3 (IBM DB2, unixODBC and win32 ODBC) |
PDO_PGSQL |
PostgreSQL |
PDO_SQLITE |
SQLite 3 and SQLite 2 |
PDO_SQLSRV |
Microsoft SQL Server / SQL Azure |
默认情况下,PDO_SQLITE 驱动程序在 php.ini 的设置中启用,因此如果你希望使用 PDO 与 MySQL 数据库进行交互,请确保通过删除前导分号取消注释以下行。
By default, the PDO_SQLITE driver is enabled in the settings of php.ini, so if you wish to interact with a MySQL database with PDO, make sure that the following line is uncommented by removing the leading semicolon.
extension=pdo_mysql
你可以通过调用 PDO 类中的 PDO::getAvailableDrivers() 静态函数来获取当前可用的 PDO 驱动程序的列表。
You can obtain the list of currently available PDO drivers by calling PDO::getAvailableDrivers() static function in PDO class.
PDO Connection
PDO 基本类的实例表示一个数据库连接。构造函数接受用于指定数据库源(称为 DSN)的参数,还可以接受用于指定用户名和密码(如果有的)的参数。
An instance of PDO base class represents a database connection. The constructor accepts parameters for specifying the database source (known as the DSN) and optionally for the username and password (if any).
以下代码段是与 MySQL 数据库建立连接的典型方式 −
The following snippet is a typical way of establishing connection with a MySQL database −
<?php
$dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
?>
如果出现任何连接错误,将抛出 PDOException 对象。
If there is any connection error, a PDOException object will be thrown.
Example
请看以下示例:
Take a look at the following example −
<?php
$dsn="localhost";
$dbName="myDB";
$username="root";
$password="";
try{
$dbConn= new PDO("mysql:host=$dsn;dbname=$dbName",$username,$password);
Echo "Successfully connected with $dbName database";
} catch(Exception $e){
echo "Connection failed" . $e->getMessage();
}
?>
它将生成以下 output −
It will produce the following output −
Successfully connected with myDB database
在错误的情况下 −
In case of error −
Connection failedSQLSTATE[HY000] [1049] Unknown database 'mydb'
PDO Class Methods
PDO 类定义以下静态方法——
The PDO class defines the following static methods −
PDO::beginTransaction
在获取连接对象后,你应调用此方法来启动事务。
After obtaining the connection object, you should call this method to that initiates a transaction.
public PDO::beginTransaction(): bool
此方法关闭自动提交模式。因此,你需要调用 commit() 方法才能持久更改数据库。调用 rollBack() 将回滚对数据库的所有更改并返回连接到自动提交模式。此方法成功返回 true,失败返回 false。
This method turns off autocommit mode. Hence, you need to call commit() method to make persistent changes to the database Calling rollBack() will roll back all changes to the database and return the connection to autocommit mode.This method returns true on success or false on failure.
PDO::commit
commit() 方法提交事务。
The commit() method commits a transaction.
public PDO::commit(): bool
由于 BeginTransaction 禁用了自动提交模式,因此你应在事务后调用此方法。它提交事务,将数据库连接返回到自动提交模式,直到下一次调用 PDO::beginTransaction() 开始新事务。此方法成功返回 true,失败返回 false。
Since the BeginTransaction disables the autocommit mode, you should call this method after a transaction. It commits a transaction, returning the database connection to autocommit mode until the next call to PDO::beginTransaction() starts a new transaction. This method returns true on success or false on failure.
PDO::exec
exec() 方法执行 SQL 语句并返回受影响的行数
The exec() method executes an SQL statement and return the number of affected rows
public PDO::exec(string $statement): int|false
exec() 方法在单个函数调用中执行 SQL 语句,并返回该语句影响的行数。
The exec() method executes an SQL statement in a single function call, returning the number of rows affected by the statement.
请注意,它不返回 SELECT 语句的结果。如果你有一个在你的程序中只执行一次的 SELECT 语句,请考虑发出 PDO::query()。
Note that it does not return results from a SELECT statement. If you have a SELECT statement that is to be executed only once during your program, consider issuing PDO::query().
另一方面,对于你需要多次发出的语句,请准备好一个 PDOStatement 对象 (使用 PDO::prepare()),然后使用 PDOStatement::execute() 发出该语句。
On the other hand For a statement that you need to issue multiple times, prepare a PDOStatement object with PDO::prepare() and issue the statement with PDOStatement::execute().
exec() 方法需要一个字符串参数,该参数表示要准备并执行的 SQL 语句,并返回由你发出的 SQL 语句修改或删除的行数。如果没有行受到影响,则 PDO::exec() 返回 0。
The exec() method need a string parameter that represents a SQL statement to prepare and execute, and returns the number of rows that were modified or deleted by the SQL statement you issued. If no rows were affected, PDO::exec() returns 0.
PDO::query
query() 方法准备并执行没有占位符的 SQL 语句
The query() method prepares and executes an SQL statement without placeholders
public PDO::query(string $query, ?int $fetchMode = null): PDOStatement|false
此方法在单个函数调用中准备并执行 SQL 语句,并将该语句作为一个 PDOStatement 对象返回。
This method prepares and executes an SQL statement in a single function call, returning the statement as a PDOStatement object.
PDO::rollBack
rollback() 方法回滚由 PDO::beginTransaction() 启动的事务。
The rollback() method rolls back a transaction as initiated by PDO::beginTransaction().
public PDO::rollBack(): bool
如果数据库被设置为自动提交模式,则此函数在回滚事务后将恢复自动提交模式。
If the database was set to autocommit mode, this function will restore autocommit mode after it has rolled back the transaction.
请注意,某些数据库(包括 MySQL)在事务中发出 DDL 语句(例如 DROP TABLE 或 CREATE TABLE)时会自动发出隐式 COMMIT,因此它将阻止你回滚事务边界内的任何其他更改。此方法成功返回 true,失败返回 false。
Note that some databases, including MySQL, automatically issue an implicit COMMIT when a DDL statement such as DROP TABLE or CREATE TABLE is issued within a transaction, and hence it will prevent you from rolling back any other changes within the transaction boundary. This method returns true on success or false on failure.
Example
以下代码在 MySQL 服务器上的 myDB 数据库中创建一个 student 表。
The following code creates a student table in the myDB database on a MySQL server.
<?php
$dsn="localhost";
$dbName="myDB";
$username="root";
$password="";
try{
$conn= new PDO("mysql:host=$dsn;dbname=$dbName",$username,$password);
Echo "Successfully connected with $dbName database";
$qry = <<<STRING
CREATE TABLE IF NOT EXISTS STUDENT (
student_id INT AUTO_INCREMENT,
name VARCHAR(255) NOT NULL,
marks INTEGER(3),
PRIMARY KEY (student_id)
);
STRING;
echo $qry . PHP_EOL;
$conn->exec($qry);
$conn->commit();
echo "Table created\n";
}
catch(Exception $e){
echo "Connection failed : " . $e->getMessage();
}
?>
Example
使用以下代码在上述示例中创建的 student 表中插入新记录——
Use the following code to insert a new record in student table created in the above example −
<?php
$dsn="localhost";
$dbName="myDB";
$username="root";
$password="";
try {
$conn= new PDO("mysql:host=$dsn;dbname=$dbName",$username,$password);
echo "Successfully connected with $dbName database";
$sql = "INSERT INTO STUDENT values(1, 'Raju', 60)";
$conn->exec($sql);
$conn->commit();
echo "A record inserted\n";
} catch(Exception $e){
echo "Connection failed : " . $e->getMessage();
}
?>
Example
以下 PHP 脚本获取 student 表中的所有记录——
The following PHP script fetches all the records in the student table −
<?php
$dsn="localhost";
$dbName="myDB";
$username="root";
$password="";
try {
$conn= new PDO("mysql:host=$dsn;dbname=$dbName",$username,$password);
echo "Successfully connected with $dbName database";
$sql = "SELECT * from student";
$statement = $conn->query($sql);
$rows = $statement->fetchAll(PDO::FETCH_ASSOC);
foreach ($rows as $row) {
var_dump($row);
}
} catch(Exception $e){
echo "Connection failed : " . $e->getMessage();
}
?>
PHP - Function References
PHP 非常丰富,有内置函数。以下是各种重要函数类别的列表。还有其他各种此处未涵盖的函数类别。
PHP is very rich in terms of Buil-in functions. Here is the list of various important function categories. There are various other function categories which are not covered here.
选择一个类别,以查看与该类别相关的所有函数的列表。
Select a category to see a list of all the functions related to that category.