一、現在開始學習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 loop\n";
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 "$a\n";//output:hello
echo "${$a}\n";//使用可變變數
//同echo "$hello\n";//output:world
echo "$hello\n";
?>
<!--[if !supportEmptyParas]--> <!--[endif]-->
<?php
#php的字串運算子
//串連運算子(“.”)
$a="first";
$b=$a."==>second";//now $b is "first==>second"
echo "$b\n";
<!--[if !supportEmptyParas]--> <!--[endif]-->
//串連賦值運算子(“.=”)
//the same to $a=$a."==>second"
$a.="==>second";//now &a is "first==>second"
echo "$a\n";
<!--[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 variable\n";//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 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
}
?>