类型:
四种标量类型:boolean, integer, float, sring
两种符合类型:array, object
如过要将一个变量轻质转换成某类型,可以使用settype() 函数
<?php
$bool = TRUE; // a boolean
$str = "foo"; // a string
$int = 12; // an integer
echo gettype($bool); // prints out "boolean"
echo gettype($str); // prints out "string"
// If this is an integer, increment it by four
if (is_int($int)) {
$int += 4;
}
// If $bool is a string, print it out
// (does not print out anything)
if (is_string($bool)) {
echo "String: $bool";
}
?>
变量:
一个美元符号后面跟上一个变量名,大小写敏感,一个有效的变量名由字母或者下划线开头,后面跟上任意数量的字母、数字或者下划线
传值方式有:传值赋值、传地址赋值
<?php
$foo = 'Bob'; // Assign the value 'Bob' to $foo
$bar = &$foo; // Reference $foo via $bar.
$bar = "My name is $bar"; // Alter $bar...
echo $bar;
echo $foo; // $foo is altered too.
?>
常量:
常量用define()函数来定义,一个常量一旦被定义就不能再改变或者取消定义
常量只能包含标量数据:boolean, integer, float, string
常量和变量不同:
-
-
常量前面没有美元符号($);
-
常量只能用 define() 函数定义,而不能通过赋值语句;
-
常量可以不用理会变量范围的规则而在任何地方定义和访问;
-
常量一旦定义就不能被重新定义或者取消定义;
-
常量的值只能是标量。
- <?php
define("CONSTANT", "Hello world.");
echo CONSTANT; // outputs "Hello world."
echo Constant; // outputs "Constant" and issues a notice.
?>
-
表达式:
表达式是php最重要的基石
- <?php
function double($i)
{
return $i*2;
}
$b = $a = 5; /* assign the value five into the variable $a and $b */
$c = $a++; /* post-increment, assign original value of $a
(5) to $c */
$e = $d = ++$b; /* pre-increment, assign the incremented value of
$b (6) to $d and $e */
/* at this point, both $d and $e are equal to 6 */
$f = double($d++); /* assign twice the value of $d before
the increment, 2*6 = 12 to $f */
$g = double(++$e); /* assign twice the value of $e after
the increment, 2*7 = 14 to $g */
$h = $g += 10; /* first, $g is incremented by 10 and ends with the
value of 24. the value of the assignment (24) is
then assigned into $h, and $h ends with the value
of 24 as well. */
?>
运算符:
流程控制:
任何php脚本都是由一系列语句构成的,一条语句可以是一个赋值语句、一个函数调用、一个循环甚至一个什么也不做的条件语句。语句通常以分号结束。
- <?php
if ($a > $b) {
print "a is bigger than b";
} elseif ($a == $b) {
print "a is equal to b";
} else {
print "a is smaller than b";
}
?>
流程控制的代替语法,包括 if,while,for,foreach 和 switch。替代语法的基本形式是把左花括号({)换成冒号(:),把右花括号(})分别换成endif;,endwhile;,endfor;,endforeach; 以及 endswitch;
- <?php
if ($a == 5):
print "a equals 5";
print "...";
elseif ($a == 6):
print "a equals 6";
print "!!!";
else:
print "a is neither 5 nor 6";
endif;
?> - <?php
do {
if ($i < 5) {
print "i is not big enough";
break;
}
$i *= $factor;
if ($i < $minimum_limit) {
break;
}
print "i is ok";
/* process i */
} while(0);
?> - foreach (array_expression as $value) statement
foreach (array_expression as $key => $value) statement
<?php
/* foreach example 1: value only */
$a = array (1, 2, 3, 17);
foreach ($a as $v) {
print "Current value of $a: $v. ";
}
/* foreach example 2: value (with key printed for illustration) */
$a = array (1, 2, 3, 17);
$i = 0; /* for illustrative purposes only */
foreach ($a as $v) {
print "$a[$i] => $v. ";
$i++;
}
/* foreach example 3: key and value */
$a = array (
"one" => 1,
"two" => 2,
"three" => 3,
"seventeen" => 17
);
foreach ($a as $k => $v) {
print "$a[$k] => $v. ";
}
/* foreach example 4: multi-dimensional arrays */
$a[0][0] = "a";
$a[0][1] = "b";
$a[1][0] = "y";
$a[1][1] = "z";
foreach ($a as $v1) {
foreach ($v1 as $v2) {
print "$v2 ";
}
}
/* foreach example 5: dynamic arrays */
foreach (array(1, 2, 3, 4, 5) as $v) {
print "$v ";
}
?> - continue 接受一个可选的数字参数来决定跳过几重循环到循环结尾。continue 语句作用到 switch 上的作用类似于 break。如果你在循环中有一个 switch 并希望 continue 到外层循环中的下一个轮回,用 continue 2
<?php while (list ($key, $value) = each ($arr)) {
if (!($key % 2)) { // skip odd members
continue;
}
do_something_odd ($value);
}
$i = 0;
while ($i++ < 5) {
echo "Outer<br> ";
while (1) {
echo " Middle<br> ";
while (1) {
echo " Inner<br> ";
continue 3;
}
echo "This never gets output.<br> ";
}
echo "Neither does this.<br> ";
}
?>
declare
require()
include()
require once()
include once()