做者:20135216 平臺:windows10 軟件:XAMPP,DreamWeaver 說明:該筆記是對網站編程語言的詳細學習
通常方法php
<?php class User { public $name,$password; function get_password() { return $this->password; } } ?>
示例html
<?php User::pwd_string();//::表示伴隨靜態方法的自調用 class User { static function pwd_string() { echo "Please enter your password"; } } ?>
類內能夠定義常量linux
<?php Translate::lookup(); class Translate { const ENGLISH = 0; const SPANISH = 1; const FRENCH = 2; } static function lookup() { echo self::SPANISH;//self加上::能夠直接引用常量 } } ?>
代碼web
<?php $object = new Subscriber; $object->name = "Fred"; $object->password = "pword"; $object->phone = "012 345 678"; $object->email = "fred@bloggs.com"; $object->display(); class User { public $name,$password; function save_user() { echo "Save User code goes here"; } } class Subscriber extends User { public $phone,$email; function display() { echo "Name:" . $this->name . "<br>"; echo "Pass:" . $this->password . "<br>"; echo "Phone:" . $this->phone . "<br>"; echo "Email:" . $this->email; } } ?>
示例編程
<?php $p1 = array("Copier","Inkjet","Laser","Photo"); echo "p1 element:" . $p1[2] . "<br>"; $p2 = array('copier' => "Copier & Multipurpose", 'inkjet' => "Inkjet Printer", 'laser' => "Laser Printer");//這裏p1與p2是不一樣的數組 echo "p2 element: ". $p2['inkjet'] . "<br>"; ?>
示例windows
<?php $paper = array('copier' => "Copier & Multipurpose", 'inkjet' => "Inkjet Printer", 'laser' => "Laser Printer"); foreach($paper as $item -> $description) echo "$item:$description<br>"; ?>
示例數組
<?php $temp = explode(' ',"This is a sentence with seven words"); print_r($temp); ?>
示例瀏覽器
<?php $j = 23; $temp = "Hello"; $address = "1 Old Street"; $age = 61; print_r(compact(explode(' ','j temp address age'))); ?>
精確度設置服務器
<?php echo "<pre>";//使得空格能顯示 printf("The result is $%15f\n",123.42/12);//將結果填充到15位 printf("The result is $%015f\n",123.42/12);//使用0,將結果填充到15位 printf("The result is $%15.2f\n",123.42/12);//將結果填充到15位,小數點以後精確2位 printf("The result is $%'#15.2f\n",123.42/12);//將結果用#填充到15位,小數點以後精確2位 ?> ![](http://i.imgur.com/wbkkRaN.png)
字符串填充編程語言
<?php echo "<pre>";//使得空格能顯示 $h = 'Rasmus'; printf("[%s]\n",$h);//標準格式輸出 printf("[%12s]\n",$h);//利用空格右對齊,輸出長度爲12 printf("[%-12s]\n",$h);//利用空格左對齊 $d = 'Rasmus Lerdorf'; printf("[%12.8s]\n",$d);//右對齊,長度爲12,截取8個字符 printf("[%-12.12s]\n",$d);//左對齊,長度爲12,截取12個字符 printf("[%-'@12.10s]\n",$d);//左對齊,長度爲12,用@填充 ?> ![](http://i.imgur.com/R7PbWn4.png)
用法
$hexstring = sprintf("%x%x%x",65,127,245);//以字符串形式返回RGB顏色組65,127,245的十六進制值,並將結果保存在$hexstring中
舉例
<?php echo date("l F jS , Y - H:ia",time()); //l表示每週的日期全稱 //F表示月份 //j表示每個月的日期 //S表示每個月中日期的後綴 //Y表示年份,四位數 //H表示天天的小時,24小時制,有前導0 //i表示分 //a表示午前或午後 //time()表示當前時間戳 ?>
用法:
if(file_exits("testfile.txt")) echo "File exits";
舉例
<?php $fh = fopen("exercise2.txt",'r+') or die("Failed to open file"); $text = fgets($fh); fseek($fh,0,SEEK_END); fwrite($fh,$text) or die("Could not write to file"); fclose($fh); echo "File 'exercise2.txt' successfully update"; ?>
舉例
<?php echo file_get_contents("http://www.cnblogs.com/lwr-/p/6349948.html"); ?>
代碼示例:
<?php echo <<<_END <html><head><title>PHP Form Upload</title></head><body> <form method = 'post' action = 'exercise2.php' enctype = 'multipart/form-data'> Select file:<input type = 'file' name = 'filename' size = '10'> <input type = 'submit' value = '上傳'> </form> _END; if($_FILES) { $name = $_FILES['filename']['name']; move_uploaded_file($_FILES['filename']['tmp_name'],$name); echo "Uploaded image '$name'<br /><img src = '$name'>"; } echo "</body></html>"; ?>
代碼
<?php echo <<<_END <html><head><title>PHP Form Upload</title></head><body> <form method = 'post' action = 'exercise2.php' enctype = 'multipart/form-data'> Select a JPG,GIF,PNG,TIF file:<input type = 'file' name = 'filename' size = '10'> <input type = 'submit' value = '上傳'> </form> _END; if($_FILES) { $name = $_FILES['filename']['name']; switch($_FILES['filename']['type']) { case 'image/ipeg':$ext = 'jpg';break; case 'image/gif':$ext = 'gif';break; case 'image/png':$ext = 'png';break; case 'image/tiff':$ext = 'tif';break; default:$ext = '';break; } if($ext) { $n = "image.$ext"; move_uploaded_file($_FILES['filename']['tmp_name'],$n); echo "Uploaded image '$name' as '$n':<br>"; echo "<img src = '$n'>"; } else echo "'$name' is not an accepted image file"; } else echo "No image has been uploaded"; echo "</body></html>"; ?>