1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
<?php include 'header.php';?> <?php /* 本文目的:总览 PHP 的主要语法,并供日后备忘。 阅读指南:本文不是文档。通过通读本文,可以了解 PHP 基本的语法及一些常用的方法。读者可以将其与其他语言比较,理解其意图与含义。 目标读者:具有程序设计基础知识,理解面向对象程序设计基本概念的程序员。 */ // 导入 include('header.php'); require('core.php'); // echo 内容支持 HTML echo "<strong>This is a bold text.</strong>"; // 变量 $NAME = 'BREEZE'; $name = 'breeze'; // 变量名对大小写敏感 // 1ABC = 25; 变量名不能以数字起始 $variable = 'name'; echo $name; echo $name.$NAME; // breezeBREEZE echo $$variable; // breeze // 常量 define("CASE_SENSITIVE_CONSTANT","CONSTANT_SAMPLE",true); // 定义一个具有大小写敏感的名称的常量 define("CASE_INSENSITIVE_CONSTANT","CONSTANT_SAMPLE",false); // 定义一个具有大小写不敏感的名称的常量 define("CONSTANT","CONSTANT_SAMPLE"); // 默认情况下,常量名称的大小写不敏感。然而,名称大小写敏感的常量依然以小写形式储存,因此在定义一个大小写敏感常量之后再定义一个同名小写常量将发生错误 echo CASE_SENSITIVE_CONSTANT; $string1 = '9'; $int1 = -42; $float1 = 42.09; $boolean1 = true; // 另一个布尔值为 false echo($string1+int1); // -33 // 方法 function getName() { // 方法名对大小写不敏感,且只能以字母或下划线(_)起始 $name = 'Breeze'; echo $name; // 将 echo 'Breeze' 而非之前定义的 'breeze' return $name; } function getGlobalName() { global $name; echo $name; // 将 echo 之前定义的 'breeze' } // 返回 NULL function echoName($name='Alice') { echo $name; } echoName('Alice'); // Alice echoName(); // Alice // 算术 $num1 =4; $num2 =3; echo $num1/$num2; // 1.33333333333 echo $num1%$num2; // 1 $num1++; --$num2; $num2 += $num1; // $num2 = 7 $t = true; $f = false; // 逻辑运算 $booleanResult = $t and $f or $t xor $f && $t || !$f if ($num1 > 1 and $num1 < 1 or $num1 >= 0 xor $num2 <= 5 && $num1 != $num2 || !($num1 <> $num2)) { } else if ($num1 == $num2) { // 相等 echo $num1++; } else if ($num1 === $num2) { // 相等且类型相同 (全等) echo $num1--; } else if ($num1 !== $num2) { // 不等或类型不同 (不全等) echo $num2++; } else { echo $num2--; } // 数组 $peopleNumeric = array("Alice","Bob"); $peopleAssociative = array("Alice"=>"17","Bob"=>"18"); $peopleMultiDimensional = array("Alice"=>array('C','Z'),"Bob"=>array('H','W','L')); echo $peopleNumber[0]; // Alice echo $peopleAssociative['Alice']; // 17 echo $peopleMultiDimensional['Bob'][2]; // L // 循环 $intForWhile = 1; while ($intForWhile<9) { echo "Current: ".$intForWhile; $intForWhile++; } do { echo "Current: ".$intForWhile; $intForWhile++; } while($intForWhile<20) for ($i = 0;$i <9;$i++) { echo "Current: ".$i; if ($i==7) { continue; } } foreach($peopleNumeric as $ $person) { echo $person; // Alice Bob } // switch switch ($peopleNumber[0]) { case "Alice": echo "Alice"; break; case "bob": case "Bob": echo "Bob"; break; default: echo "No match"; } // superglobals echo $_SERVER['SCRIPT_NAME']; // including $GLOBALS, $_REQUEST, $_POST, $_GET, $_FILES, $_ENV, $_COOKIE, $_SESSION echo $_SERVER['SCRIPT_FILENAME']; echo $_SERVER['SCRIPT_URL']; echo $_SERVER['HTTP_HOST']; echo $_SERVER['PHP_SELF']; echo $_SERVER['SERVER_ADDR']; echo $_SERVER['SERVER_NAME']; echo $_SERVER['SERVER_PORT']; echo $_SERVER['REMOTE_ADDR']; echo $_SERVER['REMOTE_HOST']; echo $_SERVER['REMOTE_PORT']; // 文件操作 $file=fopen("file.txt", "w"); // mode: r, w, a, x, r+, w+, a+, x+. a mode won't erase data fwrite($file,"TEXT\n"); // the original content is erased fclose($file); // return true or false $file=fopen("file.txt", "a"); fwrite($file,"TEXT\n"); // content of the file: TEST\nTEST\n fclose($file); $content=file("file.txt"); $count=count($content); // get length of the array foreach ($content as $line) { echo $line."\n"; } ?> |
post.php 以 POST 方式提交表单
1 2 3 4 5 6 7 8 |
<html> <body> <form action="main.php" method="post"> // data sent to main.php when the page is submitted. if action is not set, data will be sent to the file itself <p>Name: <input type="text" name="name" /></p> <p><input type="submit" name="submit" value="Submit" /></p> </form> </body> </html> |
main.php 接收 POST
1 2 3 4 5 6 7 |
<html> <body> Welcome <? php echo $_POST["name"]; // for post method, which is preferred for sending (big) data ?> </body> </html> |
session_cookie_get.php 将数据存至 session 与 Cookie
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?php session_start(); $_SESSION['temp']='TEST'; setcookie("user", "Alice", time()+(86400*7), '/'); // must be used before <html> tag if (isset($_COOKIE['user'])) { // check if the value has been set echo $_COOKIE['user']." is set in cookie"; } ?> <html> <body> <form action="actionGet.php" method="get"> <p>data included in URL, e.g., actionGet.php?name=Alice</p> <p>Name: <input type="text" name="name" /></p> <p><input type="submit" name="submit" value="Submit" /></p> </form> </body> </html> |
actionGet.php 接收 GET
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<html> <body> Welcome <?php echo $_GET['name']; ?> <?php echo $_SESSION['temp']; // TEST session_unset(); session_destroy(); if (isset($_GET['name'])) { $file=fopen("names.txt", "a"); fwrite($file, $_GET['name']); fclose($file); echo $_COOKIE['user']." is set in cookie"; } ?> </body> </html> |