PHP interviews questions

php interview questions

In this article, we will point out some of the most common and frequent PHP interview questions. PHP (recursive acronym for PHP: Hypertext Preprocessor) is a widely-used open source general-purpose scripting language that is especially suited for web development and can be embedded into HTML.

PHP is mainly focused on server-side scripting. Its basic functions include collect form data, generate dynamic page content, or send and receive cookies and much more. PHP can be used in following case scenario:

  • Server-side scripting
  • Commad line scripting
  • Writing desktop applications

We will point our most commonly asked PHP interview questions. Let’s get started:

What is the purpose of php.ini file?

Php.ini is a PHP configuration file used to customize behavior of PHP. It is used to control variables such as upload sizes, file timeouts, and resource limits.

What is the PHP latest version?

At this time, PHP latest version is 7.2.3 which was updated in 01 March 2018.

How can we display output to browser?

We can display output in 2 ways:
Example 1

 echo $variable;

Example 2


How to include file in PHP?

We can include a file in PHP using include() or require() function with path as its parameter.
Example:

include(‘path-to-file/myfile.php’);

What is the difference between include() and require()?

If a required file is not found using require(), PHP will emit a fatal error whereas for include only a warning will be emitted. include() will throw a warning if it can’t include the file, but the rest of the script will run.

What is the difference between require() and require_once()?

The require_once() statement is identical to require() except PHP will check if the file has already been included, and if so, not include (require) it again.

Is PHP a case sensitive language?

No, PHP is partially case sensitive language.

How do you execute PHP from command line?

PHP can be executed from command line as below:

php myscript.php

Does PHP support Multiple Inheritance?

No, PHP doesn’t support multiple inheritance. Multiple inheritance suffers from the Diamond Problem, which has not been (agreed upon how to be) solved in PHP yet.

What are the main different HTTP methods in PHP?

HTTP methods in PHP are GET, POST, PUT and DELETE.

What are the differences between GET and POST method?

GET is for retrieving data. We can send 1024 bytes using GET method
POST is for writing data. POST method can transfer large amount of data and POST is the secure method than GET method.

What are the rules for naming PHP variables?

Rules for naming PHP variables:

  • Variable names must begin with a letter or underscore character.
  • A variable name can consist of numbers, letters, underscores but you cannot use characters like + , – , % , ( , ) . & , etc

What is the difference between ‘==’ and ‘===’ in PHP?

The difference between the two is that '==' should be used to check if the values of the two operands are equal or not. On the other hand, '===' checks the values as well as the type of operands.

What are the different types of errors in PHP?

There are four types of errors in PHP.
Parse Error (Syntax Error): The parse error occurs if there is a syntax mistake in the script;
Fatal Error: Fatal errors are caused when PHP understands what you’ve written, however what you’re asking it to do can’t be done.
Warning Error: Warning errors will not stop execution of the script. The main reason for warning error is to include a missing file or using the incorrect number of parameters in a function.
Notice Error: Notice error is the same as a warning error i.e. in the notice error, execution of the script does not stop. Notice that the error occurs when you try to access the undefined variable, then produce a notice error.

What is break in PHP used for?

Break is used to terminate current loop or switch and executions of code continues after the loop or switch.

What is Continue used for?

continue causes the loop to skip the remainder of its body and immediately retest its condition prior to reiterating.

What are cookies in PHP?

A cookie is a small file that the server embeds on the user’s computer. Each time the same computer requests a page with a browser, it will send the cookie too.

What are sessions in PHP?

Sessions are a simple way to store data for individual users against a unique session ID.

What are different types of loops in PHP?

The different types of loops in PHP are:
For loop: loops through block of code specified by loop statement.
While loop: loops through block of code if and as long specified condition is true.
do...while − loops through a block of code once, and then repeats the loop as long as a special condition is true.
foreach − loops through a block of code for each element in an array.

How to concatenate different strings in PHP?

Concatenation in PHP can be done using dot (.) operator like:


What is the function of explode and implode?

explode is used to split a string by a string
Example:

$pizza = "piece1 piece2 piece3 piece4 piece5 piece6";
$pieces = explode(" ", $pizza);
echo $pieces[0]; // piece1
echo $pieces[1]; // piece2

implode is used to Join array elements with a string
Example:

What is number_format() used for?

number_format() is used to format number with grouped thousands.
Example:

How to connect to mysql in PHP?

Example:

setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo "Connected successfully"; 
    }
catch(PDOException $e)
    {
    echo "Connection failed: " . $e->getMessage();
    }
 

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

What is the difference between mysql_fetch_array() and mysql_fetch_assoc() ?

Mysql_fetch_array() fetch a result row as an associative array, a numeric array and also it fetches by both associative & numeric array. While, mysql_fetch_assoc() fetch a result row as an associative array.

What is the use of "enctype" attribute in a html form?

