Test a developer’s PHP knowledge with these PHP interview questions from top PHP developers and experts, whether you’re an interviewer or candidate.
PHP developers are still in high demand for web application development. And there are more and more high-end enterprise level websites getting created using PHP.
PHP Interview Questions & Answers
- What is the main difference between require() and require_once()?
require(), and require_once() perform the same task except that the second function checks if the PHP script is already included or not before executing it.
(same for include_once() and include())
2) What is the function file_get_contents() useful for?
file_get_contents() lets reading a file and storing it in a string variable.
3) How is it possible to know the number of rows returned in the result set?
The function mysqli_num_rows() returns the number of rows in a result set.
4) Which function gives us the number of affected entries by a query?
mysqli_affected_rows() return the number of entries affected by an SQL query.
5) What is the difference between mysqli_fetch_object() and mysqli_fetch_array()?
The mysqli_fetch_object() function collects the first single matching record where mysqli_fetch_array() collects all matching records from the table in an array.
6) What is the difference between ereg_replace() and eregi_replace()?
The function eregi_replace() is identical to the function ereg_replace() except that it ignores case distinction when matching alphabetic characters.
7) What is the use of Print_r function in PHP?
prinf : It is a function which takes atleast one string and format style and returns length of output string. print_r() is used for printing the array in human readable format. they both are language constructs. echo returns void and print returns 1.
8) What is the difference between $message and $$message in PHP?
They are both variables. But $message is a variable with a fixed name. $$message is a variable who’s name is stored in $message. For example, if $message contains “var”, $$message is the same as $var.
9) What are the different types of errors in PHP?
# There are four types of errors in PHP.
- Warning errors
- Notice errors
- Fatal errors
- Parse errors
10) PHP Error Constants and their descriptions
- E_ERROR: A fatal error that causes script termination
- E_WARNING: Run-time warning that does not cause script termination
- E_PARSE: Compile time parse error
- E_NOTICE: Run time notice caused due to error in code
- E_CORE_ERROR: Fatal errors that occur during PHP’s initial startup (installation)
- E_CORE_WARNING: Warnings that occur during PHP’s initial startup
- E_COMPILE_ERROR: Fatal compile-time errors indication problem with script
- E_USER_ERROR: User-generated error message
- E_USER_WARNING: User-generated warning message
- E_USER_NOTICE: User-generated notice message
- E_STRICT: Run-time notices
- E_RECOVERABLE_ERROR: Catchable fatal error indicating a dangerous error
- E_ALL: Catches all errors and warnings
11) What is Exception Handling in PHP?
With PHP 5 came a new object oriented way of dealing with errors. Exception handling is used to change the normal flow of the code execution if a specified error (exceptional) condition occurs. This condition is called an exception.
12) Why post method is used in PHP?
GET can’t be used to send binary data, like images or word documents, to the server. The data sent by GET method can be accessed using QUERY_STRING environment variable. The PHP provides $_GET associative array to access all the sent information using GET method. The POST method transfers information via HTTP headers.
13) What is the use of die in PHP?
The die() function prints a message and exits the current script. This function is an alias of the exit() function.
14) What is urlencode and urldecode in php?
urlencode(string) — This PHP function is encodes a string to be used in a query part of a URL. URL encoding is used when placing text in a query string to avoid it being confused with the URL itself. It is normally used when the browser sends form data to a web server.
urldecode(string) – This PHP function id decodes any encoded value in the given string.
15) What is stdClass in PHP?
stdClass is a PHP generic empty class and stdClass is used to create the new Object. stdClass is a kind of Object in Java or object in Python but not actually used as universal base class.
16) What are soundex() and metaphone() functions in PHP?
soundex and metaphone can be used to find strings that sound similar when pronounced out loud.
soundex() function calculates the soundex key of a string. A soundex key is a four character long alphanumeric strings that represents English pronunciation of a word.
metaphone() the metaphone() function calculates the metaphone key of a string. A metaphone key represents how a string sounds if pronounced by an English person.
17) How to get IP address of a server in PHP?
Try $_SERVER[‘SERVER_ADDR’] . It will be the IP address that the server is listening on. You can use DNS functions (e.g., gethostbyaddr() ) to get the host name.
18) When do you use define() and when do you use const. What are the main differences between those two?
PHP 5.3 there are two ways to define constants: Either using the const keyword or using the define() function:
const FOO = ‘BAR’;
The fundamental difference between those two ways is that const defines constants at compile time, whereas define() defines them at runtime. As const are language constructs and defined at compile time they are a bit faster than define().
19) When to use self over $this?
Use $this to refer to the current object. Use self to refer to the current class.
20) Which is the best Web server?
There are different web servers available for different technologies.
LAMP: Linux, Apache, MySQL and PHP.
WIMP: Windows, IIS, MySQL/MS SQL Server and PHP.
WAMP: Windows, Apache, MySQL/MS SQL Server and PHP.
LEMP: Linux, NGINX, MySQL and PHP.
21) What is .htaccess in PHP?
.htaccess is a configuration file for use on web servers running the Apache Web Server software. When a .htaccess file is placed in a directory which is in turn ‘loaded via the Apache Web Server’, then the .htaccess file is detected and executed by the Apache Web Server software.
22) What are advantages of .htaccess?
There are many advantages using .htaccess file in PHP:
- URL redirecting
- Managing error pages
- Password Protection
- Visitor blocking facilities by IP address
- Setting Config variable and Environment variable