search
HomeBackend DevelopmentPHP Tutorial8 PHP interview questions that reflect your knowledge of PHP

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
How to make PHP applications fasterHow to make PHP applications fasterMay 12, 2025 am 12:12 AM

TomakePHPapplicationsfaster,followthesesteps:1)UseOpcodeCachinglikeOPcachetostoreprecompiledscriptbytecode.2)MinimizeDatabaseQueriesbyusingquerycachingandefficientindexing.3)LeveragePHP7 Featuresforbettercodeefficiency.4)ImplementCachingStrategiessuc

PHP Performance Optimization Checklist: Improve Speed NowPHP Performance Optimization Checklist: Improve Speed NowMay 12, 2025 am 12:07 AM

ToimprovePHPapplicationspeed,followthesesteps:1)EnableopcodecachingwithAPCutoreducescriptexecutiontime.2)ImplementdatabasequerycachingusingPDOtominimizedatabasehits.3)UseHTTP/2tomultiplexrequestsandreduceconnectionoverhead.4)Limitsessionusagebyclosin

PHP Dependency Injection: Improve Code TestabilityPHP Dependency Injection: Improve Code TestabilityMay 12, 2025 am 12:03 AM

Dependency injection (DI) significantly improves the testability of PHP code by explicitly transitive dependencies. 1) DI decoupling classes and specific implementations make testing and maintenance more flexible. 2) Among the three types, the constructor injects explicit expression dependencies to keep the state consistent. 3) Use DI containers to manage complex dependencies to improve code quality and development efficiency.

PHP Performance Optimization: Database Query OptimizationPHP Performance Optimization: Database Query OptimizationMay 12, 2025 am 12:02 AM

DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi

Simple Guide: Sending Email with PHP ScriptSimple Guide: Sending Email with PHP ScriptMay 12, 2025 am 12:02 AM

PHPisusedforsendingemailsduetoitsbuilt-inmail()functionandsupportivelibrarieslikePHPMailerandSwiftMailer.1)Usethemail()functionforbasicemails,butithaslimitations.2)EmployPHPMailerforadvancedfeatureslikeHTMLemailsandattachments.3)Improvedeliverability

PHP Performance: Identifying and Fixing BottlenecksPHP Performance: Identifying and Fixing BottlenecksMay 11, 2025 am 12:13 AM

PHP performance bottlenecks can be solved through the following steps: 1) Use Xdebug or Blackfire for performance analysis to find out the problem; 2) Optimize database queries and use caches, such as APCu; 3) Use efficient functions such as array_filter to optimize array operations; 4) Configure OPcache for bytecode cache; 5) Optimize the front-end, such as reducing HTTP requests and optimizing pictures; 6) Continuously monitor and optimize performance. Through these methods, the performance of PHP applications can be significantly improved.

Dependency Injection for PHP: a quick summaryDependency Injection for PHP: a quick summaryMay 11, 2025 am 12:09 AM

DependencyInjection(DI)inPHPisadesignpatternthatmanagesandreducesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itallowspassingdependencieslikedatabaseconnectionstoclassesasparameters,facilitatingeasiertestingandscalability.

Increase PHP Performance: Caching Strategies & TechniquesIncrease PHP Performance: Caching Strategies & TechniquesMay 11, 2025 am 12:08 AM

CachingimprovesPHPperformancebystoringresultsofcomputationsorqueriesforquickretrieval,reducingserverloadandenhancingresponsetimes.Effectivestrategiesinclude:1)Opcodecaching,whichstorescompiledPHPscriptsinmemorytoskipcompilation;2)DatacachingusingMemc

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft