Php 简明教程

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.

php hello world

浏览到“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!!!!!