Latest PHP Interview Technical Questions - 2020

Posted on 04th Mar 2020 02:26 PM

We at iMatrix Solutions besides providing SEO services, we give SEO Internet Marketing Online and Classroom training by best faculty having ample experience of maintaining websites. For training enquiry and details mail us at : info@imatrixsolutions.com or contact us on +91 9000866282

latest-php-interview-technical-questions-2020

Fresher Or Experienced... Looking For Php
Technical Interview Questions. You Are at the
Right Place to Find Valuable Questions And
Answers. What Is Php(Hypertext Preprocessor) ..?
Php (Latest version 7.4.2) is an
open-source general-purpose scripting language,
that is especially suited for web development
that is used to develop Static websites
or Dynamic websites or Web applications
and can be embedded into HTML.In simple terms, we can say Php is
a server-side scripting Language. Need three things to have an environment
to work:the PHP parser (CGI or server module), a web server and a web browser.
Run web server with PHP installation.PHP output can be viewed in a web browser.All these can
run on your home machine if you want to practice PHP programming.To have a bright career in
Php web Developer,One must have knowledge on some other Technologies like...

  • HTML(Hyper Text Markup Language)
  • CSS(Cascading Style Sheets)
  • JS(JavaScript)
  • MYSQL(My-The name of co-founder Michael Widenius’s daughter,
    SQL-Structured Query Language )
  • AJAX(Asynchronous JavaScript and XML)
  • JQUERY(JavaScript library)

Basic Level PHP Interview Questions:

1Q. What is PHP Syntax?

<?php [--- PHP code goes here----] ?>

2Q. What are the data types in PHP?

  • String
  • Integer
  • Float (floating point numbers - also called double)
  • Boolean
  • Array
  • Object
  • Resource

3Q. What are Include(), Require(), Require_once() functions?

Include() : function is used to get data of one PHP file into another PHP file.
If errors occur then the include() function produces a warning but does not stop
the execution of the script and it will continue to execute.

Require() :function is used to put data of one PHP file to another PHP file.
If any error occurs it stops the execution of the script by producing a warning and a fatal
error.

Require_once():statement can be used to include a Php file in another one,
when you may need to include the called file more than once.
If the same file is called so many times,
the file will be included for the first time by ignoring further calls.

4Q.How can we write comments in PHP?

Syntax for single-line comments:

<?php
// [your comment goes here]
# [your comment goes here] ?>

Syntax for multiple-line comments:

<?php
/* [multiple-lines of comment] */ ?>

5Q.What is echo in PHP?

Php echo is a language construct used to display data on a web page,
it is not a function. Use of parentheses is not required,
if you want to pass more than one parameter to echo, then one must use parentheses.

6Q.What is a Session in PHP?

Generally, for subsequent HTTP requests, the PHP engine creates a logical object to store data,
which is called the session. sessions generally stores temporary data which
allows multiple Php pages to offer a complete functional transaction for the same login.
When the browser is closed automatically session values are deleted.

7Q.What is the difference between GET and POST?

There are two ways the client(browser) can send the information to the web server.
They are GET and POST methods.
There are two global variables in PHP to collect form data from HTML FORM and URL,
they are $_GET and $_POST.
$_GET variable is used to collect data from HTML forms using method GET. HTML
form information sent is displayed in the browser’s address bar,
and it has a limit on the amount of information to send.
$_POST variable is used to collect data from HTML forms using method POST.
Information sent from an HTML form with the POST
method is invisible and has no limits on the amount of information to send.

8Q.How we can get the number of elements in an array in PHP?

To know the number of elements in an array we have count() function.

9Q.What are access modifiers in PHP?

There are 3 types of access modifiers in Php.The visibility of a property,
a method or a constant can be defined by prefixing the declaration with these
access modifiers.

Public :Class members declared as public can be accessed everywhere.

Protected : Class members declared as protected can be accessed only
within the class itself and by inheriting from the parent class.

Private :Class members declared as private can only be accessed by
the class that defines the member.

10Q.What is a loop concept and what are different loops available in PHP?

The repetitive execution of the same block of code again and again until a certain
condition is true is called loop concept.

Types of loops are:

while : While loop is executed until the specified condition is true.

