1. Hello World
<?php
echo "Hello,World!"; //> Hello,World!
2. 注释
// /**/
3. 数据类型
数据类型 | 类型中文名 | 类型名 |
---|---|---|
标量类型 | 布尔型 | boolean |
整型 | integer | |
浮点型 | float | |
字符串 | string | |
复合类型 | 数组 | array |
对象 | object |
查看数据类型
void var_dump($expression[,$...])
4. 引号
-
单引号内容不执行
-
双引号执行
<?php
//单引号内容可以套在双引号中输出
$name = 'hello';
echo "the $name\n"; //> the hello
echo 'the $name'; //> the $name
echo "\n";
//可以使用大括号括到双引号内
$letter1 = 'a';
$letter2 = 'c';
echo "{$letter1}b{$letter2}"; //> abc
5. 可变变量
<?php
$hello = "world";
$world = "hello";
echo $$hello; //> hello
6. 并置运算符
<?php
$a='hello,';
$b='world!';
echo $a.$b; //> hello,world!
7. 字符串函数(基础)
<?php
echo strpos("Hello world!","world"); //> 6
echo "\n";
echo strlen("Hello world!"); //> 12
8. 设置常量
<?php
define("Name", "Zeng Hui!");
echo Name; //> Zeng Hui!
9. 运算符
<?php
$x = 10;
$y = 6;
echo ($x+$y); //> 16
echo "\n";
echo ($x-$y); //> 4
echo "\n";
echo ($x*$y); //> 60
echo "\n";
echo ($x/$y); //> 1.6666666666667
echo "\n";
echo ($x%$y); //> 4
echo "\n";
echo "\n";
echo ($y+=4); //> 10
echo "\n";
echo ($y-=4); //> 6
echo "\n";
echo ($x*=2); //> 20
echo "\n";
echo ($x/=2); //> 10
echo "\n";
echo "\n";
$a = "hello ";
echo ($a .= "world"); //> hello world
echo "\n";
echo "\n";
$c = 9;
echo ($c++); //> 9
echo "\n";
$c = 9;
echo (++$c); //> 10
echo "\n";
$d = 11;
echo ($d--); //> 11
echo "\n";
$d = 11;
echo (--$d); //> 10
echo "\n";
10. 逻辑关系
运算符 | 名称 | 描述 |
---|---|---|
x == y | 等于 | x 等于 y |
x === y | 恒等于 | x 的值与类型等于 y |
x != y | 不等于 | x 不等于 y |
x <> y | 不等于 | 同上 |
x !== y | 不恒等于 | x 不恒等于 y |
x > y | 大于 | x 大于 y |
x < y | 小于 | x 小于 y |
x >= y | 大于等于 | x 大于等于 y |
x <= y | 小于等于 | x 小于等于 y |
逻辑运算符 / 三目运算符与 C/C++ 相同
12. 随机数
<?php
$data = rand(0,5);
echo $data;
13. 数组
$array1 = array(100, 3.14, true, "hello");
$array2 = [100, 3.14, true, "hello"];
array(4) {
[0] => int(100),
[1] => float(3.14),
[2] => bool(true),
[3] => string(5) "hello"
}
$cars = ["Volvo", "BWM", "Toyota"];
$arr = array('tom', 'andy', 'jack');;
$length = count($arr);
for($x = 0; $x < $length; $x++) {
echo $arr[$x];
echo "<br />";
}
// 关联数组
$age = ["Peter" => "35", "Ben" => "37", "Joe" => "43"];
// or
$age['Peter'] = "35";
$age['Ben'] = "37";
$age['Joe'] = "43";
$arr2 = [
'name' => 'Tom',
'age' => '18',
'sex' => 'male'
];
foreach ($arr2 as $key => $value) {
echo $key . ": " . $value . "<br />";
};
// 删除数组元素
$array3 = array(0 => "a", 1 => "b", 2 => "c");
unset($array3[1]);
print_r($array3);
14. 函数
-
无参调用
function writeName(){ echo "Kai Jim Refsnes"; } echo "My name is "; writeName();
-
有参调用
function writeName($fname){ echo $fname . " Refsnes.<br />"; } echo "My name is "; writeName("Kai Jim"); echo "His name is "; writeName("Hege");
-
多参传入
<?php function writeName($fname,$punctuation) { echo $fname . " Refsnes" . $punctuation . "\n"; } echo "My name is "; writeName("Kai Jim","."); echo "My sister's name is "; writeName("Hege","!"); ?>
-
-
返回值
<?php function add($x,$y) { $total=$x+$y; return $total; } echo "1 + 2 = " . add(1,2); ?>
15. 超全局变量
$GLOBALS
、$_SERVER
、$_REQUEST
、$_POST
、$_GET
、$_FILES
、$_ENV
、$_COOKIE
、$_SESSION
16. 类和对象
-
创建类
<?php class User { var $name; public function getName() { return $this->name; } public function setName($name) { $this->name = $name; } } ?>
-
类的实例化对象
-
set 方法
<?php // Class User $Alice = new User; $Alice->setName("Alice123"); echo $Alice->getName(); ?>
-
构造方法
<?php class User { var $name; public function getName() { return $this->name; } // 构造方法 function __construct($name) { $this->name = $name; } } $Alice = new User("Alice123"); echo $Alice->getName(); ?>
-
17. 表单
以下两个文件必须在同一目录下
-
index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>PHP</title> </head> <body> <form action="form.php" method="post"> 账号: <input type="text" name="username"><br /> 密码: <input type="text" name="password"><br /> <input type="submit" value="提交"> </form> </body> </html>
-
form.php
<?php echo $_POST["username"]."<br />"; echo $_POST["password"]; ?>
编辑于 2022/9/19
-End-
标签:运算符,name,等于,echo,语法,速览,array,PHP,hello From: https://www.cnblogs.com/SRIGT/p/17678234.html