PHP Interview Questions

By | April 7, 2019

The more popular the PHP is the more opportunities to work with it. The features this language supports makes it unique from others. PHP is a scripting language which is easy to learn and simple to use for all level of programmers. PHP supports a large community who are ready to help you whenever you have any doubt related to PHP code. PHP also supports cross-platform that allows it to use anywhere and this is also an important reason for its popularity.

There are so many big companies in IT industries that are using PHP for their platforms, so you may have an idea that what’s the scope of career in PHP for future. But before joining any company, you must have to face the interview round and there interviewer will check your conceptual knowledge about PHP or more if you are not fresher. For that, you must be well prepared for the Interview. To help you in that, here we have prepared a list of most frequently asked interview questions for PHP. Have a look at it before your interview.

PHP Interview Questions

Question: What is PHP?
Answer: PHP aka Hypertext Preprocessor is a general purpose, an open-source, scripting language which runs on server-side that is mainly designed to use for web development. PHP scripts execute on the server. PHP scripts are dynamically typed and generate web pages efficiently and quickly. PHP is freely available to download and use. Its syntax is mostly borrowed from object-oriented programmings such as Java and Perl.

Question: What is the full form of PHP?
Answer: Hypertext Preprocessor

Question: What does PEAR stand for?
Answer: “PHP Extension and Application Repository”. PEAR extends PHP to provide a higher level of programming for web developers. PEAR also offers a CLI (Command Line Interface) that can be used to automatically install “packages”.

Question: Which programming language does PHP resemble?
Answer: PHP syntax derived from Perl and C.

Question: What are the differences between PHP & JAVA?
Answer:

PHP Java
Server Side Scripting Language High-Level General Purpose Programming
Dynamically Typed Statically Typed
It doesn’t have a rich set of APIs It supports a rich set of APIs

Question: Which version of PHP is actually used?
Answer: Version 7.1 or 7.2 is the recommended version of PHP which is mostly used.

Question: What is the command to execute a PHP script from the command line?
Answer: Use the PHP command line interface (CLI) and write the exact file name with .php extension to be executed as follows:

php script.php

Question: Which function is used to encrypt the password in PHP?
Answer: To create one-way encryption, crypt () function is used in PHP. This function is defined as crypt (input_string, salt), where input_string is defined as the string that has to be encrypted and salt represents an optional parameter. DES is used for encryption in PHP. The format is as follows:

<?php
$password = crypt('intellipaat');
print $password . "is the encrypted version of intellipaat";
?>

Question: Write a Hello world program using the variable?
Answer:

<html>
<head><title>Hello World Script using Variable</title></head>
<body>
<?php
$salutation = “Hello World!”;
echo “<p>$salutation</p>”;
?>
</body>
</html>

