国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > PHP > 正文

PHP學(xué)習(xí)筆記(整理完成)

2024-05-04 23:04:32
字體:
供稿:網(wǎng)友

一、現(xiàn)在開始學(xué)習(xí)php

老猴要弄個(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]-->

二、php與眾不同的continue

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中的數(shù)組

<?php

#php_array.php

/*默認(rèn)的方式下,phparraykey是非負(fù)整數(shù),這種情形和多數(shù)語言如c,c++,java中的數(shù)組是一致的

*從這點(diǎn)看,java中的數(shù)組其實(shí)是php中數(shù)組的一種默認(rèn)的方式;而phparray則還有javamap類的特性:keyvalue

×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

?>

四、可變變量、字符串運(yùn)算符和數(shù)組運(yùn)算符:相異于其他語言的部分

 <?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";//outputworld

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]-->

五、null

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說明:(1is_null --  檢測(cè)變量是否為 null

//2null 類型只有一個(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

}

?>
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 拜泉县| 阳西县| 平舆县| 祁阳县| 上杭县| 文安县| 天等县| 宜良县| 庐江县| 射洪县| 福泉市| 安多县| 临猗县| 新民市| 宣武区| 庄河市| 遂宁市| 普陀区| 关岭| 岳池县| 高淳县| 肥城市| 兴仁县| 晴隆县| 镇坪县| 山东省| 钦州市| 石屏县| 南涧| 文登市| 呼伦贝尔市| 通道| 凤台县| 扎鲁特旗| 大安市| 同仁县| 巩留县| 南乐县| 烟台市| 射阳县| 通州市|