INTERVIEW QUESTIONS » IMPORTANT PHP interview questions and answers for freshers

IMPORTANT PHP interview questions and answers for freshers

PHP is a web language based on scripts that allows developers to dynamically create generated web pages.

PHP means PHP: Hypertext Preprocessor.

i. PEAR means “PHP Extension and Application Repository”.

ii. It extends PHP and provides a higher level of programming for web developers.

A PHP scripting block can be placed anywhere in the document.
<?php
?>

Version 5 is the actually used version of PHP.

The latest stable version of PHP is 5.5.14 released at 27 June 2014 .

i. Software in which the source codes are freely used, modify, and shared by anyone are called Open Source Software.
ii. These can also be distributed under licenses that adhere with the Open Source Definition.

There are 4 basically types of error.
1. Parse Error – Commonly caused due to syntax mistakes in codes e.g. missing semicolon, mismatch brackets.
2. Fatal Error – These are basically run time errors which are caused when you try to access what can’t be done. E.g. accessing a dead object, or trying to use a function that hasn’t been declared.
3. Warning Error – These occurs when u try to include a file that is not present, or delete a file that is not on the server. This will not halt the script; it will give the notice and continue with the next line of the script.
4. Notice Error – These errors occurs when u try to use a variable that hasn’t been declared, this will not halt the script, It will give the notice and continue with the next line of the script.

PHP4–> cannot support oops concepts and Zend engine 1 is used.
PHP5–> supports oops concepts and Zend engine 2 is used. Error supporting is increased in PHP5. XML and SQLLite will is increased in PHP5.

To write the hello world program in PHP using variable you need to know the tags that are used in HTML and the PHP coding which is as follows:
<html>
<head><title>Hello World Script using Variable</title></head>
<body>
<?php
$salutation = “Hello World!”;
echo “<p>$salutation</p>”;
?>
</body>
</html>

1) It is future enhancement.
2) It is object oriented programming.
3) It is implemented in our real life system.
4) It used bottom up approach.
5) It used class and object.
6) C++ fully based on oops.

A session is a logical object created by the PHP engine to allow you to preserve data across subsequent HTTP requests.

A cookie is often used to identify a user. A cookie is a small file that the server embeds on the user’s computer. Each time the same computer requests a page with a browser, it will send the cookie too. With PHP, you can both create and retrieve cookie values.

Eg : var $arr = array(‘apple’, ‘grape’, ‘lemon’);

It is used to print a data in the webpage,

Example: <?php echo ‘Car insurance’; ?> ,

The following code print the text in the webpage

This is not actually a real function, It is a language construct. So you can use with out parentheses with its argument list.
Example : print(‘PHP Interview questions’);
print (‘Job Interview ‘);

1. include() statement includes and evaluates a specified line i.e. it will include a file based in the given path.
2. require() does the same thing expect upon failure it will generate a fatal error and halt the script whereas include() will just gives a warning and allow script to continue.
3. require_once() will check if the file already has been included and if so it will not include the file again.
4. include_once() statement can be used to include a php file in another one, when you may need to include the called file more than once. If it is found that the file has already been included, calling script is going to ignore further inclusions.

1. PHP files may contain text, HTML tags and scripts.

2. PHP files are returned to the browser as plain HTML.
3. PHP files have a file extension of “.php”, “.php3”, or “.phtml”.

1.MySQL is a database server.
2.MySQL is ideal for both small and large applications.

3.MySQL supports standard SQL.

4.MySQL compiles on a number of platforms.

5.MySQL is free to download and use.

PHP combined with MySQL are cross-platform (means that you can develop in Windows and serve on a Unix platform).

1. PHP runs on different platforms (Windows, Linux, Unix, etc.).
2. PHP is compatible with almost all servers used today (Apache, IIS, etc.).

3. PHP is FREE to download from the official PHP resource: www.php.net.
4. PHP is easy to learn and runs efficiently on the server side.

Common uses of PHP −
• PHP performs system functions, i.e. from files on a system it can create, open, read, write, and close them.
• PHP can handle forms, i.e. gather data from files, save data to a file, thru email you can send data, return data to the user.
• You add, delete, modify elements within your database thru PHP.
• Access cookies variables and set cookies.
• Using PHP, you can restrict users to access some pages of your website.
• It can encrypt data.

