Você está na página 1de 9

Acharya Narendra Dev College

Computer Science (Hons.)


PHP Assignment

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"

Ans: False (Only variables names are case sensitive).


(c)
Question: What is the role of "<>" operator? Give an example
showing how it can be used as a ternary operator.

Ans: "<>" is used for checking "Not Equal to" condition.

Example: a<>b? a: b
(d)
Question: Which of the two are valid variable names, why?
(i) $current_user
(ii) $current-user

Ans: $current_user is valid variable name because we can only


use “_” symbol in variable name naming convention.
(e)
Question: Find and correct the error in following code.

<?php
$my_string = „They Don‟t Know.‟ ;
Print $my_string;
?>

Ans: There is error in variable $my_string value as we can‟t use


single apostrophe in between string like that because it will
close string there only so we have to use backslash „\‟ before
apostrophe.

Correction: „They don\’t know.‟


(f)
Question: Describe implode() and explode() function with the
help of an example.

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

(ii) explode(): The explode function is used to "Split a


string by a specified string into pieces i.e. it breaks a string
into an array". The explode function in PHP allows us to
break a string into smaller text with each break occurring
at the same symbol. This symbol is known as the
delimiter. Using the explode command we will create an
array from a string.

The explode() function breaks a string into an array, but


the implode function returns a string from the elements of
an array.

Example: We have a string


$str="hello";
Now you want to make each name as an element of an
array and access it individually so what do you do

$arr=explode (",", $str);

means we have made pieces of string $text based on


separator ',' and put the resulting array in variable $arr. So
we used print_r ($arr); and the results are the following:

Array([0]=>h[1]=>e[2]=>1[3]=>1[4]=>=>o) which is
equal to:

$arr = Array ("h","e","l","l","o");


(g)
Question: Create an associative array for the following
data.
one, 1 two, 2 three, 3

Ans: $count array("one"=>"1","two"=>"2","three"=>"3");

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

In this we set the web-page title as Question 2a and the we give


here two radio buttons for Gender as Male and Female. When
we select any of the radio button in this it will print a message as
checked.

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.

Example: The query strings username and email (the names of


textboxes) are passed from a page called to another page called
when we click the submit button. .loginphp .startphp
Login.php:
<html>
<head>
<title>Login form</title>
</head>
<body>
<form action="start.php" method=”get”>
<table>
<tr>
<td>User name:</td>
<td> <input type="text" name="username></td>
</tr>
<tr>
<td>E-mail: </td>
<td><input type="text" name="email" ></td>
</tr>
<tr>
<td><input type="submit" name="sub" value="submit"></td>
</tr>
</table>
</form>
</body>
</html>

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.

Ans: Session: A session is a way to store information (in


variables) to be used across multiple
pages. Unlike a cookie, the information is not stored on the
user‟s computer it is stored on the servers. When you work with
an application, you open it, do some changes, and then you close
it. This is much like a Session. The computer knows who you
are. It knows when you start the application and when you end.
But on the internet, there is one problem: the web server does
not know who you are or what you do, because the HTTP
address doesn't maintain state. Session variables solve this
problem by storing user information to be used across multiple
pages (e.g. username, favourite color, etc). By default, session
variables last until the user closes the browser. So, Session
variables hold information about one single user, and are
available to all pages in one application.
Starting a PHP Session and creating session variables Code
Snippet:

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

Ans: <a href='page2.php?id=2489&user=tom'>link to


page2</a>
When the above link is clicked, page2.php gets the variables id
and user with data 2489 and tom respectively. Here is the code
to collect data in PHP.

echo $_GET ['id'] ;


//output 2489echo $_GET[ 'tom'] ;
// output tom

The address of page2.php can be replaced with any site name


and the same data can be passed to another site running in any
server.

(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: PHP_SELF is a super global variable that returns the


current script being executed. This variable returns the name and
path of the current file. A common use of PHP_SELF variable is
in the action field of the <form> tag. The action field of the
FORM instructs where to submit the form data when the user
presses the “submit” button. It is common to have the same PHP
page as the handler for the form as well.

Example: <form method="post" action="<?php echo


$_SERVER["PHP_SELF"];?>">
(b)

Question: Certain characters are valid in a string but are


interpreted as control characters when inserted in a database and
throw an error. Suggest the method to handle this.
Ans:
(c)

Question: Give output/error


<?php
$url = “nachiketh@example.com”;
Echo Itrim (strstr ($url,”@” ),”@” );
?>

Ans: example.com