Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Revision Questions

a. Explain if elseif else conditional statement with example.

Ans:-
<?php
$num = 11;
if($num=>10)
{
print “Number is greater number than 10”;

}
elseif($num=>12)
{
print “Number is greater than 12 ” ;
}
else
{
print “It is smaller number than 10 and 12” ;
}
?>
b. Define numeric array and associative array with example.

Ans:- In Numeric arrays each item has an index number. The first item has
index 0, the second item has index 1, etc. To access the item you have to use
the index number inside the square brackets [ ].

For example:
<?php
$cars = arrays (“Volvo”, ”BMW”, ”Toyota ”) ;
print $cars [0];
?>

Associative arrays are arrays that use key and value. It store value inside a key.
Different values can be stored inside a key.
For example:
<?php
$car = array(“brand”=>”Ford”,”model”=>”Mustang”,”year”=>”1964”);
print $car[“brand”];
?>

c. Explain include and require in php? Write their differences

Ans:- In PHP, both `include` and `require` are used to include the content of another
PHP file within the current PHP script.

The main difference between `include` and `require` are


include require
1. If the file specified in the `include` If the file specified in the `require`
statement is not found or cannot be statement is not found or cannot be
included for any reason, PHP will issue a included, PHP will issue a fatal error and
warning and continue executing the stop executing the script.
script.
2. Syntax is 2. Syntax is
<?php include ‘filename.php’ ?> <?php require ‘filename.php’ ?>

d. Why GET and POST method are used in form and write their differences?

Ans: The GET method is used to submit the HTML form data. The
information sent from an HTML form using the GET method is visible to
everyone in the browser's address bar. Therefore, the get method is not
secured to send sensitive information.
POST method is also used to submit the HTML form data. But the data
submitted by this method is collected by the predefined superglobal array
$_POST.The information sent from an HTML form using the POST method is
not visible to anyone.The data sent using POST method is secured.
e. Explain Superglobals array and list out some of the name of superglobal
array?

Ans :Superglobals array are special array which can you access from any part of
the code and you can access them from inside the function or outside of the
function. The some of the name of superglobal array are: $GLOBALS,
$_SERVER, $_REQUEST, $_POST, $_GET etc.

f. Explain while loop with example.

Ans:- A while loop is a control flow statement that allows you to execute a block
of code repeatedly as long as a specified condition evaluates to true.

<?php

$num = 12;

while($num=>26)

print “ Even number is : $num”;

$num = $num + 2;

?>
Programs

a. Write a program to print even numbers from 12 to 26.

Ans:- <?php

$num = 12;

while($num=<26)

print “Even number is : $num”;

$num = $num + 2;

?>

b. Write a program to print odd numbers from 11 to 23.

Ans:- <?php

$num = 11;

while($num=<23)

print “Even number is : $num”;

$num = $num + 2;

?>
c. Write a program using function to find out the area of rectangle using the
parameters passed by the calling function.

Ans:- <?php

function areaRect($length,$breadth)

$ans=$length*$breadth;

print”The area of rectangle is $ans”;

areaRect(10,20);

?>

d. Write a program to validate a html form in php?

Ans:-

<html>

<body>

<form action="form.php" method="POST">

<p>Name: <input type="text" name="name"></p>

E-mail: <input type="text" name="email"><br>

<p><input type="submit"></p>
</form>

<?php

$email = " ";

if($_SERVER["REQUEST_METHOD"]=="POST")

$email = check_input($_POST["email"]);

function check_input($data)

$data = trim($data);

$data = stripslashes($data);

return $data;

print "Welcome $email";

?>

</body>

</html>
e. Write down the syntax for while loop and for loop.

Ans:- <?php
while(condition)
{
code to be executed;
}
?>

<?php
for(intialisation;condition;increment/decrementOperator)
{
code to be executed;
}
?>

f. Write the output of the following program:-


i. <?php
$car = array("brand"=>"Ford", "model"=>"Mustang", "year"=>1964);
print $car["model"];
?>

Output:

Mustang.

Note:- since in associative array value is stored in key and to access the key
we have to write key in big bracket with double inverted comma so model is
key and its value is Mustang.
ii. <?php
$student = array("Alex","Emma","Thomas","Marie");
print $student[0];
print "<br>";
print $student[1];
?>

Output

Alex

Emma

Note:- This is numeric array. In numeric array we store value in index. So Alex is
stored in 0 index, Emma in 1 index, Thomas in 2 index and Marie in 3 index. To
access the value Alex stored in 0 index we have to write the 0 index in big bracket.
But in numeric array we don’t write double inverted comma.

You might also like