您好,欢迎访问一九零五行业门户网

PHP学习笔记(整理完成)

一、现在开始学习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那样的强类型语言,赋值只发生在同类型之间。
*/
?>
二、php与众不同的continuecontinue与众不同之处在于接受一个可选的数字参数来决定跳过几重循环到循环结尾。
#php_continue.php
/*
在php中,continue 在循环结构用用来跳过本次循环中剩余的代码并开始执行下一次循环。
这一点和其他语言是一致的,
不过,另有妙处:continue 接受一个可选的数字参数来决定跳过几重循环到循环结尾。
*/
$i = 0;
$j = 0;
while ($i++
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中的数组 0
    [1] => 1
    [2] => 2
    [3] => 3
    [4] => 4
    [5] => 5
)
*/
//用 count() 函数来数出数组中元素的个数
for ($i=0,$size=count($array);$i
{
    echo  $array[$i];
    echo \n;
}
/*
output:
0
1
2
3
4
5
*/
/*use foreach to loop*/
echo foreach to loop\n;
foreach($array as $temp){
    echo($temp);
    echo \n;
}
//output as above
/* foreach example 1: value only */
$a = array (1, 2, 3, 17);
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.
*/
/* 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.\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);
echo $arr[foo]; // bar
echo $arr[12];    // 1
?>
四、可变变量、字符串运算符和数组运算符:相异于其他语言的部分 
second;//now $b is first==>second
echo $b\n;
//连接赋值运算符(“.=”)
//the same to $a=$a.==>second
$a.===>second;//now &a is first==>second
echo $a\n;
/*其实可以理解为就只有一种,即连接运算符
这里的点(.)连接运算符和java语言中的字符串连接符(+)是类似的。*/
?>
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
}
*/
$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
}
*/
?>
五、nullphpmanual关于null的描述:
null
特殊的 null 值表示一个变量没有值。null 类型唯一可能的值就是 null。
在下列情况下一个变量被认为是 null:
  * 被赋值为 null。
  * 尚未被赋值。
  * 被 unset()。
null 类型只有一个值,就是大小写敏感的关键字 null。
好混乱啊,在javascript中还有关键字:var用来声明变量,php没有,美元符号($)后面跟个合法的字符串,一个php的变量就诞生了,如上所说,它尚未被赋值,应该被认为是:null。使用strlen()试图将其当作string,并算出它的长度,这样做,php引擎不认为是错用。
//phpmanual说明:(1)is_null --  检测变量是否为 null
//(2)null 类型只有一个值,就是大小写敏感的关键字 null
$fo=null;
if(is_null($fo))
{//依据上述(2),并非大写的null,本不该执行此处的,实际上并非如此,why?
    echo \$fo=null is null\n;//output:$fo=null is null
}
$foo=null;
if (is_null($f)) {
    echo \$f=null is also null;//out put:$f=null is also null
}
?>
其它类似信息

推荐信息