DEV Community

Sharad
Sharad

Posted on

PHP Interview Questions

Below are the few Latest Php programming interview questions

  1. What is PHP?

PHP: Hypertext Preprocessor is open source server-side scripting language that is widely used for creation of dynamic web applications.It was developed by Rasmus Lerdorf also know as Father of PHP in 1994.

PHP is a loosely typed language, we didn’t have to tell PHP which kind of Datatype a Variable is. PHP automatically converts the variable to the correct datatype , depending on its value.

  1. What is T_PAAMAYIM_NEKUDOTAYIM ?

T_PAAMAYIM_NEKUDOTAYIM is scope resolution operator used as :: (double colon) .Basically it used to calling static methods/variables of a Class.

Example usage:-

$Cache::getConfig($key);

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

In PHP == is equal operator and returns TRUE if $a is equal to $b after type juggling and === is Identical operator and return TRUE if $a is equal to $b, and they are of the same data type.

Example Usages:

<?php
$a=true ;
$b=1;
// Below condition returns true and prints a and b are equal
if($a==$b){
echo "a and b are equal";
}else{
echo "a and b are not equal";
}
//Below condition returns false and prints a and b are not equal because $a and $b are of different data types.
if($a===$b){
echo "a and b are equal";
}else{
echo "a and b are not equal";
}
?>

  1. What is session in PHP. How to remove data from a session?

As HTTP is state protocol.To maintain states on server and share data across multiple pages PHP session are used.PHP sessions are simple way to store data for individual users/client against a unique session ID.Session IDs are normally sent to the browser via session cookies and the ID is used to retrieve existing session data,if session id is not present on server PHP creates a new session, and generate a new session ID.

Example Usage:-

<?php
//starting a session
session_start();
//Creating a session
$_SESSION['user_info']=['user_id'=>1,'first_name'=>'php','last_name'=>'scots','status'=>'active'];

//checking session
if(isset($_SESSION['user_info'])){
echo "logged In";
}

//un setting remove a value from session
unset($_SESSION['user_info']['first_name']);

// destroying complete session
session_destroy();

?>

  1. How to register a variable in PHP session ?

In PHP 5.3 or below we can register a variable session_register() function.It is deprecated now and we can set directly a value in $_SESSION Global.

Example usage:

<?php
// Starting session
session_start();
// Use of session_register() is deprecated
$username = "PhpScots";
session_register("username");
// Use of $_SESSION is preferred
$_SESSION["username"] = "PhpScots";
?>

Also Read Laravel interview questions

  1. Where sessions stored in PHP ?

PHP sessions are stored on server generally in text files in a temp directory of server.
That file is not accessible from outside word. When we create a session PHP create a unique session id that is shared by client by creating cookie on clients browser.That session id is sent by client browser to server each time when a request is made and session is identified.
The default session name is “PHPSESSID”.

  1. What is default session time and path in PHP. How to change it ?

Default session time in PHP is 1440 seconds (24 minutes) and Default session storage path is temporary folder/tmp on server.

You can change default session time by using below code

<?php
// server should keep session data for AT LEAST 1 hour
ini_set('session.gc_maxlifetime', 3600);

// each client should remember their session id for EXACTLY 1 hour
session_set_cookie_params(3600);
?>

  1. What are PHP Magic Methods/Functions. List them.

In PHP all functions starting with __ names are magical functions/methods. Magical methods always lives in a PHP class.The definition of magical function are defined by programmer itself.

Here are list of magic functions available in PHP

__construct(), __destruct(), __call(), __callStatic(), __get(), __set(), __isset(), __unset(), __sleep(), __wakeup(), __toString(), __invoke(), __set_state(), __clone() and __debugInfo() .

  1. What is difference between include,require,include_once and require_once() ?

Include :-Include is used to include files more than once in single PHP script.You can include a file as many times you want.

Syntax:- include(“file_name.php”);

Include Once:-Include once include a file only one time in php script.Second attempt to include is ignored.

Syntax:- include_once(“file_name.php”);

Require:-Require is also used to include files more than once in single PHP script.Require generates a Fatal error and halts the script execution,if file is not found on specified location or path.You can require a file as many time you want in a single script.

Syntax:- require(“file_name.php”);

Require Once :-Require once include a file only one time in php script.Second attempt to include is ignored. Require Once also generates a Fatal error and halts the script execution ,if file is not found on specified location or path.

Syntax:- require_once(“file_name.php”);

Read More Questions from https://www.onlineinterviewquestions.com/core-php-interview-questions/

Top comments (4)

Collapse
 
vijaykhatri96 profile image
Vijay Singh Khatri

Hi Sharad,
You have shared very informative and basic interview question which are useful for fresher, Who is at the starting point of their career if a developer a looking for the interview question then can go with this post.

Collapse
 
mohithsharma profile image
Mohithsharma

Hey,

Thanks for this informative and useful article. Here is my pick for PHP Interview Questions if you are preparing for the interview.

Collapse
 
gangbchb profile image
gangbchb

Find vulnerabilities that you thought were fixed in your web apps using Detectify and the power of our crowd. Go hack yourself with xsignal.io/

Collapse
 
satyam_prg profile image
Satyam Jaiswal

Best Interview Questions for Freshers. Learn here top mcqs on php.