PHP 5.4.36 Released

第一个 PHP 页面

在 web 服务器根目录(DOCUMENT_ROOT)下建立一个文件名为 hello.php,然后完成如下内容:

Example #1 第一个 PHP 脚本:hello.php

<html>
 <head>
  <title>PHP 测试</title>
 </head>
 <body>
 <?php echo '<p>Hello World</p>'?>
 </body>
</html>

在浏览器的地址栏里输入 web 服务器的 URL 访问这个文件,在结尾加上“/hello.php”。如果本地开发,那么这个 URL 一般是 http://localhost/hello.php 或者 http://127.0.0.1/hello.php,当然这取决于 web 服务器的设置。如果所有的设置都正确,那么这个文件将被 PHP 解析,浏览器中将会输出如下结果:

<html>
 <head>
  <title>PHP 测试</title>
 </head>
 <body>
 <p>Hello World</p>
 </body>
</html>

该程序非常的简单,它仅仅只是利用了 PHP 的 echo 语句显示了 Hello World。用户一定不会满足与此。请注意该文件无需被执行或以任何方式指定。服务器会找到该文件并提供给 PHP 进行解释,因为使用了“.php”的扩展名,服务器已被配置成自动传递有着“.php”扩展名的文件给 PHP。一个普通的 HTML 文件,加上了几个特别的标签,就可以做很多非常有趣的事情!

如果试过了这个例子,但是没有得到任何输出,或者浏览器弹出了下载框,或者浏览器以文本方式显示了源文件,可能的原因是服务器还没有支持 PHP,或者没有正确配置。需要请服务器的管理员根据本手册“安装”一章的内容使得服务器支持 PHP。如果本地开发,请阅读手册有关安装的章节以确保所有的设置都正确。还要确认通过浏览器访问的 URL 确实指向了服务器上的这个文件。如果只是从本地文件系统调用这个文件,它不会被 PHP 解析。如果问题仍然存在,请通过 » PHP 在线支持中的各种方式获取帮助。

以上例子的目的是为了显示 PHP 特殊标识符的格式。在这个例子中,用 <?php 来表示 PHP 标识符的起始,然后放入 PHP 语句并通过加上一个终止标识符 ?> 来退出 PHP 模式。可以根据自己的需要在 HTML 文件中像这样开启或关闭 PHP 模式。请参阅手册中“PHP 基本语法”以获取更多信息。

Note: 关于换行

尽管换行在 HTML 中的实际意义不是很大,但适当地使用换行可以使 HTML 代码易读且美观。PHP 会在输出时自动删除其结束符 ?> 后的一个换行。该功能主要是针对在一个页面中嵌入多段 PHP 代码或者包含了无实质性输出的 PHP 文件而设计,与此同时也造成了一些疑惑。如果需要在 PHP 结束符 ?> 之后输出换行的话,可以在其后加一个空格,或者在最后的一个 echo/print 语句中加入一个换行。

Note: 关于文本编辑器

有很多文本编辑器以及集成开发环境(IDE)可以被用来建立、编辑和管理 PHP 文件。这些工具中的一部分被列在 » PHP 编辑器列表中。如果希望推荐其它的编辑器,请访问以上页面,并要求该页面的维护者将你推荐的编辑器加入到该列表中。使用支持语法高亮功能的编辑器会给开发带来很多帮助。

Note: 关于文字处理器

诸如 StarOffice Writer、Microsoft Word 和 Abiword 的文字处理器不适合用来编辑 PHP 程序。如果希望用以上这些工具的某一种来处理脚本,必须保证将结果存成了纯文本格式,否则 PHP 将无法读取并运行这些脚本。

Note: 关于 Windows 记事本

如果使用 Windows 记事本来编写 PHP 脚本,需要注意在保存文件时,文件的后缀名应该为 .php(记事本将自动在文件名后面加上 .txt 后缀,除非采取以下措施之一来避免这种情况)。当保存文件时,系统会让你指定文件的文件名,这时请将文件名加上引号(例如 "hello.php")。或者,也可以点击“另存为”对话框中的“保存类型”下拉菜单,并将设置改为“所有文件”。这样在输入文件名的时候就不用加引号了。

现在已经成功建立了一个简单的 PHP 脚本,那么再来建立一个最著名的 PHP 脚本!调用函数 phpinfo(),将会看到很多有关自己系统的有用信息,例如预定义变量、已经加载的 PHP 模块和配置信息。请花一些时间来查看这些重要的信息。

Example #2 从 PHP 获取系统信息

