Professional Documents
Culture Documents
Submitted by:
Sachin Rana
X-1255
Section A
1. (a)
Question: Give the output.
$a=100;
echo $++a." ".$a++;
Ans: Output will be: 101 101.
(b)
Question: State whether the following statement is True or
False:
"Function name in PHP is case sensitive"
Example: a<>b? a: b
(d)
Question: Which of the two are valid variable names, why?
(i) $current_user
(ii) $current-user
<?php
$my_string = „They Don‟t Know.‟ ;
Print $my_string;
?>
Ans:
(i) implode(): The implode() function returns a string from
elements of an array. It takes an array of strings and joins
them together into one string using a delimiter (string to be
used between the pieces) of your choice.
Example:
$arr = Array ("h","e","l","l","o" );
$str = implode ("-",$arr) ;
echo $str;
Output: h-e-l-l-o
Array([0]=>h[1]=>e[2]=>1[3]=>1[4]=>=>o) which is
equal to:
Section B
2.
(a)
Question: Give the built function names to
(i) Sort a numeric array
(ii) Reverse sort a numeric array
(iii) Sort an associative array based on the key
(iv) Sort an associative array based on the values.
(i) sort()
(ii) rsort()
(iii)ksort()
(iv) asort()
Ans:
(b)
Question: Write a PHP code to find and replace all the
occurrences of $sub=”to” in the string $mystr=”into onto and
unto”.
Ans:
<?php
$my_string = “into onto unto”;
$sub=“to”;
echo str_replace (sub,”aa”,my_string) ;
?>
(c)
Question: Describe the working of the following code:
<html>
<head>
<title> Question 2 a </title>
</head>
<body>
Gender: <input type=”radio” name=”gender”
<?php
if (isset( $gender) && $gender==”female”
echo”checked”;
?>
value=”female”>Female
<input type=”radio” name=”gender”
<?php
If( isset( $gender )&& $gender==”male”
echo”checked”;
?>
value=”male”>Male
</body>
</html>
Ans: Output:
Gender: Female Male
3.
(a) Question: What is a Query String? Explain with an
example.
Ans:
The information which is passed across the web pages is called
Query String. This query string can be passed from one page to
another by appending it to the address of the page. A query
string can contain two things: the query string ID and its value.
The query string passed across the web pages is stored in
$_REQUEST, $_GET, or $_POST variable. Whether we passed
the query string by using GET or POST method, it is stored in
$_REQUEST variable. If we want to access the query string you
can use these variables. We should note that whether the query
string is passed by the GET or POST method it can be accessed
by using the $_REQUEST variable. If we want to use $_GET
variable to access the query string the form method need to be
GET. Also, we can use $_POST variable to get the query string
if the form method is POST.
start.php:
<?php
echo"<strong>Welcome".$_GET[„username']."</strong><br/>";
echo "Please remember this e-mail: ".$_GET ['email']. " for
registration purpose.";
?>
(b)
Question: What is a session? Write a code snippet to start a
session and create a session variable.
<?php
session_start ; //Start the session
?>
<!DOCTYPE html>
<html>
<body>
<?php //Creating and setting session variables
$_SESSION ["favcolor"] = "green";
$_SESSION["favanimal"]= "cat";
echo "Session variables are set.";
?>
</body>
</html>
4.
(a)
Question: Give the syntax to pass and capture the variable
between the PHP webpages during navigation.
(b)
Question: Write a function in PHP which takes email address as
an input and returns username and domain name in an
associative array.
Ans:
<form action="#" method="get">
<input type="text" name="name" placeholder="Your
Name"></input><br/>
<input type="text" name="email" placeholder="Your
Email"></input><br/>
<input type="text" name="contact" placeholder="Your
Mobile"></input><br/>
<input type="submit" name="submit"
value="Submit"></input>
</form>
<?php
if $_POST ["name"] || $_POST ["email"] || $_POST
["contact"])
{
echo "Welcome: ". $_POST ['name'] . "<br />";
echo "Your Email is: ". $_POST ["email"] . "<br />"; echo
"Your Mobile No. is: ". $_POST[ "contact"] ;
}
?>
5.
(a)
Question: When is the variable $_SERVER[„PHP_SELF‟]
used? Give an example.
Ans: example.com