PHP programmer to share the written test and interview questions

Source: Internet
Author: User
Tags apd arrays hash install php php and php code php debugger strlen

Ordinary PHP Programmer pen Test


Tags: programmer PHP interview 2009-02-06 15:19


1. Use PHP to print out the day before, the print format is May 10, 2007 22:21:21


2. PHP code is as follows:


$a = "Hello";


$b =& $a;


Unset ($b);


$b = "World";


echo $a;


What is the result?


3. PHP code is as follows:


$STR = "CD";


$ $str = "Landog";


$ $str. = "OK";


Echo $CD;


What is the result?


4. Write a code with PHP, implementation does not use the 3rd variable, Exchange $a, $b value, $a, $b the initial value of their own set.


5. According to the topic request, writes the code in the PHP.


Table name User


ID Name Tel Content Date


1 313,333,663,366 College graduate 2006-10-11


3 313,612,312,331 Bachelor Degree 2006-10-15


5 020-5566556 Secondary School graduates 2006-10-15


4 Wang 513,521,212,125 College graduate 2006-12-25


2. ......


6. ......


Suppose the database connection is as follows:


$mysql _db=mysql_connect ("local", "Root", "pass");


@mysql_select_db ("db", $mysql _db);


(1) All records with name equal to "John" are queried and output.


(2) Search by ID Ascending, return only the first 3 records after sorting, and output.


6. Can javascript define two-dimensional arrays, and if not, how can you solve them?


7. Assuming that a.html and b.html are under the same folder, use JavaScript to automatically jump to b.html after opening a.html five seconds.


8. There are two documents A.html and a.php, the code is as follows:


A.html

<html>


<head>


<meta Http-equiv=content-type content=text/html;charset=utf-8>


<title>php Programmer Interview </title>


</head>


<body>


<center>


<form method= "POST" action= "a.php" >


<table border= "1" >


<tr>


<TD align= "Right" > name:</td>


<TD align= "left" ><input type= "text" ></td>


</tr>


<tr>


<TD align= "Right" > Telephone:</td>


<TD align= "left" ><input type= "text" ></td>


</tr>


<tr>


<TD align= "Right" > Mailbox:</td>


<TD align= "left" ><input type= "text" ></td>


</tr>


<tr>


<TD align= "Right" > Address:</td>


<TD align= "left" ><input type= "text" ></td>


</tr>


<tr>


<TD align= "center" colspan= "2" >


<input type= "Submit" value= "submitted" >


<input type= "reset" value= "refill" >


</td>


</tr>


</table>


</form>


</center>


</body>


</html>


A. php

<?php
$user _name = $_get[' user_name '];
$user _tel = $_get[' User_tel '];
$user _email = $_get[' User_email '];
$user _add = $_get[' User_add '];
echo Username: $user _name<br> Tel: $user _tel<br> mailbox: $user _email<br> Address: $user _add<br> ";
?>


(1) Please draw the a.html in the browser display effect chart.
(2) Input in a.html: Username = John, telephone = 020-38259977, mailbox =sunrising@srtek.cn, address = Guangzhou l, press submit button, output result is?
9. Have you ever used a version control tool, if so, please give a brief description.
10. Use a CSS style sheet to define the visited hyperlink font size is 14pt and the color is red.
11. Move any number or symbol so that the equation is set up, 102 = 101-1. Note: Move is not an exchange, the equal sign cannot be separated.
12. Regular questions, 3, 1, 4, 1, 5, 9, 2, (), please write the next expression in parentheses according to the rule.
13. Rule questions, 5, 8,-3, 5, 2, 3, 1, (), please write the next expression in parentheses according to the rule.

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Answer:

1. Echo Date (' y-m-d h:i:s ', Strtotime ('-1 day '));

2. Hello

3. Landogok

4. $a = "ABCD";
$b = "1234";
echo "Initialization a= $a, b= $b <br>";
$a = $a. $b;
$b = strlen ($b);
$b = substr ($a, 0, (strlen ($a)-$b));
$a = substr ($a, strlen ($b));
echo "After Exchange a= $a, b= $b <br>";