PHP has a total of eight data types which we use to construct our variables Integers − are whole numbers, without a decimal point, like 4195.
i. Doubles − are floating-point numbers, like 3.14159 or 49.1.
ii. Booleans − have only two possible values either true or false.
iii. NULL − is a special type that only has one value: NULL.
iv. Strings − are sequences of characters, like ‘PHP supports string operations.’
v. Arrays − are named and indexed collections of other values.
vi. Objects − are instances of programmer-defined classes, which can package up both other kinds of values and functions that are specific to the class.
vii. Resources − are special variables that hold references to resources external to PHP (such as database connections).

The foreach statement is used to loop through arrays. For each pass the value of the current array element is assigned to $value and the array pointer is moved by one and in the next pass next element will be processed.
foreach (array as value)
{
code to be executed;
}

1. The PHP $_REQUEST variable contains the contents of both $_GET, $_POST, and $_COOKIE.

2. We will discuss $_COOKIE variable when we will explain about cookies.
3. The PHP $_REQUEST variable can be used to get the result from form data sent with both the GET and POST methods.

GET function is normally used when the result of using this function does not cause any visible change on the state of the world. This is normally used in cases like searching from a database where no changes are made to the system.

POST function is used when there is a visible change on the state of the world. When we use a POST function, normally the system changes. E.g. when we add, delete or modify values from a database. A simple example is a form submission.

i. Directory,
ii. stdClass,
iii.__PHP_Incomplete_Class,
iv. Exception,
v. php_user_filter,

i. Multipurpose Internet Mail Extensions.

ii. WWW ability to recognize and handle files of different types is largely dependent on the use of the MIME (Multipurpose Internet Mail Extensions) standard. The standard provides for a system of registration of file types with information about the applications needed to process them.

We can include a file using “include() ” or “require()” function with file path as its parameter.

i).htaccess files are configuration files of Apache Server which provide a way to make configuration changes on a per-directory basis.
ii) A file, containing one or more configuration directives, is placed in a particular document directory, and the directives apply to that directory, and all subdirectories there of.

Java script submit() function is used for submit form without submit button on click call document.formname.submit()

In general, a framework is a real or conceptual structure intended to serve as a support or guide for the building of something that expands the structure into something useful.
Advantages : Consistent Programming Model Direct Support for Security Simplified Development Efforts Easy Application Deployment and Maintenance.

Array:
1) It is a complex variable.
2) It store multiple or a group of values under a single variable name.
Types:
1) Index array –> with a numeric key.
2) Associative array –> where each key has its own specific value.
3) Multidimensional array –> it containing one or more arrays within itself.

unlink() deletes the given file from the file system.
unset() makes a variable undefined.

1. To know the Image type use exif_imagetype () function.
2. To know the Image size use getimagesize () function.
3. To know the image width use imagesx () function.
4. To know the image height use imagesy() function.

Javascript is a client side scripting language whereas PHP is a server side scripting language.

mysql_connect(servername,username,password);

mysql_select_db($db_name);

$my_qry = mysql_query(“SELECT * FROM `users` WHERE `u_id`=’1′; “);

$result = mysql_fetch_array($my_qry);
echo $result[‘First_name’];

Loops in PHP are used to execute the same block of code a specified number of times.
PHP supports following four loop types:
• for − loops through a block of code a specified number of times.
• while − loops through a block of code if and as long as a specified condition is true.
• do…while − loops through a block of code once, and then repeats the loop as long as a special condition is true.
• foreach − loops through a block of code for each element in an array.
We will discuss about continue and break keywords used to control the loops execution.

i. NULL is a special type that only has one value: NULL. To give a variable the NULL value, simply assign it like this −
$my_var = NULL;
ii. The special constant NULL is capitalized by convention, but actually it is case insensitive; you could just as well have typed −
$my_var = null;
iii. A variable that has been assigned NULL has the following properties:
a.It evaluates to FALSE in a Boolean context.
b.It returns FALSE when tested with IsSet() function.

The foreach statement is used to loop through arrays. For each pass the value of the current array element is assigned to $value and the array pointer is moved by one and in the next pass next element will be processed.

foreach (array as value)
{
code to be executed;

}

1.The PHP header() function supplies raw HTTP headers to the browser and can be used to redirect it to another location.
The redirection script should be at the very top of the page to prevent any other part of the page from loading.
2.The target is specified by the Location: header as the argument to the header() function.
3.After calling this function the exit() function can be used to halt parsing of rest of the code.

sort() − Sorts an array.

