PHP Interview Questions and Answers For Freshers

PHP Interview Questions and Answers For Freshers

Spread The Love

In this article, There are 25 Important PHP Interview questions for freshers who will be going for a job interview as a junior PHP developer/ programmer. This article is only to test your Basic skills of PHP.

1. Is PHP a Server Side or Client Side Language?

Ans. Server Side Language

2. How is a constant defined in a PHP script?

Ans. Defining a Constant in PHP

define('CONSTANT_NAME',value);

3. How many types of errors in PHP?

Ans. There are 4 basic types of errors in PHP.

Parse Error or Syntax Error – These errors Commonly occur due to syntax mistakes in code E.g. missing semicolon or brackets, mismatch brackets.

Fatal Error – These errors are basically runtime errors caused when you try to access what can’t be done. These errors are critical because it stops the execution. E.g. accessing a dead object, or trying to call a function that hasn’t been declared.

Warning Error – Theses occurs when you include a file that is not present or deletes a file that is not on the server. This will not halt the script; it will give the notice or warning but continue with the next line of the script.

Notice Error – These errors occur 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.

Note: Both notices and warnings will not halt execution of your script.

4. How to include a file in PHP?

Ans. Using include() and require() function.

E.g If you want to include a file named database.php:

  • include database.php;
  • require database.php

5. What’s the difference between include and require?

Ans. Both include and require performs the same job, there is a difference on the basis of their errors.

include() –  Generates a warning message if the file can’t be included or file doesn’t exist.

require() – Generates a fatal error if the file can’t be included or file doesn’t exist. It Halts the PHP script

6. What is the main difference between require() and require_once()?

Ans. Both require(), require_once() performs the same job except that the second function checks if the PHP script is already included or not before executing it.

7. What is difference between include,require,include_once and require_once() ?

Ans. include – include()  is used to include files more than once in the single PHP script. You can include a file as many times as your requirement.

Syntax –  include(“file_name.php”);

include_once – include_once()  include a file only one time in the php script. Next attempt to include is ignored.

Syntax – include_once(“file_name.php”);

require – require() is also used to include files more than once in the single PHP script. require()generates a Fatal error and halts the script execution if the file is not found on specified location or path. You can require a file as many time you want in a single script.

Syntax –  require(“file_name.php”);

require_once – require_once() include a file only one time in the php script.Next attempt to include is ignored. require_once() also creates a Fatal error and halts the script execution, if the file is not found on a specified location or path.

Syntaxrequire_once(“file_name.php”);

8. What is the difference between $var and $$var?

Ans. $var is variable whereas $$var is reference variable like $var =himani and $$var =bhardwaj so $himani value is bhardwaj

9What is the use of header() function in php?

Ans. The header() function sends a raw HTTP header to a client browser. Remember that this function must be called before sending the actual output.

10. How to redirect a page in php?

Ans. You can redirect a PHP Page using header() :

header("Location:index.php");

11. How to find the length of a string?

Ans. strlen() function used to find the length of a string

12. Which Methods are used while submitting a form?

Ans. get and post

13. What is the difference between get and post?

Ans.

GetPost
get displays the submitted data as part of the URLinformation is not shown as it’s encoded in the request
GET allows only ASCII dataPOST has no restrictions
GET can handle a maximum of 2048 charactersPOST has no such restrictions
GET is not securePOST is a secure method

 

14. What is in_array?

Ans. in_array() is used to check the particular value in an array.

15. What is the use of count() function?

Ans. count() is used to count the length of an array.

e.g $array = array("a", "b", "c");

count($array) returns 3.

16. What is the difference between echo and print?

Ans.

echoprint
echo has a void return typePrint has a return value of 1 so it can be used in expressions
echo can take multiple parameters separated by commaprint cannot take multiple parameters
echo is slightly faster than printprint is slower than an echo

 

17. What is the difference between unset and unlink?

Ans. Unlink:  used to remove a file from the server.
Usage: unlink(‘path to file’);

Unset: used unset a variable.
usage: 
unset($var);

18. What is the difference between == and === operator in PHP ?

Ans. == is an equal operator or comparison Operator and returns TRUE Ok if $a is equal to $b after type juggling and === is an Identical operator and return TRUE if $a is equal to $b, and they are of the same data type.

The difference between these ‘==’ used to check if the values of the two operands are equal or not.

While on the other side, ‘===’ checks the values as well as the type of operands(same data type).

19. Differentiate sessions and cookies?

Ans.

SessionsCookies
Sessions are stored at server endCookies are stored in client end i.e browser as text file format.
Stored unlimited amount of dataStored limit amount of data.
Cannot access session values in easily. More secureCan access cookies values in easily. So it is less secure

20. How many types of loops in PHP?

Ans. There are 4 types of loops in PHP:

  • foreach
  • while
  • do while
  • for

21. What is the correct and the most two common way to start and finish a PHP block of code?

Ans. <?php        >?

22. What type of operation is needed passing values through a form or an URL?

Ans. If we want to pass values through a form, then we need to encode and to decode them using htmlspecialchars() and urlencode().

23. What is the default session time and path in PHP? How to change it?

Ans. Default session time in PHP is 1440 seconds (24 minutes) and Default session storage path is temporary folder/tmp on the server.

24. What do you mean by SQL injection?

SQL injection is a malicious code injection technique. It exploiting SQL vulnerabilities in Web applications

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

Ans. The default max execution time for PHP scripts is set to 30 seconds. If a php script runs longer than 30 seconds then PHP stops the script and reports an error.
You can increase the execution time by changing max_execution_time directive in your php.ini file or calling ini_set(‘max_execution_time’, 300); //300 seconds = 5 minutes function at the top of your php script.

These are some important PHP Interview Questions for Freshers according to my experience. don’t forget to share it.

  • 2
    Shares

Himani Bhardwaj

I Himani Bhardwaj founder of Plugins Reviews and also a Professional Developer. I'm a Passionate self-made Girl and plays an active role in the activities of WordPress Plugin Reviews I have also attended Chandigarh WordPress Meetup Sessions. I run an online website geared to help those who wants to Know about WordPress Topmost Plugins, Themes and also who wants to learn about PHP Concepts

Leave a Reply

Close Menu