当前位置:首页> PHP教程> php基础
关键字
文章内容
PHP学习笔记
 
 
修改时间:[2009/05/07 12:10]    阅读次数:[783]    发表者:[起缘]
 
一、现在开始学*PHP
老猴要弄个网站,提供主机空间的以php+mysql的居多,比较价格也相对较低,所以正好可以学*php.
不过,后来,他又说不急,我也就没有正式开始.今天顺便玩玩,还行,不同于java是强类型语言,php是无类型语言,这一点和_javascript是相似的。
参考如下的示例代码(改编自php manual):
<?
$bool = TRUE; // a boolean
$str = "foo"; // a string
$int = 12; // an integer
echo gettype($bool); // prints out "boolean"
echo "n";
echo gettype($str); // prints out "string"
echo "n";
$bool=12;
echo gettype($bool); // prints out "integer"
/*
这里,由于重新将数值12赋给了本来是boolean类型的变量bool,这样,变量bool的类型变成了integer,像java那样的强类型语言,赋值只发生在同类型之间。
*/
?>
<!--[if !supportEmptyParas]--> <!--[endif]-->
二、PHP与众不同的continue
continue与众不同之处在于接受一个可选的数字参数来决定跳过几重循环到循环结尾。
#php_continue.php
/*
在php中,continue 在循环结构用用来跳过本次循环中剩余的代码并开始执行下一次循环。
这一点和其他语言是一致的,
不过,另有妙处:continue 接受一个可选的数字参数来决定跳过几重循环到循环结尾。
*/
$i = 0;
$j = 0;
while ($i++ <3) {//level 3
echo "Outer
n";
while (1) {//level 2
echo " Middle
n";
while (1) {//level 1
echo " Inner
n";
continue 3;
}
echo "This never gets output.
n";
}
echo "Neither does this.
n";
$j++;
//after runs continue 3,it comes to the end of level 3
}
echo "$j=$j";//output: $j=0
?>
三、PHP中的数组
<?php
#php_array.php
/*默认的方式下,php的array的key是非负整数,这种情形和多数语言如c,c++,java中的数组是一致的
*从这点看,java中的数组其实是php中数组的一种默认的方式;而php的array则还有java中Map类的特性:key-value
×php manual中的说法“PHP 中的数组实际上是一个有序图。图是一种把 values 映射到 keys 的类型”
*/
$array=array("0","1","2","3","4","5");
print_r($array);
/*
output:
Array
(
[0] => 0
[1] => 1
[2] => 2
[3] => 3
[4] => 4
[5] => 5
)
*/
//用 count() 函数来数出数组中元素的个数
for ($i=0,$size=count($array);$i<$size;$i++)
{
echo $array[$i];
echo "n";
}
/*
output:
0
1
2
3
4
5
*/
<!--[if !supportEmptyParas]--> <!--[endif]-->
/*use foreach to loop*/
echo "foreach to loopn";
foreach($array as $temp){
echo($temp);
echo "n";
}
//output as above
<!--[if !supportEmptyParas]--> <!--[endif]-->
/* foreach example 1: value only */
<!--[if !supportEmptyParas]--> <!--[endif]-->
$a = array (1, 2, 3, 17);
<!--[if !supportEmptyParas]--> <!--[endif]-->
foreach ($a as $v) {
print "Current value of $a: $v.n";//这里使用了转义字符,使得$a作为一个字串输出
}
/*
output:
Current value of $a: 1.
Current value of $a: 2.
Current value of $a: 3.
Current value of $a: 17.
*/
<!--[if !supportEmptyParas]--> <!--[endif]-->
/* foreach example 2: value (with key printed for illustration) */
<!--[if !supportEmptyParas]--> <!--[endif]-->
$a = array (1, 2, 3, 17);
<!--[if !supportEmptyParas]--> <!--[endif]-->
$i = 0; /* for illustrative purposes only */
<!--[if !supportEmptyParas]--> <!--[endif]-->
foreach ($a as $v) {
print "$a[$i] => $v.n";
$i++;
}
$array2=array("a"=>"avalue","b"=>"bvalue","c"=>"b");
print_r($array2);
echo "****n";
echo $array2[$array2["c"]];//
//echo $array2[$array2[2]];//企图像java那样使用数组下标方式,是无效的
echo "n***n";
/*output:
****
bvalue
***
*/
$arr = array("foo" => "bar", 12 => true);
<!--[if !supportEmptyParas]--> <!--[endif]-->
echo $arr["foo"]; // bar
echo $arr[12]; // 1
?>
四、可变变量、字符串运算符和数组运算符:相异于其他语言的部分
<?php
#php的可变变量
/*可变变量就是变量名可以动态的设置和使用的变量。
一个可变变量获取了一个普通变量的值作为这个可变变量的变量名。
因为普通变量的值是可变的,所以可变变量的变量名也是可变的。
*/
//可变变量适合在什么场合使用呢?
$a = "hello";//定义一个普通变量
$$a = "world";//定义一个可变变量
echo "$an";//output:hello
echo "${$a}n";//使用可变变量
//同echo "$hellon";//output:world
echo "$hellon";
?>
<!--[if !supportEmptyParas]--> <!--[endif]-->
<?php
#php的字符串运算符
//连接运算符(“.”)
$a="first";
$b=$a."==>second";//now $b is "first==>second"
echo "$bn";
<!--[if !supportEmptyParas]--> <!--[endif]-->
//连接赋值运算符(“.=”)
//the same to $a=$a."==>second"
$a.="==>second";//now &a is "first==>second"
echo "$an";
<!--[if !supportEmptyParas]--> <!--[endif]-->
/*其实可以理解为就只有一种,即连接运算符
这里的点(".")连接运算符和java语言中的字符串连接符("+")是类似的。*/
?>
<!--[if !supportEmptyParas]--> <!--[endif]-->
<?php
#php的数组运算符:+
/* PHP 仅有的一个数组运算符是 + 运算符。
它把右边的数组附加到左边的数组后,但是重复的键值不会被覆盖。
亦即,以左边的数组为主导,若附加其上的(右边的)数组中有与其key重复的部分将被忽略
*/
$a = array("a" => "apple", "b" => "banana");
$b = array("a" =>"pear", "b" => "strawberry", "c" => "cherry");
$a1=array("c"=>"a1_cherry","d"=>"a1=d");
$c = $a + $b;
var_dump($c);
/*output:
array(3) {
["a"]=>
string(5) "apple"
["b"]=>
string(6) "banana"
["c"]=>
string(6) "cherry"
}
*/
<!--[if !supportEmptyParas]--> <!--[endif]-->
$d = $a + $b+$a1;
var_dump($d);
/*output:
array(4) {
["a"]=>
string(5) "apple"
["b"]=>
string(6) "banana"
["c"]=>
string(6) "cherry"
["d"]=>
string(4) "a1=d"
}
*/
?>
<!--[if !supportEmptyParas]--> <!--[endif]-->
五、NULL
PHPmanual关于NULL的描述:"
NULL
特殊的 NULL 值表示一个变量没有值。NULL 类型唯一可能的值就是 NULL。
在下列情况下一个变量被认为是 NULL:
* 被赋值为 NULL。
* 尚未被赋值。
* 被 unset()。
NULL 类型只有一个值,就是大小写敏感的关键字 NULL。
"
<!--[if !supportEmptyParas]--> <!--[endif]-->
好混乱啊,在javascript中还有关键字:var用来声明变量,php没有,美元符号($)后面跟个合法的字符串,一个php的变量就诞生了,如上所说,它尚未被赋值,应该被认为是:NULL。使用strlen()试图将其当作string,并算出它的长度,这样做,php引擎不认为是错用。
<?php
if(is_null($none))
print "length=".strlen($none)."n";//can output:length=0
else
print "undefined variablen";//can not come here
?>
<?
//PHPmanual说明:(1)is_null -- 检测变量是否为 NULL
//(2)NULL 类型只有一个值,就是大小写敏感的关键字 NULL
<!--[if !supportEmptyParas]--> <!--[endif]-->
$fo=null;
<!--[if !supportEmptyParas]--> <!--[endif]-->
if(is_null($fo))
{//依据上述(2),并非大写的NULL,本不该执行此处的,实际上并非如此,why?
echo "$fo=null is NULLn";//output:$fo=null is NULL
}
$foo=NULL;
if (is_null($f)) {
echo "$f=NULL is also NULL";//out put:$f=NULL is also NULL
}
?>
来源:CSDN phpfans.net收集整理