PHP Interview Questions and Answers
We are providing you an useful set of PHP questions and answers. We hope this will help you to clear a job interview at freshers and experienced level. It covers both basic and advanced PHP interview questions and answers. These Questions are useful for anyone to get a better job opportunity. Experienced candidates can check their knowledge with the help of this PHP questions list. There are roughly 50 questions and answers here :
Que1 – What is the name of the father of PHP ?
Ans - Rasmus Lerdorf is known as the father of PHP.
Que2 – What is the method of submit a form without a submit button?
Ans - Java script submit() function is used for submit form without submit button on click call document.formname.submit()
Que3 – In how many ways we can retrieve the data in the result set of MySQL using PHP?
Ans - We can do it by 4 Ways
1. mysql_fetch_row. ,
2. mysql_fetch_array ,
3. mysql_fetch_object
4. mysql_fetch_assoc
Que4 – What is the difference between mysql_fetch_object and mysql_fetch_array?
Ans - mysql_fetch_object() is similar tomysql_fetch_array(), withone difference -an object is returned, instead of array.Indirectly, that means you can only access the data by the field names,and not by their offsets (numbers are illegal property names
Que5 - What is the Mthod to create a database using PHP and MySQL?
Ans - We can create MySQL database with the use of mysql_create_db(“Database Name”)
Que6 – What is use of header() function in php ?
Ans - The header() function sends a raw HTTP header to a client. We can use herder()function for redirection of pages. It is important to notice that header() mustbe called before any actual output is seen.
Que7 -Shopping cart online validation i.e. how can we configure Paypal,etc.?
Ans - Nothing more we have to do only redirect to the payPal url after submit all information needed by paypal like amount,adresss etc.
Que8 – What is htaccess? Why do we use this and Where?
Ans - Htaccess files are configuration files of Apache Server which provide a way to make configuration changes on a per-directory basis. 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 thereof.
Que9 – How we get IP address of client, previous reference page etc?
Ans - By using $_SERVER['REMOTE_ADDR'],$_SERVER['HTTP_REFERER']
Que10 -What are the reasons for selecting lamp (Linux, apache,MySQL, PHP) instead of combination of other software programs, servers andoperating systems?
Ans - All of those are open source resource. Security of Linux is very very more than windows. Apache is a better server that IIS both in functionality and security. MySQL is world most popular open source database. PHP is more faster that asp or any other scripting language.
Qqe11 - What are the different tables present in MySQL, which type of table is generated when we are creating a table in this syntax: create table employee (eno int(2),ename varchar(10)) ?
Ans – Total 5 types of tables we can create
1. MyISAM
2. Heap
3. Merge
4. INNO DB
5. ISAM
MyISAM is the default storage engine as of MySQL 3.23 and as a result if we do not specify the table name explicitly it will be assigned to the default engine.
Que12 - What is framework? how it works? what is advantage?
Ans - 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.
Que13 -What is CURL?
Ans - CURL means Client URL Librarycurl is a command line tool for transferring files with URL syntax,
supporting FTP, FTPS, HTTP, HTTPS, SCP, SFTP, TFTP, TELNET, DICT, LDAP, LDAPS and FILE. curl supports SSL certificates, HTTP POST, HTTP PUT, FTP uploading, HTTP form based upload, proxies, cookies, user+password authentication (Basic, Digest, NTLM, Negotiate, kerberos…), file transfer resume, proxy tunneling and abusload of other useful tricks. CURL allows you to connect and communicate to many different types of servers with many different types of protocols. libcurl currently supports the http, https, ftp, gopher, telnet, dict, file, and ldap protocols. libcurl also supports HTTPS certificates, HTTP POST, HTTP PUT, FTP uploading (this can also be done with PHP’s ftp
extension), HTTP form based upload, proxies, cookies, and user+password authentication.
Que14 – What Is a Persistent Cookie?
Ans - A persistent cookie is a cookie which is stored in a cookie file permanently on the browser’s computer.
By default, cookies are created as temporary cookies which stored only in the browser’s memory. When the browser is closed, temporary cookies will be erased. You should decide when to use temporary cookies and when to use persistent cookies based on their differences:
*Temporary cookies can not be used for tracking long-term information.
*Persistent cookies can be used for tracking long-term information.
*Temporary cookies are safer because no programs other than the browser can access them.
*Persistent cookies are less secure because users can open cookie files see the cookie values
Que15 – What’s the special meaning of __sleep and __wakeup?
Ans - __sleep returns the array of all the variables than need to be saved, while __wakeup retrieves them.
Que16 – Why doesn’t the following code print the newline properly?
<?php
$str = ‘Hello, therenHow are younThanks for visiting TechInterviews’;
print $str;
?>
Ans - Because inside the single quotes the n character is not interpreted as newline, just as a sequence of two characters – and n.
Que17 – How do you find out the number of parameters passed into function?
Ans - func_num_args() function returns the number of parameters passed in.
Que18 – What are the differences between require and include?
Ans - Both include and require used to include a file but when included file not found Include send Warning where as Require send Fatal Error.
Que19 – Can we use include (“xyz.PHP”) two times in a PHP page”index.PHP”?
Ans - Yes we can use include(“xyz.php”) more than one time in any page. but it create a prob when xyz.php file contain some functions declaration then error will come for already declared function in this file else not a prob like if you want to show same content two time in page then must incude it two time not a prob.
Que20 – How can you execute a PHP script using command line?
Ans - Just run the PHP CLI (Command Line Interface) program and provide the PHP script file name as the command line argument.
Que21 – Suppose your Zend engine supports the mode <? ?> Then
how can uconfigure your PHP Zend engine to support <?PHP ?> mode ?
Ans – In php.ini file:set short_open_tag=onto make PHP support
Que22 – How can we encrypt and decrypt a data present in a MySQL table using MySQL?
Ans - AES_ENCRYPT () and AES_DECRYPT ()
Que23 – What are the differences between procedure-oriented languages and object-oriented languages?
Ans – There are lot of difference between procedure language and object oriented like below
1>Procedure language easy for new developer but complex tounderstand whole software as compare to object oriented model
2>In Procedure language it is difficult to use design pattern mvc ,Singleton pattern etc but in OOP you we able to develop design pattern
3>IN OOP language we able to ree use code like Inheritancepolymorphism etc but this type of thing not available in procedure language on that our Fonda use COPY and PASTE .
Que24 – What is the functionality of the function htmlentities?
Ans - Convert all applicable characters to HTML entities. This function is identical to htmlspecialchars() in all ways, except with htmlentities(), all characters which have HTML character entity equivalents are translated into these entities.
Que25 - 32 How can we convert the time zones using PHP?
Ans - By using date_default_timezone_get anddate_default_timezone_set function on PHP 5.1.0
<?php
// Discover what 8am in Tokyo relates to on the East Coastof the US
// Set the default timezone to Tokyo time:
date_default_timezone_set(‘Asia/Tokyo’);
// Now generate the timestamp for that particular
timezone, on Jan 1st, 2000
$stamp = mktime(8, 0, 0, 1, 1, 2000);
// Now set the timezone back to US/Eastern
date_default_timezone_set(‘US/Eastern’);
// Output the date in a standard format (RFC1123), this
will print:
// Fri, 31 Dec 1999 18:00:00 EST
echo ‘<p>’, date(DATE_RFC1123, $stamp) ,’</p>’;?>
Que26 – What is the difference between the functions unlink andunset?
Ans - Uunlink() deletes the given file from the file system.unset() makes a variable undefined.
Que27 – How can we get the properties (size, type, width, height) ofanimage using PHP image functions?
Ans - To know the Image type use exif_imagetype () function
To know the Image size use getimagesize () function
To know the image width use imagesx () function
To know the image height use imagesy() function t
Que28 -What is the maximum size of a file that can be uploadedusing PHP and how can we change this?
Ans - By default the maximum size is 2MB. and we can change the following
setup at php.iniupload_max_filesize = 2M
Que29 – How can we optimize or increase the speed of a MySQL select query?
Ans -
First of all instead of using select * from table1, use selectcolumn1, column2, column3.. from table1
Look for the opportunity to introduce index in the table you are querying.
use limit keyword if you are looking for any specific number of rows from the result set.
Que30 – How many ways we can pass the variable through the navigation between the pages?
Ans - GET/QueryString POST
Que31 – What is session_set_save_handler in PHP?
Ans - session_set_save_handler() sets the user-level session storage functions which are used for storing and retrievin associated with a session. This is most useful when a storage method other than those supplied by PHP sessions is preferred. i.e. Storing the session data in a local database.
Que32 – List the name of some tools through which we can draw E-R diagrams for mysql.
Ans - Case Studio Smart Draw
Que33 – List out the predefined classes in PHP?
Ans - Directory stdClass
__PHP_Incomplete_Class exception
php_user_filter
Que34 – How can you load data from a text file into a table?
Ans - You can use LOAD DATA INFILE file_name; syntax to load data from a text file. but you have to make sure that
a) data is delimited
b) columns and data matched correctly.
Que35 – What are default session time and path?
Ans - Default session time in PHP is 1440 seconds or 24 minutes
Default session save path id temporary folder /tmp
Que36 -Which library we will need for image work ?
Ans - We will need to compile PHP with the GD library of image functions for this to work. GD and PHP may also require other libraries, depending on which image formats you want to work with.
Que37 – What is Joomla in PHP?
Ans - Joomla is a content management system. Powerful online applications and web sites are build using Joomla.
Joomla is an open source CMS tool. Clients can easily manage their web sites with minimal amount of instructions. It is highly extensible. Joomla runs off PHP or MySQL. Joomla is used to create, maintain a structured, flexible portal, add or edit content, changes the look and feel of the site. PHP scripting is used and persisted most of its data / information in MySQL database.
Que38 -What is zend engine in PHP?
Ans - Zend engine is like a virtual machine and is an open source, and is known for its role in automating the web using PHP. Zend is named after its developers Zeev and Aandi. Its reliability, performance and extensibility has a significant role in increasing the PHP’s popularity. The Zend Engine II is the heart of PHP 5. It is an open source project and freely available under BSD style license.
Que39 – What are the different types of Errors in PHP?
Ans - There are three basic types of runtime errors in PHP:
1. Notices: These are small, non-critical errors that PHP encounters while executing a script – for example, accessing a variable that has not yet been defined. By default, such errors are not displayed to the user at all – although the default behavior can be changed.
2. Warnings: Warnings are more severe errors like attempting to include() a file which does not exist. By default, these errors are displayed to the user, but they do not result in script termination.
3. Fatal errors: These are critical errors – for example, instantiating an object of a non-existent class, or calling a non-existent function. These errors cause the immediate termination of the script, and PHP’s default behavior is to display them to the user when they take place.
Que40 – What is Type juggle in php?
Ans - In order to declare a variable in PHP, the type of the variable is not required. The data type is determined by the value / context of the variable. If integer value is assigned to the variable $num, then the variable $num is of the type integer. If string value is assigned to the variable $str, the variable $str is of the type string.
Que41- What is the difference between Notify URL and Return URL?
Ans - Notify URL and Return URL is used in Paypal Payment Gateway integration. Notify URL is used by PayPal to post information about the transaction. Return URL is sued by the browser; A url where the user needs to be redirected on completion of the payment process.
Que42 - Is PHP a loosely Typed Language? What is the difference between strongly typed and loosely typed language?
Ans - Yes, PHP is a loosely typed language. In this type of language variable doesn’t need to be declared before their use. This language converts the data according to its given value. Whereas, in strongly typed language first every type has to be declared (defined) then only it can be used.
Que43 – What is the Difference between DROP a table and TRUNCATE a table?
Ans - DROP – It will delete the table and table data. TRUNCATE – It will delete data of the table but not the table definition.
Que44- Explain “GET” and “POST” methods.
Ans - Both the methods are used to send data to the server. GET method – the browser appends the data onto the URL.
Post method – the data is sent as “standard input.”
Que45 – What is the default session time in PHP?
Ans - Until closing the browser.
Que46 – What is meant by nl2br()?
Ans - Inserts HTML line breaks (<BR />) before all newlines in a string.
Que47 – What is the use of friend function?
Ans - Sometimes a function is best shared among a number of different classes. Such functions can be declared either as member functions of one class or as global functions. In either case they can be set to be friends of other classes, by using a friend specifier in the class that is admitting them. Such functions can use all attributes of the class which names them as a friend, as if they were themselves member of that class. A friend declaration is essentially a prototype for a member function.
Que48 - How can we get second of the current time using date function?
Answer - $second = date(“s”);
Que49 -How can we increase the execution time of a PHP script?
Ans - By changing the following setup at php.inimax_execution_time = 30. Maximum execution time of each script, in seconds.
Que50 – How can I know that a variable is a number or not using a JavaScript?
Ans – Bool is_numeric ( mixed var). Returns TRUE if var is a number or a numeric string, FALSE otherwise. or use isNaN(mixed var)The isNaN() function is used to check if a value is not a number.