header
header(‘content-type:text/html; charset= utf-8‘);//设置编码格式为:utf-8 header(‘location:http://www.baidu.com‘);//设置跳转到百度首页 header(‘refresh:3; url=http://www.xiaomi.com‘);//设置页面间隔刷新
数据类型
<?php header("content-type:text/html;charset=utf-8");//浏览器渲染格式 $num=1;//整形 $str="你好哈";//字符 $float=3.1415926;//浮点 $bool=true;//布尔 echo $num; echo "<br>"; echo $str; echo "<br>"; echo $float; echo "<br>"; echo $bool; ?>
表达式
<?php header("content-type:text/html;charset=utf-8"); //if判断 $num=1; if($num==1){ echo "正确"; }else{ echo "错误"; } echo "<br>"; //switch表达式 $day="周一"; switch($day){ case "周一": echo "上班1"; break; case "周二": echo "上班2"; break; default: echo "休息"; break; } echo "<br>"; //for循环 for($i=0;$i<10;$i++){ echo "for循环".$i; echo "<br>"; } echo "<br>"; //while循环 while($num<10){ echo "while循环".$num; echo "<br>"; $num++; } echo "<br>"; //break $n=1; while($n<10){ echo "while循环".$n; echo "<br>"; if($n==5){ echo "等于5,停止,跳出循环"; echo "<br>"; break; } $n++; } echo "<br>"; //三元表达式 $answer=$num>0?"1大于0":"1小于0"; echo $answer; ?>
数组
<?php header("content-type:text/html;charset=utf-8"); $arr=array(1,2,3,4,5,6);//数组 echo $arr[3]; echo "<br>"; $foodArr=array("番茄炒鸡蛋","土豆肉丝","红烧排骨","红烧鸡块");//数组 echo $foodArr[2]; echo "<br>"; $persom=array("name"=>"wq","age"=>16,"skill"=>"吃饭");//关系型数组(类似json) echo $persom["name"]; echo "<br>"; echo $persom["age"]; echo "<br>"; echo $persom["skill"]; ?>
单双引号
<?php header("content-type:text/html;charset=utf-8"); //双引号会输出变量(如果变量存在的话),单引号不会输出变量 $person="你是好人"; echo "$person"; echo "<br>"; echo ‘$person‘; ?>
GET
<?php header("content-type:text/html;charset=utf-8"); echo $_GET["userName"]; ?>
POST
<?php header("content-type:text/html;charset=utf-8"); echo $_POST["userName"]; ?>
嵌入html中
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> *{ padding: 0; margin: 0; } div{ width: 500px; margin: 100px auto; } ul{ list-style: none; } li{ float: left; width: 100px; height: 30px; background-color: rgba(255,255,156,0.7); text-align: center; line-height: 30px; } </style> </head> <body> <div> <ul> <li>导航</li> <li>导航</li> <li>导航</li> <li> <?php echo $_GET["userName"]; ?> </li> <li> <?php echo $_GET["pwd"]; ?> </li> </ul> </div> </body> </html>
注意事项
1、拼接字符串是用.而不是+
2、在进行表单提交时,最好在127.0.0.1中进行访问
3、php文件的命名不能出现中文