0% found this document useful (0 votes)
8 views11 pages

WS Ass3

The document contains an assignment focused on web systems, specifically addressing forms, PHP syntax, and basic programming concepts. It includes questions about the purpose of forms, PHP variables, and provides examples of HTML and PHP code segments for user input and array manipulation. Additionally, it features multiple-choice questions and requires the creation of a form and PHP script to calculate the sum of user inputs.

Uploaded by

ariraghad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views11 pages

WS Ass3

The document contains an assignment focused on web systems, specifically addressing forms, PHP syntax, and basic programming concepts. It includes questions about the purpose of forms, PHP variables, and provides examples of HTML and PHP code segments for user input and array manipulation. Additionally, it features multiple-choice questions and requires the creation of a form and PHP script to calculate the sum of user inputs.

Uploaded by

ariraghad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Web System- Assignment3

Telegram : @programming_1_2
Q1. Answer the following questions.
a) For what purpose forms are used?
Forms are used for collecting and submitting data from users on a website. They provide
a way for users to enter information, make selections, and interact with the website.
b) Describe the syntax of a form.
The syntax of a form in HTML typically consists of the <form> element with attributes for
specifying the form's action, method, and other optional attributes. Inside the form,
various form controls such as <input>, <select>, and <textarea> are used to create input
fields and user interface elements.
c) What is the purpose of forms submit button?
The purpose of a form's submit button is to initiate the submission of the form data to the
server. When a user clicks the submit button, the form data is sent to the server-side
script specified in the form's action attribute for processing.
d) What does PHP stand for?
PHP stands for Hypertext Preprocessor.
e) What are the delimiters that surround PHP server scripts?
PHP server scripts are surrounded by the <?php and ?> delimiters.
f) Describe the syntax of PHP variables.
The syntax of PHP variables starts with a dollar sign ($) followed by the variable name.
Variables in PHP are loosely typed, meaning they don't require explicit declaration or type
specification. For example:
bash
Copy code
$variableName = value;
g) What is the correct way to end a PHP statement?
The correct way to end a PHP statement is with a semicolon (;).
h) What are the correct ways to add a comment in PHP?
There are two ways to add comments in PHP:
Single-line comments: They are denoted by two forward slashes (//). Anything after the //
will be considered a comment on the same line.
Multi-line comments: They are enclosed between /* and */ and can span across multiple
lines.
Q2. Choose the correct answer.

1. In PHP you can use both single quotes ( ' ' ) and double quotes ( " " ) for strings.

a) True b) False

2. What is the correct way to create a function in PHP?

a) function myFunction[ ] b) new_function myFunction()

c) create myFunction() c) non of the previous

3. What is the correct way to open the file "time.txt" as readable?

a) open("time.txt"); b) fopen("time.txt","a");

c) open("time.txt","read"); c) non of the previous


4. Which one of these variables has an illegal name?
a) $my_Var b) my_Var$

c) $my-Var d) myVar

5. In PHP, the only way to output text is with echo.

a) True b) False

6. How do you create an array "num" in PHP?

a) $ num = "one", "two", "three"; b) $sum = array("one", "two", "three");

c) $num = array["one", "two", "three"]; d) non of the previous

7. Using "for" statement is the only way to implement loops in PHP.

a) True b) False

8. "do while" loops execute at least once.


a) True b) False
Q3. Write an HTML code segment to display the following form.

<form>
<h2>Login</h2>
<label for="name">Name:</label>
<input type="text" id="name" name="name"><br>

<label for="password">Password:</label>
<input type="password" id="password" name="password"><br>

<label>Gender:</label>
<input type="radio" id="male" name="gender" value="male">
<label for="male">Male</label>
<input type="radio" id="female" name="gender" value="female">
<label for="female">Female</label><br>

<label for="job">Job:</label>
<select id="job" name="job">
<option value="teacher">Teacher</option>
<option value="engineer">Engineer</option>
<option value="student">Student</option>
<option value="other">Other</option>
</select><br>

<input type="submit" value="Submit">


</form>
Q4. Describe the output of the following PHP segments
The segment 1 defines a function
Segment 1: named F1.
<?php Inside the function, there is a static
variable $var1 initialized to 5.
function F1() It echoes the result of 2 * $var1 and
{ decreases the value of $var1 by 1.
However, there is a syntax error in
static $var1 = 5; calling the function F10, which should
echo "Result =". "2*$var1 = ".(2*$var1); be F1.
The correct usage should be F1(); F1();.
echo"<br>"; $var1--;
}
F1(); F1();
F1();
?>
The S() function is defined, which takes three
Segment 2: parameters and returns the result of a
mathematical operation involving these
<?php
parameters.
function S($x1, $x2, $x3) Initial values are assigned to variables $n1, $n2,
and $n3.
{ The S() function is called with the initial values,
and the result is echoed along with the function
$y = $x1 + $x2 * $x3;
parameters.
return $y; Values of $n1 and $n2 are updated.
The S() function is called again with the updated
} values, and the new result is echoed along with
$n1=1; $n2=3; $n3=4; the function parameters

