PHP表单处理
我们可以在PHP中创建和使用表单。要获取表单数据,需要使用PHP超级元组:$_GET和$_POST。
表单请求可以是get或post。 要从get请求中检索数据,需要使用$_GET,而$_POST用于检索post请求中的数据。
PHP GET表单
GET请求是表单的默认请求。 通过get请求传递的数据在URL浏览器上是可见的,因此它不太安全。通过get请求发送的数据量是有限的,所以发送大量数据不适合使用Get请求方法。
下面来看看一个简单的例子,在PHP中从get请求接收数据。
文件: form1.html
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="utf-8" />
<title>Get表单示例</title>
</head>
<body>
<form action="welcome.php" method="get">
Name: <input type="text" name="name"/>
<input type="submit" value="提交"/>
</form>
文件: welcome.php
<?php
$name=$_GET["name"];//receiving name field value in $name variable
echo "Welcome, $name";
?>
打开浏览器,访问:http://localhost/form1.html, 看到结果如下 -
PHP Post表单
Post请求广泛用于提交具有大量数据的表单,例如:文件上传,图像上传,登录表单,注册表单等。
通过post请求传递的数据在URL浏览器上不可见,因此它是安全的。可以通过发送请求发送大量的数据。
下面来看看一个简单的例子,从PHP中接收来自post请求的数据。
文件: form1.html
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="utf-8" />
<title>POST表单示例</title>
</head>
<body>
<form action="login.php" method="post">
<table>
<tr><td>Name:</td><td> <input type="text" name="name"/></td></tr>
<tr><td>Password:</td><td> <input type="password" name="password"/></td></tr>
<tr><td colspan="2"><input type="submit" value="登录"/> </td></tr>
</table>
</form>
文件: login.php
<?php
$name=$_POST["name"];//receiving name field value in $name variable
$password=$_POST["password"];//receiving password field value in $password variable
echo "Welcome: $name, your password is: $password";
?>
打开浏览器,访问:http://localhost/form1.html, 看到结果如下 -
PHP包含文件(include&require)
PHP允许您包括文件,以便页面内容可以重复使用多次。 有两种方法在PHP中包括文件。它们分别是 -
- include
- require
包括文件的优点
代码重用性:通过include和require语句的帮助,我们可以在许多PHP脚本中重用HTML代码或PHP脚本。
1. PHP include示例
PHPinclude用于包含基于给定路径的文件。 可以使用文件的相对路径或绝对路径。 下面来看看一个简单的PHP包含例子。
文件: menu.html
<a href="http://www.abc.com">Home</a> | <a href="http://www.abc.com/php">PHP</a> | <a href="http://www.abc.com/java">Java</a> | <a href="http://www.abc.com/html">HTML</a>
文件: include1.php
<?php include("menu.html"); ?>
<h1>This is Main Page(include)</h1>
2. PHP require示例
PHPrequire类似于include。 下面来看看一个简单的PHPrequire示例。
文件: menu.html
<a href="http://www.abc.com">Home</a> |
<a href="http://www.abc.com/php">PHP</a> |
<a href="http://www.abc.com/java">Java</a> |
<a href="http://www.abc.com/html">HTML</a>
文件: require.php
<?php require("menu.html"); ?>
<h1>This is Main Page(require)</h1>
PHP include VS require
文件丢失时包含的处理方式:include语句允许脚本继续,但require语句暂停脚本产生致命的E_COMPILE_ERROR级别错误。
PHP发送电子邮件(Email)
PHPmail()函数用于在PHP中发送电子邮件。 您可以使用PHPmail()函数来发送短信,HTML消息和附件与消息。
PHP mail()函数
语法
$to:指定邮件的接收者或接收者。 接收器必须指定以下格式之一。- user@example.com
- user@example.com, anotheruser@example.com
- User user@example.com
- User user@example.com, User2 anotheruser@example.com
$subject: 代表邮件的主题。$message: 表示要发送的邮件的消息。$additional_headers(可选):指定附加头,如From,CC,BCC等。额外的附加头也应该用CRLF(\r\n)分隔。
PHP邮件示例
文件:mailer.php
<?php
ini_set("sendmail_from", "maxsujaiswal@abc.com");
$to = "maxsujaiswal1987@gmail.com";//change receiver address
$subject = "This is subject";
$message = "This is simple text message.";
$header = "From:maxsujaiswal@abc.com \r\n";
$result = mail ($to,$subject,$message,$header);
if( $result == true ){
echo "Message sent successfully...";
}else{
echo "Sorry, unable to send mail...";
}
?>
如果在运行服务器上运行此代码,它将向指定的接收方发送电子邮件。
PHP邮件:发送HTML消息
要发送HTML消息,您需要在消息标题中提及Content-type text/html。
<?php
$to = "abc@example.com";//change receiver address
$subject = "This is subject";
$message = "<h1>This is HTML heading</h1>";
$header = "From:xyz@example.com \r\n";
$header .= "MIME-Version: 1.0 \r\n";
$header .= "Content-type: text/html;charset=UTF-8 \r\n";
$result = mail ($to,$subject,$message,$header);
if( $result == true ){
echo "Message sent successfully...";
}else{
echo "Sorry, unable to send mail...";
}
?>
PHP邮件:使用附件发送邮件
要使用附件发送消息,您需要提及许多标题信息,在下面给出的示例中使用。
<?php
$to = "abc@example.com";
$subject = "This is subject";
$message = "This is a text message.";
# Open a file
$file = fopen("/tmp/test.txt", "r" );//change your file location
if( $file == false )
{
echo "Error in opening file";
exit();
}
# Read the file into a variable
$size = filesize("/tmp/test.txt");
$content = fread( $file, $size);
# encode the data for safe transit
# and insert \r\n after every 76 chars.
$encoded_content = chunk_split( base64_encode($content));
# Get a random 32 bit number using time() as seed.
$num = md5( time() );
# Define the main headers.
$header = "From:xyz@example.com\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: multipart/mixed; ";
$header .= "boundary=$num\r\n";
$header .= "--$num\r\n";
# Define the message section
$header .= "Content-Type: text/plain\r\n";
$header .= "Content-Transfer-Encoding:8bit\r\n\n";
$header .= "$message\r\n";
$header .= "--$num\r\n";
# Define the attachment section
$header .= "Content-Type: multipart/mixed; ";
$header .= "name=\"test.txt\"\r\n";
$header .= "Content-Transfer-Encoding:base64\r\n";
$header .= "Content-Disposition:attachment; ";
$header .= "filename=\"test.txt\"\r\n\n";
$header .= "$encoded_content\r\n";
$header .= "--$num--";
# Send email now
$result = mail ( $to, $subject, "", $header );
if( $result == true ){
echo "Message sent successfully...";
}else{
echo "Sorry, unable to send mail...";
}
?>
PHP JSON
PHP可通过json_encode()和json_decode()函数对JSON进行编码和解码。
1. PHP json_encode()函数
json_encode()函数返回值JSON的表示形式。 换句话说,它将PHP变量(包含数组)转换为JSON格式数据。
语法
string json_encode ( mixed $value [, int $options = 0 [, int $depth = 512 ]] )
PHP json_encode()函数示例1
下面来看看看将数组编码为JSON格式的例子。
<?php
$arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);
echo json_encode($arr);
?>
上面示例代码执行结果如下 -
{"a":1,"b":2,"c":3,"d":4,"e":5}
PHP json_encode()函数示例2
下面来看看看将数组编码为JSON格式的例子。
<?php
$arr2 = array('firstName' => 'Max', 'lastName' => 'Su', 'email' => 'rahul@gmail.com');
echo json_encode($arr2);
?>
上面示例代码执行结果如下 -
{"firstName":"Max","lastName":"su","email":"maxsu@gmail.com"}
2. PHP json_decode()函数
json_decode()函数解码JSON字符串。 换句话说,它将JSON字符串转换为PHP变量。
语法
mixed json_decode ( string $json [, bool $assoc = false [, int $depth = 512 [, int $options = 0 ]]] )
PHP json_decode()函数示例1
下面来看看看解码JSON字符串的例子。
<?php
$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
var_dump(json_decode($json, true));//true means returned object will be converted into associative array
?>
执行上面代码得到以下结果 -
array(5) {
["a"] => int(1)
["b"] => int(2)
["c"] => int(3)
["d"] => int(4)
["e"] => int(5)
}
PHP json_decode()函数示例2
下面来看看看解码JSON字符串的例子。
<?php
$json2 = '{"firstName" : "Max", "lastName" : "Su", "email" : "maxsu@gmail.com"}';
var_dump(json_decode($json2, true));
?>
执行上面代码得到以下结果 -
array(3) {
["firstName"]=> string(5) "Max"
["lastName"]=> string(5) "Su"
["email"]=> string(15) "maxsu@gmail.com"
}
PHP if/else 语句
PHPif else语句用于测试条件。在PHP中有多种方法来使用if语句。
- if
- if-else
- if-else-if
- 嵌套if
PHP if语句
如果条件(condition)为true,则执行PHP if语句块。
语法
if(condition){
//code to be executed
}
示例代码-
<?php
$num=12;
if($num<100){
echo "$num is less than 100";
}
?>
上面代码输出结果如下 -
12 is less than 100
PHP if-else语句
PHP if-else语句被执行时可同时条件是真或假。
语法
if(condition){
//code to be executed if true
}else{
//code to be executed if false
}
示例代码-
<?php
$num=12;
if($num%2==0){
echo "$num is even number";
}else{
echo "$num is odd number";
}
?>
上面代码输出结果如下 -
12 is even number
PHP Switch 语句
PHPswitch语句用于从多个条件执行一个语句。 它的工作原理像PHP的if-else-if语句。
语法
switch(expression){
case value1:
//code to be executed
break;
case value2:
//code to be executed
break;
......
default:
code to be executed if all cases are not matched;
}
PHP switch流程图
PHP Switch示例代码
<?php
$num=20;
switch($num){
case 10:
echo("number is equals to 10");
break;
case 20:
echo("number is equal to 20");
break;
case 30:
echo("number is equal to 30");
break;
default:
echo("number is not equal to 10, 20 or 30");
}
?>
执行上面代码得到以下结果 -
number is equal to 20
PHP for 循环
PHP for循环可以用来遍历一组指定的次数的代码。如果迭代次数已知,则应优先考虑使用for循环,否则使用while循环。
for循环的语法
for(initialization; condition; increment/decrement){
//code to be executed
}
示例代码-
<?php
for($n=1;$n<=10;$n++){
echo "$n<br/>";
}
?>
输出结果如下-
1
2
3
4
5
6
7
8
9
10
PHP嵌套for循环
在PHP中,我们可以在for循环中使用for循环,它称为嵌套for循环。
在内部或嵌套for循环的情况下,对于每一次执行的外部for循环,将完全执行嵌套的内for循环。 如果外部for循环执行3次,内部for循环执行3次,内部for循环将一共要执行9次(第一个外部for循环为3次,第二个内for部循环为3次)。
示例
<?php
for($i=1;$i<=3;$i++){
for($j=1;$j<=3;$j++){
echo "$i $j<br/>";
}
}
?>
上面代码输出结果如下 -
1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3
PHP foreach循环
PHP中的foreach循环循环用于遍历数组元素。
语法
<?php
foreach( $array as $var ){
//code to be executed
}
?>
示例代码:
<?php
$season=array("summer","winter","spring","autumn");
foreach( $season as $arr ){
echo "Season is: $arr<br />";
}
?>
上面代码输出结果如下 -
Season is: summer
Season is: winter
Season is: spring
Season is: autumn
PHP while循环
PHPwhile循环可以用于遍历一组代码,如:for循环。如果迭代次数未知,则应使用while循环。
while循环语法
while(condition){
//code to be executed
}
替代语法
while(condition):
//code to be executed
endwhile;
PHP While循环示例
<?php
$n=1;
while($n<=10){
echo "$n<br/>";
$n++;
}
?>
执行上面代码得到以下结果 -
1
2
3
4
5
6
7
8
9
10
替代示例
<?php
$n=1;
while($n<=10):
echo "$n<br/>";
$n++;
endwhile;
?>
执行上面代码得到以下结果 -
1
2
3
4
5
6
7
8
9
10
PHP嵌套while循环
我们可以在PHP中使用一个while循环另一个while循环中,它被称为嵌套while循环。
在内部或嵌套while循环的情况下,嵌套while循环对一个外部while循环完全执行。 如果外部while循环执行3次,嵌套while循环执行3次,则嵌套while循环将一共要执行9次(第一个外部循环为3次,第二个内部循环为3次)。
示例
<?php
$i=1;
while($i<=3){
$j=1;
while($j<=3){
echo "$i $j<br/>";
$j++;
}
$i++;
}
?>
执行上面代码得到以下结果 -
1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3
PHP do...while循环
PHP中的 do…while循环可以用来遍历一组像php中的while循环的代码。只不过PHP中的do-while循环保证至少运行一次()。
它总是执行代码至少一次,因为在执行代码后检查条件。
do…while循环语法
do{
//code to be executed
}while(condition);
执行流程图 -
示例代码
<?php
$n=1;
do{
echo "$n<br/>";
$n++;
}while($n<=10);
?>
输出结果 -
1
2
3
4
5
6
7
8
9
10
PHP break语句
PHPbreak语句中断了当前for,while,do-while,switch和for-each循环的执行。 如果在内循环中使用break,它只中断了内循环的执行。
语法
jump statement;
break
break语句流程图如下
PHP Break:内循环
下面来看看一个简单的例子,如果i的值等于5,则使用break语句来中断for循环的执行。
<?php
for($i=1;$i<=10;$i++){
echo "$i <br/>";
if($i==5){
break;
}
}
?>
执行结果如下 -
1 2 3 4 5
PHP Break:内部循环
PHPbreak语句只中断内循环的执行。
<?php
for($i=1;$i<=3;$i++){
for($j=1;$j<=3;$j++){
echo "$i $j<br/>";
if($i==2 && $j==2){
break;
}
}
}
?>
执行结果如下 -
1 1 1 2 1 3 2 1 2 2 3 1 3 2 3 3
PHP Break:内部switch语句
PHPbreak语句也用于中断switch case的执行流程。
<?php
$num=200;
switch($num){
case 100:
echo("number is equals to 100");
break;
case 200:
echo("number is equal to 200");
break;
case 50:
echo("number is equal to 300");
break;
default:
echo("number is not equal to 100, 200 or 500");
}
?>
执行结果如下 -
number is equal to 200
