PHP入門

2009-09-23 20:38

first.php

<html>

<head>
<title>
This is my first php page   !!!
</title>
</head>

<body>
<?php
//echo 'hello world!!!';
//echo phpinfo();
$foo=25;
$bar=$foo;//定義一個變量,它的值與foo的值相等

$foo=100;
echo "<br>".'$bar='.$bar;//25
$bar=&$foo;//引用一個變量,$foo改變,$bar的值跟着改變。bar相當於foo的一個別名
echo "<br>".'$bar='.$bar;//100
echo '<br><hr></hr>';
function test(){

echo '執行test()後$bar的值爲:'.$bar;//什麼都沒有
}
test();

function test1(){
global $bar;
echo '<br>執行test1()後$bar的值爲:'.$bar;//100
}
test1();

function test2($x){
$x=200;
}
test2($bar);
echo '<br>執行test2()後$bar的值爲:'.$bar;//100

function test3(&$x){
$x=200;
}
test3($bar);
echo '<br>執行test3()後$bar的值爲:'.$bar;//200
echo '<hr></hr>';
$a=array(1=>10,2.5=>3.15,'a'=>'c',"love"=>"d");
var_dump($a);//打印數組詳細信息,如數組整體長度和數組內各元素數據類型與長度
echo '<br>';
print_r($a);//打印數組或任何對象類型
echo '<br>取數組中下標爲';
echo '<hr></hr>';
class person{

}
$p=new person();//或者寫成new person
print_r($p);
echo '<hr></hr>';
$file="D:/myphp/1.txt";
$handle=@fopen($file,"r");
echo $handle;
echo '<hr></hr>';
echo '<b>獲取變量類型用gettype(),$bar的類型爲:</b>'.gettype($bar);

$m=2;
echo '<br><b>判斷變量類型用is_int,is_string,is_float等,判斷$bar是否爲整型:</b>'.is_int($bar);
$m=(String)$m;
echo '<br><b>$m=2執行$m=(String)$m;後調用is_string($m);的結果:</b>'.is_string($m);

?>

</body>
</html>

頁面輸出:

$bar=25
$bar=100

執行test()後$bar的值爲:
執行test1()後$bar的值爲:100
執行test2()後$bar的值爲:100
執行test3()後$bar的值爲:200

array(4) { [1]=> int(10) [2]=> float(3.15) ["a"]=> string(1) "c" ["love"]=> string(1) "d" }
Array ( [1] => 10 [2] => 3.15 [a] => c [love] => d )
取數組中下標爲

person Object ( )

Resource id #3

獲取變量類型用gettype(),$bar的類型爲:integer
判斷變量類型用is_int,is_string,is_float等,判斷$bar是否爲整型:1
$m=2執行$m=(String)$m;後調用is_string($m);的結果:1

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章