php

PHP常量

设置PHP常量

语法

1
bool define ( string $name , mixed $value [ , bool $case_insensitive = false ])

实例

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
<?php
// 区分大小写的常量名
define("GREETING","欢迎访问 runoob.com");
echo GREETING; //输出欢迎访问"Runoob.com"
echo '<br>';
echo greeting; //输出 "greeting"
?>

/*输出结果:
欢迎访问 runoob.com

Warning: Use of undefined constant greeting - assumed 'greeting' (this will throw an Error in a future version of PHP) in /box/script.php on line 6
greeting*/




<?php
// 不区分大小写的常量名
define("GREETING","欢迎访问 runoob.com",ture);
echo GREETING; //输出欢迎访问"Runoob.com"
echo '<br>';
echo greeting; //输出 "greeting"
?>

/*输出结果:
Warning: Use of undefined constant ture - assumed 'ture' (this will throw an Error in a future version of PHP) in /box/script.php on line 3
欢迎访问 runoob.com
欢迎访问 runoob.com
*/

PHP字符串变量

1
2
3
4
<?php 
$txt= "Hello World!";
echo $txt;
?>

PHP并置运算符

1
2
3
4
5
6
7
<?php 
$txt1= "Hello World!";
$txt2= "What a nice day!";
echo $txt1 . " " . $txt2; #并置运算符(.)串联两个字符串。使用两次字符串要在之间插入一个空格。
?>

#输出结果:Hello World! What a nice day!

PHP strlen() 函数

1
2
3
4
5
<?php 
echo strlen("Hello World!"); #获取字符串长度
?>

#输出结果:12

PHP strpos() 函数

1
2
3
4
5
<?php 
echo strpos("Hello World!","world"); #strpos()用于在字符串中查找一个字符或一段特定文本
?>

#输出结果:6

PHP类型比较

松散比较

1
2
3
4
5
6
7
8
9
<?php 
if(42 == "42"){
echo '1、值相等';
}

echo PHP_EOL;// 换行符
?>

#输出结果:1、值相等

严格比较

1
2
3
4
5
6
7
8
9
<?php 
if(42 === "42"){
echo '2、类型相等';
} else {
echo '3、不相等';
}
?>

#输出结果:3、不相等

PHP比较0、false、null

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
<?php
echo '0 == false: ';
var_dump(0 == false);
echo '0 === false: ';
var_dump(0 === false);
echo PHP_EOL;
echo '0 == null';
var_dump(0 == null);
echo '0 === null';
var_dump(0 === null);
echo PHP_EOL;
echo 'false == null:';
var_dump(false == null);
echo 'false === null';
var_dump(false ===null);
echo '"0" == false';
var_dump("0" == false);
echo '"0" === false';
var_dump("0" === false);
echo PHP_EOL;
echo '"0" == null';
var_dump("0" == null);
echo '"0" === null';
echo PHP_EOL;
echo '"" == false:';
var_dump("" == false);
echo '"" == null ';
var_dump("" === null);


#输出结果:
0 == false: bool(true)
0 === false: bool(false)

0 == nullbool(true)
0 === nullbool(false)

false == null:bool(true)
false === nullbool(false)
"0" == falsebool(true)
"0" === falsebool(false)

"0" == nullbool(false)
"0" === null
"" == false:bool(true)
"" == null bool(false)

#bool为布尔型变量,用于逻辑运算和关系运算
#var_dump()函数用于打印与显示程序代码结构