Php interview questions and answers for freshers- Most common
What is PHP?
PHP: Hypertext Preprocessor is open source server-side scripting language that is widely used for web development. PHP scripts are executed on the server. PHP allows writing dynamically generated web pages efficiently and quickly. The syntax is mostly borrowed from C, Java and perl. PHP is free to download and use.
Who is the father of PHP ?
Rasmus Lerdorf is known as the father of PHP.
Difference between $message and $$message in PHP.
$message is a variable with a fixed name. $$message is a variable whose name is stored in $message.
If $message= “var”;
Then $$message is the same as $var.
Explain how to submit form without a submit button.
What are default session time and path?
default session time in PHP is 1440 seconds or 24 minutes
Default session save path id temporary folder /tmp
What is the difference between the functions unlink and unset?
unlink() deletes the given file from the file system.
unset() makes a variable undefined.
How many ways can we get the value of current session id?
session_id() returns the session id for the current session.
What’s the special meaning of __sleep and __wakeup?
__sleep returns the array of all the variables than need to be saved, while __wakeup retrieves them.
Explain the importance of the function htmlentities.
The htmlentities() function converts characters to HTML entities.
Explain the different types of errors in PHP.
Notices, Warnings and Fatal errors are the types of errors in PHP
Notices represents non-critical errors, i.e. accessing a variable that has not yet been defined. By default, such errors are not displayed to the user at all but whenever required, you can change this default behavior.
Warnings are more serious errors but they do not result in script termination. i.e calling include() a file which does not exist. By default, these errors are displayed to the user.
Fatal errors are critical errors i.e. calling a non-existent function or class. These errors cause the immediate termination of the script.
How to include a file to a php page?
We can include a file using “include() ” or “require()” or “include_once()”or “require_once()” function with file path as its parameter.
What’s the difference between include and require?
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.
require_once(), require(), include().What is difference between them?
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 for example. This way you make sure you don’t include the file more times and you will not get the “function re-declared” error.
Differences between GET and POST methods ?
With a GET method, you are sending your data via the URL. While, with A POST method, data is embedded in the form object and sent directly from your browser to the server. POST is a bit more safe than GET because GET could be stored in the history.
What is use of in_array() function in php ?
in_array used to checks if a value exists in an array
What is numeric array?
Numeric array − An array with a numeric index. Values are stored and accessed in linear fashion.
What is associate array?
Associative array − An array with strings as index. This stores element values in association with key values rather than in a strict linear index order.
How will you concatenate two strings in PHP?
To concatenate two string variables together, use the dot (.) operator −
echo $string1 . ” ” . $string2;
How will you find the length of a string in PHP?
The strlen() function is used to find the length of a string. Let’s find the length of our string “Hello world!” −
echo strlen(“Hello world!”);
How will you locate a string within a string in PHP?
The strpos() function is used to search for a string or character within a string. If a match is found in the string, this function will return the position of the first match. If no match is found, it will return FALSE. Let’s see if we can find the string “world” in our string −
echo strpos(“Hello world!”,”world”);
How will you generate random numbers using PHP?
The PHP rand() function is used to generate a random number. This function can generate numbers with-in a given range. The random number generator should be seeded to prevent a regular pattern of numbers being generated. This is achieved using the srand() function that specifiies the seed number as its argument.
What is use of count() function in php ?
count() is used to count all elements in an array, or something in an object
What is the difference between Session and Cookie?
The main difference between sessions and cookies is that sessions are stored on the server, and cookies are stored on the user’s computers in the text file format. Cookies can not hold multiple variables,But Session can hold multiple variables.We can set expiry for a cookie,The session only remains active as long as the browser is open.Users do not have access to the data you stored in Session,Since it is stored in the server.Session is mainly used for login/logout purpose while cookies using for user activity tracking
How to set cookies in PHP?
setcookie(“your_cookie_key”, “your_cookie_value”, time()+3600);
How to Retrieve a Cookie Value?
eg : echo $_COOKIE[“your_cookie_key”];
How we can retrieve the data in the result set of MySQL using PHP?
What is the use of explode() function ?
Syntax : array explode ( string $delimiter , string $string [, int $limit ] );
This function breaks a string into an array. Each of the array elements is a substring of string formed by splitting it on boundaries formed by the string delimiter.
What is the difference between explode() and split() functions?
Split function splits string into array by regular expression. Explode splits a string into array by string.
What is the use of mysql_real_escape_string() function?
It is used to escapes special characters in a string for use in an SQL statement
How to strip whitespace (or other characters) from the beginning and end of a string ?
The trim() function removes whitespaces or other predefined characters from both sides of a string.
What is the use of header() function in php ?
The header() function sends a raw HTTP header to a client browser.Remember that this function must be called before sending the actual out put.For example, You do not print any HTML element before using this function.
How to redirect a page in php?
The following code can be used for it, header(“Location:index.php”);
What is the difference between mysql_fetch_array() and mysql_fetch_assoc() ?
mysql_fetch_assoc function Fetch a result row as an associative array, Whilemysql_fetch_array() fetches an associative array, a numeric array, or both
What is mean by an associative array?
Associative arrays are arrays that use string keys is called associative arrays.
What is the purpose of $GLOBALS variable in PHP?
$GLOBALS − Contains a reference to every variable which is currently available within the global scope of the script. The keys of this array are the names of the global variables.
What is the purpose of $_SERVER variable in PHP?
$_SERVER − This is an array containing information such as headers, paths, and script locations. The entries in this array are created by the web server. There is no guarantee that every web server will provide any of these. See next section for a complete list of all the SERVER variables.
How will you connect a MySql database using PHP?
PHP provides mysqli_connect function to open a database connection.
$con = mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
echo "Failed to connect to MySQL: " . mysqli_connect_error();
What are interfaces n PHP?
Interfaces are defined to provide a common function names to the implementors. Different implementors can implement those interfaces according to their requirements. You can say, interfaces are skeltons which are implemented by developers.
What is the use of final keyword?
PHP 5 introduces the final keyword, which prevents child classes from overriding a method by prefixing the definition with final. If the class itself is being defined final then it cannot be extended.
What is Multidimensional array?
Multidimensional array − An array containing one or more arrays and values are accessed using multiple indices.
Distinguish between urlencode and urldecode?
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 is decodes the query part of a URL string. 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.
Note: Above are the most common Php interview questions and answers for freshers.