5. (1) $sql = "SELECT * from User where > $result = mysql_query ($sql);
while ($row = Mysql_fetch_array ($result, Mysql_assoc)) {
echo $row [' Name '];
}
(2) $sql = "SELECT * from User ORDER by ID ASC limit 0,3";
$result = mysql_query ($sql);
while ($row = Mysql_fetch_array ($result, Mysql_assoc)) {
echo $row [' Name '];
}
6. JavaScript does not support two-dimensional array definitions, you can use arr[0] = new Array () to resolve

7. The JavaScript code is as follows:
<script>
function go2b () {
window.location = "b.html";
Window.close ();
}
SetTimeout ("go2b ()", 5000); Automatic execution of GO2B () after 5 seconds
</script>

8.

(1) as shown below:

Slightly. Because too lazy to insert pictures, if you want to see the results can save the code as an HTML file, and then open the view in the browser.

(2) The output result should be:

Name:

Phone:

Mailbox:

Address:

Because a form is submitted by post, it is read in a.php with a Get method, so no value is read.

9. Slightly

a:visited {font-size:14pt; color:red;}

11.102 = 101-1

12. The answer is 6, because ∏=3.1415926

13. The answer is 2, the rule is n= (n-2) –| (n-1) | , n>=3

1. In PHP, the name of the current script (excluding the path and query string) is recorded in a predefined variable (1), while the previous page URL linked to the current page is recorded in a predefined variable (2) <?php
Address of this page, Script_name can also: php/test.php
echo $_server[' php_self ']. " <br/> ";
Link to the URL address of the previous page of the current page:
echo $_server[' Http_referer ']. " <br/> ";
See other reference Manual: Language reference variables predefined variables
Absolute pathname of the script before execution: d:inetpubwwwrootphp est.php
echo $_server["Script_filename"]. " <br/> ";
Browsing the current page user's IP address: 127.0.0.1
echo $_server["REMOTE_ADDR"]. " <br/> ";
string of Queries (query) (the first question mark in the URL?) afterwards): id=1&bi=2
echo $_server["Query_string"]. " <br/> ";
The document root directory where the current script is running: d:inetpubwwwroot
echo $_server["Document_root"]. " <br/> ";
?>

2. The execution segment &lt;?php echo 8% ( -2)?&gt; will output __.


&lt;?php


Reference manual "Language Reference" operator "arithmetic operator"% for modulo operation, output 0


Echo 8% (-2). " &lt;br/&gt; ";


Modulo $a% $b The result is negative when the $a is negative. Output-2


Echo ((-8)%3). " &lt;br/&gt; ";


Output 2


Echo (8% (-3)). " &lt;br/&gt; ";


?&gt;


3. In HTTP 1.0, the meaning of status code 401 is ____, and if you return a prompt that cannot find a file, you can use the header function with the statement ___.


A: 401 means not authorized; header ("http/1.0 404 Not Found"); [see reference manual Function Reference HTTP function header]


4. The function of the array function arsort is ____, the function of error_reporting (2047) is ____.


A: Arsort: To reverse-sort the array and keep the index relationship error_reporting (2047) is: the Errors and warnings


5. Write a regular expression, worry about all the Js/vbs script on the page (that is, the script tag and its contents are removed):


&lt;?php


$script = "The following content does not appear: &lt;script language= ' JavaScript ' &gt;alert (' cc ');&lt;/script&gt;";


Echo preg_replace ("/&lt;script[^&gt;].*?&gt;.*?&lt;/script&gt;/si", "Replace content", $script);


?&gt;


6. To install PHP in the way of the Apache module, you must first use the statement ____ to dynamically load the PHP module in the file http.conf.


Then use the statement ___ to make Apache use PHP as a script for all files with extensions PHP.


Answer: LoadModule php5_module "C:/php/php5apache2.dll"; AddType application/x-httpd-php. php


See the catalogue of reference manuals II. Installation and Configuration "6. Installation under Windows system Apache 2.0.x under Microsoft windows


