Professional Documents
Culture Documents
SEHH1016 T08-PHP Conditional Statements
SEHH1016 T08-PHP Conditional Statements
Learning Outcomes
1. Conditional statements
2. If-else statement
3. If-elseif-else statement
4. Date function and logical operators (&&) (||)
Page 1 of 5
SEHH1016 – Introduction to Internet Technology T08 – PHP Conditional Statements
date function
Example (php02_02_if.php)
<?php
$d=date("D");
if ($d=="Fri") {
echo "Hello!<br />";
echo "Have a nice weekend!";
echo "See you on Monday!";
}
?>
Page 2 of 5
SEHH1016 – Introduction to Internet Technology T08 – PHP Conditional Statements
Logical Operators
Operator Description Example (x=6; y=3)
Page 3 of 5
SEHH1016 – Introduction to Internet Technology T08 – PHP Conditional Statements
And Logical Operator
• If condition A is true, condition B is true, then condition A and condition B is true.
• If either condition A or condition B is false, then condition A and condition B is false.
Or Logical Operator
• If condition A is false, condition B is false, then condition A or condition B is false.
• If either condition A or condition B is true, then condition A or condition B is true.
Page 4 of 5
SEHH1016 – Introduction to Internet Technology T08 – PHP Conditional Statements
Create a PHP if…else statement and test it with a Web server
1. Use a text editor (e.g. Notepad) to type the following. Save the file with name
SEHH1016_T8_ex.php, and put it in the root directory of your Apache Web server. It
should be the “htdocs” folder by default.
<HTML>
<BODY>
<P> Second exercise: </P>
<?php
$numA = 2;
$txtA = "2";
if ($numA == $txtA)
{
echo "True";
}
else
{
echo "False";
}
?>
</BODY>
</HTML>
2. Save the file and view the result through the web server. Are “numA” and “txtA” equal?
3. Create two more variables: $numB and $txtB. Set $numB as 12, $txtB as “12”. Test the
following comparison:
(a) $numA > $numB: ______________
(b) $txtA > $txtB: ______________
(c) $numA > $txtB: ______________
(d) $txtA > $numB: ______________
4. Create another variable $numC. Set the value of $numC with the following. What is the
result when “echo $numC” is executed?
(a) $numA * $numB: _______________
(b) $txtA + $txtB: _______________
(c) $numA . $numB: _______________
(d) $txtA . $txtB: _______________
Page 5 of 5