PHP Interview Questions

PHP Interview Questions Download PHP Interview Questions PDF

PHP Interview Questions

GD is an open source library for creating dynamic images.

  • PHP uses GD library to create PNG, JPEG and GIF images.
  • It is also used for creating charts and graphics on the fly.
  • GD library requires an ANSI C compiler to run.

Sample code to generate an image in PHP

<?php
	header("Content-type: image/png");

	$string = $_GET['text'];
	$im = imagecreatefrompng("images/button1.png");
	$mongo = imagecolorallocate($im, 220, 210, 60);
	$px = (imagesx($im) - 7.5 * strlen($string)) / 2;
	imagestring($im, 3, $px, 9, $string, $mongo);
imagepng($im); imagedestroy($im); ?>

NO, multiple inheritance is not supported by PHP

Defining a Constant in PHP

define('CONSTANT_NAME',value); 

Mbstring

Mbstring is an extension used in PHP to handle non-ASCII strings. Mbstring provides multibyte specific string functions that help us to deal with multibyte encodings in PHP. Multibyte character encoding schemes are used to express more than 256 characters in the regular byte-wise coding system. Mbstring is designed to handle Unicode-based encodings such as UTF-8 and UCS-2 and many single-byte encodings for convenience PHP Character Encoding Requirements.

You may also Like Codeigniter interview questions

Below are some features of mbstring

  1. It handles the character encoding conversion between the possible encoding pairs.
  2. Offers automatic encoding conversion between the possible encoding pairs.
  3. Supports function overloading feature which enables to add multibyte awareness to regular string functions.
  4. Provides multibyte specific string functions that properly detect the beginning or ending of a multibyte character. For example, mb_strlen() and mb_split()

<?php
$tomorrow = mktime(0, 0, 0, date(“m”) , date(“d”)+1, date(“Y”));
$lastmonth = mktime(0, 0, 0, date(“m”)-1, date(“d”), date(“Y”));
echo ($tomorrow-$lastmonth)/86400;
?>

In PHP, one can specify two different submission methods for a form. The method is specified inside a FORM element, using the METHOD attribute. The difference between METHOD=”GET” (the default) and METHOD=”POST” is primarily defined in terms of form data encoding. According to the technical HTML specifications, GET means that form data is to be encoded (by a browser) into a URL while POST means that the form data is to appear within the message body of the HTTP request.

 GetPost
History:Parameters remain in browser history because they are part of the URLParameters are not saved in browser history.
Bookmarked:Can be bookmarked.Can not be bookmarked.
BACK button/re-submit behavior:GET requests are re-executed but may not be re-submitted to the server if the HTML is stored in the browser cache.The browser usually alerts the user that data will need to be re-submitted.
Encoding type (enctype attribute):application/x-www-form-urlencodedmultipart/form-data or application/x-www-form-urlencoded Use multipart encoding for binary data.
Parameters:can send but the parameter data is limited to what we can stuff into the request line (URL). Safest to use less than 2K of parameters, some servers handle up to 64KCan send parameters, including uploading files, to the server.
Hacked:Easier to hack for script kiddiesMore difficult to hack
Restrictions on form data type:Yes, only ASCII characters allowed.No restrictions. Binary data is also allowed.
Security:GET is less secure compared to POST because data sent is part of the URL. So it’s saved in browser history and server logs in plaintext.POST is a little safer than GET because the parameters are not stored in browser history or in web server logs.
Restrictions on form data length:Yes, since form data is in the URL and URL length is restricted. A safe URL length limit is often 2048 characters but varies by browser and web server.No restrictions
Usability:GET method should not be used when sending passwords or other sensitive information.POST method used when sending passwords or other sensitive information.
Visibility:GET method is visible to everyone (it will be displayed in the browsers address bar) and has limits on the amount of information to send.POST method variables are not displayed in the URL.
Cached:Can be cachedNot Cached
Large variable values:7607 characters maximum size.8 Mb max size for the POST method.

In PHP all functions starting with __ names are magical functions/methods. Magical methods always lives in a PHP class.The definition of magical function are defined by programmer itself.

Here are list of magic functions available in PHP

__construct(), __destruct(), __call(), __callStatic(), __get(), __set(), __isset(), __unset(), __sleep(), __wakeup(), __toString(), __invoke(), __set_state(), __clone() and __debugInfo() .

A database provides more flexibility and reliability than does logging to a file. It is easy to run queries on databases and generate statistics than it is for flat files. Writing to a file has more overhead and will cause your code to block or fail in the event that a file is unavailable. Inconsistencies caused by slow replication in AFS may also pose a problem to errors logged to files. If you have access to MySQL, use a database for logs, and when the database is unreachable, have your script automatically send an e-mail to the site administrator.

PHP is a server side scripting language for creating dynamic web pages. There are so many functions available for displaying output in PHP. Here, I will explain some basic functions for displaying output in PHP. The basic functions for displaying output in PHP are as follows:

  • print() Function
  • echo() Function
  • printf() Function
  • sprintf() Function
  • Var_dump() Function
  • print_r() Function

You can access standard error stream in PHP by using following code snippet:

$stderr = fwrite("php://stderr");

$stderr = fopen("php://stderr", "w");

$stderr = STDERR;

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

Difference between echo and print in PHP

echo in PHP

  • echo is language constructs that display strings.
  • echo has a void return type.
  • echo can take multiple parameters separated by comma.
  • echo is slightly faster than print.

Print in PHP

  • print is language constructs that display strings.
  • print has a return value of 1 so it can be used in expressions.
  • print cannot take multiple parameters.
  • print is slower than echo.

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.

The default max execution time for PHP scripts is set to 30 seconds. If a php script runs longer than 30 seconds then PHP stops the script and reports an error.
You can increase the execution time by changing max_execution_time directive in your php.ini file or calling ini_set(‘max_execution_time’, 300); //300 seconds = 5 minutes function at the top of your php script.