[su_button url=”http://www.techgeekbuzz.com/how-to-learn-php/” background=”#1263c6″ center=”yes” radius=”round”]How to Learn PHP?[/su_button]

Question: Define the Zend Engine?
Answer:

  • Zend Engine is a runtime engine and compiler which was used internally by PHP. Zend have opcodes that executed and the HTML generated is sent to the client. PHP Scripts are loaded into memory and compiled into opcodes.
  • The Zend Engine provides many features such as memory and resource management and many other standard services for the PHP language. Its reliability, performance, and extensibility played a significant role in PHP’s increasing popularity.

Question: Write the command to run the interactive PHP shell from the CLI (command line interface)?
Answer:

Write the following code in the CLI:

php -a

Question: How to display the output directly to the browser with the use of PHP?
Answer: Use the special tags <?= and ?> to display the output directly to the browser.

Question: PHP supports multiple inheritance or not?
Answer: No, PHP supports only single inheritance and only one single class is used to extend a class using the keyword ‘extended’.

Question: Tell me the main difference between PHP 4 and PHP 5?
Answer: PHP 5 offers additional features of OOP (Object Oriented Programming) than PHP 4.

Question: What do you understand a final class and a final method?
Answer:‘final’ keyword is introduced in PHP5.

A final class cannot be extended and a final method cannot be overridden.

Question: Define Echo vs print statement.
Answer:

  • echo() and print() are language constructs in PHP, which are used for output strings. The speed of both statements is almost the same and good enough.
  • print() cannot take multiple expressions, whereas echo() can take multiple expressions.
  • Print return true or false that means as success or failure, whereas echo doesn’t return true or false.

Question: What are the different types of errors in PHP?
Answer: Warnings, Notices, and Fatal errors are the types of errors in PHP.

Warnings

Warnings occurs when calling include() a file, which does not exist. By default, errors displayed to the user.

Notices

Notices is for non-critical errors. For example: accessing a variable that has not yet been defined. These type of small errors are not displayed to the user but whenever it’s required, you can change this default behavior.

Fatal errors

Fatal errors are critical errors. Example: calling a non-existent class or function. These errors cause the immediate termination of the script.

Question: What are the function htmlentities and its importance?
Answer: As the name suggests, the htmlentities() function is used to converts characters to HTML entities.

Question: How PHP and HTML are connected with each other?
Answer: HTML can be generated through PHP scripts, and it is also possible to pass pieces of information from HTML to PHP.

Question: Which operator is used to compare the objects in PHP?
Answer: The ‘==’ operator is used to test if two objects are instanced from the same class with the same attributes and equal values. If two objects are referring to the same instance of the same class then we can test by the use of the identity operator ‘===’.

Question: What is the library that is used for pdf in PHP?
Answer: The PDFlib library Version 6 is used for the PDF functions in PHP to create PDF files.

There is also an FPDF which is a PHP class and allows generating PDF files with pure PHP (without using the PDFlib library).

FPDF stands for Free PDF used for any kind of usage and also you can modify it to your needs.

Question: Brief of the new features of PHP7?
Answer:

  • Improved performance of Zend Engine 3
  • 64-bit integer support on Windows
  • AST-based compilation process
  • Newly added Closure::call()
  • Declarations of the return type
  • Unicode codepoint escape syntax
  • integer, float, string, and boolean declarations.

Question: Which type of operation is required to pass the values through a form or an URL?
Answer: To pass values through a form or an URL, we need to encode and decode them using htmlspecialchars() and urlencode().

Question: What is the full form and meaning of MIME?
Answer: MIME – Multi-purpose Internet Mail Extensions

MIME is the standard way of classifying file types over the Internet.

A MIME has two parts: a type and a subtype and they are separated by a slash (/).

[su_button url=”http://www.techgeekbuzz.com/python-vs-php/” background=”#1263c6″ center=”yes” radius=”round”]Python vs PHP[/su_button]

Question: How to execute a PHP script using the command line?
Answer: SAPI (Server Application Programming Interface) is used to execute a PHP script. Using SAPI command, the PHP code can be passed to execute directly.

Ex.:

Php –r ‘print_r(get_defined_constanrs());’

In a shell, php –v will display whether the SAPI is CLI or CGI

Question: Explain the htaccess and its use?
Answer: htaccess files are configuration files of Apache Server. These files provide a way to make configuration changes on a per-directory basis.

These .htaccess files are used to change the features and functionality of Apache web server.

  • htaccess file is used for URL rewrite.
  • htaccess file can restrict some IP addresses.
  • These files are used to protect the site password.

*On the restricted ip addresses, the site will not open.

Question: How to use image function in PHP?
Answer: A library named GD library is used to execute image functions.

Question: What is meant by PEAR in PHP?
Answer: PEAR is used for “PHP Extension and Application Repository”. PEAR is used to provide:

  • PEAR is a system to use for package maintenance and code distribution.
  • A structured library with open-sourced code for PHP users.
  • A different style for writing PHP code.
  • PHP Extension Community Library (PECL)
  • PHP Foundation Classes (PFC)
  • A mailing lists, download mirrors, and website to support the PEAR community.

Question: What’s the output buffering in PHP?
Answer: Output buffering in PHP is used to buffer a script’s output. This buffer executes before returning it to the client. Sending to output is by choice because Output buffers are stackable.

Question: Explain session in PHP.
Answer: Whenever a new user log in to the application, they must need to enter details are usually stored in a session variable. This details is then available to all pages in one application. PHP Sessions work using a unique id for each visitor.

Question: How to display text using a PHP script?
Answer: Two methods are available to do this:

<!--?php echo "Method 1"; print "Method 2"; ?-->

Question: How to find the number of days between two given dates in PHP?
Answer: You can use the code as shown below to find the start date and end date

$date2= strotime($end_date);
$date_diff = (($date1)- ($date2)) / (60*60*24)

Question: What is Memcache?
Answer: Memcache is a technology which is used to cache objects in memory to increase the speed of your web application so that they can get to them really fast. It is used by sites such as NowPublic.com, Digg.com and Facebook.com. Also, it is widely recognized as an essential ingredient in scaling any LAMP.

Question: Give that statements that are used to connect PHP and MySQL.
Answer: Below statements are used to connect PHP with MySQL:

<?
$conn = mysql_connect('localhost');
echo $conn;
?>

This statement gives the resource of the localhost. Other different ways to connect to the database and they are as follows:

<?
mysql_connect('db.domain.com:33306','root','user');
mysql_connect('localhost:/tmp/mysql.sock');
mysql_connect('localhost','rasmus','foobar',
true,MYSQL_CLIENT_SSL|MYSQL_CLIENT_COMPRESS);
?>

Question: How can we execute a PHP script using command line?
Answer: Open the PHP CLI (Command Line Interface) program.

Write the PHP script file name. For ex. “php myScript.php”, “php” is the command to invoke the CLI program.

*Note- If your PHP script was written for the Web CGI interface, it may not execute in command line environment.

Question: Please write the differences between $message and $$message?
Answer:

  • $$message can be used to store variable of a variable, while $message is used to store variable data.
  • Data stored in $$message can be changed dynamically, while data stored in $message is fixed.
  • $$message is a variable of another variable and $message is a variable.

Example

$Message = "YOU";
$you= "Me";
echo $message //Output:- you
echo $$message //output :-Me

Question: Why PHP is called a Scripting language?
Answer: PHP is a general-purpose language, which is used to write scripts. Scripts are the files consist of a set of instructions. Scripts are used to tell the computer to execute the file and print the output on the screen.

Question: How to display information of a variable that is readable by a human in PHP?
Answer: We use print_r() to display a human-readable result.

Question: Write the difference between PHP and JavaScript?
Answer: PHP is a server-side scripting language, which means that it is not allowed to interact directly with the user.

Whereas, JavaScript is a client-side scripting language, that makes possible to directly interact with the user.

Question: What is the use of ODBC in PHP?
Answer: ODBC stands for “Open Database connectivity”, which allows the user to communicate with other databases like IBM DB2 and Access. PHP supports many databases like Microsft SQL Server, dBase, Oracle, etc. Other databases like filePro, FrontBase, and InterBase can be used with ODBC connectivity.

Question: The function file_get_contents() is useful for?
Answer: Function file_get_contents() is used to read a file and store it in a string variable.

Question: What’s difference between require(), require_once(), and include()?
Answer:

  • require() is used to include and evaluate a specific file. If the file is not found, it gives a Fatal Error.
  • require_once() only includes the file which is being included once. It is recommended for the files where you have lots of functions stored.
  • include() is used to include the file, and if the file is not found, it gives a warning to the user.

Question: PHP is an open source language, therefore is there any support available to use it?
Answer: PHP is an open source language, and there is a vast community of developers and programmers to support.

Question: How to run PHP?
Answer: The steps are as follows:

  • Set up the web environment.
  • Set up the web servers. Mostly used web server is Apaches. IIS (Internet information server) provided by Microsoft also can be used.
  • Install the Web server and PHP
  • Update and administer the system for changes.

Question: Tell me the function which gives us the number of affected entries by a query?
Answer: mysqli_affected_rows() return the number of entries affected by an SQL query.

Question: Difference between echo, print, and printf()?
Answer:

  • Echo is used to print out a string and shows the content of the message written.
  • print is a construct, it returns TRUE and FALSE as per the output.
  • Printf() is a function and allows the string output to be formatted. It is the slowest medium to print the data out.

Question: How to check whether the value of a given variable is a number?
Answer: The function is_numeric() is used to check whether it is a number or not.

Question: What is the use of unlink() function?
Answer: The unlink() function is for file system handling. It deletes the file given as entry.

Question: Why IDE is recommended in PHP programming?
Answer: IDE (Integrated Development Environment) is a framework for developing applications. The features of IDE are:

  • Debugging
  • Preview
  • Testing
  • FTP
  • Project management
  • Backups

Question: Difference between PHP statement and PHP script?
Answer: PHP statements are a set of instructions used to tell PHP to perform an action.

PHP script consists of a series of PHP statements that is used for execution.

Example:

PHP statement: echo “Hi”;
PHP script:
if (time = midnight)
{
put on pajamas;
brush teeth;
go to bed;
}
Category: Interview Questions PHP Tags: , ,

About Paridhi Joshi

Paridhi Joshi is an expert web content manager with significant experience in content creation. Professionally she is dedicated to staying up to date with the latest trends and technologies in content writing and committed to bringing state-of-the-art web approaches to the workplace. She is an efficient multi-tasker who can complete multiple projects under strict deadlines.

Leave a Reply

Your email address will not be published. Required fields are marked *