echo "S($n1, $n2, $n3) = " . S($n1, $n2, $n3) .


"<br>";
$n1+=2; $n2=2+$n1;
echo "S($n1, $n2, $n3) = " . S($n1, $n2, $n3) .
"<br>";
?>
The initial array $one is created with
Segment 3: specific keys and values.
<?php A loop iterates through $one and prints each
key-value pair.
$one = array(1 => 2, 4, 6, "x" => 8); An asterisk line separates the initial output.
foreach ($one as $index => $n) The element with key 2 is removed from
$one using unset.
{ echo $index, ' contains ',$n; Another loop prints the modified $one array
after removal of key 2.
echo "<br>"; Another asterisk line separates this output.
} A new array $two is created containing only
the values from the modified $one array.
echo " ************";echo "<br>"; The values from $two are printed along with
unset($one[2]); their numeric keys

foreach ($one as $key => $value)


{ echo $key, ' contains ',$value;
echo "<br>";
}
echo " ************";echo "<br>";
$two = array_values($one);
foreach ($two as $key => $value)
{ echo $key, ' contains ',$value;
echo "<br>";
}
?>
A function F() is defined, which takes two
Segment 4: parameters and returns their product.
A variable $index is initialized with the value 1.
<?php A while loop is used to iterate while $index is
function F($x1, $x2) less than or equal to 5.
Within each iteration of the loop, the F()
{ function is called with $index and 3 as
parameters, and the result is echoed along with
$y = $x1 * $x2 ; the function parameters.
$index is incremented after each iteration of the
return $y; loop
}
$index=1;
while($index <= 5)
{
echo "F($index,3)=".F($index,3);
echo "<br>";
$index++;
}
?>
Two variables $x and $y are initialized with
Segment 5: values 5 and 7, respectively.
The addition operation between $x and $y is
<?php performed inside an echo statement, but the
$x = 5; $y = 7; subtraction operation is mistakenly written as
($x - $y) instead of the addition operation.
echo "$x + $y =". ($x - $y); echo"<br>"; The result of the subtraction operation is echoed
along with the string "$x + $y =".
include "vars2.php"; The code then includes the file "vars2.php"
which contains PHP code to initialize $x and $y
echo "$x - $y =". ($x + $y); echo"<br>"; ?> with values 4 and 3, respectively.
After including "vars2.php", the subtraction
operation between $x and $y is performed
vars.php correctly this time, and the result is echoed
along with the string "$x - $y =".
<?php $x = 4; $y = 3; ?> The output is displayed with line breaks <br>
for formatting
Q5. Write two separated codes, the first is an HTML one that enables the user
to input three values using a form. The second one is PHP code that accepts
the three values from the form and then prints out the sum of these values as
shown in the figure below.

Form Design Output Sample

index.html

<html>
<body>

<form action="add.php" method="get">


First Number: <input type="number" name="n1"><br><br>
Second Number: <input type="number" name="n2"><br><br>
Third Number: <input type="number" name="n3"><br>
<br>
<input type="submit" >
</form>

</body>
</html>
add.php

<html>
<body>

<?php $p1= $_GET["n1"];


$p2=$_GET["n2"];
$p3=$_GET["n3"];
$additon =$p1+$p2+$p3;
echo "$p1+$p2+$p3= $additon";

?><br>

</body>
</html>
Q5. Write a PHP program that declares and initializes three arrays "A", "B"
and "C" of length 5 for each array. All arrays components are initialized to be
zeros. The program then assigns values to each array as follows:
• Cells of array A should contain a value equals to its index value
multiplied by 3.
• Cells of array B should contain a value equals to its index value
subtracted from 5.
• Cells of array C should contain a value equals to the sum of
corresponding cells of A and B.
The program shout prints out the three arrays.
<!DOCTYPE html>
<html>
<body>

<?php

$A = array_fill(0,5, 0);
$B = array_fill(0,5, 0);
$C = array_fill(0,5, 0);

for ($i = 0; $i < count($A); $i++) {


$A[$i]=$i*3;
}

for ($i = 0; $i < count($B); $i++) {


$B[$i]=5-$i;
}

for ($i = 0; $i < count($C); $i++) {


$C[$i]=$A[$i]+$B[$i];

echo "Array A: ";


foreach($A as $item){
echo $item . " ";
}
echo "<br>";
echo "Array B: ";
foreach($B as $item){
echo $item . " ";
}
echo "<br>";
echo "Array C: ";
foreach($C as $item){
echo $item . " ";
}
?>

</body>
</html>

Output :

You might also like