PHP questions for Computer Science students.

What is PHP?

  • Hypertext Pre-processor (PHP) is open source server-side scripting language that is widely used for web development as well as general purpose language.
  • PHP scripts are executed on server.
  • PHP is used to make dynamic and interactive web pages.
  • The syntax is mostly borrowed from C, Java and Perl.

What are the steps involved to run PHP?

The steps which are involved and required to run PHP is as follows:
  • Set up the web environment.
  • Install MySQL.
  • Set up the web servers.
  • 1. There are many web servers that are available and the mostly used is Apache which is automatically installed with linux distribution and on windows and it is easy to install.
    2. There are other servers like IIS (Internet information server) provided by Microsoft can be used to set up the web environment.
  • Install PHP.
  • Update the system for changes.

How the web server interacts with the client in PHP?

  • After installing and configuring the PHP, the Web server looks for PHP code that is embedded in HTML file with its extension.
  • The extensions which are used are .php or .phtml.
  • When web server receives a request for the file with an appropriate extension, HTML statements are processed and PHP statements are executed on the server itself.
  • When the process gets over the output is being shown in HTML statements.

What is PEAR in PHP?

  • PHP PEAR (PHP Extension and Application Repository) is a framework and a distribution system that is used for reusable PHP components.
  • Its purpose is to provide a structured mechanism of open source code, a system for code distribution and package maintenance.
  • PEAR code is divided into packages which are registered in and downloaded from a central server at
  • The can be used for package management, release management, and account management.

Explain the purpose of output buffering in PHP.

  • Output buffering in PHP buffers a script output.
  • This buffer can be edited before returning it to the client.
  • Output buffering "send" cookies at any point in the script.
  • Cookies do not have to be necessarily sent near the start of page.
  • Output buffers are stackable and hence sending to output is by choice.

Describe session in PHP.

  • When a user logs in to the application, details are usually stored in a session variable.
  • This information is available to all pages in one application.
  • Sessions in PHP work using a unique id for each visitor.

Expain $_Server and $_Env.

  • PHP environment variables allow scripts to obtain types of data dynamically from the server.
  • $_SERVER and $_ENV arrays contain different information.
  • The information depends on the server and operating system being used.
  • Most of the information can be seen as an array for a particular server and operating system.
foreach($_SERVER as $key =>$value)
     echo “Key=$key, Value=$value\n”;

Explain echo vs. print statement.

echo() and print() are language constructs in PHP.
echo() can take multiple expressions.print cannot take multiple expressions.
echo doesn't return true or false value.print return true or false value based on success or failure.


a) echo can be use as follows.

echo "<h2>PHP is server-side scripting language!</h2>";

echo "Welcome to PHP!<br>";

echo "PHP ", "is ", "open ", "source", "langauge.";

Output file:
PHP is server-side scripting language!

Welcome to PHP!
PHP is open source language.

b) print can be use as follows.

print "<h2>PHP is Tool!</h2>";

print "Welcome to PHP!<br>";

print "PHP is open source language!";

Output file:
PHP is Tool!

Welcome to PHP!
PHP is open source language!

What is an array?Explain

An array is a special variable which can store more than one value at a time of similar data type.

Syntax: array();

There are three types of array in PHP

a) Indexed array or Numeric array:
  • This is an array with numeric index.

  • Example
    $names = array(“Mike”, “Perry”);

  • Above array can be interpreted as :- array[0]= mike and array[1]
b) Associative arrays:
  • An array in which each ID key is associated with a value.
  • This is an array with string as index.

  • Example
    $ages = array("mike"=>32, "perry"=>30);
c) Multidimensional arrays:
  • This is an array which contains one or more array and values are accessed using multiple indices.

  • Example:
    $families = array

What is use of super-global arrays in PHP?

  • Super global arrays are the built in arrays that can be used anywhere.
  • They are also called as auto-global variables as they can be used inside a function as well.
  • The arrays with the long names such as $HTTP_SERVER_VARS, must be made global before they can be used in an array.
  • This $HTTP_SERVER_VARS check your php.ini setting for long arrays.

What is the difference between $argv and $argc? Give example?

  • To pass the information into the script from outside, we can use the PHP CLI (Command line interface) method.
  • Suppose addition of two numbers has to be passed in PHP as follows:
  • Here the script name is add.php, and 2 and 3 are the numbers that has to be added by the script.
  • These numbers are available inside the script in an array called $argv.
  • This array contains all the information on the command line; the statement is stored as follows:

  • $argv[0] = add.php
    $argv[1] = 2
    $argv[2] = 3

  • So, $argv always contains at least one element — the script name.
  • Then, you can use the following statements in your script:

  • $sum = $argv[1] + $argv[2];
    echo $sum;

  • $argc is a variable that stores the numbers of elements in $argv. $argc is equal to at least 1, which is saved for the name of the script.

  • Example:
    $argc=3 using the above statements.

What are the different types of errors in PHP?

