0% found this document useful (0 votes)
67 views

(Applications Development and Emerging Technologies) : Pre-Summative Assessment

This document provides information and examples about basic PHP concepts including variables, operators, and control structures. It begins with an introduction to PHP variables, how to create them, and output variables. It then discusses arithmetic operators and provides an example. Next, it covers PHP conditional statements such as if, if/else, if/elseif/else, and switch statements with examples of each. The document aims to familiarize students with fundamental PHP syntax and programming concepts.

Uploaded by

Rean kazushikita
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)
67 views

(Applications Development and Emerging Technologies) : Pre-Summative Assessment

This document provides information and examples about basic PHP concepts including variables, operators, and control structures. It begins with an introduction to PHP variables, how to create them, and output variables. It then discusses arithmetic operators and provides an example. Next, it covers PHP conditional statements such as if, if/else, if/elseif/else, and switch statements with examples of each. The document aims to familiarize students with fundamental PHP syntax and programming concepts.

Uploaded by

Rean kazushikita
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/ 22

COLLEGE OF COMPUTER STUDIES

(Applications Development and


Emerging Technologies)

PRE-SUMMATIVE ASSESSMENT

1
PHP OUTPUT, VARIABLE FAMILARIZATION,
OPERATORS AND CONTROL STRUCTURE

Student Name /
Group Name:
Nam Role
e
Members (if Group):

Section:

Professor:
I. PROGRAM OUTCOME/S (PO) ADDRESSED BY THE LABORATORY EXERCISE
• Design, implement and evaluate computer-based systems or applications to meet
desired needs and requirements.

II. COURSE LEARNING OUTCOME/S (CLO) ADDRESSED BY THE LABORATORY


EXERCISE
• Understand and apply best practices and standards in the development of website.
III. INTENDED LEARNING OUTCOME/S (ILO) OF THE LABORATORY EXERCISE
At the end of this exercise, students must be able to:
• Familiarize various Web Architecture, tools that used in PHP
• The basic understanding before using PHP
• Familiarize in environment of web developing
• Use of comments, variables, and Echo / Print
• To understand the different types of operators that are available on PHP.
• To know what is operator precedence and operator associativity in PHP.
• To use escape sequence properly in the program.
• To know the different approach of control structures.
• To know the fundamentals syntax for conditional and looping structures.
• To properly use the compound expression using the logical operators.
• To know the rules of break, continue, and goto statements.

IV. BACKGROUND INFORMATION

Applications Development and Emerging Page 2 of


Technologies 20
Example
Write some text to the output:
<?php
echo "Hello world!";
?>

Definition and Usage


The echo() function outputs one or more strings.

Note: The echo() function is not actually a function, so you are not required to
use parentheses with it. However, if you want to pass more than one parameter
to echo(), using parentheses will generate a parse error.

Tip: The echo() function is slightly faster than print().

Tip: The echo() function also has a shortcut syntax. Prior to PHP 5.4.0, this
syntax only works with the short_open_tag configuration setting enabled.

Syntax
echo(strings)

Parameter Values
Parameter Description
strings Required. One or more strings to be sent to the output

Technical Details
Return Value: No value is returned
PHP Version: 4+

Applications Development and Emerging Page 3 of


Technologies 20
More Examples
Write the value of the string variable ($str) to the output:

<?php
$str = "Hello world!";
echo $str;
?>

Join two string variables together:

<?php
$str1="Hello world!";
$str2="What a nice day!";
echo $str1 . " " . $str2;
?>

Write the value of an array to the output:

<?php
$age=array("Peter"=>"35");
echo "Peter is " . $age['Peter'] . " years old.";
?>

Write some text to the output:

<?php
echo "This text
spans multiple
lines.";
?>

How to use multiple parameters:

<?php
echo 'This ','string ','was ','made ','with multiple parameters.';
?>

Difference of single and double quotes. Single quotes will print the variable
name, not the value:

<?php
$color = "red";

Applications Development and Emerging Page 4 of


Technologies 20
echo "Roses are $color";
echo "<br>";
echo 'Roses are $color';
?>

PHP Variables
Creating (Declaring) PHP Variables
In PHP, a variable starts with the $ sign, followed by the name of the variable:

Example
<?php
$txt = "Hello world!";
$x = 5;
$y = 10.5;
?>

After the execution of the statements above, the variable $txt will hold the
value , the variable $x will hold the value 5, and the variable $y will
hold the value .

Note: When you assign a text value to a variable, put quotes around the value.

Note: Unlike other programming languages, PHP has no command for declaring
a variable. It is created the moment you first assign a value to it.

Think of variables as containers for storing data.

PHP Variables
A variable can have a short name (like x and y) or a more descriptive name
(age, carname, total_volume).

