Core PHP interview questions

This blog is all about core PHP interview questions. It is prepared by real-time PHP developer and working in Big MNC company and 10+ years of experience also. Basically, all the core PHP interview questions are unique.

Table of Contents

What is PHP?

  1. Abbreviations of PHP is “PHP: Hypertext Preprocessor”.
  2. Old Name was Personal Home page;
  3. Its scripting language, It not base language like C, C++, so it depends upon other languages like HTML.
  4. It’s used to make e-commerce website, Blogging site, content management website, Database driven Website.
  5. Its license is free for all means open source.

Who is known as the father of PHP?

Rasmus Lerdorf in 1994

What are the types of framework and CMS Supports in PHP?

PHP is very vast like Java and .net. It also Provides a lot of CMS and frameworks for easy and better development of software and Websites. In PHP CMS basically used for an e-commerce website and blog posting website and frameworks used for dynamic data-driven secure application development and API development.

CMS are listed below Built with by PHP language
1. WordPress
2. Drupal
3. Opencart
4. Zencart
5. Magento
6. Joomla

Frameworks are listed below built with PHP language
1. Zend Framework
2. Codeigniter
3. Cakephp
4. Symphony
5. Yii2. etc.

Write sample PHP  Syntax

[SyntaxHL]<!DOCTYPE html>
<html>
<head>
<title>PHP Sample Code</title>
</head>
<body>
<?php echo ‘Hello Dude’; ?>
</body>
</html>[/SyntaxHL]

Why  “echo” and “print” are different from each other in PHP?

Echo in PHP

  • echo has a void return type.
  • echo can take multiple parameters separated by comma.
  • echo is faster than print.

 

 

Print in PHP

  • print has a return value of 1 so it can be used in expressions.
  • print cannot take multiple parameters.
  • print is slower than echo.

 

How to include a file to a PHP page?

We can include a file using “include() ” or “require()” function with file path as its parameter.

 

What’s the difference between include and require?

In PHP require() and include() are two in-built functions, these function generally used for merge a php file in a another  PHP webpage.

But if include() function is used in a and merged file does not exist then it will show warning message but execution not stop. If require() function is used to merge a file and merged file is missing then it will show fatal error and it will stop the execution.

What’s the difference between include and include_once()?

Include: -Include is used to include files more than once in a single PHP script. You can include a file as many times you want.
include(“my_file_name.php”);
Include Once:-Include once include a file only one time in PHP script. if someone includes again the same file in the same page it will be ignored by this function.
include_once(“my_file_name.php”);

How to set cookies in PHP?

 

How to Retrieve a Cookie Value in PHP?

Generally, if any web browser has the cookie, we can get this cookie by PHP function $_COOKIE[];

 

How to Delete a Cookie Value in PHP?

Cookies can not delete directly, We can only Expire it By passing Past time.

 

How to create a session? How to set a value in session ? How to Remove data from a session?

 

How to set an execution time for PHP script?

For infinite execution time PHP script, we can set in the main file or beginning of the PHP script

 

 

// Here We can set eg. 300 sec or 500 sec as per requirement.[/SyntaxHL]
More ever we can set max execution time in directives like  php.ini file.
The maximum execution time of each script, in seconds

NB: Any changes in php.ini, the server must be restart.

What is PHP session_start() and session_destroy() function?

PHP session_start() function is used to start the session. It starts a new or resumes the existing session. It returns the existing session if the session is created already. If the session is not available, it creates and returns new sessions.
PHP session_destroy() function is used to destroy all session variables completely.

What are string functions in PHP?

PHP string functions are used to manipulate string values.

 

What are different types of Print Functions available in PHP?

For Print or echo the data in web page PHP have so many functions. these are listed below.

 

What does the unlink() function mean?

The unlink() function is responsible for file system handling. It simply deletes the file given as entry.

What does the unset() function mean?

The unset() function is responsible for variable management. It will make a variable undefined.

How can you enable error reporting in PHP?

Enable error in PHP is very important for the developer during development to find the bugs or debugging.

Here if we go for a change in global then, we can set in php.ini file.

 

What are the different errors in PHP and what are the differences between them?

As of now, PHP has 3 types of error.

  1. Warning: Here If suppose you include a file let “somefile.php” but a file is not there then this errors are shown to users. It’s very important, but it does not stop the execution of the PHP script.
  2. Notice: Here If suppose you access a variable but this variable is not declared, in this case, an error will show to users. But It won’t stop the execution of the PHP script. It not a critical error.
  3. Error: Here If suppose Instantiating an object of a class which does not exist or you are calling a function which does not exist then, in this case, it will show errors. These errors result in termination of the script immediately, It’s very critical, It should be to fix to proceed further. More ever Twelve different error types are used to represent these variations internally.

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

The header() function sends a raw HTTP header to a client browser. This function must be called before sending the actual output. For example, You do not print any HTML element before using this function.

How to redirect a page in PHP?

In PHP header() is the function used for redirect the page to another page

 

How to collect  IP address from an HTTP request ? 

 

How to collect  IP address of the Web server in php ? 

 

What is $_GLOBAL ?

It is an associative array which contains references to all variables currently defined in the global scope of the script.

What is the difference between == and === operator in PHP ?

Equal operator:
$x == $y (if $x is equal to $y it will return True);
Identical operator:
$x === $y (if $x is equal to $y, and they are of the same data type then it will return true.)

What is default session time and path in PHP.

Default session time in PHP is 1440 seconds (24 minutes).

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 difference between var_dump() and print_r() ?

var_dump() will display all the information of a variable including keys values and types.
print_r() display the keys and values only in a human-readable format.

What is the importance of the “action” attribute in an HTML form?

The action attribute determines where to send the form data in the form of submission.

How do you define a constant in PHP?

Using define() directive, like define (“MYCONSTANT”,”100″)

What is the use of “ksort” in php?

It is used for sort an array by key in reverse order.

What is the difference between $message and $$message?

output:
anyvar
Hello World

What is the use of the function htmlentities?

htmlentities Convert all applicable characters to HTML entities This function is identical to htmlspecialchars() in all ways, except with htmlentities(), all characters which have HTML character entity equivalents are translated into these entities.

How to find the position of the first occurrence of a substring in a string?

strpos() is used to find the position of the first occurrence of a substring in a string
The strpos() function is case-sensitive.

 

How can we get IP address of a client in PHP?

 

Explain PHP explode() function.

 

Explain PHP split() function.

The PHP split() function splits string into an array by regular expression.

Output:

ram
111
sam
222

NB- This function is deprecated in PHP 7.X, Now they are using below functions instead.

  • preg_split()
  • explode()
  • str_split()

How many types of array are there in PHP?

There are three types of array in PHP:
Indexed array
Associative array
Multidimensional array

What is the difference between indexed and associative array?

The indexed array holds elements in an indexed form which is represented by number starting from 0 and incremented by 1. For example:

The associative array holds elements with name. For example:

 

How can we connect to a MySQL database from a PHP script?

To connect  MySQL database, we must use mysql_connect() function as follows:

 

To connect  MySQLi database, we must use mysqli_connect() function as follows:

 

What is the function persistent connection use full for?

mysql_pconnect() ensure a persistent connection to the database, it makes connection do not close when the the PHP script ends.But In mysql_connect() each time we have to connect after the script ends.

How the result set of Mysql be handled in PHP?

These are below funcions are used for result set in mysql.
mysql_fetch_array();
mysql_fetch_assoc();
mysql_fetch_object();
mysql_fetch_row();

Advertisements