Você está na página 1de 9

Acharya Narendra Dev College

Computer Science (Hons.)

PHP Assignment

Submitted by:
Sachin Rana

Section A
1. (a)
Question: Give the output.

echo $++a." ".$a++;
Ans: Output will be: 101 101.
Question: State whether the following statement is True or
"Function name in PHP is case sensitive"

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

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
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.
Question: Find and correct the error in following code.

$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

Correction: „They don\’t know.‟

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

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

$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

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");

Question: Create an associative array for the following
one, 1 two, 2 three, 3

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

Section B
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()
(iv) asort()

Question: Write a PHP code to find and replace all the
occurrences of $sub=”to” in the string $mystr=”into onto and

$my_string = “into onto unto”;
echo str_replace (sub,”aa”,my_string) ;
Question: Describe the working of the following code:

<title> Question 2 a </title>
Gender: <input type=”radio” name=”gender”
if (isset( $gender) && $gender==”female”
<input type=”radio” name=”gender”
If( isset( $gender )&& $gender==”male”

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

(a) Question: What is a Query String? Explain with an

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
<title>Login form</title>
<form action="start.php" method=”get”>
<td>User name:</td>
<td> <input type="text" name="username></td>
<td>E-mail: </td>
<td><input type="text" name="email" ></td>
<td><input type="submit" name="sub" value="submit"></td>

echo "Please remember this e-mail: ".$_GET ['email']. " for
registration purpose.";

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

session_start ; //Start the session
<!DOCTYPE html>
<?php //Creating and setting session variables
$_SESSION ["favcolor"] = "green";
$_SESSION["favanimal"]= "cat";
echo "Session variables are set.";
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

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

Question: Write a function in PHP which takes email address as
an input and returns username and domain name in an
associative array.

<form action="#" method="get">
<input type="text" name="name" placeholder="Your
<input type="text" name="email" placeholder="Your
<input type="text" name="contact" placeholder="Your
<input type="submit" name="submit"

if $_POST ["name"] || $_POST ["email"] || $_POST
echo "Welcome: ". $_POST ['name'] . "<br />";
echo "Your Email is: ". $_POST ["email"] . "<br />"; echo
"Your Mobile No. is: ". $_POST[ "contact"] ;
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


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.

Question: Give output/error

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

Ans: example.com