Rules for PHP variables:


Applications Development and Emerging Page 5 of
Technologies 20
• A variable starts with the $ sign, followed by the name of the variable
• A variable name must start with a letter or the underscore character
• A variable name cannot start with a number
• A variable name can only contain alpha-numeric characters and
underscores (A-z, 0-9, and _ )
• Variable names are case-sensitive ($age and $AGE are two different
variables)

Remember that PHP variable names are case-sensitive!

Output Variables
The PHP echo statement is often used to output data to the screen.
The following example will show how to output text and a variable:

Example
<?php
$txt = "W3Schools.com";
echo "I love $txt!";
?>

The following example will output the sum of two variables:

Example
<?php
$x = 5;
$y = 4;
echo $x + $y;
?>

Applications Development and Emerging Page 6 of


Technologies 20
Try following example to understand all the arithmetic operators.
<html>
<head>
<title>Arithmetical Operators</title>
</head>
<body>
<?php
$a = 42;
$b = 20;
$c = $a + $b;
echo "Addition Operation Result: $c <br/>";
$c = $a - $b;
echo "Subtraction Operation Result: $c <br/>";
$c = $a * $b;
echo "Multiplication Operation Result: $c <br/>";
$c = $a / $b;
echo "Division Operation Result: $c <br/>";
$c = $a % $b;
echo "Modulus Operation Result: $c <br/>";
$c = $a++;
echo "Increment Operation Result: $c <br/>";
$c = $a--;
echo "Decrement Operation Result: $c <br/>";

Applications Development and Emerging Page 7 of


Technologies 20
?>
</body>
</html>

This will produce the following result –

Addition Operation Result: 62


Subtraction Operation Result: 22
Multiplication Operation Result: 840
Division Operation Result: 2.1
Modulus Operation Result: 2
Increment Operation Result: 42
Decrement Operation Result: 43

PHP Conditional Statements


Very often when you write code, you want to perform different actions for
different conditions. You can use conditional statements in your code to do this.

In PHP we have the following conditional statements:

• if statement - executes some code if one condition is true


• if...else statement - executes some code if a condition is true and
another code if that condition is false
• if...elseif...else statement - executes different codes for more than two
conditions
• switch statement - selects one of many blocks of code to be executed

PHP - The if Statement


The if statement executes some code if one condition is true.
Syntax
if (condition) {
code to be executed if condition is true;
}
Example
<?php
$t = date("H");

Applications Development and Emerging Page 8 of


Technologies 20
if ($t < "20") {
echo "Have a good day!";
}
?>
Output "Have a good day!" if the current time (HOUR) is less than 20:

PHP – The if…else Statement


The if...else statement executes some code if a condition is true and another
code if that condition is false.
Syntax
if (condition) {
code to be executed if condition is true;
} else {
code to be executed if condition is false;
}

Example
<?php
$t = date("H");
if ($t < "20") {
echo "Have a good day!";
} else {
echo "Have a good night!";
}
?>
Output "Have a good day!" if the current time is less than 20, and "Have a good
night!" otherwise:

PHP - The if...elseif...else Statement


The if...elseif...else statement executes different codes for more than two
conditions.

Syntax

Applications Development and Emerging Page 9 of


Technologies 20
if (condition) {
code to be executed if this condition is true;
} elseif (condition) {
code to be executed if first condition is false and this condition is
true;
} else {
code to be executed if all conditions are false;
}
Example
Output "Have a good morning!" if the current time is less than 10, and "Have a
good day!" if the current time is less than 20. Otherwise it will output "Have a
good night!":

<?php
$t = date("H");

if ($t < "10") {


echo "Have a good morning!";
} elseif ($t < "20") {
echo "Have a good day!";
} else {
echo "Have a good night!";
}
?>

The PHP switch Statement


Use the switch statement to select one of many blocks of code to be
executed.

Syntax
switch (n) {
case label1:
code to be executed if n=label1;
break;
case label2:
code to be executed if n=label2;
break;
case label3:
code to be executed if n=label3;
break;

Applications Development and Emerging Page 10 of


Technologies 20
...
default:
code to be executed if n is different from all labels;
}
Example
<?php
$favcolor = "red";

switch ($favcolor) {
case "red":
echo "Your favorite color is red!";
break;
case "blue":
echo "Your favorite color is blue!";
break;
case "green":
echo "Your favorite color is green!";
break;
default:
echo "Your favorite color is neither red, blue, nor green!";
}
?>

This is how it works: First we have a single expression n (most often a


variable), that is evaluated once. The value of the expression is then compared
with the values for each case in the structure. If there is a match, the block of
code associated with that case is executed. Use to prevent the code from
running into the next case automatically. The statement is used if no
match is found.