<?php phpinfo(); ?>

add a note add a note

User Contributed Notes 8 notes

up
8
ivan at ivandigital dot com
2 months ago
Note on Line Feeds

These are 2 alternative ways to add a line feed after a php block.

---
1. Echo a "\n" at the end of the block.

<?php
   
echo "Hello World";
    echo
"\n";
?>

Alternatively you can also concatenate "\n" to the last echo/print in the block.

<?php
   
echo "Hello World" . "\n";
?>

Note: the string "\n" must be inside double quotes; using single quotes will output the literal string.

---
2. Add two line feeds after the closing ?> tag.

By explicitly adding two line feeds after the closing ?> tag, the first one will be removed by php, and the second one will appear in the output.

---
One advantage of both approaches is that you don't need to add a space after the closing ?> tag (to force a space and a line feed to be output), instead, only the line feed is output.
up
7
ryan420 at earthling dot net
11 years ago
Note on permissions of php files:  You don't have to use 'chmod 0755' under UNIX or Linux; the permissions need not be set to executable.  Again, this is more like a html file than a cgi script.  The only mandatory requirement is that the web server process has read access to the php file(s).  With many Linux systems, it is popular for Apache to run under the 'apache' account.  Given that HTML and other web files, like php, are often owned by user 'root' and group 'web' (or another similar group name), acceptable permissions might be those achieved with 'chmod 664' or 'chmod 644'.  The web server process, running under the 'apache' account, will inherit read only permissions.  The 'apache' account is not root and is not a member of the 'web' group, so the "other" portion of the permissions (the last "4") applies.
up
-1
ivan at ivandigital dot com
2 months ago
Note on Line Feeds

These are 2 alternative ways to add a line feed after a php block.

---
1. Echo a "\n" at the end of the block.

<?php
   
echo "Hello World";
    echo
"\n";
?>

Alternatively you can also concatenate "\n" to the last echo/print in the block.

<?php
   
echo "Hello World" . "\n";
?>

Note: the string "\n" must be inside double quotes; using single quotes will output the literal string.

---
2. Add two line feeds after the closing ?> tag.

By explicitly adding two line feeds after the closing ?> tag, the first one will be removed by php, and the second one will appear in the output.

---
One advantage of both approaches is that you don't need to add a space after the closing ?> tag (to force a space and a line feed to be output), instead, only the line feed is output.
up
-7
Luis
2 months ago
In my particular configuration (Apache Web Server on ArchLinux) the Document Root is /srv/http, hence I should put my helloworld.php file in that directory, not in /var/www, which doesn't exist.

You can find out checking your server configuration. Again, with my particular configuration, if I execute (in a terminal window) the following command:

   cat /etc/httpd/conf/httpd.conf | grep -e '^DocumentRoot'

I get the output:

   DocumentRoot "/srv/http"

which indicates me where to locate the .php files
up
-12
ivan at ivandigital dot com
2 months ago
Note on Line Feeds

These are 2 alternative ways to add a line feed after a php block.

---
1. Echo a "\n" at the end of the block.

<?php
   
echo "Hello World";
    echo
"\n";
?>

Alternatively you can also concatenate "\n" to the last echo/print in the block.

<?php
   
echo "Hello World" . "\n";
?>

Note: the string "\n" must be inside double quotes; using single quotes will output the literal string.

---
2. Add two line feeds after the closing ?> tag.

By explicitly adding two line feeds after the closing ?> tag, the first one will be removed by php, and the second one will appear in the output.

---
One advantage of both approaches is that you don't need to add a space after the closing ?> tag (to force a space and a line feed to be output), instead, only the line feed is output.
up
-112
naoehnadaserio at gmail dot com
11 months ago
The folder to put your helloworld.php is /var/www/ on Linux
up
-138
miklcct at gmail dot com
4 years ago
If you save your code as UTF-8, make sure that the BOM (EF BB BF) is not present as the first 3 bytes of the file otherwise it may interfere with the code if the PHP need to be run before any output (e.g. header()).
up
-149
onebadscrivener at gmail dot com
9 years ago
OS X users editing in TextEdit will need to make sure their TextEdit preferences are set to allow plain text files.  Under the TextEdit pull-down menu, choose PREFERENCES, then under NEW DOCUMENT ATTRIBUTES in the window that pops up, click PLAIN TEXT. 

Then, in the section of that same window called "saving," DESELECT "append .txt extension to plain text files."  This will allow you to save your files with a .php extension.

Then close the PREFERENCES window.  You're good to go.
To Top