php基础部分常见的函数和关键字

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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
bool setcookie ( string $name [, string $value [, int $expire = 0 [, string $path [, string $domain [, bool $secure = false [, bool $httponly = false ]]]]]] ) 
explain:This requires that you place calls to this function prior to any output, including <html> and <head> tags as well as any whitespace. 
example:setcookie("TestCookie", $value, time()+3600); 
   
   
bool define ( string $name , mixed $value [, bool $case_insensitive = false ] )  //定义一个常量 
const CONSTANT = 'Hello World'//实用关键字const定义一个常量 ,效果一样 
   
example:define("CONSTANT", "Hello world."); 
   
bool defined ( string $name ) //检查一个常量是否存在 
   
   
bool isset ( mixed $var [, mixed $... ] )   //检查一个变量是否存在 
   
void unset ( mixed $var [, mixed $... ] )  //释放一个变量 
   
   
bool function_exists ( string $function_name //检查一个函数是否存在 
   
   
string get_class ([ object $obj ] )   //获取一个对象的所属类名   
   
array get_object_vars ( object $obj )   //返回由对象属性组成的关联数组 
   
bool file_exists ( string $filename )   // 检查文件或目录是否存在 
   
   
   
比较运算符 
$a == $b等于,如果类型转换后 $a 等于 $b。 
$a === $b全等,如果 $a 等于 $b,并且它们的类型也相同。 
$a != $b不等,如果类型转换后 $a 不等于 $b。 
$a <> $b不等,如果类型转换后 $a 不等于 $b。 
$a !== $b不全等,如果 $a 不等于 $b,或者它们的类型不同。 
$a < $b小与,如果 $a 严格小于 $b。 
$a > $b大于,如果 $a 严格大于 $b。 
$a <= $b小于等于,如果 $a 小于或者等于 $b。 
$a >= $b大于等于,如果 $a 大于或者等于 $b。 
   
   
PHP 支持一个错误控制运算符:@。当将其放置在一个 PHP 表达式之前,该表达式可能产生的任何错误信息都被忽略掉。  
   
执行运算符   , 反引号运算符在激活了安全模式或者关闭了 shell_exec() 时是无效的。 
<?php 
$output = `ls -al`; 
echo "<pre>$output</pre>"
?>  
   
字符串运算符  有两个字符串(string)运算符。第一个是连接运算符("."),它返回其左右参数连接后的字符串。第二个是连接赋值运算符(".="),它将右边参数附加到左边的参数之后。 
   
数组运算符 
   
$a + $b联合       $a $b 的联合。 
$a == $b相等  如果 $a $b 具有相同的键/值对则为 TRUE。 
$a === $b全等 如果 $a $b 具有相同的键/值对并且顺序和类型都相同则为 TRUE。 
$a != $b不等  如果 $a 不等于 $b 则为 TRUE。 
$a <> $b不等    如果 $a 不等于 $b 则为 TRUE。 
$a !== $b不全等        如果 $a 不全等于 $b 则为 TRUE。 
   
   
类型运算符       instanceof 用于确定一个 PHP 变量是否属于某一类 class 的实例: 
<?php 
class MyClass{} 
class NotMyClass{} 
$a = new MyClass; 
var_dump($a instanceof MyClass); 
var_dump($a instanceof NotMyClass); 
?>  
以上例程会输出: 
bool(true) 
bool(false) 
   
   
bool is_a ( object $object , string $class_name [, bool $allow_string = FALSE ] ) //如果对象属于该类或该类是此对象的父类则返回 TRUE 
   
   
   
foreach循环数组或者对象 
foreach (array_expression as $value
    statement 
foreach (array_expression as $key => $value
    statement 
   
   
require include几乎完全一样,除了处理失败的方式不同之外。 require在出错时产生 E_COMPILE_ERROR级别的错误。换句话说将导致脚本中止而 include只产生警告(E_WARNING),脚本会继续运行。 
include 'vars.php'
   
require_once 语句和 require语句完全相同,唯一区别是 PHP 会检查该文件是否已经被包含过,如果是则不会再次包含。 
   
goto:   (相对于C语言就是一个阉割品) 
goto操作符可以用来跳转到程序中的另一位置。该目标位置可以用目标名称加上冒号来标记,而跳转指令是 goto 之后接上目标位置的标记。 
PHP 中的 goto有一定限制,目标位置只能位于同一个文件和作用域,也就是说无法跳出一个函数或类方法,也无法跳入到另一个函数。也无法跳入到任何循环或者 switch 结构中。 
可以跳出循环或者 switch,通常的用法是用 goto代替多层的 break。  
<?php 
goto a; 
echo 'Foo'
a: 
echo 'Bar'
?>  
以上例程会输出: 
Bar

编程技巧