Home >Backend Development >PHP Tutorial >8 PHP interview questions that reflect your knowledge of PHP

8 PHP interview questions that reflect your knowledge of PHP

黄舟
黄舟Original
2017-11-10 11:07:222139browse

php interview questions are ever-changing. I have also introduced to you the written test questions, core technical questions, thinkphp questions, and error-prone questions in the php interview questions. These are all questions we will encounter in interviews. I have recently discovered something very interesting. The PHP interview questions issued by many companies can directly reflect the PHP level. So today we will show you which PHP interview questions can reflect the PHP level. Show your understanding of php!

1. What is the execution result of

<?php echo count(strlen(“http://php.net”)); ?>

?

Answer: 1

Explanation: count(var) is used to count the number of elements in an array or object. When var is null or an empty array, the result is 0. If var is a normal variable, returns 1. Under normal circumstances, the number of elements or attributes in var is returned.

2. What should you pay attention to when using the list() function?

Answer: list() is a grammatical structure. List($array) is used to quickly assign elements in an array to some variables. When using it, please note that $array must be an index array, and the index value starts from 0.

3. Please explain which functions are affected after safe_mode in php.ini is turned on?


Answer: Safe_mode is the safe mode of php. After being turned on, it will mainly affect system operations, files, permission settings and other methods, and is mainly used to deal with webshells. The following are some of the affected functions:

ckdir,move_uploaded_file,chgrp,parse_ini_file,
chown,rmdir,copy,rename,fopen,require,highlight_file,show_source,include,symlink,link,touch,mkdir,unlink,exec,
shell_exec,pasathru,system,popen

It should be noted that: in php5.3 and above, safe_mode is deprecated, and in php5.4 and above, this feature is completely removed.

4. Please explain by analogy the main functions of the two regular expressions, POSIX style and Prel-compatible style.

Answer: POSIX style: match regular expression ereg and replace ereg_replace

Prel style: match regular expression preg_match and replace preg_replace

Preg_match is better than ereg Faster efficiency, preg_replace is faster than ereg_replace.

5. How to run a php script under the command (write two ways), and how to pass parameters to the php script?

Answer: The first way: first enter the php installation directory and execute php path/filename.php.

Example:

php my_script.php     php -f  "my_script.php"

The second way: php -r "php script"; (no need to add the start and end characters of php).

Example:

php -r "print_r(get_defined_constants());"

Pass parameters to php script:

First way: php -r "var_dump($argv);" -- -h (Note: If the parameters to be passed start with -, you must use the parameter list separator -- to pass the parameters correctly)

Second method: test.php file code:

#!/usr/bin/php  <?phpvar_dump($argv);?>

./ test.php -h -- foo (add #!/usr/bin/php at the beginning of the php file to directly pass parameters starting with -)

6. The magic methods in php5 are Which ones? Please give examples of how to use each.

Answer:

1. construct(): Automatically called when instantiating an object.

2. destruct(): Automatically called when the object is destroyed or the script execution ends.

3. call(): This function is executed when calling a method that does not exist on the object.

4. get(): Execute this function when obtaining the non-existent attributes of the object.

5. set(): This function is executed when setting a property that does not exist in the object.

6. isset(): This function is executed when detecting whether a certain attribute of the object exists.

7. unset(): This function is executed when destroying an attribute of the object.

8. toString(): This function is executed when the object is output as a string.

9. clone(): This function is executed when cloning an object.

10. autoload(): When instantiating an object, when the class does not exist, execute this function Automatically load the class.

11. sleep(): serialize is called before and you can specify the object attributes to be serialized.

12. Wakeup: Unserialize is called before and can perform the initialization of the object.

13. set_state(): Called when var_export is called. Use the return value of set_state as the return value of var_export.

14. invoke(): This method is executed when using the object as a function. This is generally not recommended.

7. Briefly describe the garbage collection mechanism of PHP.

Answer: Variables in php are stored in the variable container zval. In addition to storing variable types and values, zval also has is_ref and refcount fields. refcount indicates the number of elements pointing to the variable, and is_ref indicates whether the variable has an alias. If refcount is 0, the variable container is recycled. If a zval's refcount is greater than 0 after being reduced by 1, it will enter the garbage buffer. When the buffer reaches the maximum value, the recycling algorithm will loop through the zval to determine whether it is garbage and release it.

8. Use PHP to implement a two-way queue.

The queue is a linear table, based on the first-in, first-out principle.

One-way queue: can only enter from the beginning and exit from the tail.

Two-way queue: Both the head and tail can be entered and exited

class DuiLie {
private $array = array();//声明空数组
public function setFirst($item){
return array_unshift($this->array,$item);//头入列
}
public function delFirst(){
return array_shift($this->array);//头出列
}
public function setLast($item){
return array_push($this->array,$item);//尾入列
}
public function delLast(){
return array_pop($this->array,$item);//尾出列
}
public function show(){
var_dump($this->array);//打印数组
}
public function Del(){
unset($this->array);//清空数组
}
}

Summary:

The PHP interview questions introduced in this article are all those we often encounter in development work, so these questions can reflect how much you know about PHP and how much you have actually developed, so they are very Intuitive PHP interview questions, I hope it will be helpful to you!

Related recommendations:

php interview Analysis of the object-oriented questions in the question


The 10 most error-prone PHP interview questions


Sharing of php core technology issues in php interview questions


Summary of written test questions in php interview questions


The above is the detailed content of 8 PHP interview questions that reflect your knowledge of PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn