Php assignment PDF

Title Php assignment
Course B.Sc(H)Computer Science
Institution University of Delhi
Pages 9
File Size 328.4 KB
File Type PDF
Total Downloads 8
Total Views 165

Summary

Php assignment...


Description

PHP ASSIGNMENT

DEPARTMENT OF COMPUTER SCIENCE SUBMITTED BY : HIMANSHU CHAUDHARY (X-1211) SANJU KUMAR MAURYA (X-1230)

SUBMITTED TO : MISS SHIVA SAINI

Section - A 1) a) Give the output : $a=100; echo $++a." ".$a++; Ans- Output will be: 101 101 b) State whether the following statement is True or False "Function name in PHP is case sensitive." Ans- True c) 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: ab? a: b d) 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) Find and correct the error in following code

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. Error is highlighted - ‘They don’t know.’ Correction - ‘They don\’t know.’ 1) f) Describe the following functions with the help of an example i) implode() ii) explode() Ans – 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 ("A","E","I","O","U"); $str = implode("-",$arr); echo $str; output - A-E-I-O-U Explode() : The explode() function breaks a string into an array. Syntax : explode(separator,string,limit) Separator - Required. Specifies where to break the string. String - Required. The string to split. Limit - Optional. Specifies the number of array elements to return Return Value - Returns an array of strings Example –$str = 'one,two,three,four'; // zero limit print_r(explode(',',$str,0)); print ""; / / positive limit print_r(explode(',',$str,2)); print ""; // negative limit print_r(explode(',',$str,-1)); output - Array ( [0] => one,two,three,four ) Array ( [0] => one [1] => two,three,four ) Array ( [0] => one [1] => two [2] => three ) g) 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) 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.

Ans – i) ii) iii) iv)

sort() rsort() ksort() asort()

b) Write a PHP code to find and replace all the occurrences of $sub=”to” in the string $mystr=”into onto and unto”. Ans – Wrong question as repacing value not given in question we can find $sub occurrences in $mystr but replace them by what?

c) Describe the working of the following code

Question 2 a

Gender:

value=”male”>Male

Ans – Output : Gender :

Female

Male

In this we set the web-page title as Question 2 a 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) 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 texboxes) are passed from a page called login.php to another page called welcome.php when we click the submit button. Login.php:

Login form

User name: E-mail:



welcome.php:

b) 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 users computer it is stored on rhe 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, favorite 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 :





4) – a) Give the the syntax to pass and capture the variable between the PHP webpages during navigation. Ans – Syntax to pass the variable between the PHP webpages : Link to page2 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. Syntax to capture the variable between the PHP webpages : echo $_Get[‘id’]; //output 2489 echo $_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) Write a function in PHP which takes email address as an input and returns username and domain name in an associative array. Ans – function parseEmail($email) { $a=strrpos($email,’@’); return array(‘local’=>substr($email,0,$a),’domain’=>substr($email, $a)); } 5) – a) 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 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...


Similar Free PDFs