15 PHP Interview Questions for 1 Year Experienced (II)

Here is a good collection of 15 PHP Interview Questions and Answers which are mostly asked from 1 year experienced PHP programmer and are useful for others too. Don’t forget to check first part of PHP Interview Questions series as well.

15 PHP Interview Questions for 1 Year Experienced

Q1.   What are ways to include a file to a php page?

Ans.  We can include a file using ‘include() ‘, ‘include_once()‘, ‘require()‘ or ‘require_once()‘ function with as its parameter.


 Q2.   Differentiate between ‘include()‘, ‘include_once()‘, ‘require()‘ and ‘require_once()‘.

Ans.   All statements are used to copy content of one file (text/code/markup) into another php file before the server executes it.

  • include()‘ and ‘include_once()‘ will only produce a warning (‘E_WARNING‘) and the script will continue to execute even if the desired file isn’t found.
  • require()‘ and ‘require_once()‘ will produce a fatal error (‘E_COMPILE_ERROR‘) and stop the script execution if the desired file isn’t found.
  • include()‘ and ‘require()‘ both include and evaluate a specific file.
  • include_once()‘ and ‘require_once()‘ do that only if it has not been already included before (on the same page). 
  • require()‘ is recommended to use when you want to include the same file more than once (like in a ‘for‘ loop).
  • require_once()‘ is recommended to use when you want to include a file where you have a lot of functions for example. This way you make sure you don’t include the file more times and you will not get the ‘function re-declared‘ error.

Q3.   How do you define a constant?

Ans.  Using ‘define()‘ directive, like define (“PRODUCTS_PER_PAGE”, 30).


 Q4.   What Is a Session?

Ans.   It can be used to store information on the server for future use.


 Q5.   How to set cookies in PHP?

Ans.   cookies are often used to track user information.

‘setcookie(name, value, expire, path, domain, secure, httponly);’

setcookie(“sample”, “sample value”, time() + 3600);

All parameters except ‘name‘ and ‘expire‘ are optional.


Q.6.   Which extension should be used make connection to a MySQL server?

Ans.   Rather than ‘MySQL_*‘ extension, use the ‘MySQLi‘ or ‘PDO_MySQL‘ extension.


Q7.   What is the use of the function ‘explode()‘ in php?

Ans.  This function is used to split a string by special character or symbol in the string, we must be pass the string and splitting character as parameter into the function.


Q8.   What is use of ‘in_array()‘ function in php ?

Ans.  ‘in_array()‘ is used to checks if a value exists in an array.


Q9.   Difference between ‘echo‘ and ‘print‘?

Ans.

  • An ‘echo‘ does not return any value but ‘print‘ always returns 1 (integer).
  • Both ‘print‘ and ‘echo‘ take only 1 argument when used with parentheses (like a function call).
  • However, when used without parentheses, echo can take several arguments e.g.

echo “Hello”,”World”,”!”,42;

but print only takes one parameter.


Q10.   Differences between ‘GET ‘and’ POST‘ methods ?

Ans.

  • 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.
  • Choosing ‘GET‘ as the method will append all of the data to the URL and it will show up in the URL bar of your browser. The amount of information you can send back using a ‘GET‘ is restricted as URLs can only be 1024 characters.
  • A ‘POST‘ on the other hand will (typically) send the information through a socket back to the webserver and it won’t show up in the URL bar.
  • You can send much more information to the server this way and it’s not restricted to textual data either. It is possible to send files and even binary data such as serialized Java objects.

Q11.   What is the current version of PHP, MYSQL & Apache?

Ans.     PHP : 5.6.0

     MySQL : 5.7

     Apache : 2.4.10


Q12.   How can we submit a form without a submit button?

Ans.    We can submit a form without a submit button with help of javascript. We have to trigger form submission code on an event.

Event: Click on any checkbox, radio button or ‘onselect‘ of drop down list box or placing a link text like ‘Click here to Submit‘ to process the form as the example link given below:

Submit Me

Form Submission

form.submit(); or document.myform.submit();

Trigger the JavaScript code on any event and this will submit the form. 


Q13.   What are the different functions in sorting an array?

Ans.    Sorting functions in PHP:

  • asort()
  • arsort()
  • ksort()
  • krsort()
  • uksort()
  • sort()
  • natsort()
  • rsort()

Q14.   Explain the ternary conditional operator in PHP?

Ans.     CONDITION ?  TRUE EXPRESSION : FALSE EXPRESSION;

Expression preceding the ‘?‘ is evaluated, if it’s true, then the expression preceding the ‘:‘ is executed, otherwise, the expression following ‘:‘ is executed.


Q15.   Write a sample code of nested ternary conditional operator in PHP.

Ans.     $number_class = $number == 0 ? 'blue' : ($number > 0 ? 'green' : 'red');

We are assigning different strings to ‘number_class‘ variable based on a numeric value (‘$number‘). 

Like this Post? Share