lwcompany

功到自然成

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: :: 管理 ::

主要主题是

.php字符串中的变量扩展系统

.php中可用的更多数据类型

.类型之间的转换

.输入和使用变量和常量

.如何在php中构建表达式以及构建表达式所需的操作符

.使用语言中可用的控制结构

2.1 输入字符串的更多介绍

                            

1 <?php
2
3 $hour = 16;
4 $kilometres = 4;
5 $content = "cookie";
6
7 echo " 4pm in 24 hour time is {$hour}00 hours.<br/>\n";
8
9 echo <<<DONE
10 There are {$kilometres}000m in {$kilometres}km.<br/>
11 The jar is now, indeed, full of ${content}s.<br/>
12  DONE;
13
14  ?>

                 输出:     4pm in 24 hour time is 1600 hours.
                              There are 4000m in 4km.
                              The jar is now, indeed, full of cookies.

                 如果你希望在输出中生成确切的字符序列{$,就需要使用,{\$对它进行转义。

2.2 数据类型的更多介绍

                  1.数组:使用array方法来声明数组。它获得一组初始值并且返回保存所有这些值的数组对象,在默认情况下,把从0开始的整数名称或者键(key)赋给数组中的值

                             ,也可以指定要添加的新项的索引。$frunit[120]="nespola";但是你也可以使用字符串值指定键,而不使用赋值给它的默认数字。

                             $myfavourite=array("car"=>"ferrari","number“=>21,"city"=>"ouagadougou");

                        

数组运算符
  例子   名称结果
    $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

                    

1 <?php
2  $a = array("a" => "apple", "b" => "banana");
3  $b = array("a" => "pear", "b" => "strawberry", "c" => "cherry");
4
5 $c = $a + $b; // Union of $a and $b
6 echo "Union of \$a and \$b: \n";
7 var_dump($c);
8
9 $c = $b + $a; // Union of $b and $a
10 echo "Union of \$b and \$a: \n";
11 var_dump($c);
12 ?>

           

执行后,此脚本会显示:
Union of $a and $b:
array(3) {
  ["a"]=>
  string(5) "apple"
  ["b"]=>
  string(6) "banana"
  ["c"]=>
  string(6) "cherry"
}
Union of $b and $a:
array(3) {
  ["a"]=>
  string(4) "pear"
  ["b"]=>
  string(10) "strawberry"
  ["c"]=>
  string(6) "cherry"
}
                2.2.2对象
                     将在第四单元中面向对象的程序设计中使用。
                2.2.3 特殊类型和值 
                      NULL 是php中的特殊类型和值 ,它表示"无值".符合如下要求它就是null:
                           .它们被设置为区分大小写的关键字null;
                           .它们从没有赋值过
                           .使用unset方法明确清除了它们。
                      资源:有时候,php需要处理不一定来自php的对象,比如数据库或者操作系统对象的句柄。它们称为资源的特殊变量.          

2.3 强制类型转换

                      2.3.1 基础

                                    隐式强制类型转换:当遇见隐式强制类型转换的最常见的情况是:

                                                             .二进制运算操作符

                                                             .布尔表达式和表达式操作符

                                                             .需要字符串的特定方法--特定方法和操作符,比如echo\print或者字符串连接符(.)

                                   显示强制类型转换:为变量(或表达式)加上括号括起来的类型前缀,php会试图替你进行转换.

                                                              (int)\(interger)

                                                               (string)-转换为文本字符串

                                                               (object)-转换为对象                                                                  

                      2.3.2 特殊强制类型转换

                                                                转换为整数

                                                               (int)4.999

                                                                 注:null永远都转换为整数值0.

                                                                转换为浮点数

                                                                 (float)true=1.0

                                                                 把数组、对象或者资源转换为浮点值的结果是未定义的,不要试图进行这新的转换或者相信这样的转换结果

                                                                转换为字符串

                                                                 可使用类型转换符(string)或调用strval把变量转换为字符串.

                                                                  布尔型true转为字符串1,false被转换空字符串("")

                                                                 null转为空字符串('").

                                                                 转换为数组

                                                                  可使用类型转换(array)或者函数arraryr把变量或者表达式转换为数组

                                                                  null和其他未设置的变量被转换为有0个元素的空数组

                                                                 转换为对象

                                                                  你可以使用类型转换(object)把变量或者表达式转换为对象。

                                                                   把对象转换为对象仅仅返回相同对象的句柄。为所有其他类型创建stdClass类型的对象。

                     2.3.3 有用的强制类型转换函数

                                                                is_type()

                                                                .is_integer,.is_float,.is_bool,is_null,.is_object.返回布尔型 ,指出特定变量是否属于适当的类型 。

                                                                gettype()是非常有用的例程,它告诉你php当前认为变量或者表达式是什么类型。不推荐使用这个转换函数。

                                                                 settype()使用两个参数:要转换的变量和要转换为的类型 ,它表示字符串。

2.4 变量和常量

                     2.4.1 定义常量

                                                                  在php程序中,使用语言结构define定义常量,常量名子不以字符$开头,并且它们的值只能是特定类型:整数、浮点数、字符串和布尔型

                     2.4.2 按值 和按引用的变量

                                                        默认情况下,大多数变量和所有常量是按值赋值的。把一个变量的值赋给另一个变量是时,其值被复制。这种方式对于除对象外的所有类型适用

                                                        对于对象变量和资源,被复制的全部内容是底层对象或资源的句柄,但是操作的底层对象是相同的。

                                                       把变量的值赋值给另一个变量的另一种选择方式是按引用赋值。用&前缀完成的。

                                                       $a=123;

                                                       $b=&$a;

                     2.4.3 变量的范围          

                                                       函数级别的变量,内部声明的变量只在这个函数内部是合法的。

                                                       在函数外声明的变量

                                                       超级全局变量

                     2.4.4 变量的生存期

                                                       不管执行相同脚本还是不同脚本,php都不会在调用之间记忆任何内容。

                     2.4.5 预定义变量           

                                                        php提供很多预定义变量,它们给出操作环境的信息,大多是超级全局数组例如:

                                                        $GLOBALS-它包含正在执行的脚本内部全局可用的所有变量的引用

                                                        $_SERVER-脚本所在周边环境的信息

                                                        $_SESSION、$_COOKIE-它包含管理访问者和关于称为"cookie“的存储方式的信息

                                                        $_REQUEST-它包含$_post、$_GET和$_session数组

                                                        $_ENV-它包含php语言引擎所在的进程的环境变量.数组的键是环境变量的名称。

                                                        $php_errormsg-它保存php语言引擎在执行当前脚本时生成的最新的错误信息.

2.5 表达式和操作符

                      2.5.1 操作符:组合表达式

                                                        赋值:

                                                       算术操作符                                                                

                                                     例子名称结果
                                                                       -$a 取反 $a 的负值。
                                                                       $a + $b 加法 $a 和 $b 的和。
                                                                       $a - $b 减法 $a 和 $b 的差。
                                                                       $a * $b 乘法 $a 和 $b 的积。
                                                                       $a / $b 除法 $a 除以 $b 的商。
                                                                        $a % $b 取模 $a 除以 $b 的余数。

                                                        比较操作符

                                                         

                                                                  例子 名称   结果
                                                                                    $a == $b 等于 TRUE,如果 $a 等于 $b。
                                                                                    $a === $b 全等 TRUE,如果 $a 等于 $b,并且它们的类型也相同。(PHP 4 引进)
                                                                                    $a != $b 不等 TRUE,如果 $a 不等于 $b。
                                                                                    $a <> $b 不等 TRUE,如果 $a 不等于 $b。
                                                                                    $a !== $b 非全等 TRUE,如果 $a 不等于 $b,或者它们的类型不同。(PHP 4 引进)
                                                                                    $a < $b 小与 TRUE,如果 $a 严格小于 $b。
                                                                                    $a > $b 大于 TRUE,如果 $a 严格 $b。
                                                                                    $a <= $b 小于等于 TRUE,如果 $a 小于或者等于 $b。
                                                                                    $a >= $b 大于等于 TRUE,如果 $a 大于或者等于 $b。

                                                         逻辑操作符                                                  

                                                                          例子名称结果
                                                                                         $a and $b And(逻辑与) TRUE,如果 $a 与 $b 都为 TRUE
                                                                                         $a or $b Or(逻辑或) TRUE,如果 $a 或 $b 任一为 TRUE
                                                                                         $a xor $b Xor(逻辑异或) TRUE,如果 $a 或 $b 任一为 TRUE,但不同时是。
                                                                                         $a Not(逻辑非) TRUE,如果 $a 不为 TRUE
                                                                                         $a&& $b And(逻辑与) TRUE,如果 $a 与 $b 都为 TRUE
                                                                                         $a || $b Or(逻辑或) TRUE,如果 $a 或 $b 任一为 TRUE

                                                          按位操作符

                                                                      

                                                                    操作符    名称结果
                                                                                      $a & $b And(按位与) 将把 $a 和 $b 中都为 1 的位设为 1。
                                                                                      $a|| $b Or(按位或) 将把 $a 或者 $b 中为 1 的位设为 1。
                                                                                     xor ^ $b Xor(按位异或) 将把 $a 和 $b 中不同的位设为 1。
                                                                                      Not  $a Not(按位非) 将 $a 中为 0 的位设为 1,反之亦然。
                                                                                    $a << $b Shift left(左移) 将 $a 中的位向左移动 $b 次(每一次移动都表示“乘以 2”)。
                                                                                   $a >> $b Shift right(右移) 将 $a 中的位向右移动 $b 次(每一次移动都表示“除以 2”)。

                                                          字符串操作符

                                                                   连接操作符.         它对两个字符串进行操作,并且返回把二者连接在一起的单一字符串

                                                          数组操作符

                                                                  

                                                                               例子名称结果
                                                                                           $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

                                                           其他操作符

                                                                         自动递增和自动递减操作符

                                                                         $a=10;

                                                                         $b=$a++;                 b=10 ,a=11;

                                                                         $c=++$a;                 c=12,a=12;

                                                                         $d=$a--;                  d=12,a=11;

                                                                         $e=--$a;                  e=10,a=10;

                                                                         还有一个称为@a的操作符,它让php忽略特定函数调用的失败。

                                                                         最后一个操作符-外壳命令执行程序。为了它,需要把命令括在反撇号(`)之间,这样命令就被传递给处壳执行.但这样会产生安全。

                      2.5.2 组合表达式和操作符的过程

                                                                        

                                                                         结合方向运算符附加信息
                                                                                            非结合 clone new clonenew
                                                                                            左 [ array()
                                                                                            非结合 ++ -- 递增/递减运算符
                                                                                            非结合 ~ - (int) (float) (string) (array) (object) (bool) @ 类型
                                                                                            非结合 instanceof 类型
                                                                                            右结合 ! 逻辑操作符
                                                                                            左 * / % 算术运算符
                                                                                            左 + - . 算术运算符字符串运算符
                                                                                            左 << >> 位运算符
                                                                                            非结合 < <= > >= <> 比较运算符
                                                                                            非结合 == != === !== 比较运算符
                                                                                            左 & 位运算符引用
                                                                                            左 ^ 位运算符
                                                                                            左 | 位运算符
                                                                                            左 && 逻辑运算符
                                                                                            左 || 逻辑运算符
                                                                                            左 ? : 三元运算符
                                                                                            右 = += -= *= /= .= %= &= |= ^= <<= >>= 赋值运算符
                                                                                            左 and 逻辑运算符
                                                                                            左 xor 逻辑运算符
                                                                                            左 or 逻辑运算符
                                                                                            左 , 多处用到

2.6 控制结构

                      2.6.1 if语句

                                                    1.        if (expr)
                                                               statement

                                                               else

                                                    2.       elseif/else if                                                                                                    2.6.2 switch语句

1 <?php
2 if ($a == 5):
3 echo "a equals 5";
4 echo "...";
5 elseif ($a == 6):
6 echo "a equals 6";
7 echo "!!!";
8 else:
9 echo "a is neither 5 nor 6";
10 endif;
11 ?>

                                                                                                                                                                   

                                                                        switch 语句和具有同样表达式的一系列的 IF 语句相似。很多场合下需要把同一个变量(或表达式)与很多不同的值比较,并根据它等于哪个值来执行不同的代码。                                                                         这正是 switch 语句的用途。                                                                     

1 <?php
2 if ($i == 0) {
3 echo "i equals 0";
4 } elseif ($i == 1) {
5 echo "i equals 1";
6 } elseif ($i == 2) {
7 echo "i equals 2";
8 }
9
10 switch ($i) {
11 case 0:
12 echo "i equals 0";
13 break;
14 case 1:
15 echo "i equals 1";
16 break;
17 case 2:
18 echo "i equals 2";
19 break;
20 }
21 ?>

 

                      2.6.3 while/do ....while循环

                                                                  while(expr)

                                                                      block

                                                                  do

                                                                      block

                                                                  while (expr);

                                                                 

1 <?php
2 do {
3 if ($i < 5) {
4 echo "i is not big enough";
5 break;
6 }
7 $i *= $factor;
8 if ($i < $minimum_limit) {
9 break;
10 }
11 echo "i is ok";
12
13 /* process i */
14
15 } while(0);
16 ?>

                      2.6.4 for 循环                                                     

                                            for(expr1;expr2;expr3)

                                            block                          

                                              expr1:当第一次遇到FOR循环执行它一次。执行完毕后开始循环迭代。

                                              expr2:在每次迭代之前计算它。如为true,就执行代码块。

                                              expr3-在每次迭代之后计算它 

1 <?php
2 /* example 1 */
3
4 for ($i = 1; $i <= 10; $i++) {
5 echo $i;
6 }
7
8 /* example 2 */
9
10 for ($i = 1; ; $i++) {
11 if ($i > 10) {
12 break;
13 }
14 echo $i;
15 }
16
17 /* example 3 */
18
19 $i = 1;
20 for (;;) {
21 if ($i > 10) {
22 break;
23 }
24 echo $i;
25 $i++;
26 }
27
28 /* example 4 */
29
30 for ($i = 1, $j = 0; $i <= 10; $j += $i, print $i, $i++);
31 ?>
                      2.6.5 foreach循环:用于特定类型。在5单元中进行更多讲解

                      2.6.6 中断循环      :break 和continue             

posted on 2011-05-25 10:24  平渡飞扬  阅读(270)  评论(0编辑  收藏  举报