0% found this document useful (0 votes)
7 views

tutorialmines.net-PHP Interview Question and Answers for Freshers

The document provides a comprehensive list of PHP interview questions and answers tailored for freshers, covering fundamental concepts of PHP, its functionalities, and common programming tasks. It includes questions about PHP's definition, its history, file handling, error types, and practical coding examples. Additionally, it offers insights into PHP's features such as sessions, cookies, arrays, and methods for data handling.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

tutorialmines.net-PHP Interview Question and Answers for Freshers

The document provides a comprehensive list of PHP interview questions and answers tailored for freshers, covering fundamental concepts of PHP, its functionalities, and common programming tasks. It includes questions about PHP's definition, its history, file handling, error types, and practical coding examples. Additionally, it offers insights into PHP's features such as sessions, cookies, arrays, and methods for data handling.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

PHP Interview Question and Answers for Freshers

tutorialmines.net/php-interview-question-answers-freshers/

jyoti rani April 15, 2017

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.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?


1/13
A8. That 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 https://www.tutorialmines.net

2. Request send to server of https://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

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?


2/13
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 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
1. (i)$_COOKIE – It Stores all the information in an associative array of
variables passed to the current script via HTTP Cookies.
2. (ii)$_GET – Send data to server which is visible in the URL of the browser. It
is not safe to send sensitive using GET method.
3. (iii)$_POST – Send data to server which is not visible in the URL. When user
submits the form with the method set as POST Method.

3/13
4. (iv)$_REQUEST – Its combined array containing values from the $_GET,
$_POST and $_COOKIE global variables.
5. (v)$_ENV – Get/Set an associative array of variables passed to the current
script via the environment method..
6. (vi)$_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.
7. (vii)$_SERVER – It saves all information related about headers, paths, and
script locations.
8. (viii)$_SESSION – It contains session variables available to the current
script.
9. (ix)$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.

1 error_reporting(E_ALL);
2 ini_set('display_errors', '1');

Q24. How to encrypt and decrypt password in php


A24. Recommended PHP methods are password_hash and password_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.

4/13
Q27. What are the differences between Get and post methods.
A27.

GET POST

Data is being shown in the URL by this method after In this data is not shown in the URL.
submitting the data.

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

Difference between GET and POST Methods

Q28. What is the difference between the functions unlink and unset?
A28.

unset() function makes a variable undefined


unlink() function deletes the given file or unset a variable or you can say destroy a
from the file system. variable.

It returns True – when the operation No value is returned in unset function.


complete successfully or false – if operation
is not completed.

Difference between unlink and unset functions

Q29. What’s the difference between include() and require() statements?


A29. Below is the difference between both

It is used to include a file in php page with It is also used to include a file in php
filename as its parameter. page with filename as its parameter.

It does not produce any error. But a WARNING is It produce FATAL error if file is not found on
shown to the user and execution of the file specified path and halt the execution of
continues. script.

Difference between include() and require() statements

Q30. What is the difference between three statements require(),require_once()


and include()?
A30.

5/13
It is used to include a It is also used to
file in php page with include a file in php It also includes a file but it evaluates a
filename as its page with filename as specific file only when if it has not been
parameter. its parameter. included before.

It does not produce any It produce FATAL error It is used when you want to include a file
error. But a WARNING is if file is not found on which has lots of functions etc. and by
shown to the user and specified path and halt using this statement will help in saving
execution of the file the execution of script. errors example "function already decared
continues. or re-declared".

include("filename.php") require("filename.php") require_once("filename.php");

Difference between include(), require() and require_once() statements

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.

6/13
1 <html>
2 <body>
3 <h2>PHP Script to find Prime number or not!</h2>
4 <form method="post" action="">
5 Enter a number: <input type="number" name="number" min="0" />
6 <input type="submit" value="Find Prime Number" name="Submit" />
7 </form>
8 </body>
9 </html>
10 <?php
11 //here is the function to find prime number
12 //this function return true if the number is prime
13 function GetPrime($number) {
14 $res = false;
15 $i = $c = 0;
16 for ($i = 1; $i <= $number; $i++) { //loop till $i equals to $num
17
18 if ($number % $i == 0) {
19
20 $c++; //increment the value of $c
21 }
22 }
23 //if the value of $c is 2 then it is a prime number
24 //because a prime number should be exactly divisible by 2 times only (itself and
25 1)
26 if ($c == 2) {
27 $res = true;
28 }
29 return $res;
30 }
31 if (isset($_POST['Submit']) && $_POST['Submit']) {
32 $number = $_POST['number'];
33 $primenumber = GetPrime($number);
34 if ($primenumber) {
35 print "<b>" . $number . "</b> is a Prime number.";
36 } else {
37 print "<b>" . $number . "</b> is not a Prime number.";
38 }
39 }
40 ?>
41
42
43
44