PHP Loops
Often when you write code, you want the same block of code to run over and
over again a certain number of times. So, instead of adding several almost
equal code-lines in a script, we can use loops.

Loops are used to execute the same block of code again and again, as long as a
certain condition is true.

In PHP, we have the following loop types:

Applications Development and Emerging Page 11 of


Technologies 20
• while - loops through a block of code as long as the specified condition is
true
• do...while - loops through a block of code once, and then repeats the loop
as long as the specified condition is true
• - loops through a block of code a specified number of times
• - loops through a block of code for each element in an array

PHP while Loop


The while loop - Loops through a block of code as long as the specified
condition is true.

The while loop executes a block of code as long as the specified condition is
true.

Syntax
while (condition is true) {
code to be executed;
}
Example
The example below displays the numbers from 1 to 5:

<?php
$x = 1;

while($x <= 5) {
echo "The number is: $x <br>";
$x++;
}
?>
Example Explained
• $x = 1; - Initialize the loop counter ($x), and set the start value to 1
• $x <= 5 - Continue the loop as long as $x is less than or equal to 5
• $x++; - Increase the loop counter value by 1 for each iteration

PHP do while Loop

Applications Development and Emerging Page 12 of


Technologies 20
The do...while loop - Loops through a block of code once, and then repeats
the loop as long as the specified condition is true.

The do...while loop will always execute the block of code once, it will then check
the condition, and repeat the loop while the specified condition is true.

Syntax
do {
code to be executed;
} while (condition is true);
Example
The example below first sets a variable $x to 1 ($x = 1). Then, the do while
loop will write some output, and then increment the variable $x with 1. Then
the condition is checked (is $x less than, or equal to 5?), and the loop will
continue to run as long as $x is less than, or equal to 5:

<?php
$x = 1;

do {
echo "The number is: $x <br>";
$x++;
} while ($x <= 5);
?>

Note: In a loop the condition is tested AFTER executing the


statements within the loop. This means that the loop will execute its
statements at least once, even if the condition is false. See example below.

PHP for Loop


The for loop - Loops through a block of code a specified number of times.

The for loop is used when you know in advance how many times the script
should run.

Syntax

Applications Development and Emerging Page 13 of


Technologies 20
for (init counter; test counter; increment counter) {
code to be executed for each iteration;
}
Parameters:

• init counter: Initialize the loop counter value


• test counter: Evaluated for each loop iteration. If it evaluates to TRUE,
the loop continues. If it evaluates to FALSE, the loop ends.
• increment counter: Increases the loop counter value

Example
The example below displays the numbers from 0 to 10:

<?php
for ($x = 0; $x <= 10; $x++) {
echo "The number is: $x <br>";
}
?>

PHP foreach Loop


The foreach loop - Loops through a block of code for each element in an
array.

The foreach loop works only on arrays, and is used to loop through each
key/value pair in an array.

Syntax
foreach ($array as $value) {
code to be executed;
}

For every loop iteration, the value of the current array element is assigned to
$value and the array pointer is moved by one, until it reaches the last array
element.

Example
The example will output the values of the given array ($colors):

Applications Development and Emerging Page 14 of


Technologies 20
<?php
$colors = array("red", "green", "blue", "yellow");

foreach ($colors as $value) {


echo "$value <br>";
}
?>

PHP Break and Continue


You have already seen the break statement used in an earlier chapter of this
tutorial. It was used to "jump out" of a switch statement.

PHP Break
The break statement can also be used to jump out of a loop.
This example jumps out of the loop when x is equal to 4:
Example
<?php
for ($x = 0; $x < 10; $x++) {
if ($x == 4) {
break;
}
echo "The number is: $x <br>";
}
?>

PHP Continue
The continue statement breaks one iteration (in the loop), if a specified condition
occurs, and continues with the next iteration in the loop.

This example skips the value of 4:

Example
<?php
for ($x = 0; $x < 10; $x++) {
if ($x == 4) {
continue;
}
echo "The number is: $x <br>";

Applications Development and Emerging Page 15 of


Technologies 20
}
?>

V. GRADING SYSTEM / RUBRIC (please see separate sheet)


VI. LABORATORY ACTIVITY

1. Using PHP operators create a length conversion page, integrated with HTML
and CSS (note: use formula for each conversion)
Example: 1 meter = 100 centimeter

Sample Output:

Applications Development and Emerging Page 16 of


Technologies 20
2. Using conditional statement create a grade ranking program, integrated with
HTML and CSS.
Example
Grade = 92 Ranking: A-

Applications Development and Emerging Page 17 of


Technologies 20
Use the equivalents below

A: 93-100
A-: 90-92
B+: 87-89
B: 83-86
B-: 80-82
C+: 77-79
C: 73-76
C-: 70-72
D+: 67-69
D: 63-66
D-: 60-62
F: Below 60

