Core PHP Interview Questions

Core PHP Interview Questions

PHP is a recursive acronym for PHP Hypertext Preprocessor. It is a widely used open-source programming language especially suited for creating dynamic websites and mobile API's.

Download Core PHP Interview Questions PDF

Core PHP Interview Questions

The mail function is used in PHP to send emails directly from script or website. It takes five parameters as an argument.

Syntax of mail (): mail (to, subject, message, headers, parameters);

  • to refers to the receiver of the email
  • Subject refers to the subject of an email
  • the message defines the content to be sent where each line separated with /n and also one line can't exceed 70 characters.
  • Headers refer to additional information like from, Cc, Bcc. These are optional.
  • Parameters refer to an additional parameter to the send mail program. It is also optional

PEAR stand for Php Extension and Application Repository.PEAR provides:

  • A structured library of code
  • maintain a system for distributing code and for managing code packages
  • promote a standard coding style
  • provide reusable components.

Unlink: Is used to remove a file from server.
usage:unlink(‘path to file’);

Unset: Is used unset a variable.
usage: unset($var);

As HTTP is a stateless protocol. To maintain states on the server and share data across multiple pages PHP session are used. PHP sessions are the simple way to store data for individual users/client against a unique session ID. Session IDs are normally sent to the browser via session cookies and the ID is used to retrieve existing session data, if session id is not present on server PHP creates a new session, and generate a new session ID.

Example Usage:-

<?php 

// starting a session

session_start();

// Creating a session

$_SESSION['user_info'] = ['user_id' =>1,
'first_name' => 'Ramesh', 'last_name' => 'Kumar', 'status' => 'active']; // checking session if (isset($_SESSION['user_info'])) { echo "logged In"; } // un setting remove a value from session unset($_SESSION['user_info']['first_name']); // destroying complete session session_destroy(); ?>

It is an application-level package manager for PHP. It allows you to declare the libraries your project depends on and it will manage (install/update) them for you.

MIME stands for Multipurpose Internet Mail Extensions is an extension of the email protocol. It supports exchanging of various data files such as audio, video, application programs, and many others on the internet. It can also handle ASCII texts and Simple Mail Transport Protocol on the internet.

  • Session and cookie both are used to store values or data.
  • cookie stores data in your browser and a session is stored on the server.
  • Session destroys that when browser close and cookie delete when set time expires.

Include :-Include is used to include files more than once in single PHP script.You can include a file as many times you want.

Syntax:- include(“file_name.php”);

Include Once:-Include once include a file only one time in php script.Second attempt to include is ignored.

Syntax:- include_once(“file_name.php”);

Require:-Require is also used to include files more than once in single PHP script.Require generates a Fatal error and halts the script execution,if file is not found on specified location or path.You can require a file as many time you want in a single script.

Syntax:- require(“file_name.php”);

Require Once :-Require once include a file only one time in php script.Second attempt to include is ignored. Require Once also generates a Fatal error and halts the script execution ,if file is not found on specified location or path.

Syntax:- require_once(“file_name.php”);

NO, multiple inheritance is not supported by PHP

Static method is a member of class that is called directly by class name without creating an instance.In PHP uou can create a static method by using static keyword.

Example:

class A {
    public static function sayHello() {
        echo 'hello Static';
    }
}

A::sayHello();

There are 13 types of errors in PHP, We have listed all below

  • E_ERROR: A fatal error that causes script termination.
  • E_WARNING: Run-time warning that does not cause script termination.
  • E_PARSE: Compile time parse error.
  • E_NOTICE: Run time notice caused due to error in code.
  • E_CORE_ERROR: Fatal errors that occur during PHP initial startup.
    (installation)
  • E_CORE_WARNING: Warnings that occur during PHP initial startup.
  • E_COMPILE_ERROR: Fatal compile-time errors indication problem with script.
  • E_USER_ERROR: User-generated error message.
  • E_USER_WARNING: User-generated warning message.
  • E_USER_NOTICE: User-generated notice message.
  • E_STRICT: Run-time notices.
  • E_RECOVERABLE_ERROR: Catchable fatal error indicating a dangerous error
  • E_ALL: Catches all errors and warnings.

Default session time in PHP is 1440 seconds (24 minutes) and Default session storage path is temporary folder/tmp on server.

You can change default session time by using below code

<?php
// server should keep session data for AT LEAST 1 hour
ini_set('session.gc_maxlifetime', 3600);

// each client should remember their session id for EXACTLY 1 hour
session_set_cookie_params(3600);
?>

PHP (Hypertext Preprocessor) is an open source, a server-side scripting language that is used for the web applications development.

The web pages can be designed using HTML and the execution of the code is done on the user’s browser.

And, with PHP server-side scripting language, the code is executed on the server before being executed on the web browser of the user.

PHP programming language is considered as a friendly language with abilities to easily connect with Oracle, MySQL, and many other databases.

Uses and Application Areas of PHP

PHP scripts are used on popular operating systems like Linux, UNIX, Solaris, Windows, MAC OS, Microsoft and on many other operating systems. It supports many web servers that include Apache and IIS.
The use of PHP affords web developers the freedom to choose the operating system and the web server.
The following main areas of web development use the PHP programming language.

  • Command line scripting In this area of web development, with just the use of PHP parser, the PHP script is executed with no requirement of any server program or browser. This type of use of the PHP script is normally employed for simple and easy processing tasks.
  • Server-side scripting Server-side scripting is the main area of operation of PHP scripts in PHP. The following are involved in Server-side scripting:
    • Web server – It is a program that executes the files from web pages, from user requests.
    • Web browser – It is an application that is used to display the content on WWW (World Wide Web).
    • PHP parser – It is a program that converts the human-readable code into a format that is easier to understand by the computer.
  • Desktop Application Development PHP is also used to create the client-side application such as desktop applications. They are usually characterized by the GUI (Graphic User Interface). The client-side applications can be developed with knowledge of using the advanced features like PHP-GTK.

Advantages of PHP

Now, let's have a quick look at why PHP is used; that is the advantages of PHP.

  • Open Source 
    PHP is an open source software, which means that it is freely available for modifications and redistribution, unlike any other programming language. There is also an active team of PHP developers who are ready to provide any kind of technical support when needed.
  • Cross Platform
    The PHP programming language is easy to use and modify and is also highly compatible with the leading operating systems as well as the web browsers. And, that made the deployment of the applications across a wide range of operating systems and browsers much easier than before.
    PHP not only supports the platforms like Linux, Windows, Mac OS, Solaris but is also applied to web servers like Apache, IIS and many others.
  • Suits Web Development
    PHP programming language perfectly suits the web development and can be directly embedded into the HTML code.

Also Read: PHP Interview Questions