老猴要弄個(gè)網(wǎng)站,提供主機(jī)空間的以php+mysql的居多,比較價(jià)格也相對(duì)較低,所以正好可以學(xué)習(xí)php.
不過,后來,他又說不急,我也就沒有正式開始.今天順便玩玩,還行,不同于java是強(qiáng)類型語言,php是無類型語言,這一點(diǎn)和_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"
/*
這里,由于重新將數(shù)值12賦給了本來是boolean類型的變量bool,這樣,變量bool的類型變成了integer,像java那樣的強(qiáng)類型語言,賦值只發(fā)生在同類型之間。
*/
?>
<!--[if !supportemptyparas]--> <!--[endif]-->
continue與眾不同之處在于接受一個(gè)可選的數(shù)字參數(shù)來決定跳過幾重循環(huán)到循環(huán)結(jié)尾。
#php_continue.php
/*
在php中,continue 在循環(huán)結(jié)構(gòu)用用來跳過本次循環(huán)中剩余的代碼并開始執(zhí)行下一次循環(huán)。
這一點(diǎn)和其他語言是一致的,
不過,另有妙處:continue 接受一個(gè)可選的數(shù)字參數(shù)來決定跳過幾重循環(huán)到循環(huán)結(jié)尾。
*/
$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_array.php
/*默認(rèn)的方式下,php的array的key是非負(fù)整數(shù),這種情形和多數(shù)語言如c,c++,java中的數(shù)組是一致的
*從這點(diǎn)看,java中的數(shù)組其實(shí)是php中數(shù)組的一種默認(rèn)的方式;而php的array則還有java中map類的特性:key-value
×php manual中的說法“php 中的數(shù)組實(shí)際上是一個(gè)有序圖。圖是一種把 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() 函數(shù)來數(shù)出數(shù)組中元素的個(gè)數(shù)
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";//這里使用了轉(zhuǎn)義字符/,使得$a作為一個(gè)字串輸出
}
/*
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那樣使用數(shù)組下標(biāo)方式,是無效的
echo "/n***/n";
/*output:
****
bvalue
***
*/
$arr = array("foo" => "bar", 12 => true);
<!--[if !supportemptyparas]--> <!--[endif]-->
echo $arr["foo"]; // bar
echo $arr[12]; // 1
?>
<?php
#php的可變變量
/*可變變量就是變量名可以動(dòng)態(tài)的設(shè)置和使用的變量。
一個(gè)可變變量獲取了一個(gè)普通變量的值作為這個(gè)可變變量的變量名。
因?yàn)槠胀ㄗ兞康闹凳强勺兊模钥勺冏兞康淖兞棵彩强勺兊摹?/span>
*/
//可變變量適合在什么場(chǎng)合使用呢?
$a = "hello";//定義一個(gè)普通變量
$$a = "world";//定義一個(gè)可變變量
echo "$a/n";//output:hello
echo "${$a}/n";//使用可變變量
//同echo "$hello/n";//output:world
echo "$hello/n";
?>
<!--[if !supportemptyparas]--> <!--[endif]-->
<?php
#php的字符串運(yùn)算符
//連接運(yùn)算符(“.”)
$a="first";
$b=$a."==>second";//now $b is "first==>second"
echo "$b/n";
<!--[if !supportemptyparas]--> <!--[endif]-->
//連接賦值運(yùn)算符(“.=”)
//the same to $a=$a."==>second"
$a.="==>second";//now &a is "first==>second"
echo "$a/n";
<!--[if !supportemptyparas]--> <!--[endif]-->
/*其實(shí)可以理解為就只有一種,即連接運(yùn)算符
這里的點(diǎn)(".")連接運(yùn)算符和java語言中的字符串連接符("+")是類似的。*/
?>
<!--[if !supportemptyparas]--> <!--[endif]-->
<?php
#php的數(shù)組運(yùn)算符:+
/* php 僅有的一個(gè)數(shù)組運(yùn)算符是 + 運(yùn)算符。
它把右邊的數(shù)組附加到左邊的數(shù)組后,但是重復(fù)的鍵值不會(huì)被覆蓋。
亦即,以左邊的數(shù)組為主導(dǎo),若附加其上的(右邊的)數(shù)組中有與其key重復(fù)的部分將被忽略
*/
$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]-->
phpmanual關(guān)于null的描述:"
null
特殊的 null 值表示一個(gè)變量沒有值。null 類型唯一可能的值就是 null。
在下列情況下一個(gè)變量被認(rèn)為是 null:
* 被賦值為 null。
* 尚未被賦值。
* 被 unset()。
null 類型只有一個(gè)值,就是大小寫敏感的關(guān)鍵字 null。
"
<!--[if !supportemptyparas]--> <!--[endif]-->
好混亂啊,在javascript中還有關(guān)鍵字:var用來聲明變量,php沒有,美元符號(hào)($)后面跟個(gè)合法的字符串,一個(gè)php的變量就誕生了,如上所說,它尚未被賦值,應(yīng)該被認(rèn)為是:null。使用strlen()試圖將其當(dāng)作string,并算出它的長(zhǎng)度,這樣做,php引擎不認(rèn)為是錯(cuò)用。
<?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 -- 檢測(cè)變量是否為 null
//(2)null 類型只有一個(gè)值,就是大小寫敏感的關(guān)鍵字 null
<!--[if !supportemptyparas]--> <!--[endif]-->
$fo=null;
<!--[if !supportemptyparas]--> <!--[endif]-->
if(is_null($fo))
{//依據(jù)上述(2),并非大寫的null,本不該執(zhí)行此處的,實(shí)際上并非如此,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
}
?>新聞熱點(diǎn)
疑難解答