In this article on PHP Interview Questions, I have compiled a list of fundamental PHP interview questions and answers that were asked in a real interview for a PHP web developer position. Every PHP web developer should know these questions/answer of PHP. So, either you are preparing for any interview in PHP development or are a web developer, you should go through the following list of interview questions in PHP. These PHP questions are mix of basic PHP questions to advance questions that expert PHP engineers know. Let’s have a look…
PHP Interview Questions
Q. How to include a file to a php page?
A. We can include a file using “include() ” or “require()” functions with as it’s parameter.
Q. What is the difference between include and require?
A. If the file is not found by require(), it will cause a fatal error and halt the execution of the script. If the file is not found by include(), a warning will be issued, but execution will continue.
Q. require_once(), require(), include(). What is difference between them?
A. require() includes and evaluates a specific file, while require_once() does that only if it has not been included before (on the same page).
So, require_once() is recommended to use when you want to include a file where you have a lot of functions to use. This way you make sure you don’t include the file more times and you will not get the “function re-declared” error.
Q. How do you define a constant?
A. Using define() directive, like define (“MYCONSTANT”,150)
Q. What Is a Session?
A. Session is used to store information on the server for later use. Session information is temporary and will be deleted after the user has left the website.
Q. How to set cookies in PHP?
A. Cookies are often used to track user information and are stored on client.
Setcookie(name, value, expire, path, domain);
eg: Setcookie(“sample”, “ram”, time()+3600);
Q. How to create a mysql connection?
A. mysql_connect (servername, username, password);
Q. Difference between mysql_connect and mysql_pconnect?
A. In short mysql_pconnect() makes a persistent connection to the database which means a SQL link that do not close when the execution of your script ends.
mysql_connect() provides only new connection for the database while using mysql_pconnect() , the function would first try to find a (persistent) link that’s already open with the same host, username and password. If one is found, an identifier for it will be returned instead of opening a new connection. The connection to the SQL server will not be closed when the execution of the script ends. Instead, the link will remain open for future use.
Q. How to open a file?
A. <?php
$file=fopen(“welcome.txt”, “r”);
?>
Q. What is the use of mysql_fetch_array() function in php?
A. This function returns a row from the table as an associative array or numeric array. This extension is deprecated as of PHP 5.5.0.
Q. Difference between mysql_fetch_row(), mysql_fetch_array(), mysql_fetch_assoc(), mysql_fetch_object().
A. mysql_fetch_row: This function will return a row where the values will come in the order as they are defined in the SQL query and the keys will span from 0 to one less than the number of columns selected.
mysql_fetch_assoc: This function will return a row as an associative array where the column names will be the keys storing corresponding value.
mysql_fetch_array : This function will actually return an array with both the contents of mysql_fetch_row and mysql_fetch_array merged into one. It have both numeric and string keys which will let you access your data in whatever way you’d find easiest.
mysql_fetch_object() : This function gets a row from the mysql_query() function and returns an object on success, or FALSE on failure or when there are no more rows.
Q. What is the use of the function ” explode() ” in php?
A. This function is used to split a string by special character or symbol in the string, we must be pass the string and splitting character as parameter into the function.
Q. What is use of in_array() function in php?
A. in_array used to checks if a value exists in an array.
Q. Differences between GET and POST methods?
A. We can send 1024 bytes using GET method but POST method can transfer large amount of data and POST is the more secure than GET method.
Choosing GET as the “method” will append all of the data to the URL and it will show up in the URL bar of your browser. The amount of information you can send back using a GET is restricted as URLs can only be 1024 characters.
A POST on the other hand will (typically) send the information through a socket back to the webserver and it won’t show up in the URL bar. You can send much more information to the server this way and it’s not restricted to textual data either. It is possible to send files and even binary data such as serialized Java objects!
GET can be bookmarked and it’s the default method of form submission while POST must be specified and can’t be bookmarked.
Q. What is SSL?
A. SSL (Secure Sockets Layer) is the standard security technology for establishing an encrypted link between a web server and a browser. This link ensures that all data passed between the web server and browsers remain private and integral.
To be able to create an SSL connection a web server requires an SSL Certificate.
Q. How to calculate the sum of values in an array?
A. “array_sum” method used for calculate sum of values in an array.
Q. What is the use of “ksort” in php?
A. it is used for sort an array by key in reverse order.
Q. What is the difference between Session and Cookie?
A. The main difference between cookies and sessions is that cookies are stored in the user’s browser, and sessions are not.
A cookie can keep information in the user’s browser until deleted. If a person has a login and password, this can be set as a cookie in their browser so they do not have to re-login to your website every time they visit. The trouble is that a user can block cookies or delete them at any time.
Sessions are not reliant on the user allowing a cookie. They work instead like a token allowing access and passing information while the user has their browser open. The problem with sessions is that when you close your browser you also lose the session.
Q. How do you create a session, add data to a session and remove data from a session
A. session_start() //creates a session
eg. session_start();
$_SESSION[‘favcolor’] = ‘green’;
The session_unset() function frees all session variables currently registered.
session_destroy() destroys all of the data associated with the current session. It does not unset any of the global variables associated with the session, or unset the session cookie.
Q. If cookies are disabled will the sessions work? Why?
A. If cookies are disabled you must use a different method to pass the session id and to make session work.
Q. What is the difference between IDE, framework, CMS and Blog?
A. IDE is the software you use to develop the program. For example: Eclipse is an IDE.
The Framework is a set of both libraries and best practices that help you not re-invent the wheel and provide a set of guidelines on how to develop. But you have to use them and build your site yourself.
CMS are powered with a back-end with a host of plugins and components, so that you can manage front-end. It’s just a matter of themeing and adding content.
A blog (a contraction of the term “web log”) is a type of website, usually maintained by an individual with regular entries of commentary, descriptions of events, or other material such as graphics or video.
Q. What is the difference between $var and $$var
A. $var is a variable
$$var is Variable to variable. A variable variable takes the value of a variable and treats that as the name of a variable.
Means see the below example
Example:
$var = “Hello”;
$$var = “World”;
At this point two variables have been defined and stored in the PHP symbol tree: $var with contents “hello” and $hello with contents “world”.
So echo $var; will print Hello, echo $$var; will print World.
Q. Write a function to calculate x to the power y without using the pow function of PHP
A. function calculatePower ($nr, $power) {
if($power == 0)
return 1;
return calculatePower ($nr, $power-1) * $power;
}
Here x wil be pass to $nr and y will be pass to $power.
Q. Following operations are true or false? (Operator Precedence)
$one = true;
$two = null;
$a = isset($one) && isset($two);
$b = isset($one) and isset($two);
echo $a;
echo $b;
A. $a will output false, while $b will output true.
Q. What is the reason for this asked in previous question?
A. These are small aspects but very important a programmer must know. In the first phrase it uses “&&” operator to bind those two expressions which will give the expected answer. But the second one gives an unexpected answer where “and” operator is used.
The reason behind is the “higher precedence” between these 3 operators,
&& = and
In the first condition it will first check for the “&&” operation and then for the “=”
But in the second condition it will first look for the “=” operator and then for the “and”.
In order to fix this you can force the precedence by using parenthesis as follows:
echo $b = (isset($one) and isset($two));
Q. Current PHP Version
A. PHP 5.5.9 (in February 2014)
Q. Current MYSQL Version
A. Current Generally Available Release: 5.6.16
Q. What is method/function overloading and overriding?
A. Function overloading and overriding are two different concepts. Function overloading occurs when you define the same function name twice (or more) using different set of parameters.
For example:
class Addition {
function compute($first, $second) {
return $first+$second;
}
function compute($first, $second, $third) {
return $first+$second+$third;
}
}
In the example above, the function compute is overloaded with two different parameter signatures. *This is not yet supported in PHP. An alternative is to use optional arguments:
class Addition {
function compute($first, $second, $third = 0) {
return $first+$second+$third;
}
}
Function overriding occurs when you extend a class and rewrite a function which exists in the parent class:
class Substraction extends Addition {
function compute($first, $second, $third = 0) {
return $first-$second-$third;
}
}
Here, compute overrides the behavior set forth in Addition.
Q. Name mysql storage engines.
A. InnoDB, MyISAM (Default), Memory (This engine was formerly known as the HEAP engine.), CSV, Example, Archive, Blackhole, Merge, Federated, BDB (BerkeleyDB).
Q. What are differences between InnoDB and MyISAM?
A. InnoDB is more complex while MyISAM is simpler.
InnoDB has transactions while MyISAM does not.
InnoDB has foreign keys and relationship constraints while MyISAM does not.
InnoDB has better crash recovery while MyISAM is poor at recovering data integrity at system crashes.
MyISAM has full-text search index while InnoDB has not.
InnoDB consumes more system resources
MyISAM is faster than InnoDB.
Q. What are IMAP and POP3? What is difference betweenthem?
A. IMAP and POP are different protocols for handling e-mails.
POP stands for “Post Office Protocol”. IMAP stands for “Internet Message Access Protocol”.
POP is Faster (sends your e-mails and gets e-mails from you). IMAP Keeps a copy of everything you do on the server.
Q. How to increase the execution time of a php script?
A. Increase PHP Script Execution Time Limit Using ini_set().
ie. ini_set(‘max_execution_time’, 300);
Place this at the top of PHP script or change in php.ini file.
Q. What is difference between echo and print?
A. echo does not return any value but print always returns 1 (integer).
Both print and echo take only 1 argument when used with parentheses (like a function call).
However, when used without parentheses, echo can take several arguments e.g. echo “Hello”,”World”,”!”,42; but print only takes one parameter.
Q. What are strstr and stristr?
A. strstr() is case sensitive and it returns part of a given string from the first occurrence of a given substring to the end of the string. For example: strstr(“user@example.com”,”@”) will return “@example.com”.
stristr() does the same but it’s not case sensitive.
<?php
$name = “info@xieXnxo.com”;
$test = strstr($name, “X”);
echo $test; // prints Xnxo.com
echo “<br /><br />”;
$test = stristr($name, “X”);
echo $test; // prints xieXnxo.com
?>
Q. What is the difference between single quoted strings and double quoted strings?
A. In double quotes strings, variables in the strings will be evaluated. This is why it is slower at runtime.
On a lighter but still relevant note, having been to a few php interviews lately, OOP questions pop up quite frequently. The perennial favourite is, of course, “what is the difference between public, private and protected”! I must admit I was somewhat taken aback recently when a recruitment agent had the temerity to ask me the differences between an abstract class and an interface and then proceeded to ask me about iterator classes – the cheek! 😉
Bis thank you for this valuable comment. We will include OOP fundamentals in upcoming article.
Thanks – nice to know!
Welcome.