---
title: "PHP入门级别代码"
author: "cakin24"
date: "2016-12-24T19:27:15Z"
tags: []
categories: ["编程语言"]
slug: "php"
draft: false
lastmod: "2016-12-24T19:27:15Z"
id: 2347128
url: "https://cakin24.iteye.com/blog/2347128"
---

实例1：第一个PHP程序

01. `<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">`
02. `<htmlxmlns="http://www.w3.org/1999/xhtml">`
03. `<head>`
04. `<metahttp-equiv="Content-Type"content="text/html; charset=utf-8"/>`
05. `<title>第一个PHP程序</title>`
06. `</head>`
07. `<body>`
08. `<?php`
09. `echo "第一个PHP程序";`
10. `?>`
11. `</body>`
12. `</html>`

运行结果：

![](http://dl2.iteye.com/upload/attachment/0122/1732/0d17a5d5-95eb-3566-81a0-206225682984.png)  
 

 

实例2：输出日期

01. `<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">`
02. `<htmlxmlns="http://www.w3.org/1999/xhtml">`
03. `<head>`
04. `<metahttp-equiv="Content-Type"content="text/html; charset=utf-8"/>`
05. `<title>中文输入</title>`
06. `</head>`
07. `<body>`
08. `<pstyle="margin-top:10px;margin-left:180px">`
09. `<?php`
10. `echo "<font color='#11ffff00'>北京时间:".date("Y-m-d H:i:s ")."</font>"`
11. `?>`
12. `</body>`
13. `</html>`

运行结果：

![](http://dl2.iteye.com/upload/attachment/0122/1734/4e8ee5ec-719f-3a8b-9b50-d9b399243920.png)  
 

注意事项：

修改php.ini的配置如下。

\[Date]

; Defines the default timezone used by the date functions

; http://php.net/date.timezone

date.timezone ="Asia/Hong\_Kong"

然后重启Apache服务，否则显示的将是格林威治时间。

 

实例3：输出中文

01. `<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">`
02. `<htmlxmlns="http://www.w3.org/1999/xhtml">`
03. `<head>`
04. `<metahttp-equiv="Content-Type"content="text/html; charset=utf-8"/>`
05. `<title>输出中文</title>`
06. `</head>`
07. `<body>`
08. `<?php`
09. `echo "PHP欢迎你";`
10. `?>`
11. `</body>`
12. `</html>`

运行结果：

![](http://dl2.iteye.com/upload/attachment/0122/1736/7547fa0d-f173-3cf8-81bb-516a3f204e73.png)  
 

 

实例4：输出图片

01. `<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">`
02. `<htmlxmlns="http://www.w3.org/1999/xhtml">`
03. `<head>`
04. `<metahttp-equiv="Content-Type"content="text/html; charset=utf-8"/>`
05. `<title>输出图片</title>`
06. `</head>`
07. `<body>`
08. `<?php`
09. `echo '<img src="images/PHP参考手册.png" width="1200" height="800" />'`
10. `?>`
11. `</body>`
12. `</html>`

运行结果：

![](http://dl2.iteye.com/upload/attachment/0122/1738/54af55e2-e4d4-3cbe-a770-432d4b898a93.png)  
 

 

实例5：输出字符串

01. `<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">`
02. `<htmlxmlns="http://www.w3.org/1999/xhtml">`
03. `<head>`
04. `<metahttp-equiv="Content-Type"content="text/html; charset=utf-8"/>`
05. `<title>输出字符串</title>`
06. `</head>`
07. `<body>`
08. `<?php`
09. `echo "输出字符串";`
10. `?>`
11. `</body>`
12. `</html>`

运行结果：

![](http://dl2.iteye.com/upload/attachment/0122/1740/4e74fdf9-98f6-336a-acdd-abb64d8385ee.png)  
 

 

实例6：输入多个字符串

01. `<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">`
02. `<htmlxmlns="http://www.w3.org/1999/xhtml">`
03. `<head>`
04. `<metahttp-equiv="Content-Type"content="text/html; charset=utf-8"/>`
05. `<title>发布最新公告信息</title>`
06. `</head>`
07. `<body>`
08. `<pstyle="margin-top:10px;margin-left:180px">`
09. `<?php`
10. `echo "<font color='#11FF00'>从即日起，登录本站的用户可免费下载手机铃声！"."[".date("Y-m-d H:i:s")."]</font>";`
11. `?>`
12. `</body>`
13. `</html>`

运行结果：

![](http://dl2.iteye.com/upload/attachment/0122/1742/a9209b6c-802a-35d3-8486-b5b840579171.png)