PHP(Persnonal Home Pages, Hypertext Processor)
简介:
HTML静态而乏味
利用纯HTML Web页面,服务器只能提供静态HTML,而
静态HTML只能显示内容。
PHP为Web页面赋予生命
通过引入PHP,Web服务器能够
动态生成HTML页面。
PHP脚本在服务器上运行
PHP脚本看上去与正常的HTML Web页面很相似,因为他们同时都可以包含HTML代码和CSS代码。实际上,一旦PHP脚本在服务器上运行结束,每一个PHP脚本都会转换为HTML和CSS。也就是最终浏览器收到的是PHP脚本的执行结果。
HTML form表单中的
action告诉服务器要用哪个PHP脚本来处理这个表单。
<form method="post"
action="report.php">
</form>
大多数的PHP脚本同时包含有PHP代码和HTML代码,服务器将所有内容作为HTML传送给客户Web浏览器之前,这些PHP代码会运行并转换为HTML,HTML代码则原样传送不做任何改变。
PHP代码规则:
- 总是用<?php和?>包围
...
?>
- 每个PHP语句都以分号结束
- 如果Web页面中有PHP代码,一个很好的想法是将Web服务器上的文件命名为扩展名.php而不是.html
- PHP的变量名总以一个美元符号$开头
- PHP变量是区分大小写的,而PHP命令(如echo)是不区分大小写的,但是 PHP建议使用小写加下划线的方式来定义变量。
- PHP字符串可以是单引号,也可以是双引号。连接字符串和变量需要用点号"."。如 echo 'Describe them:' .$_POST['describe']. '<br />';
PHP中的特殊变量:
$_POST
| 超级全局变量,PHP内置,直接绑定到HTML表单使用的表单提交方法,包含输入到表单的各部分数据。 是一个数组 HTML表单域中的name属性,定义了数组的下标 |
|
|
|
|
|
|
|
|
单引号双引号
换行符(\n)只能在双引号中才能被转义。(在单引号中只支持\'和\\,即在单引号字符串中嵌入单引号标点和\)
单引号被认为是原始文本,而PHP在处理双引号时会寻找变量。在一个双引号中遇到变量是,PHP会在串中插入该变量的值,就像连接字符串一样。
示例代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Aliens Abducted Me - Report an Abduction</title>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<h2>Aliens Abducted Me - Report an Abduction</h2>
<p>Share your story of alien abduction:</p>
<form method="post" action="report.php">
<label for="firstname">First name:</label>
<input type="text" id="firstname" name="firstname" /><br />
<label for="lastname">Last name:</label>
<input type="text" id="lastname" name="lastname" /><br />
<label for="email">What is your email address?</label>
<input type="text" id="email" name="email" /><br />
<label for="whenithappened">When did it happen?</label>
<input type="text" id="whenithappened" name="whenithappened" /><br />
<label for="howlong">How long were you gone?</label>
<input type="text" id="howlong" name="howlong" /><br />
<label for="howmany">How many did you see?</label>
<input type="text" id="howmany" name="howmany" /><br />
<label for="aliendescription">Describe them:</label>
<input type="text" id="aliendescription" name="aliendescription" size="32" /><br />
<label for="whattheydid">What did they do to you?</label>
<input type="text" id="whattheydid" name="whattheydid" size="32" /><br />
<label for="fangspotted">Have you seen my dog Fang?</label>
Yes <input id="fangspotted" name="fangspotted" type="radio" value="yes" />
No <input id="fangspotted" name="fangspotted" type="radio" value="no" /><br />
<img src="fang.jpg" width="100" height="175"
alt="My abducted dog Fang." /><br />
<label for="other">Anything else you want to add?</label>
<textarea id="other" name="other"></textarea><br />
<input type="submit" value="Report Abduction" name="submit" />
</form>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Aliens Abducted Me - Report an Abduction</title>
</head>
<body>
<h2>Aliens Abducted Me - Report an Abduction</h2>
<?php
$name = $_POST['firstname'] . ' ' . $_POST['lastname'];
$when_it_happened = $_POST['whenithappened'];
$how_long = $_POST['howlong'];
$how_many = $_POST['howmany'];
$alien_description = $_POST['aliendescription'];
$what_they_did = $_POST['whattheydid'];
$fang_spotted = $_POST['fangspotted'];
$email = $_POST['email'];
$other = $_POST['other'];
$to = 'owen@aliensabductedme.com';
$subject = 'Aliens Abducted Me - Abduction Report';
$msg = "$name was abducted $when_it_happened and was gone for $how_long.\n" .
"Number of aliens: $how_many\n" .
"Alien description: $alien_description\n" .
"What they did: $what_they_did\n" .
"Fang spotted: $fang_spotted\n" .
"Other comments: $other";
mail($to, $subject, $msg, 'From:' . $email);
echo 'Thanks for submitting the form.<br />';
echo 'You were abducted ' . $when_it_happened;
echo ' and were gone for ' . $how_long . '<br />';
echo 'Number of aliens: ' . $how_many . '<br />';
echo 'Describe them: ' . $alien_description . '<br />';
echo 'The aliens did this: ' . $what_they_did . '<br />';
echo 'Was Fang there? ' . $fang_spotted . '<br />';
echo 'Other comments: ' . $other . '<br />';
echo 'Your email address is ' . $email;
?>
</body>
</html>