欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

自学php的几个例子(包含AMP(Apache、MySQL、PHP)环境搭建链接)

最编程 2024-01-11 11:19:11
...

学习PHP之前需要先搭建PHP运行的环境(即服务器+PHP+数据库)来使PHP成功运行,具体环境搭建教程可参考pharen(http://www.cnblogs.com/pharen/archive/2012/02/06/2340628.html)的教程(亲测可用),教程中一些资源链接可能已经失效,下面给出部分资源链接:
PHP资源:http://windows.php.net/download#php-5.6

Apache(msi安装包)资源:http://archive.apache.org/dist/httpd/binaries/win32/

有几个要点需要注意(笔者作为菜鸡就被坑了):

(1)软件安装路径不要包含中文或者空格

(2)Apache版本要与PHP版本兼容,较老版本Apache无法支持新版本的PHP,例如:1、php5.2支持Apache2.0和Apache2.2;2、php5.3、php5.4同时支持Apache2.2和Apache2.4;3、php5.5只支持Apache2.4

(3)需要安装PHP编译器的运行环境,不同版本PHP需要的VC支持库版本也不同:

apache.org下载的Apache都是vc6版本,否则就根据不同文件说明安装不同的运行库。

vc11运行库x86/x64版本:http://www.microsoft.com/en-us/download/details.aspx?id=30679
vc10运行库x86版本:http://www.microsoft.com/en-us/download/details.aspx?id=5555
vc10运行库x64版本:http://www.microsoft.com/en-us/download/details.aspx?id=14632
vc9运行库x86版本:http://www.microsoft.com/en-us/download/details.aspx?id=5582
vc9运行库x64版本:http://www.microsoft.com/en-us/download/details.aspx?id=15336

常见问题参考资料连接如下:

(1)http://www.2cto.com/os/201312/264364.html

(2)http://jingyan.baidu.com/article/ceb9fb10d909c48cac2ba06c.html

php教程简要入门版可参见W3C(供入门菜鸟使用):

下面给出几个PHP学习粗浅的例子,仅供娱乐:
例子1:表单提交处理页面

formSubmit.php文件:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>php basis-1</title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <meta name="author" content="Wayne Ng" />
        <meta name="description" content="basis-1" />
        <meta name="revised" content="Wayne Ng,2016/2/16" />
        <style>
        .errColor{color: red;}
        </style>
    </head>
    <body>
        <?php
        function process_input($data){
        $data = trim($data);
        $data = stripslashes($data);
        $data = htmlspecialchars($data);
        return $data;
        }
        $name = $email = $website = $comment = $gender = "";
        $nameErr = $emailErr = $genderErr = $websiteErr = "";
        if($_SERVER["REQUEST_METHOD"] == "POST"){
            //提取name信息
            if(empty($_POST["name"])){
                $nameErr = "名字是必须的";
            }
            else{
                $name = process_input($_POST["name"]);
                //检查名字是否仅包含字母和空格
                if(!preg_match("/^[a-zA-Z ]*$/", $name)){
                    $nameErr = "名字只能包含英文字母和空格";
                }
            }
            //提取email信息
            if(empty($_POST["email"])){
                $emailErr = "邮箱是必须的";
            }
            else{
                $email = process_input($_POST["email"]);
                //检查邮箱名字是否合法
                if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email)) {
                    $emailErr = "无效的 email 格式!"; 
                }
            }
            //提取website信息
            if(empty($_POST["website"])){
                $website = "";
            }
            else{
                $website = process_input($_POST["website"]);
                if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%
                    =~_|]/i",$website)) {
                    $websiteErr = "无效的 URL"; 
                }
            }
            //提取comment信息
            if(empty($_POST["comment"])){
                $comment = "";
            }
            else{
                $comment = process_input($_POST["comment"]);
            }
            //提取gender信息
            if(empty($_POST["gender"])){
                $genderErr = "性别是必须的";
            }
            else{
                $gender = process_input($_POST["gender"]);
            }
        }
        ?>
        <h2>PHP表单实例</h2>
        <p><span class="errColor">* 必填项目</span></p>
        <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
        姓名:&nbsp&nbsp&nbsp <input type="text" name="name" value="<?php echo $name;?>"/>
        <span class="errColor">*<?php echo $nameErr;?></span><br />
        邮箱:&nbsp&nbsp&nbsp <input type="text" name="email" value="<?php echo $email;?>"/>
        <span class="errColor">*<?php echo $emailErr;?></span><br />
        网址:&nbsp&nbsp&nbsp <input type="text" name="website" value="<?php echo $website;?>"/>
        <span class="errColor"><?php echo $websiteErr;?></span><br />
        评论:&nbsp&nbsp&nbsp <textarea name="comment" rows="5" cols="40"><?php echo $comment;?></textarea><br />
        性别:&nbsp&nbsp&nbsp
              <input type="radio" name="gender" <?php if(isset($gender) && $gender=="male") echo "checked";?> value="male"><input type="radio" name="gender" <?php if(isset($gender) && $gender=="female") echo "checked";?> value="female"><span class="errColor">*<?php echo $genderErr;?></span><br /><br />
        <input type="submit" name="submit" value="提交表单">  
        </form>
        <?php
        echo "<h2>您提交的表单数据如下所示:</h2>";
        echo "名字:&nbsp&nbsp".$name."<br />";
        echo "邮箱:&nbsp&nbsp".$email."<br />";
        echo "网址:&nbsp&nbsp".$website."<br />";
        echo "评论:&nbsp&nbsp".$comment."<br />";
        echo "性别:&nbsp&nbsp".$gender."<br />";
        ?>
    </body>
