PHP维基简介
PHP(全称:PHP:Hypertext Preprocessor,即“PHP:超文本预处理器”)是一种开源的通用计算机脚本语言,尤其适用于网络开发并可嵌入HTML中使用。PHP的语法借鉴吸收C语言、Java和Perl等流行计算机语言的特点,易于一般程序员学习。PHP的主要目标是允许网络开发人员快速编写动态页面,但PHP也被用于其他很多领域。
http://php.net/ 查看相关文档
Hello world
http://alanwang.co/php/index.php
<?php
echo "This is PHP!"
?>
输出错误日志
新建php.ini文件,记录服务器上的设置和配置,在开发环境使用,生产不要使用。
输入配置信息
display_errors = on
display_startup_errors = on
PHP变量
<?php
$test = "I'am a variable";
$test1 = "I\"am a variable";
$number = 75; //变量定义不能以数字作为开头
$name = alan;
$a = name;
echo "My name is $name"; //第一种输出方式
echo "My name is ".$name; //第二种输出方式
echo $$a; //输出变量的变量;
?>
PHP数组
<?php
$myArray = array("pizza","chocolate","coffee");
print_r($myArray);
echo $myArray[0];
echo "<br /><br />";
$anotherArray=array( //另一种定制数组方式
"China" => "Chinese",
"France" => "France" ,
"USA" => "English"
);
print_r($anotherArray);
$anotherArray[Japan] = "Japanese"; //给定义好的数组增加元素
echo "<br /><br />";
print_r($anotherArray);
unset($anotherArray["USA"]) ; //删除数组中的一个元素 变量不需要加双引号,定义的字符串需要加双引号,因为字符串在内存中是以二进制的0即null结尾的。
echo "<br /><br />";
print_r($anotherArray);
echo "<br /><br />";
$name = "alan";
unset($name); //unset也可以删除一般的变量的值
echo $name;
?>
PHP IF语句
<?php
$number = 1;
$anotherNumber = 1;
$thirdNumber = 2;
if($number == $anotherNumber AND $anotherNumber == $thirdNumber){ // || 用 OR
echo "true!";
}else{
echo "false";
}
?>
For语句和Foreach语句
<?php
for($i = 0 ; $i < 10; $i++){
echo $i."<br />";
}
$myArray = array("cat","dog","turtle","pig");
foreach( $myArray as $key => $value){ //输出数组中的所有key和value
echo "Key : $key Value : $value <br />";
}
?>
PHP发送邮件
<?php
$emailTO = "xxx@gmail.com";
$subject = "I hope this works";
$body = "I think you are great!";
$headers = "from gmail";
if(mail($emailTO,$subject,$body,$headers)){
echo "send successed!"
}else{
echo "failed";
}
?>
$_GET变量
相关介绍http://www.runoob.com/php/php-get.html
<body>
<?php
if($_GET[name]){ //判断如果界面中的name赋值了,则输出echo中的内容
echo "Your name is ".$_GET[name];
}else{
echo "please input your name";
}
?>
<form>
<label for="name">Name </label>
<input type="text" name="name" />
<input type="submit" name="submit" value="enter your name"/>
</form>
</body>
POST变量
<?php
$friend = array("Tom","Peter","Alan");
$flag = false;
//print_r($_POST); //使用post在网址栏没有显示填入信息,保密。
if($_POST["name"] ){ //判断如果界面中的name赋值了,则输出echo中的内容
foreach ($friend as $name) {
if($_POST["name"] == $name){
echo "I know you ,your name is ".$name;
$flag = true;
}
}
if(!$flag){
echo "I do not know you ".$_POST["name"];
}
}else{
echo "please input your name";
}
?>
<form method="post">
<label for="name">Name </label>
<input type="text" name="name" />
<input type="submit" name="submit" value="enter your name"/>
</form>
实现表格的填写,校验,提交后发送到邮箱
<?php
if ($_POST["submit"]){
if(!$_POST["name"]){
$error = "<br / >请输入您的姓名";
}
if(!$_POST["email"]){
$error .= "<br / >请输入您的邮箱"; //加. 相当于 +=的意思
}else if(!filter_var($_POST["email"],FILTER_VALIDATE_EMAIL)){ //邮箱校验函数
$error .= "<br / >您的邮箱格式不正确";
}
if(!$_POST["text"]){
$error .= "<br / >请输入您的留言";
}
if($error){
$result = '<div class = "alert alert-danger"> <strong>这里有错误</strong>'.$error.'</div>'; //不要忘了后面有.
}else{
if(mail("xxx@gmail.com","来自alan的网站","
姓名:".$_POST['name']."
邮箱:".$_POST['email']."
内容:".$_POST['text'],$_POST['email'])){
$result = '<div class = "alert alert-success"> <strong>提交成功</strong></div>';
}else{
$result = '<div class = "alert alert-danger"> <strong>抱歉,没有提交成功</strong></div>';
}
}
}
?>
<!doctype html>
<html>
<head>
<title>Example Domain</title>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<meta charset="utf-8" />
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<style type="text/css">
.container {
border: 2px solid grey;
border-radius: 5px;
}
#button {
margin-left: 600px;
}
</style>
</head>
<body>
<div class="container col-md-6 mx-auto mt-4 py-4 ">
<h1>请输入您的信息</h1>
<?php
echo $result;
?>
<form method="post">
<div class="form-group">
<label for="name">姓 名</label>
<input name="name" type="text" class="form-control" value="<?php echo $_POST['name'] ?>"> <!--这里的php语句是保证提交后输入框中的值保留-->
</div>
<div class="form-group">
<label for="email">邮 箱</label>
<input name="email" type="email" class="form-control" value="<?php echo $_POST['email'] ?>">
</div>
<div class="form-group">
<label for="email">留 言</label>
<textarea class="form-control" name="text" value="<?php echo $_POST['text'] ?>"></textarea>
</div>
<input type="submit" class="btn btn-success btn-lg" name="submit" id="button" value="提交" />
</form>
</div>
<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
</body>
</html>
查询天气预报
<?php
$city=$_GET['city'];
$city=str_replace(" ", "", $city);
$content=file_get_contents("http://www.weather-forecast.com/locations/".$city."/forecasts/latest");
preg_match("/<\/b><span class=\"read-more-small\"><span class=\"read-more-content\"> <span class=\"phrase\">(.*?)<\/span>/s", $content, $matches);
echo $matches[0];
?>
<!doctype html>
<html>
<head>
<title>Ken’s website</title>
<meta charset="utf-8" />
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap-theme.min.css" integrity="sha384-fLW2N01lMqjakBkx3l/M9EahuwpSfeNvV63J5ezn3uZzapT0u7EYsXMjQV+0En5r" crossorigin="anonymous">
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
<script
src="https://code.jquery.com/jquery-1.12.3.js"
integrity="sha256-1XMpEtA4eKXNNpXcJ1pmMPs8JV+nwLdEqwiJeCQEkyc="
crossorigin="anonymous"></script>
<style>
html, body {
height:100%;
}
.container {
background-image:url("background.jpg");
width:100%;
height:100%;
background-position:center;
background-size:cover;
padding-top:180px;
}
.center {
text-align:center;
}
.white {
color:white;
}
p {
padding-top:10px;
padding-bottom:10px;
}
button {
margin-top:15px;
}
.alert {
margin-top:28px;
display:none;
}
</style>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-6 col-md-offset-3 center">
<h1 class="center white">Weather Predictor</h1>
<p class="lead center white">Enter your city below to get a forecase for the weather.</p>
<form class="form-group">
<input type="text" class="form-control" name="city" id="city" placeholder="Eg. London, Paris, San Francisco..."/>
</form>
<button id="findMyWeather"class="btn btn-success btn-lg center">Find My Weather</button>
<div id="success" class="alert alert-success">Success</div>
<div id="fail" class="alert alert-danger">Could not found the weather data of your city, Please try again later.</div>
<div id="noCity" class="alert alert-danger">Please enter a city.</div>
</div>
</div>
</div>
<script>
$("#findMyWeather").click(function(event) {
event.preventDefault();
$(".alert").hide();
if ($("#city").val() != "") {
$.get("scraper.php?city="+$("#city").val(), function(data) {
if (data=="") {
$("#fail").fadeIn();
} else {
$("#success").html(data).fadeIn();
}
});
} else {
$("#noCity").fadeIn();
}
});
</script>
</body>
</html>