Type of errors in PHP are.

a) Notices:
  • Notices represents non-critical errors, i.e. accessing a variable that has not defined.
  • Such errors are not displayed to the user by default.(but ,we can change the setting to show error).
b) Warnings:
  • Warnings are more serious errors but they do not result as script termination. i.e calling include() a file which does not exist.
  • By default, these errors are displayed to the user.
c)Fatal errors:
  • Fatal errors are critical errors i.e. calling a non-existent function or class.
  • Fatal errors cause the immediate termination of the script.

Explain different types of messages available in PHP?

a) Error message
  • Error message is received when the script has some problem that is preventing it from running correctly. The message consists of the possible solution or identification of the problem.
  • Example : Parse error: occurs when something is missed while writing the script.
b) Warning message
  • Warning message is received when the script finds out some problem that doesn’t allow the script to run.
  • Warning message indicates something is wrong with the script.
  • Example : if you write print_r() instead of print_r($varname) then a warning will occur like this:
    Warning: print_r() expects at least 1 parameter, 0 given
    The script will execute in continuation.
c) Notice
  • Notice is received when there is a condition where a script might have an error or may be it is working.
  • Example : echo variables that don’t exist in the script.

Explain different types of statements which are present in PHP?

a)Simple statement:
  • These are the echo statements and end with a semicolon (;).
  • PHP ignores white spaces between simple statements and reads statements only if semicolon is present in the code.
b)Complex/Conditional statements:
  • These are the statements which deals with certain conditions that have to be executed to meet certain specific requirements.
  • These are if and else block or switch statements.
  • PHP reads the complete statement and doesn’t stop at the first semicolon it encounters.
  • It looks for starting and ending braces to end the execution.
c)Looping statements:
  • statements that are repeated in a block.
  • The feature that enables you to execute the statements repeatedly is called as loop.
  • Example: for loop, while loop, do..while loop.

Write a statement to show the joining of multiple comparisons in PHP?

PHP allows multiple comparisons to be grouped together to determine the condition of the statement.

It can be done by using the following syntax:

Comparison1 and |or| xor comparison2 and |or| xor comparison3 and |or| xor.

Then operators that are used with comparisons are as follows:

1. and : result is positive when both comparisons are true.
2. or : result is positive when one of the comparisons or both of the comparisons are true.
3. xor : result is positive when one of the comparisons is true but not both of the comparisons.


$resCity == “Reno” or $resState == “NV” and $name == “Sally”

What is MIME(Multi-purpose Internet Mail Extensions)?

  • MIME represents a standard way of classifying file types over the Internet.
  • Web servers and browsers have a list of MIME types, which facilitates file transfer of the same type in the same way.
  • A MIME type has two parts, type and subtype.
  • MIME is separated by slash (/).

  • Example:
    MIME for Microsoft Word files is an application and the subtype is msword, i.e. application/ msword.

How to execute a PHP script using command line?

  • PHP script using command line can be executed by using SAPI (Server Application programming Interface).
  • PHP code can be passed to execute directly by using SAPI.

  • Syntax:
    Php –r ‘print_r(get_defined_constanrs());

Explain the purpose of output buffering in PHP.

  • Output buffering in PHP buffers a script output.
  • This buffer can be edited before returning it to the client.
  • Without output buffering, PHP sends data to the web server as soon as it is ready.
  • Output buffering send cookies at any point in the script.
  • Cookies do not have to be necessarily sent near the start of page.
  • Output buffers are stackable and hence sending it to output is by choice.

Write down the syntax to know the number of days between two given dates using PHP?

The start date and end date can be found out as shown below

$date1= strotime($start_date);

$date2= strotime($end_date);

$date_diff = (($date1)- ($date2)) / (60*60*24)

What is the difference between $message and $$message?

$message is a simple variable.$$message is reference variable.
$message is a variable with a fixed name and it consists of a fixed value.$$message contains the variable itself.

Explain PHP is an embedded scripting language.

  • PHP is used as an embedded scripting language for the web.
  • PHP is embedded in HTML code.
  • HTML tags are used to enclose the PHP language.
  • HTML With PHP code is written in the same way as you write JavaScript in HTML.

Explain IDE in PHP.

  • IDE stands for Integrated Development environment.
  • IDE is a framework for developing applications.
  • It includes programming editor where you can edit and write the development programs.
The features of IDE are as follows:

1. Debugging: Debugging helps to find the bugs in a program.
2. Preview: Preview allows instant preview of the program you are writing.
3. Testing: We can test or check scripts using testing feature.
4. FTP: FTP helps to upload and download the file while connecting to the server.
5. Project management: Project management organizes scripts into projects, manages the files in the project, includes file checkout and check-in features.
6. Backups: Backups are created automatically of your Web site at periodic intervals.