Sample Output

Name: First Name MI. Lastame

Rank: Grade: Picture


A 95

3. Using Looping Statements write a program which will give you all of the
potential combinations of a two-digit decimal combination, printed in a
comma delimited format:

Sample output :
00, 01, 02, 03, 04, 05, 06, 07, 08, 09, 10, 11, 12, 13, 14, 15, 16, 17, 18,
19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37,

Applications Development and Emerging Page 18 of


Technologies 20
38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56,
57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75,
76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94,
95, 96, 97, 98, 99,

Snip and paste your source codes here. Snip it directly from the IDE so that colors
of the codes are preserved for readability. Include additional pages if necessary.
Applications Development and Emerging Page 19 of
Technologies 20
VII. QUESTION AND ANSWER

1. What program you use to create your webpage?


I use php to create the webpage
2. What kind of variables that you use in the webpage?
3. Is it important to study html and css before using PHP?
Yes, it is important to study it.
4. What are the different ways to produce an output in PHP?
echo and print
5. What are the different conditional statements? Describe each
If statement is used to execute the code if a condition is true.
▪ If-Else statement If the expression is evaluated to nonzero (true) then if block statement(s)
are executed.
If-Else If ladder is used to execute one code from multiple conditions.
Switch statement

6. Can you create a condition inside of a condition?


Yes
7. Do you think these conditional statements is important in the program? Then why?
Conditional statements are what allow a program to make decisions. Without the ability to make
decisions, programs could only run from one end to the other, or run in an infinite loop without
any way to terminate
8. What are the different looping statements in PHP? Describe each
- while loop - Loops through a block of code as long as the specified condition is true.
- The do...while loop - Loops through a block of code once, and then repeats the loop as
long as the specified condition is true
- for loop - Loops through a block of code a specified number of times.
- foreach loop - Loops through a block of code for each element in an array.
9. What is the importance of looping?
they are very helpful to accomplish the tasks in an accurate manner.

VIII. REFERENCES

1. https://www.w3schools.com/css/
2. https://www.w3schools.com/html/
3. https://www.w3schools.com/php/php_variables.asp
4. https://www.w3resource.com/php/operators/arithmetic-operators.php
5. https://www.tutorialspoint.com/php/php_arithmatic_operators_examples.htm
6. https://www.math10.com/en/algebra/convenrsion-factors-length-area-volume-mass-speed-
energy-power-force.html
7. https://www.w3schools.com/php/php_if_else.asp
8. https://www.w3schools.com/php/php_switch.asp
Applications Development and Emerging Page 20 of
Technologies 20
9. https://www.foxinfotech.in/2019/01/php-form-example-student-grading-system.html
10. https://www.w3schools.com/php/php_looping.asp
11. https://www.w3schools.com/php/php_looping_while.asp
12. https://www.w3schools.com/php/php_looping_do_while.asp
13. https://www.w3schools.com/php/php_looping_for.asp
14. https://www.w3schools.com/php/php_looping_foreach.asp
15. https://www.w3schools.com/php/php_looping_break.asp

Applications Development and Emerging Page 21 of


Technologies 20
Note: The following rubrics/metrics will be used to grade students’ output.

Program (100 (Excellent) (Good) (Fair) (Poor)


pts.)
Program Program executes Program executes Program executes Program does not
execution (20pts) correctly with no with less than 3 with more than 3 execute (10-
syntax or runtime errors (15-17pts) errors (12-14pts) 11pts)
errors (18-20pts)
Correct output Program displays Output has minor Output has Output is incorrect
(20pts) correct output errors (15-17pts) multiple errors (10-11pts)
with no errors (12-14pts)
(18-20pts)
Design of output Program displays Program displays Program does not Output is poorly
(10pts) more than minimally display the designed (5pts)
expected (10pts) expected output required output
(8-9pts) (6-7pts)
Design of logic Program is Program has Program has Program is
(20pts) logically well slight logic errors significant logic incorrect (10-
designed (18- that do no errors (3-5pts) 11pts)
20pts) significantly
affect the results
(15-17pts)
Standards Program code is Few inappropriate Several Program is poorly
(20pts) stylistically well design choices inappropriate written (10-11pts)
designed (18- (i.e. poor variable design choices
20pts) names, improper (i.e. poor variable
indentation) (15- names, improper
17pts) indentation) (12-
14pts)
Delivery The program was The program was The program was The program was
(10pts) delivered on time. delivered a day delivered two delivered more
(10pts) after the deadline. days after the than two days
(8-9pts) deadline. (6-7pts) after the deadline.
(5pts)

Applications Development and Emerging Page 22 of


Technologies 20

You might also like