background image

 

编程背景的话,可以发现 www.srmqgg.com

 

一切都得心应手。

    与 Perl 语言一样,在 PHP 中,如果在双引号包含的字符串中含有变量的话,该变量将用相应的变

 

量值替换;如果字符串被单引号包含,则不做替换。例如:

  <?php    

  $name = ‘PETER’

 

  $greeting_1 = “Hello, $name

”  

! ;

  $greeting_2 = ‘Hello, $name

’  

! ;

  echo “$greeting_1\n”

 

  echo “$greeting_2\n”

 

  ?> 

  

 

显示结果为:

  Hello, PETER

 

  Hello, $name

 

  

(注:上述代码中的 \n”

 

为换行符,只能在双引号字符串下使用)     

     

  B. 

 

变量    

  PHP 允许用户象使用常规变量一样使用环境变量。例如,在页面 http://www.nba.com/scores/index.html 中

 

包含如下代码:    
  <?php 

     echo “[$REQUEST_URI]”

 

;    

  ?> 

     则输出结果为[/scores/index.html]    

  

 

       C. 

 

数组    

  用户在使用 PHP

 

创建数组时,可以把数组索引(包括常规索引或关联索引)加入方括号中。例如:

     $fruit[0] = ‘banana’;    

  $fruit[1] = ‘apple’;    

  $favorites['animal'] = ‘tiger’;    

  $favorites['sports'] = ‘basketball’;    

    如果用户在向数组赋值时不指明数组下标,PHP 将自动把该对象加入到数组末尾。例如对于上述

$fruit

 

数组可以用以下方式赋值而保持结果不变,    

  $fruit[] = ‘banana’;    

  $fruit[] = ‘apple’;    

  同样,在 PHP

 

中,用户还可以根据需要建立多维数组。例如:    

  $people[‘David’][‘shirt’] = ‘blue’;    

  $people[‘David’][‘car’] = ‘red’;    

  $people[‘Adam’][‘shirt’] = ‘white’;    

  $people[‘Adam’][‘car’] = ‘silver’;    

  在 PHP 中,用户还可以使用 array()

 

函数快速建立数组。例如:    

  $fruit = array(‘banana’,‘apple’);    

  $favorites = array(‘animal’ => ‘tiger’, ‘sports’ => ‘basketball’);    

  或者使用 array()

 

函数创建多维数组:    

  $people = array (‘David’ => array(‘shirt’ => ‘blue’,’car’ => ‘red’),    

  ‘Adam’ => array(‘shirt’ => ‘white’,‘car’ => ‘silver’));    

  此外,PHP 还提供了内置函数 count()

 

用于计算数组中的元素数量。例如:    

  $fruit = array(‘banana’, ‘apple’);