PHP Interview Questions and answers (3)

Source: Internet
Author: User

1. Which of the following statements will not add John to the users array?
$ Users [] = 'john ';
John is successfully added to the array users.
Array_add ($ users, 'john ');
The function array_add () is not defined.
Array_push ($ users, 'john ');
John is successfully added to the array users.
$ Users | = 'john ';
Syntax error.

2. What are the differences between sort (), assort (), and ksort? Under what circumstances are they used?

Sort ()
Sort the values of elements in the array in alphabetical order. The index key is numbered from 0 to n-1. It is mainly used to sort the array when the value of the array index key is irrelevant.
Assort ()
PHP does not have the assort () function, so it may be asort.
Asort ()
Like sort (), array elements are arranged in alphabetical order. The difference is that all index keys are retained, which is especially suitable for sorting Lenovo arrays.
Ksort ()
Sort the index key values in alphabetical order based on the index key values in the array. This is especially suitable for Lenovo arrays that want to sort the index key.

3. What will the following code generate? Why?

$ Num = 10;
Function multiply (){
$ Num = $ num * 10;
}
Multiply ();
Echo $ num;

Because the function multiply () does not specify $ num as a global variable (such as global $ num or $ _ GLOBALS ['num']), the value of $ num is 10.

4. What are the differences 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. Therefore, when you change the value of a variable in the function, the entire application will see the new value of this variable.
The value of a regular variable is transmitted to the function. When the function changes the value of this variable, only the function sees the new value, and the other part of the application still sees the old value.

$ MyVariable = "its 'value ";
Myfunction (& $ myVariable); // transmit parameters using reference

Send parameters to the function using reference, so that the variable changed by the function can be retained even after the function is completed.

5. Which of the following functions can be used to insert a function library into the script being executed?
Different understandings of this question may have different answers. My first thought is to insert a PHP function library like include (), include_once (), require (), require_once (), but be careful, the "function library" should also include com objects and. net function library, so our answers should also include com_load and dotnet_load. When someone mentions the "function library", don't forget these two functions.

6. What are the differences between foo () and @ foo?
Foo () executes this function. Any interpretation errors, syntax errors, and execution errors are displayed on the page.
@ Foo () when executing this function, all the preceding error messages are hidden.
Many applications use @ mysql_connect () and @ mysql_query to hide mysql error messages. I think this is a serious mistake because errors should not be hidden and you must properly handle them, solve them if possible.

7. How do you detect errors for PHP applications?
I do not often do this. I have tried many different debugging tools and it is not easy to set these tools in Linux. However, I will introduce a debugging tool that has been quite noticed recently.
Php-advanced PHP debugger or PHP-APD. The first step is to execute the following command for installation:

After installing pear install APD, add the following statements at the beginning of your script to start Error Detection:

Apd_set_pprof_trace (); after the execution is complete, open the following file to view the execution log:
APD. dumpdir
You can also use pprofp to format logs.
For more information, see http://us.php.net/manual/en/ref.apd.php.