7. Statement include and require can include another file in the current file, which is the difference of ____; to avoid including the same file multiple times, you can replace them with the statement ____.


A: When a failure is handled, include () produces a warning and require () causes a fatal error; Require_once ()/include_once ()


8. An argument to a function cannot be a reference to a variable unless it is set to on in PHP.ini.


A: Allow_call_time_pass_reference boolean: Is it enabled to force parameters to be passed by reference when called on a function call, see Appendix G of the Reference manual


The meaning of the left join in 9.SQL is __ if Tbl_user records the student's name and number (ID),


Tbl_score recorded students (some students were expelled after the test, no records) of the study number (ID) and test results (score) as well as examination subjects (subject), to print out the names of each student and corresponding to each section of the total score, you can use the SQL statement ____.


Answer: Natural left outer connection


Create Database Phpinterview;


Use Phpinterview


CREATE TABLE Tbl_user


(


ID int NOT NULL,


Name varchar NOT NULL,


Primary KEY (ID)


);


CREATE TABLE Tbl_score


(


ID int NOT NULL,


Score Dec (6,2) NOT NULL,


Subject varchar () NOT NULL


);


Insert into Tbl_user (ID, name) VALUES (1, ' Beimu ');


Insert into Tbl_user (ID, name) VALUES (2, ' Aihui ');


Insert into Tbl_score (ID, score, subject) VALUES (1, 90, ' language ');


Insert into Tbl_score (ID, score, subject) VALUES (1, 80, ' math ');


Insert into Tbl_score (ID, score, subject) VALUES (2, 86, ' math ');


Insert into Tbl_score (ID, score, subject) VALUES (2, 96, ' language ');


Select A.id,sum (b.score) as Sumscore


From Tbl_user A left join Tbl_score B


On A.id=b.id


GROUP BY a.ID


10. In PHP, Heredoc is a special string whose closing sign must be ____


A: The line containing the end identifier cannot contain any other characters except ";"


11. Write a function that can traverse all files and subfolders under a folder.


&lt;?php


function My_scandir ($dir)


{


$files =array ();


if (Is_dir ($dir))


{


if ($handle =opendir ($dir))


{


while (($file =readdir ($handle))!==false)


{


if ($file!= "." &amp;&amp; $file!= "...")


{


if (Is_dir ($dir.) /". $file))


{


$files [$file]=my_scandir ($dir.) /". $file);


}


Else


{


$files []= $dir.] /". $file;


}


}


}


Closedir ($handle);


return $files;


}


}


}


Print_r (My_scandir ("D:program filesinternet Explorermui"));


?&gt;


