Php interview questions displayed on the technical exchange forum

Source: Internet
Author: User
1. compare the main functions of POSIX and Perl-compatible regular expressions.

1. compare the main functions of POSIX and Perl-compatible regular expressions.
Ereg preg_match/ereg_replace preg_replace

Ereg preg_match is slower than ereg_replace preg_replace
Ereg_replace preg_replace supports POSIX-style unsupported syntaxes, such as non-greedy matching, assertions,
Condition submode.

POSIX and Perl Compatible Regular expressions have their own characteristics, but they are not completely consistent. BRE/ERE

2. explain the impact of safe_mode on php system functions in PHP. ini.

After enabling the PHP Security mode:
Some functions are blocked, such as dl () and shell_exec.
Some functions check whether the operated file or directory has the same UID as the script being executed. when safe_mode_gid is set, the GID check is relaxed.
The directory set by safe_mode_include_dir and its sub-directories (the directory must be included in include_path or the full path) will pass the UID/GID check when the file is included.
The specified limit is actually a prefix rather than a directory name. This means that "safe_mode_include_dir =/dir/incl" will allow access to "/dir/include" and "/dir/incls" if they exist. If you want to control access to a specified directory, add a slash at the end, for example, "safe_mode_include_dir =/dir/incl /".
Some functions can only perform operations such as exec (), system (), passthru (), popen () in the directory set by safe_mode_exec_dir ()
Some functions do not work, such as set_time_limit () and max_execution_time.

3. Which of the following are magic methods and functions in PHP5?

_ Sleep will call this function before serialize () serializes an object
_ Wakeup: This function is called before unserialize () deserialization.
_ ToString this function is called when an object is converted to a string.
_ Construct constructor called When instantiating an object
_ Destruct destructor called when the object strength is released
_ Call: This function is called when no methods in the class are called.
_ Get, called when accessing an attribute not in a class
_ Set, called when setting the value of an attribute not included in a class
_ Isset, called when an external isset function is called to check whether a class has an attribute
_ Unset, called when deleting an attribute of a class
_ Set_state, called when the attribute and value of the object are exported using var_export
_ Clone is called when copying an instance of a class
_ Autoload: when a class is used and this class is not loaded, it will be called by the user.

4. please write for transfer and explain how to run the PHP script (write two methods) under the command line and pass parameters to the PHP script at the same time?

Parameters for php-f script path execution
Add # in the first line of the PHP script #! /Usr/bin/php and set the script permission to executable.
./Script name-parameter

5. what is PHP's garbage collection mechanism?

It seems that reference count is used. when a variable is initialized or assigned to another variable, the reference count is increased by 1, when a variable or assigned variable is destroyed or no reference count is detected, when the garbage collection process is running, the variables whose reference count is 0 will be released from the memory (the program will be interrupted during garbage collection ). All resources are released when the script execution is complete.

6. enable the object to perform a foreach loop like an array, requiring the attribute to be private.
(PHP5 implementation in Iterator mode, write a class of implementation Iterator interface)

7. write a piece of PHP code to ensure that multiple processes are successfully written to the same file at the same time.

8. use PHP to implement a two-way queue

9. use a regular expression to extract the specified attribute values of a tag in an html or xml code segment. (you must consider the case where the attribute values are not case sensitive, there is a space between the attribute name value and the equal sign ). Assume that the attr attribute value of the test tag needs to be extracted. create a string containing the tag.

10. use the socket-related function (non-curl) to implement the following function: construct a post request that is sent to the specified request path (such as http://www.example.com: 8080/test) of the specified port of the specified http server ). The request contains the following variables:

Username: Gentle
Password (pwd): & 123 = 321 & 321 = 123 &
Personal Profile (intro): Hello world!

The http server requires the following cookies for simple user action tracking:

Cur_query: you & me
Last_tm :... (The unix timestamp of the last request, which is set to 10 minutes before the current request time)
Cur_tm :... (Unix timestamp of the current request)

Set the timeout value to 10 seconds. after a request is sent, the response content of the http server is output.
Copy the content to the clipboard code: Function encode ($ data, $ sep = '&'){
While (list ($ k, $ v) = each ($ data )){
$ Encoded. = ($ encoded? "$ Sep": "");
$ Encoded. = rawurlencode ($ k). "=". rawurlencode ($ v );
}
Return $ encoded;
}

Function post ($ url, $ post, $ cookie ){
$ Url = parse_url ($ url );
$ Post = encode ($ data ,'&');
$ Cookie = encode ($ cookieArray ,';');
$ Fp = fsockopen ($ url ['host'], $ url ['port']? $ Url ['port']: 80, $ errno, $ errstr, 10 );
If (! $ Fp) return "Failed to open socket to $ url [host]";

Fputs ($ fp, sprintf ("POST % s HTTP/1.0 \ n", $ url ['path'], $ url ['query']? "?" : "", $ Url ['query']);
Fputs ($ fp, "Host: $ url [host] \ n ");
Fputs ($ fp, "Content-type: application/x-www-form-urlencoded \ n ");
Fputs ($ fp, "Content-length:". strlen ($ encoded). "\ n ");
Fputs ($ fp, "Cookie: $ cookie \ n ");
Fputs ($ fp, "Connection: close \ n ");
Fputs ($ fp, "$ post \ n ");

While (! Feof ($ fp )){
Echo fgets ($ fp, 128 );
}
Fclose ($ fp );
}

$ Url = 'http: // www.example.com: 8080/test ';
$ Encoded = username = gentle knife & pwd =
$ Post = array (
'Username' => 'gentle knife ',
'Pwd => '& 123 = 321 & 321 = 123 &',
'Intro => 'Hello world! '
);
$ Cookie = array (
'Cur _ query' => 'you & me,
'Last _ tm '=> time ()-600,
& Apos; cur _ tm & apos; = & apos; time ()
);

Post ($ url, $ post, $ cookie );
11. how do you check the execution efficiency of PHP scripts (usually the script execution time) and the efficiency of Database SQL statements (usually the database Query time ), what are the bottlenecks of script execution and database query?
1. script execution time. enable xdebug and use WinCacheGrind for analysis.
2. database query. mysql uses EXPLAIN to analyze and query, and enables slow query of slow query log records.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.