CP311 - Internet Programming and Applications II

Examination Answers


Question: What is the value displayed when the following is executed? Testscript.php?c=25

<?php
Function process($c,$d=25){
$retval = $c+$d=$_GET['C']
return $retval;
}
echo process[5.30];
?>

(5 Marks)


Question: What is the output of the following PHP script?

<?php
$a = 4;
$b = 5;
$c = 7;
$d = 8;
$e = 1.0;
$f = $c + $d * 3;
$g = $f % 9;
$h = $b - $a + $c + 3;
$i = $h << $c;
$j = $i * $e;
print $j;
?>

(5 Marks)


Question: Evaluate the output of the following PHP script if the variables $a, $b and $c in order are assigned the following values at the declaration phase:

a) False, true, false b) true, false, true

<?php
$string = "Hello world";
$a;
$b;
$c; //values should be assigned here
if ($a) {
    if ($b and !$c) {
        echo "Goodbye Cruel world!";
    } else if (!$b && !$c) {
        echo "Nothing here";
    } else {
        if (!$b) {
            if (!$a && (!$b and $c)) {
                echo "Hello, world!";
            } else {
                echo "Goodbye world!";
            }
        } else {
            echo "Not quite";
        }
    }
}
?>

(10 Marks)


Question: What will be the output of the following script:

<?php
$array = array(1,2,3,5,8,13,21,34,55);
$sum=0;
for($i=0; $i<5; $i++){
    $sum+= $array[$array[$i]];
}
echo $sum;
?>

(5 Marks)


Question: Write a PHP code that processes the following form:

<fieldset>
    <legend>Registration Form</legend>
    <form action="abc.php" method="post" name="myForm">
        Name: <input type="text" name="name"><br>
        Password: <input type="text" name="lname"><br>
        NIN: <input type="text" name="nin"><br>
        <input type="submit" name='submit' value="submit">
    </form>
</fieldset>

Your form should examine the name, password, and National Id Number (NIN) submitted, and verify that they are valid. A valid name is any non-empty string. A valid password is any string that is at least 6 characters long. A valid NIN contains 24 digits. (10 Marks)


Question:

i) How do we comment PHP code in a PHP page? (1 Mark)


ii) What is the difference between POST method and GET method? (2 Marks)


iii) What is the difference between $_FILES['user_file']['name'] and $_FILES['user_file']['tmp_name']? (2 Marks)


iv) How can we capture an error during uploading files in PHP? (2 Marks)


v) How can we change the maximum size of a file to be uploaded in PHP? (2 Marks)


vi) HTTP is a stateless protocol. Explain. (1 Mark)


vii) Explain the two ways of introducing state in HTTP protocol. (2 Marks)


viii) What is the difference between unset() and session_destroy() in PHP? (2 Marks)


ix) With the help of code segment explain the correct approach of using the setcookie() function. (3 Marks)


x) Write a simple program to count the number of ‘hits’ on a webpage. (3 Marks)


xi) What is the difference between echo and print? (2 Marks)


xii) What is the difference between require() and include()? (2 Marks)


Question: The following question use a database CS0219, table called users with the following structure: assume the table already has some data inside.

CREATE TABLE users(
    RegNo VARCHAR(20) NOT NULL PRIMARY KEY,
    FirstName VARCHAR(20) NOT NULL,
    LastName VARCHAR(20) NOT NULL,
    SurName VARCHAR(20)
)

i) Write a code to make connection between PHP and Database. servername = 127.0.0.1 user = root, password = "" (2 Marks)


ii) Write a PHP code to insert your details to the database (your RegNo, firstname, lastname, surname). (3 Marks)


iii) Write a PHP code to retrieve all users in the database and display their details on the browser. (3 Marks)


iv) Write a PHP code to display all users sorted by first name in descending order. (3 Marks)


v) Write a PHP code to display all users whose surnames are starting with a letter “A”. (3 Marks)


Question:

a) Write PHP functions to accomplish each of the following tasks. Assume MySQL database server has been installed on the local machine and the improved version of php has been used.

i) Connect to the server with username root and password cive. (2 Marks)


ii) Select a database called CP311 to be used for queries. (1 Mark)


iii) Execute a query to select all rows and columns from the table called users. (2 Marks)


iv) Get the next row from a result set row. (1 Mark)


v) Close the database connection. (1 Mark)


Question: Make a web page that uses a SESSION to keep track how many times a user has viewed the page. The first time a particular user looks at the page, it should print something like “Number of views: 1”. The second time the user looks at the page, it should print “Number of views: 2”, and so on. (5 Marks)


Question: Write a simple PHP program to capture details from the form and display in a web. (First Name, Gender and comments). (5 Marks)


Question: Modules are helpful for many sites that include sections of repeated code. Explain the following three functions, make the difference between them clear, include(), require(), require_once(). (6 Marks)


Question:

a) Explain the difference between Simple Object Access Protocol (SOAP) and Representational State Transfer (REST) web services. (3 Marks)


b) PHP is a weakly typed language, explain what this statement means as regards to PHP variables. (2 Marks)