You are on page 1of 22

Student Declaration

I__________________________________ Registration No.__________________, hereby

declare that I will not be involved in any kind of cheating/copying/plagiarizing in solving the

assignment based paper of Final Term Examination 2020. I take full responsibility of my

conduct. If I found involved in any kind of such activity of cheating/copying/plagiarizing

then Institute reserves the right to take any disciplinary action against me.

Student Signature
Final Exam / Spring 2020 (Paper Duration 24 hours)
To be filled by Teacher

Course No.: CS-566 Course Title: Web Technologies


Total Marks: 20 Date of Exam: 10-08-2020
Degree: BSCS Semester: 4 th Section A
Marks
Q.No. 1 2 3 4 5 6 7 8 9 10 Obtained/
Total Marks
Marks
Obtaine
d
Marks in Words:
Name of the teacher: Muhammad Ahmad
Who taught the course:Signature of teacher / Examiner:

To be filled by Student

Registration No.: ………………………………………….……… Name:……………………………………………………..

Answer the following questions.

Q.No.1 Write short answers of the following questions. (05 Marks)


I. Date function helps programmer to get current date time of system. Write PHP code
to print date in following format 08-07-19.
II. Explain transient and persistent object with the help of example.

 Transient objects
Transient objects are the objects which lie in application memory... once application
is ended this object also gets vanished. A newly created instance of a persistent class
which is not associated with a Session, has no representation in the database and no
identifier value. Such objects are considered in
transient state
For Example:
 When the Microsoft word is opened it is in transient state.
 Transient object include instances of boundary and control classes and the
objects use in applications like on-screen calculators.
 Persistent objects
Persistent objects have permanent memory... they remain in memory until they are
explicitly removed. An object associated with Session is said to be in persistent state
and has a representation in the database , an identifier value. You can make a transient
instance persistent by associating it with a Session.
For Example:
 When a file is saved on the disk it is in
persistent state.
 Persistent objects include the books , borrowers and loan in a library system.A
loan of a book to a borrower may be created on one occasion on one machine,
and is accessed from another machine on another occasion, for example to
send out overdue book reminders.

III. Consider we have a web page which may include HTML, CSS and PHP code when
we view page as view page source (Ctrl+u) PHP code cannot be seen there. Why?
IV. Write code to print current day using switch statement. Get current date from date
function.

V. Sometimes we need to store data temporarily or permanently in web browser please


identify the storage area of data in web browser with respect to the client and server
preferences.

Question #2: An array is special type of variable. Do you agree with this statement? Justify
your answer. PHP provides us different types of array explain each type with the help of
program and also write the output of that program. (05 Marks)
Answer:
PHP Arrays
 An array variable is a storage area holding a number or text. The problem is, a
variable will hold only one value.
 An array is a special variable, which can store multiple values in one single variable.
 If you have a list of items (a list of car names, for example), storing the cars in single
variables could look like this:

Example:
<?php
$cars = array("Volvo", "BMW", "Toyota");
echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . ".";
?>
Output:
I like Volvo, BMW and Toyota.

 There are three different kind of arrays and each array value is accessed using an ID
c which is called array index.
 Numeric array − An array with a numeric index. Values are stored and accessed in
linear fashion.
 Associative array − An array with strings as index. This stores element values in
association with key values rather than in a strict linear index order.
 Multidimensional array − An array containing one or more arrays and values are
accessed using multiple indices
PHP Numeric Arrays
> A numeric array stores each array element with a numeric index.
> There are two methods to create a numeric array.
In the following example the index is automatically assigned (the index starts at 0):

In the following example we assign the index manually:


In the following example you access the variable values by referring to the array
name and index:

The code above will output:

For Example:
<html>
<body>

<?php
/* First method to create array. */
$numbers = array( 1, 2, 3, 4, 5);

foreach( $numbers as $value ) {


echo "Value is $value <br />";
}

/* Second method to create array. */


$numbers[0] = "one";
$numbers[1] = "two";
$numbers[2] = "three";
$numbers[3] = "four";
$numbers[4] = "five";

foreach( $numbers as $value ) {


echo "Value is $value <br />";
}
?>

</body>
</html>
Output:
Value is 1
Value is 2
Value is 3
Value is 4
Value is 5
Value is one
Value is two
Value is three
Value is four
Value is five

Associative Arrays
With an associative array, each ID key is associated with a value.
> When storing data about specific named values, a numerical array is not always the
best way to do it.
> With associative arrays we can use the values as keys and assign values to them.
In this example we use an array to assign ages to the different persons:

This example is the same as the one above, but shows a different way of creating the
array:
Example
<html>
<body>

<?php
/* First method to associate create array. */
$salaries = array("mohammad" => 2000, "qadir" => 1000, "zara" => 500);

echo "Salary of mohammad is ". $salaries['mohammad'] . "<br />";


echo "Salary of qadir is ". $salaries['qadir']. "<br />";
echo "Salary of zara is ". $salaries['zara']. "<br />";

/* Second method to create array. */


$salaries['mohammad'] = "high";
$salaries['qadir'] = "medium";
$salaries['zara'] = "low";
echo "Salary of mohammad is ". $salaries['mohammad'] . "<br />";
echo "Salary of qadir is ". $salaries['qadir']. "<br />";
echo "Salary of zara is ". $salaries['zara']. "<br />";
?>

