Udemy网站课程9-PHP

PHP维基简介

PHP(全称:PHP:Hypertext Preprocessor,即“PHP:超文本预处理器”)是一种开源的通用计算机脚本语言,尤其适用于网络开发并可嵌入HTML中使用。PHP的语法借鉴吸收C语言JavaPerl等流行计算机语言的特点,易于一般程序员学习。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>
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 218,036评论 6 506
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,046评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,411评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,622评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,661评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,521评论 1 304
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,288评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,200评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,644评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,837评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,953评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,673评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,281评论 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,889评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,011评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,119评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,901评论 2 355

推荐阅读更多精彩内容