Top PHP Interview Questions and Answers (2023)

What is PHP?

PHP is very popular server-side scripting language used for developing web applications. PHP stands fro Hypertext Preprocessor.


Why do you use PHP?

There are many advantages of using PHP.

  1. First of all, PHP is completely free to use.
  2. It supports multiple databases.
  3. Simple Installation and Cross platform availability
  4. The most commonly used database MySQL used with PHP is also free to use.
  5. Many frameworks are used for web applications in PHP, like CodeIgniter, CakePHP, Symphony, Laravel etc.
  6. It has many CMS are used in PHP like, WordPress, Magento, OpenCart, Drupal, Joomla etc.
  7. Lots of libraries

What was the old name of PHP?

Personal Home Page.


Which programming languages does the PHP resemble to?

PHP has similar syntax to Perl and C languages.


What is “echo” in PHP?

PHP echo is a language construct which output one or more strings. Echo is not a function. So, parenthesis is not required in echo.

For example –

<?php
echo "Welcome to Share Query";
?>

What are the difference between echo and print?

echoprint
This is a type of output string.This is also a type of output string.
Not written with parenthesis.print can be or not be written with parenthesis.
echo can output one or more strings.It can output only one string at at time and returns 1.
echo does not return any value. so, it is faster than print.print is slower than echo.
echo ($arg1[,$arg2…])print($arg)


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

  • unset() sets a variable to “undefined”.
  • unlink() deletes a file that we pass to it from the file system.

How many types of errors in PHP?

In PHP, there are mainly three types of errors:

  1. Notices
  2. Warnings
  3. Fatal
  1. Notices – This type of error is simple and non-critical errors. Occurred during the script execution.
    For example – accessing an undefined variable will throw notice error.
  2. Warnings – This type of errors are important than notices. However, scripts continue the execution.
    For example – using include() function to include a file if file doesn’t exist, it will show warnings.
  3. Fatal – This type of errors is the most critical errors. Fatal errors causes termination of scripts execution.
    For example – using require() function to access non-existent file will show fatal errors.

What is the difference between GET and POST?

GETPOST
It requests data from a specified resource.It submits data to be processed to a specified resource.
Data sent by the GET method are displayed in the URL.Data sent by the POST method not visible in the URL.
GET requests can be cached.POST requests are never cached.
GET requests remain in the browser history.POST requests do not remain in the browser history.
GET request can be bookmarked.POST request cannot be bookmarked.
GET request should not be used while sending sensitive information.POST method should be used while sending sensitive information like passwords etc.
GET request has length restrictions.POST request has no length restrictions.

Is PHP a case-sensitive language?

PHP is partially case-sensitive language. PHP is case sensitive in regards to function only but it should not include the function(s) which is defined by the user.
– If you define a function name in lowercase and call it in uppercase, it will work.
– Variable names in PHP are case-sensitive.
for example –

function display(){
echo "Hello World";
}

OUTPUTS:
display(); //Hello World
Display(); //Hello World
DISPLAY(); //Hello World
function display(){
$x = "Hello World";
echo $X;
}
OUTPUTS:
display(); //Notice: Undefined variable: X

In all cases the output will be the same. So, Function names in PHP is case insensitive whereas, variable names are case sensitive.


How do you run a PHP script from command line?

In order to run a PHP script in command line you have to put the filename as:
test.php


What are the uses of explode() and implode() functions?

Explode() function is used to split a string into the array. And Implode() function is used to make a string by combining the array elements.

For example –

<?php
$str = "I like learning PHP";
print_r (explode(" ",$str));
$strarr = array('PHP','Wordpress','OpenCart');
echo "<br>";
echo implode(" ",$strarr);
?>
Output:
Array ( [0] => I [1] => like [2] => learning [3] => PHP ) 
PHP WordPress OpenCart

What is the main difference between PHP 4 and PHP 5?

PHP 4PHP 5
It was powered by Zend engine 1.0Powered by Zend engine II.
Constructor have same name as the Class nameIn PHP 5, Constructor is declared as __construct and Destructor as __destruct().
Everything was pass by value in PHP 4All objects are passed by reference in PHP 5
Allow to declare an abstract class.
Allow to declare a class and method as final.
PHP 5 has 3 level of visibility: Public, Protected and Private.
PHP 5 introduces the concept of exception handling.
Interfaces in order to implement the concept of multiple inheritance.
PHP 5 introduces magic methods (_call, _get,_set)

What are the data types in PHP?

PHP supports following data types

  • String
  • Integer
  • Float
  • Array
  • Object
  • Boolean
  • NULL
  • Resource

How to turn on to get useful error messages in PHP?

By enabling PHP error reporting, you can see a helpful error messages. It is very good practices to check error messages during debugging process.

To Turn on error reporting message:

Open php.ini file and find the “display_errors”.
Check if display_errors = “on” or declare “ini_set(‘display_errors’, 1)” in your script.

Then, include “error_reporting(E_ALL)” in your script to display all types of errors messages during execution.


How to get the number of elements in an array?

By using count() function, you can get the number of elements present in an array.
for example –

<?php
$numbers = array(1,3,4,5,2,3,5,6,6,7,7,7,7,7,74,4,56,3,34,33);
echo count($numbers);
?>
Output:
20

How can we get the IP address of the client?

$_SERVER[“REMOTE_ADDR”] is used to get the IP address of the client.


What’s the difference between the include() and require() functions?

Both functions help in including a file. But on require() function, if file not found it will terminate the process while the using include statement PHP will pass and jump to next step of the execution if file is not found.


What is the use of header() function in PHP?

header() function is an inbuilt function in PHP. header() function is used to send a raw HTTP header before any other output has been sent.
– It must be called before sending the any other actual output.


What does isset() function?

Ths isset() function is used to check if the variable is defined or not null.


What is the array in PHP?

An array is used to store multiple value of similar types.


How many types of array are there in PHP?

There are total 3 types of array in PHP

  1. Indexed array
  2. Associative array
  3. Multidimensional array

Explain the string functions in PHP?

String functions in php are part of the PHP core. String functions help in manipulating the strings.

Some of the string functions are :

  • strtolower() – Converts a string to lowercase letters
  • strtoupper() – Converts a string to uppercase letters
  • ucfirst() – Converts the first character of a string to uppercase
  • ucwords() – Converts the first character of each word in a string to uppercase
  • strrev() – Reverses a string
  • strstr() – Finds the first occurrence of a string inside another string
  • substr() – Returns a part of a string
  • trim() – Removes whitespace or other characters from both sides of a string
Wordpress Social Share Plugin powered by Ultimatelysocial
Wordpress Social Share Plugin powered by Ultimatelysocial