<?#asp風格短標記 echo "hello world!<br/>";?><?php $date = "2013-5-21";?><p>today's date is <?php echo $date;?></p><?php print("<br/>print test!"); $name = "zhangsan"; $age = 20; printf("<br/>name:%s,age:%d",$name,$age); $str = sprintf("<br/>hello...<br/>"); echo $str; $a = true; $lst["aa"] = "aaaaa"; $lst["bb"] = "bbbb"; $lst["cc"] = "cccc"; var_dump($lst); print_r($lst); $k = (int)10.8; #強制類型轉換 print("<br/>".$k); #類型轉換 $i = "123"; echo "<br/>i的類型為:".gettype($i); settype($i, integer); echo "<br/>轉換後i的類型為:".gettype($i); $t = "ssss"; function checkType($obj){ if(is_integer($obj)){ echo "<br/>傳遞的類型為int"; }elseif (is_bool($obj)){ echo "<br/>傳遞的類型為bool"; }elseif (is_string($obj)){ echo "<br/>傳遞的類型為string"; }else{ echo "<br/>未知類型..."; } } checkType("100000"); checkType(200); checkType(false); #引用賦值 $y = "hahaha"; #$z = $y; $r = &$y; #$z = "hhhhh"; $r = "uuuu"; echo "<br/>$y"; #傳引用 function refTest(&$kk){ $kk = 2 * $kk; } $dd = 100; refTest($dd); echo "<br/>".$dd; #傳值 function valTest($kk){ $kk = 5 * $kk; } $k1 = 500; valTest($k1); echo "<br/>".$k1; #變數範圍 #局部變數與全域變數 $t2 = 1200; #全域變數 function changeNumber(){ $t2 = 1500;#局部變數 } changeNumber(); echo "<br/>$t2"; #列印1200 function changeNumber1(){ global $t2; #global $t2 = 1800; error $t2 = 1800; } changeNumber1(); echo "<br/>$t2"; #列印1800 function Test() { $GLOBALS["txn"] = 500; #$GLOBALS 必須大寫 } Test(); echo "<br/>".$GLOBALS["txn"]; #靜態變數 function keep_trace(){ static $count = 0; $count++; echo "$count<br/>"; } keep_trace(); keep_trace(); keep_trace(); keep_trace(); #超級全域變數 foreach($_SERVER as $k => $v){ print("$k => $v<br/>"); } #$_GET if(isset($_GET["name"])){ printf("目前使用者:%s",$_GET["name"]); } #$_POST if(isset($_POST["name"])){ printf("目前使用者:%s",$_POST["name"]); } #cookie setcookie("domain","my.com"); if(isset($_COOKIE["domain"])){ printf("當前域資訊:%s<br/>",$_COOKIE["domain"]); } #files #$_FILES["file"]["name"]; #$_FILES["file"]["type"]; #$_FILES["file"]["size"]; #$_FILES["file"]["error"]; #seession $_SESSION["user"] = "wangwu"; echo "當前操作人:".$_SESSION["user"]."<br/>"; #常量的定義 define("PI",3.1415926); $pi2 = PI * 2; echo "2pi:$pi2<br/>"; ?>