8. What is "=? Here is an example where "=" is true but "=" is false.
"=" Is a function that can return both Boolean "false" and Boolean "false" values. It can also return a function that is not a Boolean value but can be assigned a "false" value. strpos () and strrpos () are two examples.
The second part of the problem is a bit difficult. It is false to think of "=", but "=" is a real example, but it is very easy. On the contrary, there are few examples. 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, convert to boolean value "false"} If (strpos ("ABC", "A") = true) {// This part will be executed, because "=" ensures that the return value of strpos () is not converted into a Boolean value .}

9. How do you define a class myclass without a member function or feature?

Class myclass {}

10. How do you generate a myclass object?

$ Obj = new myclass ();

11. How can I access the features of a category and change its value in a category?
Usage statement: $ this-> propertyName, for example:

Class myclass {private $ propertyName; public function _ construct () {$ this-> propertyName = "value ";}}

12. What are the differences between include and include_once? What about require?
All three files are used to insert other files in the script, depending on whether url_allow_fopen is approved. This file can be obtained from inside or outside the system. But there are also tiny differences between them:
Include (): This function allows you to insert the same file multiple times in the script. If the file does not exist, it will issue a system warning and continue executing the script.
Include_once (): similar to include (), as shown in its name, the file is inserted only once during script execution.
Require (): similar to include (). It is also 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 and cause the script to stop running.

13. Which of the following statements can redirect the browser to another page?
Redir ()
This is not a PHP function and may cause execution errors.
Header ()
This is the correct answer. The header () is used to insert the volume header, which can be used to redirect the browser to another page. For example:
Header ("Location: http://www.search-this.com /");
Location ()
This is not a PHP function and may cause execution errors.
Redirect ()
This is not a PHP function and may cause execution errors.


14. Which of the following functions can be used to open an archive for reading/writing?
Fget ()
This is not a PHP function and may cause execution errors.
File_open ()
This is not a PHP function and may cause execution errors.
Fopen ()
This is the correct answer. fopen () can be used to open a file for reading/writing. In fact, this function has many options. For more information, see php.net.
Open_file ()
This is not a PHP function and may cause execution errors.
15. What are the differences between mysql_fetch_row () and mysql_fetch_array?
Mysql_fetch_row () stores a column of the database in a zero-based array. The first column is indexed 0 in the array, the second column is indexed 1, and so on. Mysql_fetch_assoc () stores a column of the database in an associated array. The index of the array is the column name, for example, if my database query returns the "first_name", "last_name", and "email" columns, the index of the array is "first_name", "last_name", and "email ". Mysql_fetch_array () can return the values of mysql_fetch_row () and mysql_fetch_assoc () at the same time.
16. What is the following code used? Please explain.

$ Date = '2014/1/123'; print ereg_replace ("([0-9] +)/([0-9] +)/([0-9] +) "," \ 2/\ 1/\ 3 ", $ date );

This is to convert a date from MM/DD/YYYY to DD/MM/YYYY. A good friend of mine told me that this regular expression can be split into the following statements. For such a simple expression, there is no need to disassemble it. It is purely for the convenience of explanation:

// Corresponding to one or more 0-9, followed by an oblique number $ regExpression = "([0-9] + )/"; // apply one or more values to 0-9, followed by another oblique sign $ regExpression. = "([0-9] +)/"; // corresponding to one or more 0-9 $ regExpression again. = "([0-9] +)"; as for \ 2/\ 1/\ 3, it is used to correspond to parentheses. The first parenthesis is for the month, the second bracket corresponds to the date, and the third bracket corresponds to the Year.

17. I will give you a line of text $ string. How do you compile a regular expression to remove the HTML Tag in $ string?
First, PHP has built-in function strip_tags () to remove HTML tags. Why write regular expressions? Well, I will answer the question as follows:

$ StringOfText = "<p> This is a test </p>"; $ expression = "/<(. *?)> (.*?) <\/(. *?)> /"; Echo preg_replace ($ expression," \ 2 ", $ stringOfText); // someone says it can also be used/(<[^>] *>) /$ expression = "/(<[^>] *>)/"; echo preg_replace ($ expression, "", $ stringOfText );

18. What is the difference between the methods for distinguishing arrays from scattered lists in PHP and Perl?
This is exactly why I always tell others to select the appropriate programming language. If you only use one language, how can you answer this question? This problem is simple. All arrays of Perl variables start with @. For example, @ myArray, PHP begins with $, for example, $ myArray.
For Perl, % is used to indicate the hash. For example, % myHash is used while PHP is not separate. $ is still used, for example, $ myHash.
19. How do you use PHP to solve the stateless nature of HTTP?
The two most important choices are session and cookie. The session method is to add session_start () at the beginning of each page, and then store SESSION variables using the $ _ session hash. As for cookie, you only need to remember one principle: Call the set_cookie () function before outputting any text. In addition, you only need to use the $ _ COOKIE hash to access all cookie variables.
Another less reliable method is to use the visitor's IP address, which has a specific risk.
20. What is the GD function library used?
This may be my favorite function library. GD has been built into the PHP system since PHP 4.3.0. This function allows you to process and display images in various formats. Another common purpose of this function is to create images. Another option other than GD is ImageMagick, but this function library is not built in PHP and must be installed on the server by the system administrator.
21. Try writing several methods to output a piece of HTML code.
Well, you can use any output statement in PHP, including echo, print, and printf. Most people use echo in the following example:

Echo "My string $ variable"; you can also use this method:

Echo <ENDThis text is written to the screen as output and this $ variable is parsed too. if you wanted you can have <span> HTML tags in here as well. </span> The END; remarks must be on a line of its own, and can't contain any extra white space. END;

22. Is PHP better than Perl? Please discuss.
We should not trigger a battle for a simple problem, as I often say: "select the appropriate language for your work, and do not change your work language ." I personally think Perl is very suitable for command line tools. Although it has a good performance in Web applications, its real strength can be fully utilized in command line. Similarly, although PHP can be used in the Console environment, I personally think it has a better performance in Web applications. PHP has a large number of functions specially designed for Web applications, perl seems to be designed based on command behavior.
Personally, I like both languages. I often use Perl, PHP, and Java during my college years. Unfortunately, I use C # at work #, but at home, I spent a lot of time practicing PHP, Perl, Ruby (now learning) and Java to keep my skills updated. Many people ask me how C and C ++ do they still have a chance to take 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, they are not designed for this kind of work, "select the appropriate language for work". If I need to write a console application to demonstrate the performance comparison between bubble sort, quick sort, and merge sort, I will definitely use C/C ++. If I need to write a album system, I will use PHP or C # (I think the. NET language for user interface creation is more than the web page ).

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.