Php Questions for Interview

  • 04-02-2023
  • 424
  • PHP
  • Haresh Chauhan

Hello Artisan,

In this post, I will tell the most important php questions for the interview pass out or getting core basic knowledge of PHP, in this tutorial I will tell you basic to advance level php questions that may help you to clarify your php job interview or can say in some basic information about php script and basic understanding.

Question 1 : What Are Superglobal Variable in Php

Superglobal variable means predefined variable, this variable you can access from any of php application files. below provided variables are without any work without any scope. You can access these all variables from any file, class, or function without any process or special action.

$GLOBALS
$_SERVER
$_REQUEST
$_POST
$_GET
$_FILES
$_ENV
$_COOKIE
$_SESSION

Question 2 : Loop in Php and It's Types

When you want to run a specific code of block over and over again for a certain number of times loop will help you, the advantage of a loop is to reduce the duplication of the code. The loop always works based on the array variable. to run a loop over several times on a specific code you need to pass a count or array variable to run a specific certain number of times.

There are four types of loops facilitated by php. all these loop uses are different. You can still use any loop your wish there is no limitation.

  • while
  • do...while
  • for
  • foreach

Question 3 : What is a Php Session?

The session is one type of local browser storage for the temporary base. You know when we open any website in the browser using the internet they ask for the login using your username and password, or any type of secret details, after you login session store your login details and all your details in the browser cache until you do not close the open page, once you log in all your information store in the browser for the getting knows to the browser that you are already login and who you are.

The session identifies you, who you are, and whether you authenticate or not on the specification web page.

The session is not destroyed by closing the web page or ending the browser you opened. Session variable fixed your problem by knowing your identity to your browser and accessing multiple pages of the php application.

For the use session, we need to define the session_start() function at the top of the php file. also if you want to destroy the session run the session_destroy() function and the session will destroy.

<?php 
session_start()

$_SESSION['name'] = 'Webappfix';

// CAN ACCESS THIS VARIALBE ON ANY PAGE OF THE APPLICATION
print_r($_SESSION);

// remove all session variables
session_unset();

// destroy the session
session_destroy();
?>

Question 4 : Define Database Connetion in Php?

For communication with the database, we need to define a connection with the specified database, for executing the query first we just need to define a database connection then we can communicate with the database, For the transformation of the communication between users and the database we need to connect with the database and then after we can insert database in the database and retrieve data from the database table.

<?php
$servername = "localhost";
$username = "username";
$password = "password";

// Create connection
$conn = new mysqli($servername, $username, $password);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>

Above given an example of how to do to connect with the database php the php configuration. Therefore you must need basic knowledge of the database and its use of it.

Question 5 : How to Comment Code in Php

Comment disable specific lines of the code from the executing, There are two types of comments you can do. First, you can comment on single line code or you can comment on multiple line comment together.

Comments help you to identify code logic, you can write comment code in your language. the comment will remind you what you did that time. or remind your logic before the written php code.

Let's see an example of how to comment code in php.

<?php
// This is a single-line comment
    
# This is also a single-line comment

/*
This is a multiple-lines comment block
that spans over multiple
lines
*/

?>

Question 6 : What is Array in Php & Types of Array

Array stores multiple values only in one variable, there are three types of array available you can define in php. of the creating array variable you need to use the array() php function.

  • Indexed arrays
  • Associative arrays
  • Multidimensional arrays
array("Volvo", "BMW", "Toyota")
array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
array (
    array("Volvo",22,18),
    array("BMW",15,13),
    array("Saab",5,2),
    array("Land Rover",17,15)
)

Question 7 : What is Function in Php

The real power of the php is its function, already php provided 1000+ ready functions that you can you directly by calling it. also, you can still make your own function.

The function of php helps us to use repetitive action by calling the function. the function identifies y its name, any function in php must need its name the identify the function action.

<?php
function name() {
    echo "Webappfix!";
}
    
name(); // call the function

// EXAMPLE 2

function name($name){
    echo $name;
}

name('Webappfix');

// EXAMPLE 3

function name($name){
    return $name;
}

$name = name('Webappfix');

echo $name;
?>

Question 8 : Datatype in Php

For the different types of data like number, name, amount, and file need to use a different variable.

String // "webappfix"
Integer // 123
Float // 10.10
Boolean // true, flase
Array // array()
Object, // {}
NULL, // just for define variable
Resource // files

Question 9 : How to Handel Php Exception

An Exception target errors, syntax, or unexpected behavior in the application while executing the code.

<?php
function divide($dividend, $divisor) {
    if($divisor == 0) {
    throw new Exception("Division by zero");
    }
    return $dividend / $divisor;
}

try {
    echo divide(5, 0);
} catch(Exception $e) {
    echo "Unable to divide.";
}
?>

Question 10 : If Statement in Php

If the condition works based on the true or false bool operation. Sometimes you want to execute the code base on the condition. the IF statement works based on the condition provided.

If coding executes the inside given code if the condition is true.

If...else, certain coding true the then execute inside the if condition codes else false.

else if works more than one condition.

<?php
$t = date("H");
    
if ($t < "20") {
    echo "Have a good day!";
}

// example 2

if ($t < "20") {
    echo "Have a good day!";
} else {
    echo "Have a good night!";
}

// example 3

if ($t < "10") {
    echo "Have a good morning!";
} elseif ($t < "20") {
    echo "Have a good day!";
} else {
    echo "Have a good night!";
}
?>

We always thanks to you for reading our blogs.


dharmesh-image

Dharmesh Chauhan

(Swapinfoway Founder)

Hello Sir, We are brothers origin from Gujarat India, Fullstack developers working together since 2016. We have lots of skills in web development in different technologies here I mention PHP, Laravel, Javascript, Vuejs, Ajax, API, Payment Gateway Integration, Database, HTML5, CSS3, and Server Administration. So you need our service Please Contact Us

haresh-image

Haresh Chauhan

(Co-Founder)


We Are Also Recommending You :