</html>

显示效果:

例子2:文件提交

FileSubmit.php文件:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>php basis-3</title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <meta name="author" content="Wayne Ng" />
        <meta name="description" content="basis-3" />
        <meta name="revised" content="Wayne Ng,2016/2/17" />
        <style>
        .warning{color: red;}
        </style>
    </head>
    <body>
        
        <h2>文件上传</h2>
        <span class="warning">*文件大小必须小于100kb</span>
        <form method="post" action="/uploadFile.php" enctype="multipart/form-data">
        <label for="file">文件名:</label>
        <input type="file" name="file" id="file" /><br />
        <input type="submit" name="submit" value="提交" />
        </form>
    </body>
</html>

uploadFile文件:

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?php
    //限制上传文件大小(必须小于100kb)
    if($_FILES["file"]["size"] < 100 * 1024){
        if($_FILES["file"]["error"] > 0){
            echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
        }
        else{
            echo "上传文件:".$_FILES["file"]["name"] . "<br />";
            echo "文件类型:".$_FILES["file"]["type"] . "<br />";
            echo "文件大小:".round($_FILES["file"]["size"]/1024, 1) . "kb<br />";
            echo "临时文件:".$_FILES["file"]["tmp_name"] . "<br />";
            //检验同名文件是都已经存在
            if(file_exists("upload/" . $_FILES["file"]["name"])){
                echo $_FILES["file"]["name"] . "已经存在!";
            }
            else{
                move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]);
                echo "保存于:" . "upload/" . $_FILES["file"]["name"];
            }
        }
    }
    else{
        echo "文件大小必须小于100kb";
    }
?>

效果显示:

例子3:文件读写操作

fileOperation.php文件:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>php basis-2</title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <meta name="author" content="Wayne Ng" />
        <meta name="description" content="basis-2" />
        <meta name="revised" content="Wayne Ng,2016/2/17" />
    </head>
    <body>
        <?php
            function printFile($file){
                while(!feof($file)){
                    echo fgets($file)."<br />";
                }
            }
            $fileName = "spam.txt";
            //查看原始文件内容
            echo "文件原始内容:<br />";
            $fileContent = fopen($fileName, 'r') or die("Unable to open file!");
            printFile($fileContent);
            fclose($fileContent);
            //文件末尾追加内容
            $fileContent = fopen($fileName, 'a+') or die("Unable to open file!");
            fwrite($fileContent, "Maybe carrot doesn't tastes terrible.\n");
            fclose($fileContent);
            //查看修改后文件内容
            echo "文件修改后的内容:<br />";
            $fileContent = fopen($fileName, 'r') or die("Unable to open file!");
            printFile($fileContent);
            fclose($fileContent);
            
        ?>
    </body>
</html>

显示效果:

例子4:XML文件解析

game.xml文件:

<?xml version = "1.0" encoding = "UTF-8"?>
<!DOCTYPE game SYSTEM "game.dtd">
<!--
    Date:2016/1/26
    Writer:Wayne Ng
    Theme:MapleStory hero briefInstruction
-->
<game>
    <warrior>
        <hero>
            <name>英雄</name>
            <weapons>剑、斧、钝器</weapons>
            <skill>终极打击</skill>
        </hero>
        <hero>
            <name>圣骑士</name>
            <weapons>剑、钝器</weapons>
            <skill>神圣冲击</skill>
        </hero>    
        <hero>
            <
						

上一篇: DNF被制裁没点B数?不好意思,没有。一位普通玩家的心声

下一篇: 基于SSM框架游戏攻略与信息平台的设计与实现