PHP Interview Question and Answers for Freshers
PHP is one of the most server site scripting language used all over the world. Today we are going to discuss the basic questionnaire for freshers. When you are going for interview these below questions and answers are must for every one. You can also enjoy free PDF to download with.
- Q1. What is PHP?
- A1. PHP is server side scripting language. It is the most widely used web technology to create dynamic web pages. There are many PHP based frameworks and Open sources available for free to use.
Some examples include WordPress, Drupal, Laravel etc. We can embed PHP with HTML and can further write server-side code with PHP for web development.
- Q2. What is the full form of PHP?
- A2. Acronym for PHP is : Hypertext Preprocessor earlier called(Personal Home Page). It is a widely-used open source general-purpose scripting language. Especially suited for web development related to dynamic nature of websites.
- Q3. Which is the latest version of PHP?
- A3. Stable release: 7.3.5 / 02 May 2019;
- Stable release: 7.1.4 / 13 Apr 2017; http://php.net/downloads.php
- Q4. Who is known as the father of PHP?
- A4. Rasmus Lerdorf, is considered as the father of PHP.
- Q5. When was PHP developed?
- A5. PHP as it’s known today is actually the successor to a product named PHP/FI. Created in 1994 by Rasmus Lerdorf, the very first incarnation of PHP was a simple set of Common Gateway Interface (CGI) binaries written in the C programming language.
- You can read full history on php.net Url i.e.http://php.net/manual/en/history.php.php
- Q6. Name offical website of PHP?
- A6. http://php.net/
- Q7. What are the extensions of PHP File?
- A7. .php, .phtml, .php3, .php4, .php5, .php7, .phps .
- Q8. What language is PHP written in?
- A8. The separate program, the PHP interpreter, is itself written in C. C# is a compiled language, but it is not compiled to machine code. Instead, it is compiled to a specialist language, byte code, to be run on a virtual machine. Java is another example of such a setup.
- Q9. What is a PHP File?
- A9. A PHP file contains HTML tags, server side code, scripts and text. Files with .php extension can contain text, HTML tags and scripts. Code between the php script tags i.e “<?php ?>” is processed by server and then it is returned in the HTML form in User’s browser. A web browser understands only HTML language.
- Q10. Tell how PHP sends output to Browser? OR How a file is processed when it sends output to browser ?
- A10. It works in below way
1. Open website http://www.tutorialmines.net
2. Request send to server of http://www.tutorialmines.net
3. Call PHP Files.
4. PHP Scripts are loaded into memory and compiled into Zend opcode.
5. These opcodes are executed and the HTML generated.
6. Same HTML is send back to Browser.
- Q11. What are sessions in PHP?
- A11. Sessions are way of storing user information with specific unique ID across multiple pages. They are stored in server and have specific set values as defined.By default PHP Session variable ends the as user closes the browser
- Q12. What are cookies in PHP?
- A12. Cookies are the files that are used to identify user via Server. As user opens a url Server embeds a small files on the user’s computer on which the specific information is taken as identification to mark the visit accordingly.
- Q13. What are arrays in PHP?
- A13. Arrays in PHP are defined as data structure which can hold multiple values within one single variable.
- Q14. What are different types of errors in PHP?
- A14. There are three basic types of errors in PHP which are described below:
- 1.Notices: There are non-critical errors as their behavior is non-existent and doesn’t affect the page load at all. Not shown to user at all.
- 2.Warnings: These warnings can be seen by users but still not result in script termination.
- 3.Fatal errors: These are critical errors which can terminate the whole function altogether
- Q15. What is use of in_array() function in PHP?
- A15. This in_array() function is used to check if a value exists in array
- Q16. What is use of count() function in PHP?
- A16. This count() function is used to count all elements within any array or object
- Q17. How to merge two arrays in PHP?
- A17. With PHP array_merge() function we can merge elements or multiple values of arrays into single array.
- Q18. What is array_combine in php?
- A18. With PHP array_combine() function we can create arrays by elements from one keys array and one values.
- Q19: What are the various methods/ways to pass data from one web page to another web page?
- A19. Below are the ways to do it.
- 1. Session
- 2. Cookie
- 3. Database
- 4. URL parameters
- Q20. How you can include a file into a php page?
- A20. By using include() and require() statements with filename as its parameter.
- Q21. What are super global variables?
- A21. Below are the super global variables
- $_COOKIE – It Stores all the information in an associative array of variables passed to the current script via HTTP Cookies.
- $_GET – Send data to server which is visible in the URL of the browser. It is not safe to send sensitive using GET method.
- $_POST – Send data to server which is not visible in the URL. When user submits the form with the method set as POST Method.
- $_REQUEST – Its combined array containing values from the $_GET, $_POST and $_COOKIE global variables.
- $_ENV – Get/Set an associative array of variables passed to the current script via the environment method..
- $_FILES – Get an associative array of items uploaded to the current script via the HTTP POST method. Used to upload the file, image and videos etc on server. It contains all the information i.e name, extension, temporary name etc.
- $_SERVER – It saves all information related about headers, paths, and script locations.
- $_SESSION – It contains session variables available to the current script.
- $GLOBALS – PHP super global variable which is used to access global variables from anywhere in the PHP script. Contains all the global variables associated with the current script.
- Q22. How to get IP address in php?
- A22. Simplest way is to use
$_SERVER["REMOTE_ADDR"];
but there are many variables like proxies, server, client, system, current and public that may require specific changes.
- Q23. How do I get PHP errors to display?
- A23. To display all PHP errors you need to make certain changes in the code as default they are set to off.
So add the below code and you will get all the errors and warnings reported too.
error_reporting(E_ALL); ini_set('display_errors', '1');
- Q24. How to encrypt and decrypt password in php?
- A24. Recommended PHP methods are
password_hash
andpassword_verify
which are respectively used as hashing and checking method. MD5 was used before but now has become obsolete.
- Q25. What are traits in php?
- A25. These are PHP mechanisms for code re-usability to ease developers for using code in different class hierarchies.
- Q26. How to send email with PHP?
- A26. With PHP mail() function you can easily configure and send emails. Although on local servers the whole process is bit more complex.
- Q27. What are the differences between Get and post methods?
- A27.
[table caption=”Difference between GET and POST Methods” width=”100%” colwidth=”50%|50%” colalign=”left|left”]
GET,POST
Data is being shown in the URL by this method after submitting the data., In this data is not shown in the URL.
It not good strategy to send sensitive data via GET method, We can send sensitive data by POST method.
It has limit of only 2kb data able to send for request, We can pass unlimited data by this method
[/table]
- Q28. What is the difference between the functions unlink and unset?
- A28.
[table caption=”Difference between unlink and unset functions” width=”100%” colwidth=”50%|50%” colalign=”left|left”]
unlink() function deletes the given file from the file system., unset() function makes a variable undefined or unset a variable or you can say destroy a variable.
It returns True – when the operation complete successfully or false – if operation is not completed.,No value is returned in unset function.
[/table]
- Q29. What’s the difference between include() and require() statements?
- A29. Below is the difference between both
[table caption=”Difference between include() and require() statements” width=”100%” colwidth=”50%|50%” colalign=”left|left”]
It is used to include a file in php page with filename as its parameter., It is also used to include a file in php page with filename as its parameter.
It does not produce any error. But a WARNING is shown to the user and execution of the file continues.,It produce FATAL error if file is not found on specified path and halt the execution of script.
[/table]
- Q30. What is the difference between three statements require(),require_once() and include()?
- A30.
[table caption=”Difference between include(), require() and require_once() statements” width=”100%” colwidth=”33%|33%|33%” colalign=”left|left|left”]
It is used to include a file in php page with filename as its parameter., It is also used to include a file in php page with filename as its parameter., It also includes a file but it evaluates a specific file only when if it has not been included before.
It does not produce any error. But a WARNING is shown to the user and execution of the file continues.,It produce FATAL error if file is not found on specified path and halt the execution of script.,It is used when you want to include a file which has lots of functions etc. and by using this statement will help in saving errors example “function already decared or re-declared”.
include(“filename.php”),require(“filename.php”),require_once(“filename.php”);
[/table]
- Q. Is it possible value of the constant changes during the script execution?
- A. Simply the answer is NO as the name suggests it to be constant. In PHP once declared the value of the constant can’t be changed.
PHP Logical Interview Question and Answer
- Q. How to php program to check whether a number is prime or not ?
- A. A prime number is a number that is divisible only by itself and 1. Here is program below and you can test the program in our editor as well.
<html> <body> <h2>PHP Script to find Prime number or not!</h2> <form method="post" action=""> Enter a number: <input type="number" name="number" min="0" /> <input type="submit" value="Find Prime Number" name="Submit" /> </form> </body> </html> <?php //here is the function to find prime number //this function return true if the number is prime function GetPrime($number) { $res = false; $i = $c = 0; for ($i = 1; $i <= $number; $i++) { //loop till $i equals to $num if ($number % $i == 0) { $c++; //increment the value of $c } } //if the value of $c is 2 then it is a prime number //because a prime number should be exactly divisible by 2 times only (itself and 1) if ($c == 2) { $res = true; } return $res; } if (isset($_POST['Submit']) && $_POST['Submit']) { $number = $_POST['number']; $primenumber = GetPrime($number); if ($primenumber) { print "<b>" . $number . "</b> is a Prime number."; } else { print "<b>" . $number . "</b> is not a Prime number."; } } ?>
- Q. How to php program to check leap year?
- A.
<html> <body> <h2>Leap Year Program</h2> <form action="" method="post"> <input type="text" name="year" placeholder="Enter Year Here" /> <input type="submit" name="submit" /> </form> </body> </html> <?php if( $_POST ) { //get the year $year = $_POST[ 'year' ]; //check if entered value is a number if(!is_numeric($year)) { echo "Strings not allowed, Input should be a number"; return; } //multiple conditions to check the leap year if( (0 == $year % 4) and (0 != $year % 100) or (0 == $year % 400) ) { echo "$year is a leap year"; } else { echo "$year is not a leap year"; } } ?>
- Q. How to Print Fibonacci Series in PHP?
- A. Fibonacci series is the one in which you will get your next term by adding previous two numbers. Here is the program to print the Fibonacci Series in PHP with try it editor in the end to test your logical ideas.
<?php $num = 0; $a = 0; $b = 1; echo "<h3>Fibonacci series for first 10 numbers: </h3>"; echo $a.' '.$b.' '; while ($num < 8 ) { $c = $a + $b; echo $c.' '; $a = $b; $b = $c; $num = $num +1; } ?>
Initializing first and second number as 0 and 1.it will print first and second number. start our loop. So third number will be the sum of the first two numbers.
- Q. Write php program to print pyramid!
- A. Here is a php program to print pyramid with script and test the try it editor as well.
<?php $i=1; for($j=5;$j>=1;$j--) { echo str_repeat(" ",$j-1); echo str_repeat('*',$i++); echo "<br />"; } ?>
- Q. How to Print Factorial of a number in PHP?
- A.
<?php $num = 5; $factorial = 1; for ($i=$num; $i>=1; $i--) { $factorial = $factorial * $i; } echo "Result is $factorial"; ?> Result is 120.
- Q. Find the largest number between the given three numbers!
- A.
<html> <head> <title>Find Largest Numbers in an Array </title> </head> <body> Enter the Numbers separated by Commas <br /> (eg: 10,20,30) <br /><br /> <form method="post" action=""> <input type="text" name="number"/> <button type="submit">Check</button> </form> </body> </html> <?php if($_POST) { $number = $_POST['number']; $numArray = explode(',', $number); $largest = $numArray[0]; foreach($numArray as $num){ if($num > $largest){ $largest = $num; } } echo "Your Largest Number is: $largest <br />"; } ?>
- Q. how to Print numbers from 1 to 10 in for loop?
- A
<?php for ($i = 1; $i <= 10; $i++) { echo "The Number is: $i <br>"; } ?>
- Q. how to Print numbers from 1 to 10 in While loop?
- A.
<?php $x = 1; while($x <= 10) { echo "The number is: $x <br>"; $x++; } ?>
- Q. How to print table of given number in PHP?
- A. Here is the code to print table of a given number in PHP
<!Doctype html> <html> <body> <form action="" method="post"> Enter a number: <input type="text" name="number" /> <input type="submit" value="Table" /> </form> <?php if($_POST){ $num=$_POST['number']; echo "Table of $num: <br>"; for($i=1;$i<=10;$i++) { $value=$num*$i; echo "$num*$i=$value<br>"; } } ?> </body> </html>
Q: What will be the values of $a and $b after the code below is executed? Explain your answer.?
Answer :
$a=’2′;
$b =&$a;
$b= “3$b”; //values get parse in double quotes
echo ‘$b is = ‘. $b;
echo ‘<br>$a is = ‘. $a;
Both $a and $b will be equal to the string “32” after the above code is executed.
Output will be :
$b is = 32
$a is = 32
Its because:
The statement $b = &$a;
sets $b
equal to a reference to $a
(as opposed to setting $b
to the then-current value of $a
). Thereafter, as long as $b
remains a reference to $a
, anything done to $a
will affect $b
and vice versa.
after executing the statement $b = "3$b"
, $b
is set equal to the string "3"
followed by the then-current value of $b
(which is the same as $a
) which is 2, so this results in $b
being set equal to the string "32"
(i.e., the concatenation of "3"
and "2"
). And, since $b
is a reference to $a
, this has the same affect on the value of $a
, so both end up equal to "32"
& (amperdsand) is used when we are using “Pass by Reference”.
References makes it possible for two variables to refer to the same content. In other words, a variable points to its content (rather than becoming that content). Passing by reference allows two variables to point to the same content under different names. The ampersand ( & ) is placed before the variable to be referenced.
Q: What will be the values of $a and $b after the code below is executed? Explain your answer.?
Answer :
$a=2;
$b =&$a;
echo ‘<br>after referencing $a = ‘.$b;
$b= ‘3$b’; //values does not get parsed in single quotes
echo ‘<br>’;
echo ‘$b is = ‘. $b;
echo ‘<br>$a is = ‘. $a;
after refrencing $a = 2
Both $a and $b will be equal to the string “3$b” after the above code is executed.
Output will be :
$b is = 3$b
$a is = 3$b
Also find out more Interview and Questions for getting regular information now!
- HTML5 interview Questions and Answers for Freshers
- CSS3 interview questions and answers for Freshers
- Bootstrap 3 Interview Questions and Answers for Freshers
- PHP interview question and answers for freshers
- Responsive Web Design Interview Questions And Answers for Freshers
- WordPress Interview questions and answers for freshers
- Web Developer Interview Questions & Answers
- MySQL Interview Question and Answers
- Web Designer Interview Question & Answers
- SEO Interview Questions and Answers