Professional Documents
Culture Documents
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
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
To be filled by Student
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.
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):
For Example:
<html>
<body>
<?php
/* First method to create array. */
$numbers = array( 1, 2, 3, 4, 5);
</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);
</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
),
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;
</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;
</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