Aims:
the block can contain any kind of statement…including conditional statements
if ( Boolean expression )
{
zero, one or more statements
}
$age = 23; $nationality = 'Nerd'; if ( $age > 18 ) { echo '<p>You are able to pay tax in Nerdland</p>'; if ( $nationality == 'Nerd' ) { echo '<p>You are able to stand for election in Nerdland</p>'; } }
Also, what if the person is a 17 year old Nerd? Or a 17 year old Geek? Or a 23 year old Geek?
if ( $num_of_faults != 0 ) { if ( $total_downtime / $num_of_faults >= 3 ) { echo 'Average downtime is too high!'; } }
if ( $total_downtime / $num_of_faults >= 3 ) { if ( $num_of_faults != 0 ) { echo 'Average downtime is too high!'; } }
al_outcome.php
(next slide), which takes data from this
form, and determines whether the student passes or not:
<form action="al_outcome.php" method="get"> <input type="text" name="cont" /> <input type="text" name="exam" /> <input type="submit" /> </form>
al_outcome.php
$cont = (int) $_GET['cont']; $exam = (int) $_GET['exam']; $message = '<p>You have failed</p>'; if echo $message;
$cont >= 40
and $exam >= 40
if ( $age > 18 && $nationality == 'Nerd' ) { … }
al_outcome.php
(next slide)
using &&
al_outcome.php
$cont = (int) $_GET['cont']; $exam = (int) $_GET['exam']; $message = '<p>You have failed</p>'; if echo $message;
expr1 | expr2 | expr1 && expr2 |
---|---|---|
true | true | true |
true | false | false |
false | true | false |
false | false | false |
See
&&
) is shown (next slide)
wc_outcome.php
(next slide but one) to
determine whether a student passes or not without using conjunction
(Hint: more than one if
)
wc_outcome.php
that uses conjunction$cont = (int) $_GET['cont']; $exam = (int) $_GET['exam']; $message = '<p>You have passed</p>'; if ( $cont < 40 && $exam < 40 ) { $message = '<p>You have failed</p>'; } echo $message;
wc_outcome.php
$cont = (int) $_GET['cont']; $exam = (int) $_GET['exam']; $message = '<p>You have failed</p>'; if echo $message;
$cont >= 40
or $exam >= 40
wc_outcome.php
(next slide)
using ||
wc_outcome.php
$cont = (int) $_GET['cont']; $exam = (int) $_GET['exam']; $message = '<p>You have failed</p>'; if echo $message;
expr1 | expr2 | expr1 || expr2 |
---|---|---|
true | true | true |
true | false | true |
false | true | true |
false | false | false |
See
<!-- This is how comments are done in HTML --> /* This is how comments are done in CSS */ /* It is also one way that comments are done in PHP */ # But this is another way: from the hash to the end of the line // And this is a third way: from the slashes to the end of the line is a comment -- This is a common way in SQL (but using /* */ and # are also allowed)