Summary of common problems in PHP
If you have any error, you have better ideas, welcome to post.
Please check the PHP manual, MySQL manual, and the settings inside PHPinfo before you mention the question.
Also hope you read PHP programming standards
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 to OFF in the version after php4.2
If you want to get the variable submitted from another page:
Method 1: Find register_global in php.ini and set it to ON.
Method 2: Put this EXTRACT ($ _ POST); Extract ($ _ get); (Note Extract ($ _ session) must have session_start ()).
Method 3: A read variable $ A = $ _ get ["a"]; $ b = $ _ post ["b"], etc., although this method is troubles, but is more secure.
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:
CODE:
[Copy to CLIPBOARD]
PHP
Ob_start ();
Session_start ();
Echo "
";Echo "This page is _GET variables are:";
Print_r ($ _ get);
Echo "The _post variables obtained on this page are:";
Print_r ($ _ post);
Echo "The _cookie variable from this page is:";
Print_r ($ _ cookie);
Echo "The _ssion variables obtained on this page are:";
Print_r ($ _ session);
ECHO " pre>";
?>
Then set in php.ini: include_path = "c: / PHP" and put the debug.php in this folder.
You can include this file in each 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:
CODE:
[Copy to CLIPBOARD]
PHP
Session_start ();
$ Name = "This is a session example";
Session_register ("name"); // Notice, don't write: session_register ("$ name");
Echo $ _SESSION ["Name"];
// $ _SESSION ["name"] "This is a session example"
?>
After PHP4.2, you can pay for the session:
CODE:
[Copy to CLIPBOARD]
PHP
Session_start ();
$ _SESSION ["Name"] = "Value";
?>
Cancel session can be like this:
CODE:
[Copy to CLIPBOARD]
PHP
session_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, it is wrong.
========================================== 1 line
2 line PHP
3 line session_start (); // has output in the first line
4 lines .....
5 line?>
=================================================================================================================================================================================
Tip 1:
Anyone who appears "...... Headers Already Sent ........", it is to output information to the browser before session_start ().
It is normal to remove the output, (Cookie will also have this error, the same reason is the cause of the error)
Tip 2:
If your session_start () is placed in the 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) establish a folder TMP in the C drive
(2) Open php.ini, 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.
CODE:
[Copy to CLIPBOARD]
PHP
$ VAR = "Hello PHP"; // Modified to $ VAR = "Hello PHP"; try what results
$ pos = "Receive.php? Name =". $ VAR;
HEADER ("Location: $ POST");
?>
Receive.php content:
CODE:
[Copy to CLIPBOARD]
PHP
Echo "
";Echo $ _Get ["name"];
ECHO " pre>";
?>
The correct way is:
CODE:
[Copy to CLIPBOARD]
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.
CODE:
[Copy to CLIPBOARD]
PHP $ STR = "This character is long, ^ _ ^";
$ Short_STR = Showsh ($ STR, 4); // Intercept 4 Chinese characters in front, the result is: this character ...
Echo "$ Short_STR";
Function Csubstr ($ STR, $ START, $ LEN)
{
$ Strlen = Strlen ($ STR);
$ clen = 0;
For ($ I = 0; $ I <$ Strlen; $ i , $ Clen )
{
IF ($ clen> = $ start $ g)
Break;
IF (Substr ($ Str, $ I, 1)> 0xA0)
{
IF ($ clen> = $ s)
$ Tmpstr. = Substr ($ STR, $ I, 2);
$ i ;
}
Else
{
IF ($ clen> = $ s)
$ Tmpstr. = SUBSTR ($ STR, $ I, 1);
}
}
Return $ TMPSTR;
}
Function Showsh ($ STR, $ LEN)
{
$ TEMPSTR = CSUBSTR ($ STR, 0, $ LEN);
IF ($ STR <> $ TEMPSTR)
$ Tempstr. = "..."; // What is the end of the end, modify it here.
Return $ TEMPSTR;
}
6: Specify your SQL statement
In the form, in front of the field, "` ", this will not occur because of misuse the keyword,
Of course I don't recommend you to use keywords.
E.g
$ 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
CODE:
[Copy to CLIPBOARD]
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
CODE:
[Copy to CLIPBOARD]
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?
CODE:
[Copy to CLIPBOARD]
PHP
$ arr = get_defined_functions ();
Function PHP () {
}
Echo "
";Echo "Here you display all functions supported by the system, and custom function PHP / N";
Print_r ($ ARR);
ECHO " pre>";
?>
10: How to compare two dates a few days
CODE:
[Copy to CLIPBOARD]
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, 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 "I have already 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) cannot be turned off.
If you want to close any error prompts, open php.ini, find display_errors, set to display_errors = off. After any errors 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
Set 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: Found in php.ini:
Automatically add files before or after any php document.
Auto_prepend_file = auto_prepend_file.php; attached to the head
Auto_APpend_file = auto_append_file.php; It is attached to the tail
After you will be equivalent to each PHP file
CODE:
[Copy to CLIPBOARD]
PHP
Include "auto_prepend_file.php";
....... / / Here is your program
INCLUDE "auto_append_file.php";
?>
13: How to use the PHP upload file
CODE:
[Copy to CLIPBOARD]
Upload File Form Title> Head>