跳转到内容

PHP 和 MySQL 编程/语法概述

来自维基教科书,开放的书籍,面向开放的世界

PHP 标签

[编辑 | 编辑源代码]

<?php [code] ?>

[编辑 | 编辑源代码]
  • 包含 PHP 代码
  • 嵌入在普通 HTML 代码中
  • 在 PHP 标签中,语句用 ; 分隔(通常也紧跟着一个换行符)。

示例

 <?php
 print "Hello World\n";
 $date = date("Y-m-d H:i:s");
 print "The date and time at the moment is $date";
 ?>

注释掉一行

示例

echo "Hello"; // Everything from the hash is commented out

示例

// This entire line is commented out

// 功能相同

/* (text) */

[编辑 | 编辑源代码]

注释掉 /* 和 */ 之间的所有内容

示例

 /*All of this is
 commented out.
 Even this line!*/

PHP 中的变量用 $ 前缀表示。

示例

 $a = "Hello World"; # this assigns the string "Hello World" to $a.
 $b = "$a, I'm Ralfe"; # this assigns "Hello World, I'm Ralfe" to $b.
 $b = $a.", I'm Ralfe"; # exactly the same as the previous example.

PHP 支持动态变量。

示例

$c = “response”;

$$c = “Hello Ralfe”; # this assigns “Hello Ralfe” to $response.


PHP 变量不需要事先声明,也不需要类型定义。PHP 处理所有数据类型转换。

  • 示例
$a = 4;

$b = 12;

print “The value of a is $a.; # Uses a as a String.

$c = $a + $b; # $a is now used as an Integer again.

PHP 支持布尔变量,可以赋值为 1 或 0,或 true 或 false。

示例

$a = true;

$b = 1;  # $a and $b are equal, though not identical.

$c = false;

$d = 0; # $c and $d are equal, though not identical.

有用的定义

[编辑 | 编辑源代码]
相等 vs 相同
相等
值在数值上相等,但类型可能不同。使用 == 比较运算符。例如 false 等于 0。
相同
值在数值上相等,类型也相同。使用 === 比较运算符。例如 false 仅与 false 相同,它与 0 不相同。

运算符

[编辑 | 编辑源代码]

算术运算符

[编辑 | 编辑源代码]

示例

$a = 4;

$b = 2;

$a + $b = 6;  // Addition

$a - $b = 2;  // Subtraction

$a * $b = 8;  // Multiplication

$a / $b = 2;  // Division

$a % $b = 0;  // Modulus

$a++;  // Increment

$a--;  // Decrement

赋值运算符

[编辑 | 编辑源代码]

示例

$a = 4;

$b = $a;

// $b = 4;

比较运算符

[编辑 | 编辑源代码]

示例

$a == $b // test if two values are equal

$a === $b // test if two values are identical

$a != $b // test if two values are not equal

$a !== $b // test if two values are not identical

$a < $b // test if the first value is less than the second

$a > $b // test if the first value is greater than the second

$a <= $b // test if the first value is less than or equal to the second

$a >= $b // test if the first value is greater than or equal to the second

示例

$a = "Fill the halls ";

$b = "with poisoned ivy...";

$c = $a . $b;  # the '.' operator concatenates two variables.

// $c = "Fill the halls with poisoned ivy...";

PHP 支持数值索引数组和关联数组。

示例

$a = array(1, 2, 3, 4);

// $a[0] = 1;

// $a[1] = 2;

// $a[2] = 3;

// $a[3] = 4;

$b = array("name" => "Fred", "age" => 30);

// $b['name'] = "Fred";

// $b['age'] = 30;

决策和循环语句

[编辑 | 编辑源代码]

If ... else 语句

[编辑 | 编辑源代码]

示例

 $a = 1;
 $b = 10;
 
 if ($a > $b) {
    echo "a is greater than b";
 }
 else if ($a == $b) {
    echo "a is equal to b";
 }
 else {
    echo "a is not greater than b";
 }
 
 // OUTPUT:
 // a is not greater than b

Switch 语句

[编辑 | 编辑源代码]

示例

 $a = 100;
 
 switch($a) {
    case(10):
       echo "The value is 10";
       break;
 
    case(100):
       echo "The value is 100";
       break;
 
    case(1000):
       echo "The value is 1000";
       break;
 
    default:
       echo "Are you sure you entered in a valid number?";
 }
 
 // OUTPUT:
 // The value is 100

For 语句

[编辑 | 编辑源代码]

示例

 for ($i = 0; $i < 10; $i++) {
    # initialize $i ; while condition ; increment statement
    echo $i;
 }
 
 // OUTPUT:
 // 0123456789

Foreach 语句

[编辑 | 编辑源代码]

示例

 $a = array(1, 2, 3, 4, 5);
 
 foreach ($a as $val){
    echo $val;
 }
 
 // OUTPUT:
 // 12345

While 语句

[编辑 | 编辑源代码]

示例

 while ($row = mysql_fetch_row($result)){
    print $row[0];
 }

Do ... while 语句

[编辑 | 编辑源代码]

示例

 $i = 0;
 # Note that it might seem that $i will
 do{
    # never be printed to the screen, but
    print $i;
    # a DO WHILE loop always executes at
 } while ($i >0);
 # least once!

示例

 function square_number($number) {
    return ($number * $number);
 }
 
 $answer = square_number(10);
 echo "The answer is {$answer}";
 
 // OUTPUT:
 // The answer is 100

示例

 class dog {
    var $name;
    
    function __construct($name){
       $this->name = $name;
    }
    
    function bark(){
       echo "Woof! Woof!";
    }
    
    function who_am_i() {
       echo "My name is {$this->name}, and I am a dog";
    }
 }
 
 $the_dog = new dog("John");
 $the_dog->who_am_i();
 
 // OUTPUT:
 // My name is John, and I am a dog

从 PHP 5.3.3 开始,与类同名的函数将不再充当构造函数。5.3.3 之前的版本

function __construct($name)

行可以替换为

function dog($name)

以实现相同的效果。 [1]


  1. https://php.ac.cn/manual/en/language.oop5.decon.php
华夏公益教科书