do..while : Executes a block of code once, and repeats the loop as long as
the mentioned/written condition is true.

for : The for loop is used when we know in advance how many times
the program should run.

foreach : This loop works only on arrays, and is used to loop through
each key/value pair in an array.

Experienced Level PHP Interview Questions:

1Q.What are Traits in PHP?

Php only supports single inheritance a child class can inherit only from one single parent.
Suppose an application needs multiple inheritance we have to use OOP Traits.

In multiple classes, to declare methods Traits are used.
The methods can have any access modifier (public, private, or protected).

Traits are declared with the trait keyword, and used by use keyword.

<?php
trait message1 {
public function msg1() {
echo "I Am Good ";
}
}
class Welcome {
use message1;
//message1 is declared as trait which is used in welcome class by use keyword
}
$obj = new Welcome();
$obj->msg1();
?>

2Q.How to set the timezone in PHP and how to display current date and time in PHP?

Date_default_timezone _set();Function is used to select preferred time zone .

date(); Function is used to display current date and time;

<?php
date_default_timezone_set("America/New_York");
echo "The time is " . date("Y/m/d");
In date() function Y-represents a year,m-represents a month,d-represents the day of the month ?>

3Q.What is $_SERVER["PHP_SELF"] variable in PHP?

To know the filename of the currently executed script can be done with $_SERVER[“PHP_SELF”],
which is a superglobal variable in PHP.

<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">

In simple terms we can say $_SERVER["PHP_SELF"]
super global variable sends the submitted form data to the page itself,
instead of jumping to a different page. This way,
the user will get error messages on the same page as the form.

4Q.What is JSON in PHP?

JSON means JavaScript Object Notation, is a syntax for storing and exchanging data.

JSON format is a text-based format,data exchange can be easily done
from and to server .There are two methods in JSON.

json_encode() : Function is used to encode a value to JSON format.

<?php
$age[‘indexed’] = array("ram"=>30, "shyam"=>37, "suresh"=>44);
//encoding an associative array into a JSON object:
$age[‘assoc’] = array("Volvo", "BMW", "Toyota");
//encoding an indexed array into a JSON array:
echo json_encode($age);
?>
json_decode() : Function is used to decode a JSON object into a PHP object or an associative array.
//decodes JSON data into a PHP object:
<?php
$jsonobj = '{"ramu":35,"ramesh":37,"raju":43}';
var_dump(json_decode($jsonobj));
?>
o/p is
object(stdClass)#1 (3) { ["ramu"]=> int(35) ["ramesh"]=> int(37) ["raju"]=> int(43) }

5Q.What is a Framework in PHP what are different types of Frameworks available ?

A Framework is a basic platform that allows us to develop web applications.
which provides structure so that developers can save time. Minimises Workload.
Each framework has its own strengths and weaknesses,
and all frameworks are not unique. They all vary in the views of documentation, community,
and the database they support.

Frameworks Available:
  1. Laravel
  2. Codigniter
  3. Symfony
  4. CakePHP
  5. Yii
  6. Zend Framework
  7. Phalcon
  8. FuelPHP
  9. PHPixie
  10. Slim

6Q.How to get the Client IP Address in PHP?

$_SERVER is an array that contains server variables created by the web server.
By using $_SERVER['REMOTE_ADDR'] or $_SERVER['REMOTE_HOST'] variables
we can get the client IP Address.

7Q.What are different types of errors in PHP?

It is a type of mistake or condition of having incorrect knowledge of the code.

  1. Parse error or Syntax Error: This error occurs when something goes wrong
    in the source code,compiler throws the error.
  2. Fatal Error: When undeclared function is presented in source code fatal error occurs.
    This means that function is called without the definition of function.
  3. Warning Errors: When a missing file is included warning error occurs.
  4. Notice Error: It is similar to warning error. It means that the program contains something
    wrong but it allows the execution of script.

8Q.How can we establish a connection between Database(MYSQLi) and PHP?

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "example";
// Create connection
$conn = mysqli_connect($servername, $username, $password,$dbname);
// Check connection
if(! $conn )
{
die('Could not connect: ' . mysqli_error());
}
echo 'Connected successfully';
mysqli_close($conn);
?>