PHP Interview Questions And Answers

PHP Interview Questions & Answer

 

Are you looking for PHP Interview Questions and Answers, then this is the right place for you. Here we will discuss various interview question of PHP.

PHP interview questions is a set of practice questions for people who are going for php interviews, it covers the basic topics you must have knowledge about.

PHP interview questions blog is dedicated to helping programmers prepare for interviews. Browse our collection of interview questions.

What is PHP?

PHP stands for Hypertext PreProcessor scripting language. PHP is server side language. PHP is used almost in every big company due to its power and easy to design and development.

 

What is the starting and ending tag of PHP language?

PHP programming language is written between <?php and ?> tag. so <?php is the starting tag and ?> is the ending tag of PHP Programming language

<?php


//php_code

?>

php_code is lines of php language written inside php tag.

How to comment code in PHP?

In PHP programming language, there are two ways to comment a code.

Single line comment: In PHP, Single line comment is done by using //. Put // before the code, it will treat that line as comment.

Multi-line comment: In PHP multi-line comment can be put by using /*  */.  All the code written between the tag /* */ is treated as comment.

commented code does not give any value. Its just for code quality purpose.

<?php
echo " Welcome to ByteArray.in";

//this is single line comment

/*
This is multi-line comment
in PHP
*/
?>

Output

Welcome to ByteArray.in

 

What is the use of echo in PHP?

echo is used to print anything written inside it. Whenever any value is written inside echo it will evaluate and print it.

<?php
echo "hi ! ByteArray.in";
?>


output

hi ! ByteArray.in

 

How to check the PHP version using echo?

Using phpVersion() method we can geth the installed Php Version.

<?php
echo phpVersion();
?>

 

How to declare variable in PHP?

In Php, Variables can be declare by using Dolllar sign $.

<?php
$name="ByteArray.in";
echo $name;
?>

 

How many different data types are available in PHP?

Data type refer type of data PHP variable can store. In PHP following are the data types:

  • Integer
  • String
  • Boolean
  • Array
  • Decimal
  • Object
  • Null

 

How to create a function in PHP?

function is the block of statement to execute. In PHP, function is created with the word function.

function fnName() {
/*
* Lines of code inside function.
*/
}

 

How to create a constant in PHP?

Constant are those whose value can not be changed. In PHP, constant is created with define().

define(name, value, case-sensitive);

example

<?php

define(“hostName”,”192.168.1.1″,true);

echo hostname;
?>

case-sensitive true means hostName will be case in-sensitive.

 

How to get the current date in PHP?

Using Date() function we can get the current date in PHP. Date() function take the parameter. By passing the parameter value we can get the date in desired format.

<?php
echo "Current date is " . date("Y/m/d");
?>

 

How to concatenate two Strings in PHP?

Strings is concatenated by using dot . in PHP.

Example:

<?php

$fName="ByteArray";
$lName=".in";

echo $fName.$lName;

?>

 

What is include in PHP?

include keyword is used to import the file into another file.

<?php  require("header.php"); ?>

 

What is require in PHP and how require is different than include?

require is also used to import the file into another file. require is differ from include in the way that

  • require does not throw error if require file is not found and execution will not stop.
  • include throw error if file is not found and stop the further execution.

 

What is the difference between require and require_once in PHP?

The difference between require and require_once is that

  • require – will include the file as many times it finds the require(filename) statement.
  • require_once – will include the file only once. It will not include it again and again.

 

How to read a file in PHP?

In PHP readfile() function is used to read the file.

readfile(filename_to_read);

 

readfile is the function name and filename_to_read is the file to be read.

 

How to create a file in PHP?

In PHP, file can be created by using fopen function.

 

How to upload image in PHP?

Image can be uploaded through form. In form we have input type tag.

For image to upload in PHP, type of input tag will be file.

<input type="file" name="uploadFile" id="uploadFile">

form action will be post and along with that there will be enctype="multipart/form-data" tag.

so, finally we have,

<form action="imgeupload.php" method="post" enctype="multipart/form-data">
  Upload the Image
  <input type="file" name="uploadFile" id="uploadFile">
  <input type="submit" value="Upload" name="submit">
</form>

 

How to set the cookie in PHP?

In PHP, Cookie is set through setcookie() funtion. As per the PHP official documentation, setcookie take some parameters which are

setcookie(name,value,expires,path,domain,secure,httponly);

only name parameter is required, other are optional.

 

How to retrieve the cookie in PHP?

In PHP, cookie is retrieved using the global variable $_COOKIE.

$_COOKIE[$cookie]

Value of the cookie variable will be retrieved.

 

What is isset and unset in PHP?

  • isset in PHP is used to check if variable exist on which it applies and that should be not null. isset will return true if variable exist and not null, otherwise it returns false.
  • unset in PHP is used to reset the value of the variable on which it applies.

 

How to collect the form data in PHP?

Form data can be submitted in two ways either GET or POST

GET : Get form data is fetched with PHP global variable $_GET.

POST : Post form data is fetched with PHP global variable $_POST.

$_GET[“name”]

$_POST[“name”]

 

How to set the session in PHP?

In PHP, session is set through PHP global variable $_SESSION.

Before setting the session. session_start() should be written.

<?php
// Start the session
session_start();


// Set session variables
$_SESSION["username"] = "byteArray.in";
?>

In above example, username session is set.

 

What is json_encode and json_decode in PHP?

json_encode : json_encode function in PHP is used to convert the value in JSON format.

json_decode : json_decode function in PHP is used to convert the json value to desired format.

 

How to handle exception in PHP?

Its obvious when we write code in any programming language, there is possibility of exception in code. In PHP, we can handle exception through try catch block.

try  {

/*suspected code */

} catch(Exception $ex) {

//handle the exception

}

 

How to throw custom exception in PHP?

In PHP, custom exception can be thrown using throw new Exception ().

 

How to create a class in PHP?

In PHP, class is created with class keyword. A class is a template to create the object.

<?php

class Vehicle {

//code related to class Vehicle.

}

?>

How to create an object in PHP?

In PHP, object is created with new keyword.

class Vehicle {

//code related to class Vehicle.

}

$car = new Vehicle();

$bus = new Vehicle();

car and bus are two objects of class vehicle.

 

What are various access modifier in PHP?

There are majorly 3 types of access modifiers in PHP

  • public
  • private
  • protected

 

How to create constructor in PHP?

_construct() function is used to create constructor in PHP

 

What is the use of static keyword in PHP?

static keyword is used with methods and variables. The purpose of static keyword is to access the variables and methods without creating the object of the class. We need not to create object to access static variables and static methods.

 

How to get data type of any variable in PHP?

To get data type of any variable gettype() function is used in PHP

 

What is the purpose of empty() function in PHP?

empty(var) function in PHP check whether var is empty or not. It will return true if var is 0 or null.