1. Which of the following sentences will not add John to the users array?
$users [] = ' John ';
John was successfully added to the array users.
Array_add ($users, ' John ');
The function Array_add () is not defined.
Array_push ($users, ' John ');
John was successfully added to the array users.
$users | | = ' John ';
Syntax error.
What is the difference between 2.sort (), Assort (), and Ksort ()? Under what circumstances are they used separately?
Sort ()
The index keys are renumbered from 0 to n-1 according to the values of the elements in the array, sorted in alphabetical order. It is mainly used to sort the array when the value of the array key is unrelated to the itch.
Assort ()
PHP does not have a assort () function, so it may be the clerical error of Asort ().
Asort ()
Like sort (), the elements of the array are arranged in alphabetical order, and all the key keys are preserved, especially for associative arrays.
Ksort ()
Sorted alphabetically in alphabetical order according to the value of the index key in the array, especially for associative arrays that want to sort the index keys.
3. What does the following code produce? Why?
$num = 10;
function Multiply () {
$num = $num *10;
}
Multiply ();
Echo $num;
Because the function multiply () does not specify that the $num is a global variable (such as global $num or $_globals[' num '), the value of the $num is 10.
4. What is the difference between reference and a regular variable? How to pass by reference? Under what circumstances do we need to do this?
Reference transmits the address of a variable rather than its value, so when you change the value of a variable in a function, the entire application sees the new value of the variable.
A normal variable transmits to the function its value, when the function changes the value of the variable, only this function to see the new value, the other parts of the application still see the old value.

$myVariable = "its ' value";


MyFunction (&amp; $myVariable); Using the reference transfer parameter to transfer the reference parameter to the function, you can make the variable that the function changes, even if the new value remains after the function ends.


5. Can some functions be used to insert a function library in a script that is currently executing?


There are different answers to the different interpretations of the subject, my first thought was to insert PHP libraries with the include (), include_once (), require (), require_once (), but, to be careful, the function library should also include COM objects and. NET function library, so our answer should also include Com_load and Dotnet_load, and the next time someone mentions a "function library," Don't forget the two functions.


What is the difference between 6.foo () and @foo ()?


Foo () performs this function, and any interpretation errors, grammatical errors, and execution errors will be displayed on the page.


@foo () Hides all of the above error messages when executing this function.


Many applications use @mysql_connect () and @mysql_query to hide the MySQL error message, which I think is a serious mistake, because the error should not be hidden, you have to deal with them properly and possibly solve them.


7. How do you debug a PHP application?


I don't usually do that, I've tried a lot of different debugging tools, and it's not easy to set these tools in a Linux system. But I'll introduce you to a recent High-profile debugging tool.


php-advanced PHP Debugger or PHP-APD, the first step is to perform the following instruction installation:


Pear Install APD installs the following statement at the beginning of your script to begin debugging:


Apd_set_pprof_trace (); After completion, open the following file to check the execution log:


Apd.dumpdir


You can also use PPROFP to format the log.


For more information, refer to http://us.php.net/manual/en/ref.apd.php.


8. What is "= ="? Try to cite a "= =" is true but "= = =" is a false example.


"= = =" is a function that either returns Boolean "false" or returns an Strpos () and Strrpos (), which is not a Boolean value but can be assigned a "false" value.


The second part of the question is a bit difficult, think of a "= =" is a false, but "= = =" is a real example is very easy, the opposite example is very few. But I finally found the following example:


if (Strpos ("abc", "a") = = True) {//This part will never be executed because the position of "a" is 0, converted to Boolean "false"}if (Strpos ("abc", "a") = = True) {//This part will be executed because The return value for the "= = =" Guarantee function Strpos () is not converted to a Boolean value.}


9. How would you define a category MyClass that has no member functions or attributes?


Class myclass{}


10. How do you produce a MyClass object?


$obj = new MyClass ();


11. How do you access the attributes of this category within a category and change its value?


Use statements: $this-&gt;propertyname, for example:


Class myclass{private $propertyName; public function __construct () {$this-&gt;propertyname = ' value ';}}


What's the difference between 12.include and include_once? And what about require?


All three are used to insert other files into the script, depending on whether Url_allow_fopen is approved or not, this file can be obtained from inside or outside the system. But there is also a fine distinction between them:


Include (): This function allows you to insert the same file multiple times in a script, and if the file does not exist, it emits a system warning and continues to execute the script.


Include_once (): It is similar to include (), as its name shows, the file will only be inserted once during the execution of the script.


Require (): similar to include (), it is used to insert other files in the script, but if the file does not exist, it will issue a system warning, this warning will cause a fatal error to abort the execution of the script


13. Which of the following functions can turn the browser to another page?


Redir ()


This is not a PHP function and can cause an execution error.


Header ()


This is the correct answer, header () is used to insert the volume header data that can be used to move the browser to another page, for example:


Header ("location:http://www.search-this.com/");


Location ()


This is not a PHP function and can cause an execution error.


Redirect ()


This is not a PHP function and can cause an execution error.

14. Which of the following functions can be used to open a file for reading/writing?


Fget ()


This is not a PHP function and can cause an execution error.


File_open ()


This is not a PHP function and can cause an execution error.


fopen ()


This is the correct answer, fopen () can be used to open the file to read/write, in fact there are many options for this function, please refer to php.net for more information.


Open_file ()


This is not a PHP function and can cause an execution error.


What is the difference between 15.mysql_fetch_row () and mysql_fetch_array ()?


Mysql_fetch_row () stores a column of the database in a zero-base array, the first column in the array index 0, the second column in index 1, and so on. MYSQL_FETCH_ASSOC () store a column of the database in an associative array, the array index is the field name, such as my database query sent back to the "first_name", "last_name", "email" three fields, the array index is "first _name "," last_name "and" email ". Mysql_fetch_array () can return the value of Mysql_fetch_row () and MYSQL_FETCH_ASSOC () at the same time.


16. What is the following code to do? Please explain.


$date = ' 08/26/2003 ';p rint ereg_replace ([0-9]+)/([0-9]+)/([0-9]+], "[Url=file://2///1///3%22, $date]\2/\1/\3", $ Date[/url]);


This is to convert a date from MM/DD/YYYY format to dd/mm/yyyy format. A good friend of mine told me that the normal expression could be broken down into the following statement, for such a simple representation is in fact no need to disassemble, purely for the convenience of explanation:


Corresponds to one or more 0-9, followed by an oblique number $regexpression = "([0-9]+)/";//should be one or more 0-9, followed by another oblique number $regexpression. = "([0-9]+)/";/again corresponds to one or more 0-9$regexpression. = "([0-9]+)"; for [Url=file://2///1///3]\2/\1/\3[/url] it is used to correspond to parentheses, the first bracket is the month, and the second bracket corresponds to the date. The third bracket corresponds to the year.


17. Give you a line of text $string, how would you write a regular expression to remove the HTML tags in the $string?


First of all, PHP has built-in function strip_tags () to remove HTML tags, why should you write the regular expression? Well, as an interview question, I would say:


$stringOfText = "&lt;p&gt;this is a test&lt;/p&gt;"; $expression = "/&lt; (. *?) &gt; (. *?) &lt;/(. *?) &gt;/"; Echo preg_replace ($expression," [Url=file://2/]\2[/url] ", $stringOfText);//Someone says you can use/(&lt;[^&gt;]*&gt;)/$ expression = "/(&lt;[^&gt;]*&gt;)/"; Echo preg_replace ($expression, "", $stringOfText);


What are the differences between the 18.PHP and Perl methods for resolving arrays and hashes?


That's why I always tell people to choose the right programming language, but how can you answer the question if you only use one language? The problem is simple, Perl all array variables start with @, such as @myArray, PHP follows $ as the beginning of all variables, such as $myArray.


As for Perl, the hash table uses%, such as%myhash,php, and still uses $, such as $myHash.


19. How do you use PHP to solve the stateless nature of HTTP?


The two main options are session and cookie. The way to use the session is to add session_start () at the beginning of each page, and then use the $_session hash table to store the session variable. As for cookies, you only need to remember one principle: Call the Set_cookie () function before outputting any text, and access all the cookie variables simply by using the $_cookie hash table.


There is also a less reliable way to use the IP address of the visitor, which has a specific risk.


What is the 20.GD function library for?


This may be my favorite function library, since the PHP 4.3.0 version of GD built in the PHP system. This library allows you to work with and display a variety of formats, and another common use is to make the map files. Another alternative to GD is ImageMagick, but this library is not built into PHP and must be installed by the system administrator on the server.


21. Try to write several ways to output a section of HTML code.


Well, you can use any of the output statements in PHP, including Echo, print, and printf, and most people use the echo of the following example:


echo "My string $variable"; You can also use this method:


The Echo &lt;&lt;&lt;endthis text is written to the screen as output and this $variable is parsed too. If you wanted your can have &lt;span&gt; HTML tags in as well.&lt;/span&gt; the end; Remarks must is on a line of it own, and can ' t contain any extra white spaces. End;


is 22.PHP better than Perl? Please discuss.


Let us not provoke a war of words for a simple question, as I have often said: "Choose the right language for your work, and do not indulge your work with the language." "I personally think Perl is a great fit for command-line tools, and although it's good for web apps, its real power is on the command line." In the same way, PHP can be used in the console environment, but the individual believes that it has a better performance in Web applications, PHP has a large number of Web applications specifically designed for the function, Perl seems to be the order of the design of the act.


Personally I like both languages, I used to use Perl, PHP, and Java during college, but I used C # in my work, but at home I spent a lot of time practicing PHP, Perl, Ruby (now learning) and Java, keeping my knowledge of skills up to date. A lot of people ask me how C and C + +, whether they still have the opportunity to occupy a seat in my application, my answer is basically "no", my recent work focuses on web development, although C and C + + can also be used to write Web pages, but they are not designed for this work, "to choose the right language for work", If I need to write a console application to show the performance comparisons of the bubble sort, quick sort, and merge sort, I will definitely use C + +. If I need to write a photo-book system, I will use PHP or C # (I think that the user interface is more than a Web page. NET language).

PHP Classic Face Test (serial 4)


1 Please explain the difference between the value in PHP and the reference in the pass. When does the value pass the reference?


A: Passing a value only passes the value of one variable to another, and the reference indicates that the two points to the same place.


2 What's the effect of error_reporting this function in PHP?


Answer: the error_reporting () function sets the error_reporting directive at runtime. PHP has many levels of errors, the using this function sets the "Level for" duration (runtime) of your script.


3 Write a function with a regular expression (Regular Expression) to verify that the e-mail message is in the correct format.


For:


&lt;?php


if (Isset ($_post[' action ')) &amp;&amp;


$_post[' action ']== ' submitted ')


{


$email =$_post[' email '];


if (!preg_match/^ (?: w+.?) *w+@ (?: w+.?) *w+$/", $email))


{


Echo


"E-mail detection failed";


}


Else


{


Echo


"E-mail detection success";


}


}


Else


{


?&gt;


&lt;html&gt;


&lt;head&gt;&lt;title&gt;email Testing &lt;/title&gt;


&lt;script type= "Text/javascript" &gt;


function Checkemail (stext)


{


var reg=/^ (?: w+.?) *w+@ (?: w+.?) *w+$/;


var Email=document.getelementbyid (stext). value;


if (!reg.test (email))


{


Alert ("E-mail detection failed");


}


Else


{


Alert ("e-mail format is correct");


}


}


&lt;/script&gt;


&lt;/head&gt;


&lt;body&gt;


&lt;form action= "&lt;?php echo $_server[' php_self ']?&gt;" method= "POST" &gt;


E-mail: &lt;input type= "text" id= "email" name= "email"


/&gt;&lt;br/&gt;


&lt;input type= "hidden" name= "action" value= "submitted"


/&gt;


&lt;input type= "button" name= "button" value= "Client Detection" onclick= "checkemail (' email ')"


/&gt;


&lt;input type= "Submit" name= "Submit" value= "server-side detection"


/&gt;


&lt;/form&gt;


&lt;/body&gt;


&lt;/html&gt;


&lt;?php


}


?&gt;

4 briefly describes how to get the current execution script path, including the obtained parameters.
<?php
Echo
"http://". $_server[' server_name '].$_server[' php_self '. $_server[' query_string '];
echo "http://". $_server[' http_host '].$_server[' Request_uri ';
?>

5 has a one-dimensional array, which stores the shape data, write a function that arranges them in order from big to small. Requires high execution efficiency. and explain how to improve execution efficiency. (This function must be implemented by itself and cannot be used with PHP functions)

&lt;?php


Function Bubblesort (&amp; $arr)


{


$cnt =count ($arr);


$flag = 1;


for ($i =0; $i &lt; $cnt; $i + +)


{


if ($flag ==0)


{


Return


}


$flag = 0;


For ($j =0 $j &lt; $cnt-$i-1; $j + +)


{


if ($arr [$j]&gt; $arr [$j +1])


{


$tmp = $arr [$j];


$arr [$j]= $arr [$j +1];


$arr [$j +1]= $tmp;


$flag = 1;


}


}


}


}


$test =array (1,3,6,8,2,7);


Bubblesort ($test);


Var_dump ($test);


?&gt;


6 Please give an example of how to speed up the loading of the page in your development process
A: To use the server resources to open, timely shutdown server resources, database Add index, page can generate static, pictures and other large files separate server. Using the Code optimization tool.

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.