Try it Editor

Q. How to php program to check leap year?


A.

7/13
1 <html>
2 <body>
3 <h2>Leap Year Program</h2>
4 <form action="" method="post">
5 <input type="text" name="year" placeholder="Enter Year Here" />
6 <input type="submit" name="submit" />
7 </form>
8 </body>
9 </html>
10 <?php
11 if( $_POST )
12 {
13 //get the year
14 $year = $_POST[ 'year' ];
15 //check if entered value is a number
16 if(!is_numeric($year))
17 {
18 echo "Strings not allowed, Input should be a number";
19 return;
20 }
21 //multiple conditions to check the leap year
22 if( (0 == $year % 4) and (0 != $year % 100) or (0 == $year % 400) )
23 {
24 echo "$year is a leap year";
25 }
26 else
27 {
28 echo "$year is not a leap year";
29 }
30 }
31 ?>
32
33
34
35
36
37

Try it Editor

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.

8/13
1 <?php
2 $num = 0;
3 $a = 0;
4 $b = 1;
5 echo "<h3>Fibonacci series for first 10 numbers:
6 </h3>";
7 echo $a.' '.$b.' ';
8 while ($num < 8 )
9 {
10 $c = $a + $b;
11 echo $c.' ';
12 $a = $b;
13 $b = $c;
14 $num = $num +1;
15 }
?>

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.

Try it Editor

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.

1 <?php
2 $i=1;
3 for($j=5;$j>=1;$j--)
4 {
5 echo str_repeat("&nbsp;",$j-1);
6 echo str_repeat('*',$i++);
7 echo "<br />";
8 }
9 ?>
10
11
12

Try it Editor

Q. How to Print Factorial of a number in PHP?


A.

9/13
1 <?php
2 $num = 5;
3 $factorial = 1;
4 for ($i=$num; $i>=1; $i--)
5 {
6 $factorial = $factorial * $i;
7 }
8 echo "Result is $factorial";
9 ?>
10 Result is 120.
11
12

Try it Editor

Q. Find the largest number between the given three numbers!


A.

10/13
1 <html>
2 <head>
3 <title>Find Largest Numbers in an Array </title>
4 </head>
5 <body>
6 Enter the Numbers separated by Commas <br />
7 (eg: 10,20,30)
8 <br /><br />
9 <form method="post" action="">
10 <input type="text" name="number"/>
11 <button type="submit">Check</button>
12 </form>
13 </body>
14 </html>
15 <?php
16 if($_POST)
17 {
18 $number = $_POST['number'];
19 $numArray = explode(',', $number);
20
21 $largest = $numArray[0];
22
23
24 foreach($numArray as $num){
25
26
27 if($num > $largest){
28 $largest = $num;
29 }
30
31 }
32
33 echo "Your Largest Number is: $largest <br />";
34 }
35 ?>
36
37

Try it Editor

Q. how to Print numbers from 1 to 10 in for loop?


A

1 <?php
2 for ($i = 0; $i <= 10; $i++) {
3 echo "The Number is: $i
4 <br>";
5 }
?>

Try it Editor

11/13
Q. how to Print numbers from 1 to 10 in While loop?
A.

1 <?php
2 $x = 1;
3 while($x <= 10) {
4 echo "The number is: $x
5 <br>";
6 $x++;
7 }
8 ?>

Try it Editor

Q. How to print table of given number in PHP?


A. Here is the code to print table of a given number in PHP

1 <!Doctype html>
2 <html>
3 <body>
4 <form action="" method="post">
5 Enter a number:
6 <input type="text" name="number" />
7 <input type="submit" value="Table" />
8 </form>
9 <?php
10 if($_POST){
11 $num=$_POST['number'];
12 echo "Table of $num: <br>";
13 for($i=1;$i<=10;$i++)
14 {
15 $value=$num*$i;
16 echo "$num*$i=$value<br>";
17 }
18 }
19 ?>
20 </body>
21 </html>
22
23
24
25
26
27
28
29
30

Try it Editor

Enjoy more Interview and Questions for getting regular information now!

CSS3 interview questions and answers for Freshers


12/13
HTML5 Interview questions and answers For Freshers
Bootstrap 3 Interview Questions and Answers for Freshers
Responsive Web Design Interview Questions And Answers for Freshers
WordPress Interview questions and answers for freshers

13/13

You might also like