PHP Interview Questions and Answers (2024) | Set-2

In this article, you will learn PHP Interview Questions and Answers that are most frequently asked in interviews. Before proceeding to learn PHP Interview Questions and Answers Set 2, first we learn the complete PHP Tutorial and PHP Interview Questions and Answers.

PHP Interview Questions and Answers

PHP is the Hypertext Preprocessor. It is a server-side scripting language designed specifically for web development. It is open-source which means it is free to download and use. It is very simple to learn and use. The files have the extension “.php”.

Pre-requisites

PHP Interview Questions

1. How can you enable error reporting in PHP ?

If you are getting errors and you don’t have idea about those errors then you can use inbuilt error_reporting() function. This function will give you information about those errors where and why it is happening. The best place to include this function is the beginning of your PHP script. You can also set this function for some specific script or you can set it for all the scripts in your server by editing the php.ini file.

There are various type of errors in PHP but it contains basically four main types of errors.

  • Parse error or Syntax Error: It is the type of error done by the programmer in the source code of the program. Parse errors can be caused dues to non-closed quotes, missing or extra parentheses, non-closed braces, missing semicolon, etc.
  • Fatal Error: It is the type of error where PHP compiler understands the PHP code but it recognizes an undeclared function. It means that function is called without the definition of the function.
  • Warning Errors: The main reason for warning errors are including a missing file. This means that the PHP function call the missing file.
  • Notice Error: It is similar to warning error. It means that the program contains something wrong but it allows the execution of script.

3. What is inheritance in PHP ?

Inheritance in PHP means the child class can inherit all the properties and protected methods from it’s parent class and extend keyword is used to define the inheritance.

PHP doesn’t support multiple inheritance but by using Interfaces in PHP or using Traits in PHP instead of classes we can implement it.

The trait is a type of class which enables multiple inheritance. Classes, case classes, objects, and traits can all extend no more than one class but can extend multiple traits at the same time.

  • GET: It requests data from a specified resource. In this method, the data is sent as URL parameters that are usually strings of name and value pairs separated by ampersands (&).
    Syntax:

    <?php
        $_GET['variable_name'];
    ?>
  • POST: In this method the data is sent to the server as a package in a separate communication with the processing script. Data sent through POST method will not be visible in the URL.
    Syntax:

    <?php
        $_POST['variable_name'];
    ?>
  • Unlink() function: The unlink() function is an inbuilt function in PHP which is used to delete a file. The filename of the file which has to be deleted is sent as a parameter and the function returns True on success and False on failure. The unlink() function in PHP accepts two-parameters filename and context.
  • Unset() function: The Unset() function is an inbuilt function in PHP which is used to remove the content from the file by emptying it. It means that the function clears the content of a file rather than deleting it. The unset() function not only clears the file content but also used to unset a variable, thereby making it empty accepts a single parameter variable.

8. If x = 10 and y = “10” then what does the condition x === y returns ?




<?php
$x = 10;  
$y = "10";
  
var_dump($x === $y); 
?> 


It will return bool(false).

9. What is Nullable types in PHP ?

This feature is new to PHP, Nullable adds a leading question mark indicate that a type can also be null.




function Beginner(): ?int  {
    return null; // ok
}


By default maximum upload file size for PHP scripts is set to 128 megabytes. But you can change it, the maximum size of any file that can be uploaded on a website written in PHP is determined by the values of max_size that can be posted or uploaded, mentioned in the php.ini file of the server. In case of a hosted server need to contact the administrator of the hosting server but XAMPP has interpreters for the scripts written in PHP and Perl. It helps to create a local HTTP server for developers and it provides them full physical and administrative access to the local server. Hence it is the most widely used server and it is very easy to increase the limit on upload files to the desired value in this server.

Use PHP inbuilt function ini_set(option, value) where the parameters are the given configuration option and the value to be set. It is used when you need to override the configuration value at run-time. This function is called from your own PHP code and will only affect the script which calls this function. Use init_set(‘max_execution_time’, 0) when you want to set unlimited execution time for the script.




// The program is executed for 3mns. 
<?php 
ini_set('max_execution_time', 180); 
?> 


  • include() function: This function is used to copy all the contents of a file called within the function, text wise into a file from which it is called. This happens before the server executes the code.
  • require() function: The require() function performs same as the include() function. It also takes the file that is required and copies the whole code into the file from where the require() function is called.
  • Public Access modifier: This modifier is open to use inside as well as outside the class.
  • Protected Access modifier: This modifier is open to use within the class in which it is defined and its parent or inherited classes.
  • Private Access modifier: This modifier is open to use within the class that defines it. ( It can’t be accessed outside the class means in inherited class).

The spaceship operator or combined comparison operator is denoted by “<=>“. This is a three-way comparison operator and it can perform greater than, less than and equal comparison between two operands.

    This <=> operator offers combined comparison:

  • Return 0 if values on either side are equal
  • Return 1 if value on the left side is greater
  • Return -1 if the value on the right side is greater
  • __construct() methods: Constructors are the very basic building blocks that define the future object and its nature. You can say that the Constructors are the blueprints for object creation providing values for member functions and member variables.
    function __construct() {
            
        // Initialize the object and its
        // properties by assigning values
    }
  • __destruct() methods: Destructors are used to destroying the objects and automatically called at the end of execution.
    function __destruct() {
        
        // Destroying the object or
        // clean up resources here 
    }

16. What is urlencode() and urldecode() ?

  • urlencode() function: The urlencode() function is an inbuilt function in PHP which is used to encode the URL. This function returns a string which consists all non-alphanumeric characters except -_. and replaced by the percent (%) sign followed by two hex digits and spaces encoded as plus (+) signs.
  • urldecode() function: The urldecode() function is an inbuilt function in PHP which is used to decode the URL which is encoded by encoded() function.

The line break can be removed from string by using str_replace() function. The str_replace() function is an inbuilt function in PHP which is used to replace all the occurrences of the search string or array of search strings by replacement string or array of replacement strings in the given string or array respectively.

There are three ways of removing an extension from the string. They are as follows

  • Using an inbuilt function pathinfo
  • Using an inbuilt function basename
  • Using a string functions substr and strrpos

19. How to check the value of variable is a number, alphanumeric or empty ?

You can use is_numeric() function to check whether it is a number or not. The ctype_alnum() function is used to check whether it is an alphanumeric value or not and use the empty() function to check variable is empty or not.

Yes it is possible by using the strip_tags() function. This function is an inbuilt function in PHP which is used to strips a string from HTML, and PHP tags. This function returns a string with all NULL bytes, HTML and PHP tags stripped from a given $str.