PHP interview questions (2)

Source: Internet
Author: User
Tags flock php web server
PHP interview question (2) 1. please compare the main functions of POSIX and Perl-compatible regular expressions
Ereg preg_match
Ereg_replace preg_replace


2. explain the impact of safe_mode on php system functions in PHP. ini.
Safe_mode is a basic and secure shared environment. On a PHP web server shared by multiple users, when the server enables the safe_mode mode, the following functions will be affected. First, the function that tries to access the file system will be limited to the user ID of the running server. to try to operate a file, you must have the permission to read or write the file.
Therefore, when safe_mode is enabled, the following functions will receive restrictions:
Ckdir, move_uploaded_file, chgrp, parse_ini_file, chown, rmdir, copy, rename, fopen, require, highlight_file, show_source, include, symlink, link, touch, mkdir, unlink
The above are all functions related to the operating file system. In addition, some php extended functions are also limited. extensions cannot be directly loaded in the program, but can only be loaded in php. ini, and if php needs to execute the operating system program, it must specify the program path in safe_mode_exec_dir; otherwise, the execution will fail. In addition, exec, shell_exec, pasathru, system, popen, and other functions will receive restrictions.


3. Which of the following are magic methods and functions in PHP5?
_ Sleep
_ Wakeup
_ ToString
_ Set_state
_ Construct,
_ Destruct
_ Call,
_ Get,
_ Set,
_ Isset,
_ Unset
_ Sleep,
_ Wakeup,
_ ToString,
_ Set_state,
_ Clone
_ Autoload


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?
$ PHP_HOME/bin/php-r 'echo $ argv [1]; 'Hello
$ PHP_HOME/bin/php hello. php hello

Window. if the php installation directory is c: \ program files \ php5 \, run the command window to enter the path and enter php hello. php press enter to execute the hello. php file,
If you want to point to the php file in another path, you can go to the php path/hello. php, which is called the CLI mode. we usually see the CGI mode through the browser. As for passing parameters, the php file in cli mode is directly followed by parameters after the file name, multiple parameters are separated by spaces. in the PHP file, two variables are used to obtain the parameters. one is $ argv, the other is $ argc, and the other is an array that passes parameters, by default, the first option is the name of the PHP file, and the latter is the number of arrays of $ argv.
In linux, general programs will be installed under/usr/bin/php. you can use man php to check them. if there is any information, you can use them. the usage is similar to that in window. If the preceding step is true, you can run the php file directly in the php file. if man php has no information, the current php execution file is not in the environment path, you can modify the environment path to include the php path, or enter the php path similar to window, and execute the php file. Others are similar to Windows.


5. what is PHP's garbage collection mechanism?
In PHP, when no variable points to this object, this object becomes garbage. PHP will destroy it in the memory. this is the GC mechanism of PHP to prevent memory overflow.
When a PHP thread ends, all memory space currently occupied will be destroyed, and all objects in the current program will be destroyed at the same time. GC processes generally start to run with each SESSION. gc aims to automatically destroy and delete these files after the session file expires.
Executing these functions can also be used for recovery.
_ Destruct/unset/mysql_close


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)
PHP5 supports interfaces and has built-in Iterator interfaces. Therefore, if you define a class and implement the Iterator interface, your class object is ZEND_ITER_OBJECT. otherwise, ZEND_ITER_PLAIN_OBJECT.
For the class ZEND_ITER_PLAIN_OBJECT, foreach obtains the default attribute array of the object through HASH_OF, and then performs foreach on the array.
The class object of ZEND_ITER_OBJECT performs foreach by calling the Iterator interface related functions implemented by the object. Therefore, you can give the following answers to this question:
[Php]
Class sample implements Iterator
{
Private $ _ items = array (1, 2, 3, 4, 5, 6, 7 );


Public function _ construct (){
; // Void
}
Public function rewind () {reset ($ this-> _ items );}
Public function current () {return current ($ this-> _ items );}
Public function key () {return key ($ this-> _ items );}
Public function next () {return next ($ this-> _ items );}
Public function valid () {return ($ this-> current ()! = False );}
}
$ Sa = new sample ();
Foreach ($ sa as $ key => $ val ){
Print $ key. "=>". $ val;
} [/Php]
The above code runs normally in my php 5.3.

7. write a piece of PHP code to ensure that multiple processes are successfully written to the same file at the same time.
Function writeData ($ path, $ mode, $ data ){
$ Fp = fopen ($ path, $ mode );
$ Retries = 0;
$ Max_retries = 100;
Do {
If ($ retries> 0 ){
Usleep (rand (1, 10000 ));
}
$ Retries + = 1;
} While (! Flock ($ fp, LOCK_EX) and $ retries <= $ max_retries );

If ($ retries = $ max_retries ){
Return false;
}

Fwrite ($ fp, "$ data \ n ");
Flock ($ fp, LOCK_UN );
Fclose ($ fp );
Return true;
}


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.


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.
3. xdebug analyzes the number of function executions and the specific time.
4. the online system uses strace to track specific system calls of related processes.


Php lamp Engineer Test Paper
Question 1
What does Print out?
A) 3
B) False
C) Null
D) 1
E) 0


Question 2
Which of the following snippets prints a representation of 42 with two decimal places?
A) printf ("%. 2d \ n", 42 );
B) printf ("% 1.2f \ n", 42 );
C) printf ("% 1.2u \ n", 42 );


Question 3
Given
$ Text = 'content-Type: text/XML ';
Which of the following prints 'text/XML '?
A) print substr ($ text, strchr ($ text ,':'));
B) print substr ($ text, strchr ($ text, ':') + 1 );
C) print substr ($ text, strpos ($ text, ':') + 1 );
D) print substr ($ text, strpos ($ text, ':') + 2 );
E) print substr ($ text, 0, strchr ($ text ,':')
Question 4
What is the value of $?
$ A = in_array ('01', array ('1') = var_dump ('01' = 1 );
?>
A) True
B) False
Question 5
What is the value of $ result in the following PHP code?
Function timesTwo ($ int ){
$ Int = $ int * 2;
}
$ Int = 2;
$ Result = timesTwo ($ int );
?>;
Answer: NULL
Question 6
The code below ___________ because ____________.
Class Foo {
?>
Function bar (){
Print "bar ";
}
}
?>
A) will work, class definitions can be split up into multiple PHP blocks.
B) will not work, class definitions must be in a single PHP block.
C) will not work, class definitions must be in a single file but can be in multiple PHP blocks.
D) will work, class definitions can be split up into multiple files and multiple PHP blocks.
Question 7
When turned on, ____________ will _________ your script with different variables from HTML forms and cookies.
A) show_errors, enable
B) show_errors, show
C) register_globals, enhance
D) register_globals, inject
Question 8
What will be the output of the following PHP code:
Echo count (strlen ("http://php.net "));
?>
Answer: 1
Question 9
What is the best all-purpose way of comparing two strings?
A) Using the strpos function
B) Using the = operator
C) Using strcasecmp ()
D) Using strcmp ()
Question 10
What is the difference between "print ()" and "echo ()"?
Answer: print is a function, echo is a language construct

Related Article

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.