</body>
</html>
Output:
Salary of mohammad is 2000
Salary of qadir is 1000
Salary of zara is 500
Salary of mohammad is high
Salary of qadir is medium
Salary of zara is low

Multidimensional Arrays
In a multidimensional array, each element in the main array can also be an array.
And each element in the sub-array can be an array, and so on
Example
In this example we create a two dimensional array to store marks of three students in three
subjects −

This example is an associative array, you can create numeric array in the same fashion.
Live Demo
<html>
<body>

<?php
$marks = array(
"mohammad" => array (
"physics" => 35,
"maths" => 30,
"chemistry" => 39
),

"qadir" => array (


"physics" => 30,
"maths" => 32,
"chemistry" => 29
),

"zara" => array (


"physics" => 31,
"maths" => 22,
"chemistry" => 39
)
);

/* Accessing multi-dimensional array values */


echo "Marks for mohammad in physics : " ;
echo $marks['mohammad']['physics'] . "<br />";

echo "Marks for qadir in maths : ";


echo $marks['qadir']['maths'] . "<br />";

echo "Marks for zara in chemistry : " ;


echo $marks['zara']['chemistry'] . "<br />";
?>
</body>
</html>
Output:
Marks for mohammad in physics : 35
Marks for qadir in maths : 32
Marks for zara in chemistry : 39

Q.No.3. .As we know that web applications are interpreted on web browser which may need
to get hosting from hosting provider companies. Is there any solution to make our own server
for local testing of our application? If yes then write detail on that package otherwise justify
your answers. (05 Marks)

Answer:

Q.No.4. Loop is an essential part of any programming language to execute the block of code.
What is loop in PHP? Write detail note on its type and also provide example which provides
implementation of loop also write the output of that code. (05 Marks)
Answer:
PHP Loop
Often when you write code, you want the same block of code to run over and over again in a
row. Instead of adding several almost equal lines in a script we can use loops to perform a
task like this.
> In PHP, we have the following looping statements:
while - loops through a block of code while a specified condition is true
> do...while - loops through a block of code once, and then repeats the loop as long as a
specified condition is true
> for - loops through a block of code a specified number of times
> foreach - loops through a block of code for each element in an array

While loop
The while loop executes a block of code while a condition is true. The example below defines
a loop that starts withi=1. The loop will
continue to run as
long as i is less
than, or equal to 5.
i will increase by 1
each time the loop
runs:
Syntax
while (condition) {
code to be executed;
}

Example:
Example
This example decrements a variable value on each iteration of the loop and the counter
increments until it reaches 10 when the evaluation is false and the loop ends.

Live Demo
<html>
<body>

<?php
$i = 0;
$num = 50;

while( $i < 10) {


$num--;
$i++;
}

echo ("Loop stopped at i = $i and num = $num" );


?>

</body>
</html>
Output:
Loop stopped at i = 10 and num = 40

Do-While Loop
The do...while statement will always execute the block of code once, it will then check the
condition, and repeat the loop while the condition is true.
The next example defines a loop that starts with i=1. It will then increment i with 1, and write
some output. Then the condition is checked, and the loop will continue to run as long as i is
less than, or equal to 5:

Syntax
do {
code to be executed;
}
while (condition);
Example
The following example will increment the value of i at least once, and it will continue
incrementing the variable i as long as it has a value of less than 10 −

Live Demo
<html>
<body>
<?php
$i = 0;
$num = 0;

do {
$i++;
}

while( $i < 10 );
echo ("Loop stopped at i = $i" );
?>

</body>
</html>
Output
Loop stopped at i = 10
For-Loop
The for statement is used when you know how many times you want to execute a statement
or a block of statements.

Syntax
for (initialization; condition; increment){
code to be executed;
}
Parameters:
> init: Mostly used to set a counter (but can be any code to be executed once at the beginning
of the loop)
> condition: Evaluated for each loop iteration. If it evaluates to TRUE, the loop continues. If
it evaluates to FALSE, the loop ends.
> increment: Mostly used to increment a counter (but can be any code to be executed at the
end of the loop)
The example below defines a loop that starts with i=1. The loop will continue to run as long
as i is less than, or equal to 5. i will increase by 1 each time the loop runs:
Example
The following example makes five iterations and changes the assigned value of two variables
on each pass of the loop −

Live Demo
<html>
<body>

<?php
$a = 0;
$b = 0;

for( $i = 0; $i<5; $i++ ) {


$a += 10;
$b += 5;
}

echo ("At the end of the loop a = $a and b = $b" );


?>

</body>
</html>
Output:
At the end of the loop a = 50 and b = 25
Foreach-Loop
For every loop iteration, the value of the current array element is assigned to $value (and the
array pointer is moved by one) - so on the next loop iteration, you'll be looking at the next
array value
Syntax
foreach (array as value) {
code to be executed;
}

The following example demonstrates a loop that will print the values of the given array:
Example
Try out following example to list out the values of an array.

Live Demo
<html>
<body>

<?php
$array = array( 1, 2, 3, 4, 5);
foreach( $array as $value ) {
echo "Value is $value <br />";
}
?>

</body>
</html>
Output
Value is 1
Value is 2
Value is 3
Value is 4
Value is 5

You might also like