How can we increase the execution time of a PHP script?

  • Syntax : void set_time_limit(int seconds)
  • Set the number of seconds to run script.
  • The default limit is 30 seconds or, if it exists, the max_execution_time value defined in the php.ini.
  • If seconds is set to zero, no time limit is imposed.
  • When called, set_time_limit() restarts the timeout counter from zero.
  • In other words, if the timeout is the default 30 seconds, and 25 seconds into script execution a call such as set_time_limit(20) is made, the script will run for a total of 45 seconds before timing out.

Explain type casting in PHP?

  • PHP automatically stores the data and interprets according to itself.
  • Type casting is a way to assign the variable according to requirement and not allowing PHP to assign it automatically.

  • Example:
    $newint = (int) $var1;

    $newfloat = (float) $var1;

    $newstring = (string) $var1;

  • The value in the variable on the right side of the equal sign is stored in the variable on the left side as the specified type.

Explain error handling in PHP?

  • PHP uses the trigger to print the error in a program.

  • Example:
    If ($height_of_door > $height_of_house)
         trigger_error(“Impossible condition”,E_USER_ERROR);

  • The E_USER_ERROR in the statement tells that the condition is an error.
  • Impossible condition is a string message which is displayed when an error is encountered.
  • If the condition comes out to be true then the following message is displayed as Fatal error: Impossible condition.
  • E_USER_WARNING or E_USER_NOTICE can be used instead of E_USER_ERROR, to have PHP to treat the condition as a warning or notice.
  • Own statements can be written to perform error handling actions such as send a message, log a message or stop the script.

  • Syntax:
    If ($height_of_door > $height_of_house)
         echo “This is impossible<br>”;

  • If $height_of_door is larger than $height_of_house, the message is echoed, and exit() stops the script.

What are escaping characters? Explain with an example?

  • Special characters are the characters that have some special meaning attached to it.

  • For example:
    $, #, % etc.
    A backslash (\) before the special symbol is known as escaping characters.

    Two strings produce the same output
    $string = ‘The variable name is $var1’;
    $string = “The variable name is \$var1”;

    The output from either string is the following:

    The variable name is $var1

    Suppose if we want to store a string as follows:
    $string = _Where is Tom’_s house_;
    echo $string;\

  • The string can be interpreted by PHP by putting a backslash (\) in front of the single quote.
  • The backslash tells PHP that the single quote does not have any special meaning; it’s just an apostrophe.

  • Example:
    $string = _Where is Tom\’_s house_;

What are the different components used in PHP for formatting?

The components that are used in PHP for formatting are as follows:

a) % : It indicates the start of the formatting instruction.

b) Padding character (pad) : It is used to fill out the string when the value to be formatted is smaller than the width assigned. Pad can be a space, 0, or any character preceded by a single quote (‘).

c) -: A symbol meaning to left-justify the characters.
  • If (-:) is not included, the characters are right-justified.
d) width:
  • The number of characters to use for the value.
  • If the value doesn’t fill the width, the padding character is used to pad the value.
  • Example:
    if the width is 5, the padding character is 0, and the value is 1, the output is 00001.
e) dec:
  • The number of decimal places to use for a number.
  • This value is preceded by a decimal point.
f) type : The type of value use s(string) for string, f (float) for numbers that you want to format with decimal places.

How to create reusable code in PHP?

  • Applications created in PHP perform the same task in same script or in different scripts.
  • Functions are used to create a reusable code.
  • Functions are group of PHP statements that performs a specific task.
A header and footer can be created for all the web pages which has to be used again and again.
echo ‘<img src=”greenrule.jpg” width=”100%” height=”7” />

<address>My Great Company

<br />1234 Wonderful Rd.

<br />San Diego, CA 92126


<p>or send questions to</p>

<a href=””>sales </a>

<img src=”greenrule.jpg” width=”100%” height=”7” />’

Instead of typing header and footer for each page, a function can be created and used to add footer and header in every page.

The function can be named as:

Explain PHP Heredoc?

  • PHP heredoc syntax allows creation of multiple lines of string without using quotations.

  • Example:
  • The heredoc starts with <<< operator and the multiple lines are followed. <<<identifier_name is used as an identifier making the start of heredoc.
  • To end the heredoc, the identifier is written again.

  • Syntax:

    $str = <<<Sample

    This is a sample string

    A sample string


    Echo $str;


Write the statements that are used to connect PHP with MySQL.


$conn = mysql_connect('localhost');

echo $conn;


Above statement gets the resource of the localhost.

Other different way in which you can connect to the database as follows:






What does ODBC do in context with PHP?

  • ODBC stands for Open Database connectivity, which can allow user to communicate with other databases like Access and IBM DB2.
  • PHP supports many databases like dBase, Microsft SQL Server, Oracle, etc.
  • ODBC connectivity also supports databases like filePro, FrontBase and InterBase.

What is the difference between PHP and JavaScript?

PHP is server side scripting language, which means that it can’t interact directly with the user.

JavaScript is client side scripting language, that is used to interact directly with the user.