PHP基础语法

来源:互联网 发布:顶级域名服务器 权威 编辑:程序博客网 时间:2024/06/09 16:43
<html><body><?phpinclude 'menu.php';require "menu.php";include 'vars.php';echo "No.0: I have a $color $car <br/>";echo "No.1: Hello, World!<br/>";print "No.2: My name is Angelo<br/>";//This is a comment;$txt1 = "Hello World!";$number = 16;echo "No.3: $txt1 <br>";$txt2 = "GaoChao";echo "No.4: ".$txt1. ": " .$txt2."<br>";echo "No.5: ". strlen($txt1) . "<br>";echo "No.6: ".strpos($txt1, "World")."<br>";echo "No.7: ".++$number;echo "<br>";echo "No.8: ";//if语句$d = date("D");if ($d == "Fri")    echo "Have a nice weekend!";else    echo "Have a nice ".$d."!";    if ($d == "Mon")    {        echo "<br>Hello!";        echo "Have a nice Monday! <br/>";    }//elseif语句echo "No.9: ";if ($d == "Fri")    echo "Have a nice weekend!";elseif ($d == "Sun")    echo "Have a nice Sunday!";elseif ($d =="Mon")    echo "Have a nice day!";//switch语句$x = "Number 2";echo "<br>No.10: ";switch ($x)    {        case "Number 1": echo "Number1"; break;        case "Number 2": echo "Number2"; break;        case "Number 3": echo "Number3"; break;        default: echo "false";    }//一维数组echo"<br/>No.11: ";$names = array("Peter", "Angelo", "Gao");echo $names[0]. " and ". $names[1]. " and ". $names[2];//关联数组echo "<br/>No.12: ";$ages = array("Peter"=>32, "Angelo"=>23, "Gao"=>26);//$ages['Peter'] = 32;...echo "Peter's age is: ".$ages['Peter'].";  ";echo "Angelo's age is: ".$ages['Angelo'].";  ";echo "Gao's age is: ".$ages['Gao']."  ";//多位数组echo "<br/>No.13: ";$families = array(    "Griffin" => array    (        "peter",        "Lois",        "Megan"    ),    "Quagmire" => array    (        "Glenn"    ),    "Brown" => array    (        "Cleveland",        "Loretta",        "Junior"    ));echo $families['Griffin'][2];//while循环echo "<br>No.14: ";$i = 1;while ($i <= 5)    {        echo "The number is ". $i . "; ";        $i++;    }    //do while循环  echo "<br>";$i = 0;      do    {        $i++;        echo "The number is ". $i . ";";    }    while($i < 5);     //for循环   echo "<br>No.15: ";for ($i = 1; $i <=5; $i++)    {        echo "Hello: " . $i . "; ";    }//foreach语句echo "<br>No.16: ";foreach ($ages as $v)    {        echo "Value: " . $v . "; ";    }//自定义函数echo "<br>No.17: ";function writeMyName()    {        echo "GaoChao";    }writeMyName();//PHP函数-添加参数echo "<br>No.18: ";function writeMyName2($fname)    {        return $fname . " Angelo";    }echo "My name is: " . writeMyName2("Miachel");echo "<br>";function writeMyName3($fname, $punctuation)    {        echo $fname . " Angelo" . $punctuation . "<br/>";    }echo "My name is: ";writeMyName3("John", "...");//Date()函数: date(formate, timestamp)echo "<br>No.19: ";echo date("Y/m/d"); //Y:2013;y:13; M:Dec; m:12; D:Wed; d:25;echo "<br/>";echo date("Y.m.d");echo "<br/>";echo date("Y-m-d");echo "<br/>";echo date("Y, m, d");//mktime(hour, minute, second, month, day, year)$tomorrow = mktime(0, 0, 0, date("m"), date("d") + 1, date("Y"));echo "<br/>";echo "<br/>No.20: ";echo "Tomorrow is: " . date("Y-m-d", $tomorrow);//fopen();echo "<br/>No.21: ";//$file = fopen("welcome.txt", "w") or exit("Unable to open file!");$file = fopen("welcome.txt", "w+") ;//or exit("Unable to open file!");if(feof($file)) echo "End of file";fclose($file);//fgets():逐行读取echo "<br/><br/>No.22: ";$file = fopen("welcome.txt", "r") or exit("Unable to open file!");while(!feof($file))    {        echo fgets($file). "<br/>";    }fclose($file);//fgetc():逐字符读取文件echo "<br/><br/>No.23: ";$file = fopen("welcome.txt", "r") or exit("Unable to open file!");while(!feof($file))    {        echo fgetc($file);    }fclose($file);//include requireinclude 'header.php';require 'header.php';?></body></html>

0 0