写给程序员的 PHP 语法快速入门


This is a bold text.";

 // 变量
 $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 方式提交表单


 
  
// data sent to main.php when the page is submitted. if action is not set, data will be sent to the file itself

Name:

main.php 接收 POST


 
Welcome 
 

session_cookie_get.php 将数据存至 session 与 Cookie

 tag
 if (isset($_COOKIE['user'])) { // check if the value has been set
  echo $_COOKIE['user']." is set in cookie";
 }
?>

 
  

data included in URL, e.g., actionGet.php?name=Alice

Name:

actionGet.php 接收 GET


 
Welcome