tutorialmines.net-PHP Interview Question and Answers for Freshers
tutorialmines.net-PHP Interview Question and Answers for Freshers
tutorialmines.net/php-interview-question-answers-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.
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
4. PHP Scripts are loaded into memory and compiled into Zend opcode.
By default PHP Session variable ends the as user closes the browser
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
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.
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');
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
Q28. What is the difference between the functions unlink and unset?
A28.
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.
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".
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
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
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
1 <?php
2 $i=1;
3 for($j=5;$j>=1;$j--)
4 {
5 echo str_repeat(" ",$j-1);
6 echo str_repeat('*',$i++);
7 echo "<br />";
8 }
9 ?>
10
11
12
Try it Editor
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
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
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
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!
13/13