PHP Programming Interview Question and Answers - Beginner Level to Advance Level
Categories - Interview Prepration Laravel PHP Framework PHP Tags - PHP Laravel   Maniruzzaman Akash   2 years ago   926   5 minutes   0

PHP Programming Interview Question and Answers - Beginner Level to Advance Level

Beginner Level Interview Questions


What is PHP and What it's stand for?
PHP is a web language based on scripts that allow developers to dynamically create generated web pages. PHP Full meaning is - Hypertext Preprocessor.

 

Which programming language does PHP resemble?
PHP syntax resembles Perl and C Programming language.

 

Who developed PHP and when it was developed?

PHP was developed by Rasmus Lerdorf in 1994.

 

How do you execute a PHP script from the command line?

Just use the PHP command line interface (CLI) and write as below:

php script.php

Here script.php is the php file. eg - index.php.

 

Why do we use PHP?

There are many benefits of using PHP. Mainly, PHP is totally free to use programming language. Everyone can use PHP without any cost and host the site at a very minimal cost.

PHP supports multiple databases. The most commonly used database is MySQL which is also free to use. Many PHP frameworks are used now for web development, such as Laravel, CakePHP, CodeIgniter, Zend PHP etc.

 

Is PHP support OOP - Object Oriented Programming?

Yes. From PHP version 5, php comes with the Object Oriented Programming. It was not OOP friendly in PHP version 4.

 

Main difference between PHP 4 and PHP 5?

PHP4 doesn't support Object Oriented Programming(OOP), where PHP5 support Object Oriented Programming(OOP)

 

 

There is also a better Error handling Exceptions used in PHP5.

 

Is PHP a strongly typed language now?

Yes, from PHP version 7, php introduces this strongly typed feature. Let's see a simple example of code so that you can understand PHP now is a strongly types languge. Before PHP version 7 php was not actually a strongly typed language.

declare(strict_types = 1);

function sum(int $a, int $b): int {
    return $a + $b;
}

sum(1, 2);

Look, first we've to declare strict_types = 1.

Then we can write strongly typed PHP like int $a, int $b and return type of function is also int.

 

What does PEAR stand for PHP?

PEAR means PHP Extension and Application Repository. It extends PHP programming language and provides a higher level of programming for web developers.

 

What is the correct and the most two common ways to start and finish a PHP block of code?

There are two ways to start and close of a PHP block.

  1. <?php [ -- Write PHP code -- ] ?>
  2. <? [ -- Write PHP code -- ] ?>

the second one is known as PHP shorter syntax.

 

What is variable in PHP and how to define a variable in PHP?

A variable in PHP starts with the $ sign, followed by the name of the variable· A variable name must start with a letter or the underscore character. Here is some examples of a PHP variable -

$age = 20; // correct
$12 = 10; // Incorrect, Number can not started as variable name
$_age = 20; // correct, Underscore can be started as PHP vatiable.

  

What is composer and how can we start with composer ?

  • Composer is the dependency management tool or library/package management tool for PHP.
  • We can start with composer by downloading it from https://getcomposer.org
  • After that we can install our necessary package for our PHP application. eg: composer require guzzlehttp/guzzle .

 

Can we maintain project without composer ?

Yes, ofcourse. Just assume it as PHP. and inject any file directly if needed. But for a big project without composer it's very hard to maintain such kind of PHP application without composer.

 

 

Intermediate Level Interview Questions


 

What is static method and properties and how to use?

Static method and properties can be accessed without instantiating the class. In a scenerio where, we don't need to instatiate the class, and we need to use the property or methid frequently, then static method and property is usefull. we can write that using static keyword.

To access static property/method from other class, we can access like Class::method_name() or Class::property. or static::method_name() or static::$property

To access static property/method from same class use self keyord, use like - self::method_name().

To access from child use parent keyword, like - parent::method_name();

Example -

<?php

public class Settings {
   public static $api_key = "API KEY";

   public static function getConfig() {
      return self::$api_key; // using from same class using `self`
  }
}


// Payment class
class Payment {
  public static function pay() {
    // Use it from other class
    Settings::getConfig();
  }
}

// Child class
class StripePayment extends Payment {
   public function stripePay() {
      // using parent keyword
      parent::pay();
   }
}

 

 

What is namespace in PHP and why it's come and how to use?

Namespace are a way of grouping items. Namespace solved actually two problems. It comes from PHP 5.3.0.

  1. When we've some classes that actually perform a specific task, then we can group that using namespace
  2. If we want to make same class name of two or more than one class, then namespace is very effective.

 

Example:Suppose we've some payment related classes, we can group that in a Payment folder. Inside that folder, we can add some classes like Payment.php, PaypalPayment.php, StripePayment.php etc.

Payment.php class:

<?php
namespace DevsEnv\Payment;

class Payment
{
   // payment related core methods & variables
}

StripePayment.php class

<?php
namespace DevsEnv\Payment;

class StripePayment extends Payment
{

}

 

 

 

Advanced Level Interview Questions


 

What is iterable in PHP and how to use it?

Iterable is a behavioral design pattern. It allows to travels complex data structures and get it's datas without internal properties access. PHP has a built in Iterator interface from PHP version 7.1.

We can just use an Iterator by making a class implementing the Iterator interface in PHP. It must implement the following properties and methods - key(), current(), next(), rewind() and valid().

 

Example -

<?php

namespace DevsEnv\Iterators;

class SimpleIterator implements \Iterator {

  private $items = [];
  private $current_id = 0;

  public function __construct($items) {
    // array_values() makes sure that the keys are numbers
    $this->items = array_values($items);
  }

  public function current() {
    return $this->items[$this->current_id];
  }

  public function key() {
    return $this->current_id;
  }

  public function next() {
    $this->current_id++;
  }

  public function rewind() {
    $this->current_id = 0;
  }

  public function valid() {
    return $this->current_id < count($this->items);
  }
}

 

 

 

 

Previous
PHP If-else-elseif and Switch-case
Next
PHP String Functions - All necessary String functions in PHP to manage strings better.