I am going to share basic PHP Interview Questions with Answers. It's really helpful for PHP fresher and PHP experienced candidate for getting the right PHP job.
stristr() is similar to strstr(). However, it is case-insensitive.
Echo is faster than print because it does not return any value.
Dynamic Websites : A webpage or website which was developed by using any dynamic languages like PHP, ASP.NET, JSP etc
Advantages of output buffering for Web developers
– Turning on output buffering alone decreases the amount of time it takes to download and render our HTML because it’s not being sent to the browser in pieces as PHP processes the HTML.
– All the fancy stuff we can do with PHP strings, we can now do with our whole HTML page as one variable.
– If you’ve ever encountered the message “Warning: Cannot modify header information – headers already sent by (output)” while setting cookies, you’ll be happy to know that output buffering is your answer.
(or)
$browser = get_browser();
foreach ($browser as $name => $value) {
echo “$name $value
\n”;
}
Any_Question_Returns_Boolean ? What_if_True : What_if_False;
$string1 = “Hello”;
$string2 = ” World!”;
$stringall = $string1.$string2;
echo $stringall;
Set value into session : $_SESSION[‘USER_ID’]=1;
Remove data from a session : unset($_SESSION[‘USER_ID’];
header(“Location:page_to_redirect.php”); exit();
Retriving Cookie in PHP
echo $_COOKIE[“cookie_name”];
– Specify the path into which the file is to be stored.
– Insert the following code in php script to upload the file.
move_uploaded_file($_FILES[“file”][“tmp_name”], “myfolder/” . $_FILES[“file”][“name”]);
sha1() – Calculate the sha1 hash of a string
hash() – Generate a hash value
crypt() – One-way string hashing
DESTRUCTORS : PHP 5 introduces a destructor concept similar to that of other object-oriented languages, such as C++. The destructor method will be called as soon as all references to a particular object are removed or when the object is explicitly destroyed or in any order in shutdown sequence.
echo nl2br(“Hello \n World”);
Ans :
Hello
World
MIME types represents a standard way of classifying file types over Internet.
Web servers and browsers have a list of MIME types, which facilitates files transfer of the same type in the same way, irrespective of operating system they are working in.
A MIME type has two parts: a type and a subtype. They are separated by a slash (/).
MIME type for Microsoft Word files is application and the subtype is msword, i.e. application/msword.
Model: Model represents the information in application.
View: View represents the visual representation of information and data that you have entered in the application.
Controller: Controller is actually how and in which way you want to read the information in application.
Simply, JavaScript codes are executed by your web browser, like the catchy animations or simple calculations . Your browser does all the processing. While PHP runs on the server, where your webpage is stored. The server computer does all PHP processing and it sends the result to your browser.
< ?php // split the phrase by any number of commas or space characters, // which include ” “, \r, \t, \n and \f $keywords = preg_split(“/[\s,]+/”, “hypertext language, programming”); print_r($keywords); ?>
explode — Split a string by string
< ?php // Example 1 $pizza = “piece1 piece2 piece3 piece4 piece5 piece6″; $pieces = explode(” “, $pizza); echo $pieces[0]; // piece1 echo $pieces[1]; // piece2 ?>
Cookies can’t hold multiple variables on the other hand Session can hold multiple variables.
You can manually set an expiry for a cookie, while session only remains active as long as browser is open.
Top 50 PHP Interview Questions and Answers
Define Object-Oriented Methodology?
Object orientation is a software/Web development methodology that is based on the modeling a real world system. An object is the core concept involved in the object orientation. An object is the copy of the real world enity. An object oriented model is a collection of objects and its inter-relationshipsDescribe functions strstr() and stristr()?
Both the functions are used to find the first occurrence of a string. Parameters includes: input String, string whose occurrence needs to be found, TRUE or FALSE (If TRUE, the functions return the string before the first occurrence.)stristr() is similar to strstr(). However, it is case-insensitive.
Difference echo() and print()?
Echo can output one or more string but print can only output one string and always returns 1.Echo is faster than print because it does not return any value.
Differences between GET and POST methods ?
We can send 1024 bytes using GET method but POST method can transfer large amount of data and POST is the secure method than GET method .Differentiate between require and include?
Require and include both are used to include a file, but if file is not found include sends warning whereas require sends Fatal error.Explain the difference between static and dynamic websites?
Static Websites : A webpage or website which was developed by using HTML alone.Dynamic Websites : A webpage or website which was developed by using any dynamic languages like PHP, ASP.NET, JSP etc
Explain the importance of the function htmlentities
The htmlentities() function converts characters to HTML entities.Explain the purpose of output buffering in PHP
Without output buffering (the default), your HTML is sent to the browser in pieces as PHP processes through your script. With output buffering, your HTML is stored in a variable and sent to the browser as one piece at the end of your script.Advantages of output buffering for Web developers
– Turning on output buffering alone decreases the amount of time it takes to download and render our HTML because it’s not being sent to the browser in pieces as PHP processes the HTML.
– All the fancy stuff we can do with PHP strings, we can now do with our whole HTML page as one variable.
– If you’ve ever encountered the message “Warning: Cannot modify header information – headers already sent by (output)” while setting cookies, you’ll be happy to know that output buffering is your answer.
Get size of a file using php?
filesize() function returns the size of the specified file.How can we change the maximum size of the files to be uploaded?
We can change the maximum size of files to be uploaded by changing upload_max_filesize in php.ini.How can we get the browser properties using PHP?
echo $_SERVER[‘HTTP_USER_AGENT’];(or)
$browser = get_browser();
foreach ($browser as $name => $value) {
echo “$name $value
\n”;
}
How can we increase the execution time of a php script?
By default the PHP script takes 30secs to execute. This time is set in the php.ini file. This time can be increased by modifying the max_execution_time in seconds. The time must be changed keeping the environment of the server. This is because modifying the execution time will affect all the sites hosted by the server.How can you retrieve a cookie value?
echo $_COOKIE [“cookie_name”];How can you submit a form without a submit button?
Possible only by using JavaScript. You can use JavaScript submit() function to submit the form without explicitly clicking any submit button.How do you connect MySQL database with PHP?
There are two methods to connect MySQL database with PHP. Procedural and Object Oriented style.How is it possible to set an infinite execution time for PHP script?
The set_time_limit(0) added at the beginning of a script sets to infinite the time of execution to not have the PHP error ‘maximum execution time exceeded’.It is also possible to specify this in the php.ini file.How is the ternary conditional operator used in PHP?
It is composed of three expressions: a condition, and two operands describing what instruction should be performed when the specified condition is true or false as follows:Any_Question_Returns_Boolean ? What_if_True : What_if_False;
How php concatenation works?
Two strings can be joined together by the use of a process called as concatenation. A dot (.) operator is used for this purpose. Example is as follows:$string1 = “Hello”;
$string2 = ” World!”;
$stringall = $string1.$string2;
echo $stringall;
How to create a session? How to set a value in session ? How to Remove data from a session?
Create session : session_start();Set value into session : $_SESSION[‘USER_ID’]=1;
Remove data from a session : unset($_SESSION[‘USER_ID’];
How to delete a file from the system
unlink() – deletes the given file from the file system.How to find the length of a string?
strlen() function used to find the length of a stringHow to redirect a page in php?
The following code can be used for it,header(“Location:page_to_redirect.php”); exit();
How to set cookies in PHP and Retrieve a Cookie Value? Setting Cookie in PHP
setcookie(“cookie_name”, “cookie_value”, time()+(60*60*24*5));Retriving Cookie in PHP
echo $_COOKIE[“cookie_name”];
How to strip whitespace (or other characters) from the beginning and end of a string ?
The trim() function removes whitespaces or other predefined characters from both sides of a string.How to upload files using PHP?
– Select a file from the form using <input type=”file”>– Specify the path into which the file is to be stored.
– Insert the following code in php script to upload the file.
move_uploaded_file($_FILES[“file”][“tmp_name”], “myfolder/” . $_FILES[“file”][“name”]);
Is multiple inheritance supported in PHP?
PHP includes only single inheritance, it means that a class can be extended from only one single class using the keyword ‘extended’.PHP being an open source is there any support available to it?
PHP is an open source language, and it is been said that it has very less support online and offline. But, PHP is all together a different language that is being developed by group of programmers, who writes the code. There is lots of available support for PHP, which mostly comes from developers and PHP users.What are the encryption functions in PHP?
md5() – Calculate the md5 hash of a stringsha1() – Calculate the sha1 hash of a string
hash() – Generate a hash value
crypt() – One-way string hashing
What are the functions to be used to get the image’s properties (size, width and height)?
The functions are getimagesize() for size, imagesx() for width and imagesy() for height.What are the methods of form submitting in PHP?
We can use GET (or) POST for form submission in PHP.What does $_FILES means?
$_FILES is an associative array composed of items sent to the current script via the HTTP POST method.What does $_SERVER means?
$_SERVER is an array including information created by the web server such as paths, headers, and script locations.What does accessing a class via :: means?
:: is used to access static methods that do not require object initialization. (Scope Resolution Operator)What does ODBC do in context with PHP?
PHP supports many databases like dBase, Microsft SQL Server, Oracle, etc. But, it also supports databases like filePro, FrontBase and InterBase with ODBC connectivity. ODBC stands for Open Database connectivity, which is a standard that allows user to communicate with other databases like Access and IBM DB2.What does the PHP error ‘Parse error in PHP – unexpected T_variable at line x’ means?
This is a PHP syntax error expressing that a mistake at the line x stops parsing and executing the program.What does the scope of variables means?
The scope of a variable is the context within which it is defined. For the most part all PHP variables only have a single scope. This single scope spans included and required files as well.What is a session?
A session is a logical object enabling us to preserve temporary data across multiple PHP pages. A PHP session is no different from a normal session. It can be used to store information on the server for future use. However this storage is temporary and is flushed out when the site is closed.What is Constructors and Destructors?
CONSTRUCTOR : PHP allows developers to declare constructor methods for classes. Classes which have a constructor method call this method on each newly-created object, so it is suitable for any initialization that the object may need before it is used.DESTRUCTORS : PHP 5 introduces a destructor concept similar to that of other object-oriented languages, such as C++. The destructor method will be called as soon as all references to a particular object are removed or when the object is explicitly destroyed or in any order in shutdown sequence.
What is meant by nl2br()?
New Line to HTML Breakecho nl2br(“Hello \n World”);
Ans :
Hello
World
What is MIME?
MIME – Multi-purpose Internet Mail Extensions.MIME types represents a standard way of classifying file types over Internet.
Web servers and browsers have a list of MIME types, which facilitates files transfer of the same type in the same way, irrespective of operating system they are working in.
A MIME type has two parts: a type and a subtype. They are separated by a slash (/).
MIME type for Microsoft Word files is application and the subtype is msword, i.e. application/msword.
What is MVC?
MVC stands for Model, View, and Controller. PHP MVC is an effective way to manage the code into 3 different layers.Model: Model represents the information in application.
View: View represents the visual representation of information and data that you have entered in the application.
Controller: Controller is actually how and in which way you want to read the information in application.
What is PDO classes?
The PHP Data Objects (PDO) extension defines a lightweight, consistent interface for accessing databases in PHP. It is a data-access abstraction layer, so no matter what database we use the function to issue queries and fetch data will be same. Using PDO drivers we can connect to database like DB2, Oracle, PostgreSQL etc.What is sql injection?
SQL injection is a malicious code injection technique. It exploiting SQL vulnerabilities in Web applicationsWhat is the array in PHP?
Array is used to store multiple values in single name. In PHP, it orders in keys and values pairs. It is not a datatype dependent in PHP.What is the default session time in php?
The default session time in php is until closing of browserWhat is the difference between Exception::getMessage and Exception::getLine ?
Exception::getMessage lets us getting the Exception message and Exception::getLine lets us getting the line in which the exception occurred.What is the difference between PHP and JavaScript?
While JS is used for client side scripting (except in node.js) and PHP is used for server side scripting.Simply, JavaScript codes are executed by your web browser, like the catchy animations or simple calculations . Your browser does all the processing. While PHP runs on the server, where your webpage is stored. The server computer does all PHP processing and it sends the result to your browser.
What is the difference between preg_split() and explode()?
preg_split — Split string by a regular expression< ?php // split the phrase by any number of commas or space characters, // which include ” “, \r, \t, \n and \f $keywords = preg_split(“/[\s,]+/”, “hypertext language, programming”); print_r($keywords); ?>
explode — Split a string by string
< ?php // Example 1 $pizza = “piece1 piece2 piece3 piece4 piece5 piece6″; $pieces = explode(” “, $pizza); echo $pieces[0]; // piece1 echo $pieces[1]; // piece2 ?>
What is the difference between session and cookie?
The main difference between session and cookies is that cookies are stored on the user’s computer while sessions are stored on the server side.Cookies can’t hold multiple variables on the other hand Session can hold multiple variables.
You can manually set an expiry for a cookie, while session only remains active as long as browser is open.
Comments
Post a Comment