Summary of some common problems in PHP ***************************************************************** *******************
Summary to progress?
Big cabbage core
*********************************************************** ****************
1: Why can't I get a variable?
I didn't get any value when I output $ Name when I output $ Name when I output $ NAME?
Register_global defaults in the version after php4.2 is OFF If you want to get the variable submitted from another page:
Method 1: Find the register_global in php.ini, set it to ON. Method: Put this extract ($ _ post) in front of the received web page; ($ _ get); (note extract ($ _ session) There is a session_start ()). Method 3: A read variable $ A = $ _ get ["a"]; $ b = $ _ post ["b"], etc., this method is trouble, but is safe.
2: Debug your program
You must know what a variable must be known at runtime. I did this, build a file debug.php, the content is as follows:
PHP code: ----------------------------------------------- ---------------------------------
Phpob_start (); session_start (); echo "
";Echo "This page" The _GET variable is: "; Print_R ($ _ get);
Echo "The _post variables obtained on this page are:"; Print_R ($ _ POST);
Echo "The _cookie variables available on this page are:"; Print_R ($ _ cookie);
Echo "This page" _Session variables are: "; Print_R ($ _ session); Echo" pre> ";?>>
-------------------------------------------------- ------------------------------
Then set in php.ini: include_path = "C: / PHP" and place debug.php in this folder, and you can include this file in each web page, view the obtained variable name and value.
3: How to use session
Anyone is related to session, you must call the function session_start ();
Paying for the session is very simple, such as:
PHP code: ----------------------------------------------- ---------------------------------
phpsession_start (); $ name = "This is a session example"; session_register ("name"); // notice, do not write: session_register ("$ name"); echo $ _SESSION ["name"]; // Then $ _SESSION ["Name"] is "this is a session example"?>
-------------------------------------------------- ------------------------------
After PHP4.2, you can pay for the session:
PHP code: ----------------------------------------------- --------------------------------- Phpsession_start (); $ _ session ["name"] = "Value" ;?>
-------------------------------------------------- ------------------------------
Cancel session can be like this:
PHP code: ----------------------------------------------- ---------------------------------
phpsession_start (); session_unset (); session_destroy ();?>>
-------------------------------------------------- ------------------------------
Cancel a session variable has bugs above PHP4.2 or more.
note:
1: There is no output before calling session_start (). For example, the following is wrong. =============================== =========== 1 line 2 line Php3 line session_start (); // before the first line already output 4 lines ..... 5 lines?> ====== =====================================
Tip 1:
Any "....... HEADERS ALREADY SENT ..." , The same cause of error)
Tip 2:
If your session_start () is placed in a loop statement, and it is difficult to determine where to output information to the browser, you can use the following method: 1 line Php ob_start ();?> ........ Here is your program ...
2: What is wrong?
Warning: session_start (): Open (/ tmp / sess_7d190aa36b4c5ec13a5c1649cc2da23f, o_rdwr) failed: .... Because you don't specify the storage path of the Session file.
Solution: (1) Open php.ini in the C disk, find session.save_path, modified to session.save_path = "c: / tmp"
4: Why do I send variables to another web page, I only get the first half, and all lost in space.
PHP code: ----------------------------------------------- ---------------------------------
php $ var = "Hello PHP"; // Modified to $ VAR = "Hello PHP"; try what results $ pos = "Receive.php? Name =". $ var; header ("location: $ POST ");?> --------------------------------------------- -----------------------------------
Receive.php content:
PHP code: ----------------------------------------------- ---------------------------------
Phpecho "
"; echo $ _get ["name"]; ECHO " pre>";?>-------------------------------------------------- ------------------------------
The correct way is:
PHP code: ----------------------------------------------- ---------------------------------
PHP $ VAR = "Hello PHP"; $ pos = "Receive.php? Name =". Urlencode ($ VAR); Header ("Location: $ POST");?>
-------------------------------------------------- ------------------------------
You don't need to use Urldecode () in the receiving page, and the variable will be automatically encoded.
5: How to intercept the specified length of Chinese characters without the end of "?>", Exceeding the part to "..." instead
In general, the variables to be intercepted come from mysql, first to ensure that the length is long enough, generally CHAR (200), can keep 100 Chinese characters, including punctuation.
PHP code: ----------------------------------------------- ---------------------------------
Php $ str = "This character is long, ^ _ ^"; $ Short_Str = Showsh ($ STR, 4); // Intercept four Chinese characters, the result is: this character ... Echo "$ Short_STR" Function Csubstr ($ Str, $ Start, $ LEN) {$ Strlen = Strlen ($ STR); $ Clen = 0; for ($ I = 0; $ I
= $ LEN) Break; IF (Substr ($ Str, $ I, 1))> 0xA0) {IF ($ Clen> = $ Start) $ TMPSTR. = Substr ($ Str, $ I , 2); $ I ;} else {IF ($ Clen> = $ start) $ TMPSTR. = Substr ($ Str, $ I, 1);}} Return $ TMPSTR;} Function Showsh ($ STR, $ LEN) {$ Tempstr = CSUBSTR ($ STR, 0, $ LEN); if ($ STR <> $ TEMPSTR) $ TEMPSTR. = "..."; // To end, you can modify it here.
Return $ TEMPSTR;} --------------------------------------------- -----------------------------------
6: Specify your SQL statement
In the form, in front of the field, add "` ", this will not appear errors because of misuse keywords, of course, I don't recommend you to use keywords.
For example $ sql = "Insert Into` xltxlm`, `author`,`, `date`) Values ('XLTXLM', 'Use`', 1, 'Criterion Your SQL String' , '2003-07-11 00:00:00') "
"` "How to enter? On the Tab button.
7: How to make the string of the HTML / PHP format are not interpreted, but is displayed as an original
PHP code: ----------------------------------------------- ---------------------------------
PHP $ STR = "
PHP h1>"; echo "is explained:" $ Str. "
processed:"; Echo Htmlentities (NL2BR ($ Str));? >-------------------------------------------------- ------------------------------
8: How to get the variable value outside the function in the function
PHP code: ----------------------------------------------- ---------------------------------
Php $ a = "php"; foo (); function foo () {global $ a; // Delete here to see what results Echo "$ a";}?>>
-------------------------------------------------- ------------------------------
9: How do I know what function is supported by default?
PHP code: ----------------------------------------------- ---------------------------------
php $ arr = get_defined_functions (); function php () {} echo "
"; echo "here display all functions supported by the system, and self-qualified function PHP / N"; Print_R ($ ARR); ECHO " pre>";?> ---------------------------------------------------------------------------------------------------------------------------------- ----------------------------------------10: How to compare two dates a few days
PHP code: ----------------------------------------------- ---------------------------------
PHP $ DATE_1 = "2003-7-15"; // can also be: $ DATE_1 = "2003-6-25 23:29:14"; $ DATE_2 = "1982-10-1"; $ DATE_LIST_1 = EXPLODE ("-", $ DATE_1); $ DATE_LIST_2 = EXPLODE ("-", $ DATE_2); $ D1 = MKTIME (0, 0, $ DATE_LIST_1 [1], $ DATE_LIST_1 [2], $ DATE_LIST_1 [0 ]); $ d2 = mktime (0,0,0, $ date_list_2 [1], $ DATE_LIST_2 [2], $ DATE_LIST_2 [0]); $ days = round (($ D1- $ D2) / 3600/24) Echo "Even I have struggled $ days day ^ _ ^";?> --------------------------------- -----------------------------------------------
11: Why after I upgrade PHP, the original program appears full screen: undefined variable:
This is the meaning of warning, because the variable is not defined. Open php.ini, find the bottom ERROR_REPORTING, modify to Error_Reporting = E_ALL & ~ E_NOTICE
For PARSE ERROR Error Error_Reporting (0) Can't turn off. If you want to close any error prompts, open php.ini, find Display_ERRORS, set to display_errors = off. Any error will not be prompted later.
What is Error_Reporting?
12: I want to add a file in front of each file, but in the end. But a very troublesome added
1: Open the php.ini file settings include_path = "C:"
2: Write two files auto_prepend_file.php and auto_append_file.php Save in the C drive, they will automatically attach to the head and tail of each PHP file.
3: Find in php.ini: Automatically Add Files Before or After any PHP Document.Auto_prepend_file = auto_prepend_file.php; It is attached to the head auto_APpend_file = auto_append_file.php;
After you will be equivalent to each PHP file
PHP code: ----------------------------------------------- ---------------------------------
php include "auto_prepend_file.php";
....... / / Here is your program
INCLUDE "auto_append_file.php";?>
-------------------------------------------------- ------------------------------
13: How to use the PHP upload file
PHP code: ----------------------------------------------- ---------------------------------
Upload file form title> head>