The enctype attribute specifies how the form-data should be encoded when submitting it to the server.

How will you get form data sent with GET method in PHP?

We can get form data using GET associative array. Example:

$_GET[‘title’]

How will you get form data sent with POST method in PHP?

We can get form data using POST associative array. Example:

$_POST[‘title’]

How to get IP Address of used using PHP?
We can get IP Address of user using below code:

$_SERVER['REMOTE_ADDR']

How to retrieve user browser’s detail using PHP?

Using PHP native variable:

$_SERVER['HTTP_USER_AGENT']

What if the function of file_get_contents() in PHP?

The file_get_contents() reads a file into a string. This function is the preferred way to read the contents of a file into a string. Because it will use memory mapping techniques, if this is supported by the server, to enhance performance.

How to generate random string in PHP?

Random string in PHP can be generated using rand() function. Example:

rand(11111, 9999); //one output 23413

How to make redirect in PHP?

We can redirect in page using header() function.
Example:

header('Location: '.$newURL);
die();

What is the difference between single quoted string and double quoted string in PHP?

The simplest way to specify a string is to enclose it in single quotes. Single quote is generally faster, and everything quoted inside treated as plain string.

echo 'Start with a simple string';
echo 'String\'s apostrophe';
echo 'String with a php variable'.$name;

Use double quotes in PHP to avoid having to use the period to separate code (Note: Use curly braces {} to include variables if you do not want to use concatenation (.) operator) in string.

echo "Start with a simple string";
echo "String's apostrophe";
echo "String with a php variable {$name}";

Explain Object Oriented Programming?

Object Oriented Programming. Object Oriented programming is a programming style that is associated with the concept of Class, Objects and various other concepts revolving around these two, like Inheritance, Polymorphism, Abstraction, Encapsulation etc.

Explain MVC to a 5 years old child.

Essentially MVC is about responsibility.
I would tell the kid that there are three people working in a shop. One is responsible for taking customers’ requests, one is responsible for fetching the item the customer wants and the third one is responsible for wrapping it in a nice packaging and giving it back to the customer.

What are the different types of array sorting in PHP?

sort() - sort arrays in ascending order
rsort() - sort arrays in descending order
asort() - sort associative arrays in ascending order, according to the value
ksort() - sort associative arrays in ascending order, according to the key
arsort() - sort associative arrays in descending order, according to the value
krsort() - sort associative arrays in descending order, according to the key

What is the difference between unset() and unlink()?

unset() is used to make a variable undefined. Unlink() is used to delete files.

How to access SESSION variable?
Session variable can be accessed using associative array like:

$_SESSION[‘my_variable’]

How will you destroy SESSION?

A PHP session can be destroyed by session_destroy() function.

How to send email using PHP?

In PHP, mail can be send using mail() function.
Example:

mail ( string $to , string $subject , string $message [, mixed $additional_headers [, string $additional_parameters ]] )

Where,
to - Receiver, or receivers of the mail.
Subject - Subject of the email to be sent.
Message - Message to be sent.
additional_headers (optional) - String or array to be inserted at the end of the email header.

What is the function of urlencode() and urldecode() in PHP?

urlencode() returns the URL encoded version of the given string. URL coding converts special characters into % signs followed by two hex digits. For example: urlencode(“10.00%”) will return “10%2E00%25”. URL encoded strings are safe to be used as part of URLs.
urldecode() returns the URL decoded version of the given string.

How can you get current date time in PHP?

In PHP, we can get date time using date() function.
Example:

$date = date('Y-m-d H:i:s');
// Or:
$date = date('Y/m/d H:i:s');

// This would return the date in the following formats respectively:
$date = '2018-03-06 17:33:07';
// Or
$date = '2018/03/06 17:33:07';

What is SQL Injection?

SQL injection is a code injection technique that might destroy your database. SQL injection is one of the most common web hacking techniques. SQL injection is the placement of malicious code in SQL statements, via web page input.

How can you prevent SQL injection?

It can be prevented using:
Using PDO (for any supported database driver):
Using MySQLi (for MySQL):
Using Web Application Firewall (WAF)

Is it possible to submit a form without submit button in html form?
Yes.

conclusion

There are most commonly asked PHP interview question. Moreover, you are likely to get latest features of PHP 7. To know more details about it, you can read this article "New Features In PHP 7 : Explained"

Read More Articles

3 COMMENTS

  1. My partner and I stumbled over here coming from a different web address and
    thought I might as well check things out. I like what
    I see so now i’m following you. Look forward to finding out about your web page again.

  2. I’m truly enjoying the design and layout of your blog.
    It’s a very easy on the eyes which makes it much more enjoyable for me to come here and visit more
    often. Did you hire out a developer to create your theme?
    Excellent work!

LEAVE A REPLY

Please enter your comment!
Please enter your name here