Singly quoted strings are treated almost literally, whereas doubly quoted strings replace variables with their values as well as specially interpreting certain character sequences.
<?php
$variable = “name”;
$literally = ‘My $variable will not print!\\n’;
print($literally);
print “<br />”;
$literally = “My $variable will print!\\n”;
print($literally);
?>
This will produce following result −
My $variable will not print!\n
My name will print

1.Interfaces are defined to provide a common function names to the implementors.
2.Different implementors can implement those interfaces according to their requirements.
3.You can say, interfaces are skeltons which are implemented by developers.

PHP 5 introduces the final keyword, which prevents child classes from overriding a method by prefixing the definition with final.
If the class itself is being defined final then it cannot be extended.

Sql:
1) It is structured query language.
2) It is declarative computer language aimed at querying relational database.
Mysql:
1) It is a relational database.
2) It is a piece of software optimized for data storage and retrieval.

we can update 64 columns in single mysql query.

Data Definition Language deals with database schemas and descriptions of how the data should reside in the database, therefore language statements like CREATE TABLE or ALTER TABLE belong to DDL.
DML deals with data manipulation, and therefore includes most common SQL statements such SELECT, INSERT etc.
Data Control Language includes commands such as GRANT, and mostly concerns with rights, permissions and other controls of the database system.

Mysql_connect:

– Opens a new connection to the database
– The database connection can be closed
– Opens the page every time the page is loaded.

Mysql_pconnect:

– Opens a persistent connection to the database.
– The database connection can not be closed.
– The page need not be opened every time the page is loaded.

i. PHP Driver
ii. JDBC Driver
iii.ODBC Driver
iv. C WRAPPERPYTHON
v. DriverPERL Driver
vi. RUBY Driver
vii.CAP11PHP Driver

$_COOKIE – Stored information in Cookie.
$_GET – Send data in URL as paramter.
$_POST – Send data to server OR Page as POST Method.
$_REQUEST – Its combined array containing values from the $_GET, $_POST and $_COOKIES.
$_ENV – Get/Set varaible in environment.
$_FILES – Used to upload the file, image and videos etc.
$_SERVER – It have all server information.
$GLOBALS – Contains all the global variables associated with the current script.

echo $_SESSION[‘REMOTE_ADDR’];

1.Single inheritance.
2.Multiple Level inheritance.

1.CMS means Content Management System. It is web software who provide the website full fledged content management where owner can add/update/delete the html/files/images/videos etc.
Today’s CMS do much more as compare to its name.
For example: Joomla/Wordpress is CMS but you can create E-commererce website and can accpet all types of payment.
2.In Today’s CMS, you can create many different types of website like Ecommerce website, Personal blogging and Forum etc.

echo md5(uniqid(rand(), true));

SQL or Structured Query Language is a programming language designed for managing data held in a Relational Database Management System. Mysql is a open source, relational database management System.

Group By is used for retrieving information about a group of data. It is generally used with some aggregate function like SUM, AVG etc. ORDER BY is used to sort the records using column name. It can sort column in both ascending and descending order.

1.When we have to fetch records from more than one table we can use JOIN keyword.
The process is known as joining the tables.
2.There are various types of join like INNER JOIN, LEFT JOIN, RIGHT JOIN, and OUTER JOIN.

INNER Join compares two tables and only returns results where a match exists. Records from the 1st table are duplicated when they match multiple results in the 2nd. INNER joins tend to make result sets smaller, but because records can be duplicated this isn’t guaranteed.
LEFT join means keep all records from the 1st table no matter what and insert NULL values when the 2nd table doesn’t match.
RIGHT Join means the opposite: keep all records from the 2nd table no matter what and insert NULL values when the 1st table doesn’t match.

You must take personal responsibility.
You cannot change the circumstances,
the seasons, or the wind, but you can change yourself.
That is something you have charge of.

You must take personal responsibility.
You cannot change the circumstances,
the seasons, or the wind, but you can change yourself.
That is something you have charge of.

You must take personal responsibility.
You cannot change the circumstances,
the seasons, or the wind, but you can change yourself.
That is something you have charge of.

You must take personal responsibility.
You cannot change the circumstances,
the seasons, or the wind, but you can change yourself.
That is something you have charge of.

You must take personal responsibility.
You cannot change the circumstances,
the seasons, or the wind, but you can change yourself.
That is something you have charge of.

You must take personal responsibility.
You cannot change the circumstances,
the seasons, or the wind, but you can change yourself.
That is something you have charge of.

