PHP Interview Questions

There is given PHP interview questions and answers that have been asked in many companies. Let's see the list of top PHP interview questions.

How to add 301 redirects in PHP?

You can add 301 redirect in PHP by adding below code snippet in your file.

header("HTTP/1.1 301 Moved Permanently"); 
header("Location: /option-a"); 
exit();
.

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

$_SERVER[“REMOTE_ADDR”];
.

Explain PHP split() function.

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

What are Traits?

Traits are a mechanism that allows you to create reusable code in languages like PHP where multiple inheritance is not supported. A Trait cannot be instantiated on its own.

It’s important that a developer knows the powerful features of the language (s)he is working on, and Trait is one of such features.

Example of Traits

trait users{
	function getUserType() {}
	function getUserDescription() {}
	function getUserDelete() {}
}

class  ezcReflectionMethod extends  ReflectionMethod {
	user users;
}

class ezcReflectionFunction extends  ezcReflectionFunction {
	user users;
}
.

What is namespaces in PHP?

PHP Namespaces provide a way of grouping related classes, interfaces, functions and constants.

# define namespace and class in namespace
namespace Modules\Admin\;
class CityController {
}
# include the class using namesapce
use Modules\Admin\CityController ;
.

Explain some of the PHP string functions?

There are many array functions in PHP:

  • strtolower()
  • strtoupper()
  • ucfirst()
  • lcfirst()
  • ucwords()
  • strrev()
  • strlen()
.

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

The header() function is used to send a raw HTTP header to a client. It must be called before sending the actual output. For example, you can’t print any HTML element before using this function..

What is the difference between GET and POST?

  • GET displays the submitted data as part of the URL, during POST this information is not shown as it’s encoded in the request.
  • GET can handle a maximum of 2048 characters, POST has no such restrictions.
  • GET allows only ASCII data, POST has no restrictions, binary data are also allowed.
  • Normally GET is used to retrieve data while POST to insert and update.

Understanding the fundamentals of the HTTP protocol is very important to have for a PHP developer, and the differences between GET and POST are an essential part of it.

.

What is purpose of @ in Php ?

In PHP @ is used to suppress error messages.When we add @ before any statement in php then if any runtime error will occur on that line, then the error handled by PHP.

How can you increase the maximum execution time of a script in PHP?

You need to change the value of the max_execution_time directive in the php.ini file for increasing the maximum execution time.

if you want to set the max execution time for 120 seconds, then set the value as follows,

max_execution_time = 180

.