PHP Interview Question Answer

What is PHP?

PHP is a general-purpose scripting language especially suited to web development. PHP stands for Hypertext Preprocessor.

It supports many databases like MySQL, Oracle, Sybase, Solid, PostgreSQL, generic ODBC etc.

What is a PHP File?

PHP file have .php file extension which represent plain-text file that contains the source code written in the PHP.

Generally, PHP files contain collection of text, HTML, CSS, JavaScript, and PHP code. The code and contents of this file is executed on the server, and the result is returned to the browser as plain HTML

What are the common uses of PHP?

Basically, PHP is used for created dynamical webpages which means, the some or all of the content of web page is created using database. Although, here are some common uses of PHP :

  • It can handle forms, i.e. gather data from input, save data to a file or database, even email that data return to user.
  • PHP can create, open, read, write, delete, and close files on the server
  • You can add, delete, modify data within your database with the help of PHP.
  • Access cookies variables and set cookies to store data on client computer and use it.
  • PHP can encrypt data
  • PHP can be used to control user-access. i.e. give permission to user for specific web pages.

What is PEAR in PHP?

PEAR is a framework and repository for reusable PHP components. PEAR stands for PHP Extension and Application Repository. It contains all types of PHP code snippets and libraries. It also provides a command line interface to install “packages” automatically.


Is PHP a case sensitive language?

PHP is partially case sensitive. The variable names are case-sensitive but function names are not. If you define the function name in lowercase and call them in uppercase, it will still work. User-defined functions are not case sensitive but the rest of the language is case-sensitive.

Who is known as the father of PHP?

Rasmus Lerdorf is known as father of PHP.

What was the old name of PHP?

The old name of PHP was Personal Home Page.

What is the difference between static and dynamic websites?

The content of Static Website is literally static and doesn’t change in response to user actions. A static website is usually created with HTML and CSS.

Compared to static websites, which are purely informational, a dynamic website is more functional. It allows users to interact with the information that is listed on the page. Of course, that requires utilizing more than just HTML code.

What is the name of scripting engine in PHP?

The Zend Engine is the open source scripting engine that interprets the PHP programming language. It was originally developed by Andi Gutmans and Zeev Suraski while they were students at the Technion – Israel Institute of Technology.

What is main difference between PHP4 and PHP5?

PHP4 doesn't support oops concept and it uses Zend Engine 1.

PHP5 supports oops concept and it uses Zend Engine 2.


How to execute a PHP script from the command line?

To execute a PHP script, use the PHP Command Line Interface (CLI) and specify the file name of the script in the following way:

    	php filename.php

What is the meaning of "escaping to PHP"?

The PHP parsing engine needs a way to differentiate PHP code from other elements in the page. The mechanism for doing so is known as ‘escaping to PHP’. Escaping a string means to reduce ambiguity in quotes used in that string.

Why to use PHP?

The popularity of PHP is the logical result of its numerous advantages, all of which make it a powerful and effective development tool. Below is the short list of reasons why PHP is a great choice for your web app, which will be subsequently described in more detail.

  • A large base of reference and educational materials
  • Better loading speed of websites
  • Supports majority databases
  • Great synergy with HTML
  • Excellent flexibility and combinability
  • Excellent Community Support
  • Free to use

What are the different types of PHP variables?

There are 8 data types in PHP which you can use to construct the variables:

  1. Integers− are whole numbers, without a decimal point, like 1234.
  2. Doubles − are floating-point numbers, like 72.52.
  3. Booleans − have only two possible values either true or false which represent 0 and 1.
  4. NULL − is a special type that only has one value: NULL.
  5. Strings − are sequences of characters, like 'name'.
  6. Arrays − are named and indexed collections of other values.
  7. Objects − are instances of programmer-defined classes, which can package up both other kinds of values and functions that are specific to the class.
  8. Resources − are special variables that hold references to resources external to PHP.

What are the rules for naming a PHP variable?

  • A variable starts with the $ sign, followed by the name of the variable
  • A variable name must start with a letter or the underscore character
  • A variable name cannot start with a number
  • Variable names are case-sensitive ($age and $AGE are two different variables)
  • A variable name can only contain alpha-numeric characters and underscores.


What is NULL?

NULL is a special data type which can have only one value. A variable of data type NULL is a variable that has no value assigned to it. It can be assigned as follows:

    	$var = NULL;

How to define constant in PHP?

A constant is an identifier (name) for a simple value. The value cannot be changed during the script. Unlike variables, constants are automatically global across the entire script.

define("Message", "Welcome to CodeWolfy!");

What is the purpose of constant() function?

The constant() function will return the value of the constant. This is useful when you want to retrieve value of a constant.

    define("Message", "Welcome To CodeWolfy!"); 
    echo Message; 
    echo constant("Message"); 

Which programming language does PHP resemble to?

PHP has resemble its syntax from Perl and C.

What is "echo" in PHP?

PHP echo output one or more string. It is a language construct not a function. So the use of parentheses is not required. But if you want to pass more than one parameter to echo, the use of parentheses is required.

    	echo "Welcome to Codewolfy";

What is "print" in PHP?

Print is language construct in PHP. Which print one string into output. Following is example of Print :

    	print "Welcome to CodeWolfy!";


What is the difference between echo and print in PHP?

Echo can output one or more string but print can only output one string and always returns 1.

Echo is faster than print because it does not return any value.

What are the ways to define a constant in PHP?

In PHP, Constants can be defined in two ways :

  1. Using define() function
  2. Using const() function

What are magic constants in PHP?

PHP magic constants are predefined constants, which change based on their use. They start with a double underscore (__) and end with a double underscore (__). Like __LINE__

    echo "<h3>Example for __LINE__</h3>"; 
    echo "You are at line number " . __LINE__ . "<br><br>";  
   //It will print "You are at line number 3"

What are the different loops in PHP?

There is three type of loops in PHP :

  1. For loop
  2. While loop
  3. do-while loop
  4. for each loop