PHP 是一种流行的服务器端脚本语言,用于开发动态网页和 Web 应用程序。
// 单行注释/*
多行注释
*/
变量
$variable_name = value;
数据类型
- 字符串:
"Hello, World!"
,'123'
- 整数:
123
,-45
- 浮点数:
3.14
,2.718
- 布尔值:
true
,false
- 数组:
$array = array(1, 2, 3);
- 对象:
$object = new ClassName();
- NULL 值:
null
4. 运算符
- 算术运算符:
+
,-
,*
,/
,%
- 比较运算符:
==
,!=
,<
,>
,<=
,>=
- 逻辑运算符:
&&
,||
,!
5. 控制结构
条件语句
if (condition) {// code block to be executed if condition is true
} elseif (another_condition) {// code block to be executed if another_condition is true
} else {// code block to be executed if all conditions are false
}
循环结构
for ($i = 0; $i < 5; $i++) {// code block to be executed repeatedly until condition is false
}while (condition) {// code block to be executed as long as condition is true
}foreach ($array as $value) {// code block to be executed for each element in the array
}
. 函数
function functionName($param1, $param2) {// function body with parameters and return statement if needed
}
包含文件和引入文件内容:
include 'filename.php'; #包含并运行指定文件中的代码。
require 'filename.php'; #引入并运行指定文件中的代码,如果有错误则停止执行。
include_once 'filename.php'; #只包含一次指定文件中的代码。
require_once 'filename.php'; #只引入一次指定文件中的代码。