You must take personal responsibility.
You cannot change the circumstances,
the seasons, or the wind, but you can change yourself.
That is something you have charge of.

Lorem ipsum dolor sit amet, consectetur adipisicing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna
aliqua. Ut enim ad minim veniam, quis nostrud exercitation
ullamco laboris nisi ut aliquip ex ea commodo consequat

Change does not roll in on the wheels of inevitability,
but comes through continuous struggle.
And so we must straighten our backs and work for
our freedom. A man can’t ride you unless your back is bent.

You must take personal responsibility.
You cannot change the circumstances,
the seasons, or the wind, but you can change yourself.
That is something you have charge of.

You must take personal responsibility.
You cannot change the circumstances,
the seasons, or the wind, but you can change yourself.
That is something you have charge of.

You must take personal responsibility.
You cannot change the circumstances,
the seasons, or the wind, but you can change yourself.
That is something you have charge of.

You must take personal responsibility.
You cannot change the circumstances,
the seasons, or the wind, but you can change yourself.
That is something you have charge of.

You must take personal responsibility.
You cannot change the circumstances,
the seasons, or the wind, but you can change yourself.
That is something you have charge of.

You must take personal responsibility.
You cannot change the circumstances,
the seasons, or the wind, but you can change yourself.
That is something you have charge of.

You must take personal responsibility.
You cannot change the circumstances,
the seasons, or the wind, but you can change yourself.
That is something you have charge of.

You must take personal responsibility.
You cannot change the circumstances,
the seasons, or the wind, but you can change yourself.
That is something you have charge of.

Lorem ipsum dolor sit amet, consectetur adipisicing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna
aliqua. Ut enim ad minim veniam, quis nostrud exercitation
ullamco laboris nisi ut aliquip ex ea commodo consequat

Change does not roll in on the wheels of inevitability,
but comes through continuous struggle.
And so we must straighten our backs and work for
our freedom. A man can’t ride you unless your back is bent.

You must take personal responsibility.
You cannot change the circumstances,
the seasons, or the wind, but you can change yourself.
That is something you have charge of.

You must take personal responsibility.
You cannot change the circumstances,
the seasons, or the wind, but you can change yourself.
That is something you have charge of.

You must take personal responsibility.
You cannot change the circumstances,
the seasons, or the wind, but you can change yourself.
That is something you have charge of.

You must take personal responsibility.
You cannot change the circumstances,
the seasons, or the wind, but you can change yourself.
That is something you have charge of.

You must take personal responsibility.
You cannot change the circumstances,
the seasons, or the wind, but you can change yourself.
That is something you have charge of.

You must take personal responsibility.
You cannot change the circumstances,
the seasons, or the wind, but you can change yourself.
That is something you have charge of.

You must take personal responsibility.
You cannot change the circumstances,
the seasons, or the wind, but you can change yourself.
That is something you have charge of.

You must take personal responsibility.
You cannot change the circumstances,
the seasons, or the wind, but you can change yourself.
That is something you have charge of.

Lorem ipsum dolor sit amet, consectetur adipisicing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna
aliqua. Ut enim ad minim veniam, quis nostrud exercitation
ullamco laboris nisi ut aliquip ex ea commodo consequat

Change does not roll in on the wheels of inevitability,
but comes through continuous struggle.
And so we must straighten our backs and work for
our freedom. A man can’t ride you unless your back is bent.

You must take personal responsibility.
You cannot change the circumstances,
the seasons, or the wind, but you can change yourself.
That is something you have charge of.

You must take personal responsibility.
You cannot change the circumstances,
the seasons, or the wind, but you can change yourself.
That is something you have charge of.

You must take personal responsibility.
You cannot change the circumstances,
the seasons, or the wind, but you can change yourself.
That is something you have charge of.

You must take personal responsibility.
You cannot change the circumstances,
the seasons, or the wind, but you can change yourself.
That is something you have charge of.

You must take personal responsibility.
You cannot change the circumstances,
the seasons, or the wind, but you can change yourself.
That is something you have charge of.

You must take personal responsibility.
You cannot change the circumstances,
the seasons, or the wind, but you can change yourself.
That is something you have charge of.

You must take personal responsibility.
You cannot change the circumstances,
the seasons, or the wind, but you can change yourself.
That is something you have charge of.

You must take personal responsibility.
You cannot change the circumstances,
the seasons, or the wind, but you can change yourself.
That is something you have charge of.