This article collects the PHP development often encounter some doubts or some small problems, we can refer to.
The difference between some of the above functions,
the difference between 1.isset () and Empty ()
Both are used to test variables. But Isset () is a test for whether a variable is assigned, and empty () tests whether a variable that has already been assigned is empty. If a variable is not assigned the reference is allowed in PHP, but there is a notice hint. If a variable is assigned a null value, $foo = "" or $foo=0 or $foo=false, then empty ($foo) returns True, Isset ($foo) also returns true, meaning that the null value does not unregister a variable. To unregister a variable, you can use Unset ($foo) or $foo=null.
2. How to display error messages
When php.ini display_errors = on and error_reporting = E_all, all errors and hints are displayed, preferably open for error correction when debugging, if you use the previous PHP notation, most of the error information is about undefined variables. Variables are called before they are assigned, and the solution is to detect or block
3. What is the difference between single quotes and double quotes? What is the difference between Hou Yong
In single quotes, any variable ($var), special escape character (such as "T r n", etc.) will not be parsed, so the parsing speed of PHP is faster, and the escape character only supports "'" and "" to escape the single quotation mark and backslash itself; in double quotes, the variable ($var) value is placed in the string. Special escape characters are also parsed into specific individual characters, and there are special functional escapes specifically for both of these features, such as "$" and ". In this way, although the program is more convenient to write, but also the parsing of PHP is very slow, in the array, if the subscript is not an integer, but the string type, be sure to enclose the subscript in single quotation marks, the correct notation is $array[' key ', rather than $array[key], Because the incorrect wording will make the PHP parser think that key is a constant, and then first determine whether the existence of a constant, does not exist when the "key" as subscript into the expression, while starting the error event, resulting in a notice-level error. Therefore, do not use double quotes in the vast majority of cases where single quotes can be used.
What's the difference 4.print,echo,print_r? What's the time for each?
Both Echo and print can be output, except that echo is not a function, there is no return value, and print is a function with a return value, so relative to the output echo is faster, and print_r is often used to print information about variables, typically used in debugging
5. In PHP There are times when you need to open a remote file
Open remote file functions: fopen (http://XXX.com/a.php), Fsockopen (http://XXX.com/a.php), file_get_contents (http://XXX.com/a.php), etc. )
In a php5,apache2.2.x environment, you will be prompted to fail to open the file stream, and the HTTP request fails (failed to open stream:http request failed!)
In php.ini, there are two options:
Allow_url_fopen =on (indicates that a remote file can be opened via a URL),
User_agent= "PHP" (indicating which script to access the network, the default is preceded by a ";" to remove.) )
Restart the Apache service.
6. How do I get auto_increment values in advance?
mysql_connect (' localhost ', ' root ', ' root ') or Die (' cannot connect to the server ');
mysql_select_db (' Test '); Connecting to a database
$sql = "Show create TABLE Id_user"; Id_userd for Table name
$query = mysql_query ($sql);
$arr = Mysql_fetch_array ($query);
$b = Strstr ($arr [1], ' auto_increment= '); Get substring, including auto_increment=5 DEFAULT Charset=utf8)
$result = Intval (substr ($b, 15)); SUBSTR () Gets the string from the 16th position of the string and then converts the obtained string int type
Echo ' auto_increment value '. $result; Output: The ID value for the next time the data is inserted
7. Leverage access to client information
Getenv ("REMOTE_ADDR") can get the IP of the visitors
Getenv ("Http_user_agent") can get the type of the viewer's operating system, browser type
Getenv can get all the things that $_env this global variable
Print_r ($_env) can see a lot of things
Like what
echo getenv (allusersprofile); You can see the value of AllUsersProfile
echo getenv (Pathext); You can see the value of Pathext
the difference between 8.mysql_fetch_row () and mysql_fetch_array ()
Mysql_fetch_array () is an extended version of Mysql_fetch_row (). In addition to storing data as a numeric index in an array, you can store the data as an associated index, using the field name as the key name. Using Mysql_fetch_array () is not significantly slower than using mysql_fetch_row (), and it also provides significantly more values.
The optional second parameter in Mysql_fetch_array (), result_type, is a constant that can accept the following values: Mysql_assoc,mysql_num and Mysql_both. This feature is for PHP 3.0.7 new Plus. The default value for this parameter is Mysql_both.
usage and efficacy of 9.EOD
is better than single quotes and double quotes, and can contain line breaks. EOD can be changed to other characters.
10. Using Gdate () to get a long list of numbers how to convert to normal time?
Gmdate returns GMT time, except that it is identical to the date () function. If you need local time, use Date () to format the timestamp.
11. What is the difference between the ()//?
//is a pattern delimiter, which represents a regular rule inside.
() represents a sub-pattern, a//can have a lot of () composition, you can use 1 2 or $ $ to match the value of the front face mode.
What's the difference between 12.require and require_once?
Require repeated calls will load the file you drink multiple times; require_once is only loaded once, regardless of how many times you actually call it, mainly for complex file containment relationships
For example, B contains a,c contains a, but at the same time C contains B, then if you use require, it may cause two loads a
13. What is the difference between acquiring three IP addresses?
$_server[' REMOTE_ADDR '];
$_server[' Http_client_ip '];
$_server[' http_x_forwarded_for '];
$_server[' REMOTE_ADDR ']; The IP of the access side (possibly the user, possibly the proxy)
$_server[' Http_client_ip ']; Agent-side (may exist, can be forged)
$_server[' http_x_forwarded_for ']; The user is the agent in which IP is used (may exist or can be forged)
the difference between 14.$_get and $_post?
One: The Get method is to limit the size of the transfer value, not more than 2K. Post does not limit the size of the transmit value.
Two: Get is used to obtain data from the server, and post is used to pass data to the server.
Three: Get adds the data in the form as Variable=value, after the URL that the action points to, and both use "?" connections, and each variable is connected by a "&" connection; Post is the data in the form that is placed in the data body of the forms, in the same way that the variables and values are passed to the URL that the action points to.
Four: Get is unsafe, because in the transfer process, the data is placed in the requested URL, and now many of the existing servers, proxy servers or user agents will log the request URL to the journal file, and then placed somewhere, so that there may be some privacy information to be seen by third parties. In addition, users can see the submitted data directly in the browser, and some system internal messages will be displayed in front of the user. All the actions of the post are not visible to the user.
15. You can add public,private in front of functions in the class, but not in front of functions in the library.
function custom Whatever you are, public is a class Property object that is publicly used, and privat is a class Property object that is used independently;
The public or private in a class refers to this method (to be aware that the "method") is publicly exposed or belongs to the private class and you refer to the library of functions, which is a library of "functions," not a method in a class, so there is no need to have a predecessor adornment.
16. How is the page execution time calculated?
|
copy code |
$mtime = Explode (', microtime ()); $starttime = $mtime [1] + $mtime [0]; ... ... ... $mtime = Explode (' ', Microtime ()); $endtime = $mtime [1] + $mtime [0]; $usedtime = $endtime-$starttime; printf (" %0.4f S", $usedtime); |
UTF8 encoded Script session_start (), header (), Settcookie () and other functions error, prompting "headers already sent".
The usual editor of the UTF8 encoding script identifies the UTF8 encoding format with a three-byte BOM encoding at the head of the file, three bytes that are not visible to the normal file editor, and output is preceded by HTML output. The above error is indicated when you execute the above function. Workaround: Use EditPlus, etc. can clear the BOM editor, clear the BOM (set to UTF8 clear BOM) save it.
The difference between single quotes, double quotes, and anti-quotes in PHP
PHP single quotation marks ('), double quotation marks ("") can reference strings. In single quotes, the variable is not escaped, the double-quote escaped variable, and the variable escape in the anti-quote is executed as a shell command.
The following is an introduction to some common problems in development
Let's start with a slightly more subtle question until some fatal mistake. Altogether divided into three parts.
The first part, the lesser error
First, Printf (),
This function is primarily used to format the display of data. Use when you want to change the display format of a data.
For example, the value of Pi (3.1415926) is displayed with different precision.
The code is as follows |
Copy Code |
printf ("Pi is:%.2FN n ", M_PI); printf ("Pi is also:%.3FN n ", M_PI); printf ("Pi is also:%.4FN n ", M_PI); ?>
|
But many programmers use this function only to display some variable values and function return values. Because printf () formats the data at a slower speed before displaying it, the print and echo are applied only to show the data to increase speed.
Second, the semantic examination
PHP is a weakly typed language, meaning it is not defined before a variable is used, which makes programming a lot easier and more flexible, but you have to know exactly what kind of variable it should be, because the variable still actually corresponds to a type at run time (various types
can be freely converted between each other), there is no type of variable that does not exist. It is possible that PHP will not be able to check out your semantic errors, but because of variable type changes, some potential problems can occur. Another notable issue is the scope of the variable, which may also cause
Some of the potential problems occur. The
has the following basic variables in PHP:
Boolean, Resource, Integer, double, string, array, and object.
Third, the use of temporary variables
temporary variable abuse can cause the program to run less efficiently. When to use a temporary variable can be considered based on the following two points:
1, whether the variable is used at least two times.
2, whether the use of this variable will significantly improve the readability of the program.
If one is not satisfied, the use of the variable is omitted. For example:
The code is as follows |
Copy Code |
$tmp = Date ("F D, h:i a"); Print $tmp; ?> It should be changed to: Print Date ("F D, h:i a"); ?>
Another example: String reverse_characters (String str) Reverse all of the characters in a string. function Reverse_characters ($STR) { Return implode ("", Array_reverse (Preg_split ("//", $STR))); }
?> |
The readability is not strong, can change to:
The code is as follows |
Copy Code |
String reverse_characters (String str) Reverse all of the characters in a string. function Reverse_characters ($STR) { $characters = Preg_split ("//", $STR); $characters = Array_reverse ($characters);
Return implode ("", $characters); }
?>
|
Some problems the network finds
FAQs in PHP 2009-09-10 11:07 cannot pass a variable between pages "1"
Get,post,session Automatic global variables are turned off in the latest PHP version, so the variables to be submitted from the previous page are to use $_get[' foo '],$_post[' foo '],$_session[' foo ' to get
Of course, you can also modify the automatic global variable to ON (php.ini to register_globals = on), or it is better to force yourself to be familiar with the new wording, considering compatibility.
"2" Win32 apache2 error when passing Chinese parameter with get method
test.php?a= Hello &b= You
Passing parameters can cause an internal error
Workaround: "Test.php?a=". UrlEncode (Hello). " &b= ". UrlEncode (you're good too)
The session under "3" Win32 does not work properly
php.ini default Session.save_path =/tmp
This is obviously the configuration under Linux, Win32 PHP can not read and write session files resulting in the session can not be used
Change it to an absolute path, for example Session.save_path = C:windowstemp
"4" Displays error message
When php.ini display_errors = on and error_reporting = E_all, all errors and hints are displayed, preferably open for error correction when debugging, if you use the previous PHP notation, most of the error information is about undefined variables. Variables are called before they are assigned, and the solution is to detect or block
For example, $foo can be displayed, if (Isset ($foo)) echo $foo or echo @ $foo
"5" Win32 Mail () cannot send email
The SendMail configured under Linux can be sent, and the SMTP server needs to be called to send e-mail under Win32
Modify php.ini SMTP = IP//ip is an SMTP server without authentication (hard to find on the web)
PHP the best way to send mail is to send the socket directly to the other email server instead of forwarding the server
There is a good class, but need to modify the letter will be rapid, the revised version will be launched in the recent
"6" header already sent this error usually occurs when you use the header, he may be several reasons:
1, you're pring or echo before you use the header.
2. You have a blank line in front of your current file
3. You may include a file with a blank line at the end of the file or an output that will also cause this error.
There is also the use of Session_register ()
"7" first installed MySQL if you do not have a password set, you should use
Update Mysql.user Set Password=password ("YourPassword") where user= "root"
"8" does not change after changing php.ini
Restart the Web server, such as Iis,apache and so on, before applying the latest settings
"9" PHP installed on 2003 (ISAPI installation method)
PHP4 's php4isapi.dll seems to be in conflict with 2003 and can only be installed in CGI mode.
Step one, go first in a setup program, I am Loaded is: Php-4.2.3-installer.exe, you can also go to the latest version, before installing Php-4.2.3-installer.exe ensure that your IIS6.0 boot up and be able to access. After installation, in the default Web site--application configuration
Step Two: Click Web Service Extensions--Create a new Web service extension.
Step three: Extension-->php, and then add
Step four: Find the path to Php.exe to add it up.
Step five: OK, yes!
Step six: Select PHP's service extensions, then click Allow.
"10" Sometimes the SQL statement does not work and the database operation fails
The easiest way to debug, echo the SQL, to see if the value of the variable can not be
"11"
|
copy code |
"!--? php //Available from PHP 3 Prin T $HTTP _post_vars[' username '); //From PHP 4.1.0 available Print $_post[' username ']; Print $_request[' username ']; Import_request_variables (' P ', ' p_ '); Print $p _username; //if PHP directive Register_globals = On is available. However, since PHP 4.2.0 The default value is Register_globals = off the use of this method is not advocated. Print $username; ? |
Using get forms is similar, except to use the appropriate get pre-defined variables. GET also applies to query_string (URL in the "?" After the information). So, for example, http://www.example.com/test.php?id=3 contains get data that can be accessed using $_get[' ID '. See $_request and Import_request_variables ().
The default value of Register_globals before PHP 4.2.0 is on. In PHP 3, its value is always on. You are encouraged not to rely on this directive, and it is recommended that it be off when coding.
"12" To temporarily save your variable to a temporary file:
The code is as follows |
Copy Code |
< P>if (file_exists (' temp.php ')) { $x =r (' temp.php '); @eval ("$a = $x;"); } if (!is_array ($a)) { #重新构造数组A $a =array ("AF" and "FSDF"), "f" = "df"); } W (' temp.php ', Var_export ($a, true)); Var_dump ($a); Read, write document data function R ($file _name) { $filenum = @fopen ($file _name, "R"); @flock ($filenum, lock_sh); $file _data= @fread ($filenum, FileSize ($file _name)); @fclose ($filenum); return $file _data; } Function W ($file _name, $data, $method = "W") { $filenum = @fopen ($file _name, $method); Flock ($filenum, LOCK_EX); $file _data=fwrite ($filenum, $data); Fclose ($filenum); return $file _data; } |
The difference between the "13" include and the Require
There is not much difference between the two, if the file to be included does not exist, include prompt notice, and then continue to execute the following statement, require prompt fatal error and exit; As far as I test, Win32 platform they are all first included after execution, Therefore, it is best not to have include or require statements in the included files, which can cause directory confusion. Perhaps Linux under different circumstances, not yet tested; If a file does not want to be included multiple times you can use Include_once or require_once
The application of the "14" session in functions and methods: the variables you intend to register into the session must all be global.
The reason is this:
PHP's Session_register function simply remembers the name of the variable and does not remember the value of the variable.
The value of this variable is really to be remembered on the server after the entire script is run, that is, the value of the variable is read and saved to the server-side temp directory at the end of the script run, so that all functions or methods outside the or a variable defined as a global variable within a function or method will succeed in the register, while others will be unset at the end of the script run.
The session "15" in PHP is implemented based on cookies, so it does not disappear immediately after all session windows are closed. This is different from other scripting languages and makes me feel depressed.
A: No, no, the friend upstairs did not study the session of PHP, if it is necessary to say that the session of PHP is related to the cookie, that is, a session_id is recorded in the client
In the temporary directory on the server side, a file with the same name as session_id is generated, which is the variable and their value that really records your success register. Similarly, if the client disables the use of cookie,php, the session_id value is automatically passed as get, so that it is not lost. Therefore, the relationship between the cookie and the session is not so inseparable. Also, the session of PHP cannot be said to be based on cookies.
The difference between "16" isset () and Empty ()
If a variable is not assigned to the reference in PHP is allowed, but there will be a notice hint;
Both are test variables, but Isset () is the test variable is assigned, and empty () is the test of an already assigned variable is empty
If a variable is assigned a null value, $foo = "" or $foo=0 or $foo =false, then empty ($foo) returns True, Isset ($foo) also returns true, meaning that the null value does not unregister a variable. To unregister a variable, you can use Unset ($foo) or $foo=null
"17" Method of uploading multiple files one time via HTTP protocol
There are two ideas that are two implementations of the same method.
1, set multiple file input boxes in the form, name their names with an array, as follows:
The code is as follows |
Copy Code |
In this way, the following tests are done on the server side echo "
echo " ";2, set multiple file input boxes in the form, but with different names, as follows:
Do the same test on the server side: echo "
echo " "; |
"18" can create a temporary PHP format of the file, where needed to include in, so that the temporary PHP files defined in the variable will have, can replace the session.
"19" As mentioned above, now the Get,post way to submit can not be used directly, and sometimes very troublesome, they wrote a section of code, to convert them into global variables, in order to use, the session variable similarly available.
Convert a variable submitted by a Get or post to a global variable:
The code is as follows |
Copy Code |
foreach ($_get as $key = = $value) { $ $key = $value; } foreach ($_post as $key = = $value) { $ $key = $value; }
|
http://www.bkjia.com/PHPjc/632206.html www.bkjia.com true http://www.bkjia.com/PHPjc/632206.html techarticle This article collects the PHP development often encounter some doubts or some small problems, we can refer to. The difference between some of the above functions, the difference between 1.isset () and empty () are both ...