1, include的时候,根目录在哪里?
include_once( $_SERVER["DOCUMENT_ROOT"] . "/include.php" ); //注意要大写。
2, 字符串取字串用{} [] 都可以
$s = "a string";
$s[1] = "_"; // $s = "a_string"; 用$s{1}也可以
3,常量用define来定义, define( "HI", "Hello,world" ); print ( HI );
4,特殊的运算符@,安静的意思,出了错也不理睬。 @( 10/0 )
5,``反引号表示指向命令,取回结果. print( nl2br(`ls -a`) )
6, foreach 这样用 foreach( array as key=>value ) {}
7, 求数组的长度用count, 比如 count($array);
8, 取得用户的输入用$_REQUEST[], 用GET提交的可以用$_GET[]来提取,用POST提交的可以用$_POST[]来提取。
9, $_SERVER["PHP_SELF"]就是script本身
10,What is cookies? This is the best difine I see. "Cookies are small things of data created by Web Server but stored on the client."
11, 客户端发送来的cookies在$_COOKIE[]中,if you want to set cookie from server site, use setcookie() function. example: setcookie("TestCookie", $value, time()+3600); /* expire in 1 hour */
12, File upload 用$_FILES来处理,下面是一个例子:
<html><head><title>Listing 7.4</title></head><body><?php //check for file upload if(isset($_FILES['upload_test'])) { if($_FILES['upload_test']['error'] != UPLOAD_ERR_OK) { print("Upload unsuccessful!<br>\n"); } else { //delete the file unlink($_FILES['upload_test']['tmp_name']); //show information about the file print("Local File: " . $_FILES['upload_test']['tmp_name'] . "<br>\n"); print("Name: " . $_FILES['upload_test']['name'] . "<br>\n"); print("Size: " . $_FILES['upload_test']['size'] . "<br>\n"); print("Type: " . $_FILES['upload_test']['type'] . "<br>\n"); print("<hr>\n"); } }?><form enctype="multipart/form-data" action="<?=$_SERVER['PHP_SELF'] ?>" method="post"> <input type="hidden" name="MAX_FILE_SIZE"value="1024000"> <input name="upload_test" type="file"> <input type="submit"value="test upload"> </form> </body> </html>
13, fopen, and fopen mode
fopen( $filename, "r" ) //readonly
"r+" - read,write
"w" - write, will overwirte the origal one, if not exist, will try create one
"w+" - write, read
"a" - append
"a+" - append and read
14, An example to show how to use session
<?php //start session session_start(); //Set variable based on form input if(isset($_REQUEST['inputName'])) { $_SESSION['Name'] = $_REQUEST['inputName']; } //Increment counter with each page load if(isset($_SESSION['Count'])) { $_SESSION['Count']++; } else { //start with count of 1 $_SESSION['Count'] = 1; }?><html><head><title>Listing 7-6</title></head><body><?php //print diagnostic info print("<b>Diagnostic Information</b><br>\n"); print("Session Name: " . session_name() . "<br>\n"); print("Session ID: " . session_id() . "<br>\n"); print("Session Module Name: " . session_module_name() . "<br>\n"); print("Session Save Path: " . session_save_path() . "<br>\n"); print("Encoded Session:" . session_encode() . "<br>\n"); print("<hr>\n"); if(isset($_SESSION['Name'])) { print("Hello, {$_SESSION['Name']}!<br>\n"); } print("You have viewed this page " . $_SESSION['Count'] . " times!<br>\n"); //show form for getting name print("<form " . "action=\"{$_SERVER['PHP_SELF']}\" " . "method=\"post\">" . "<input type=\"text\" name=\"inputName\" " . "value=\"\"><br>\n" . "<input type=\"submit\" value=\"change name\"><br>\n" . "</form>"); //use a link to reload this page print("<a href=\"{$_SERVER['PHP_SELF']}\">reload</a><br>\n");?></body></html>