Documente Academic
Documente Profesional
Documente Cultură
În acest capitol vom parcurge instructiunile: if, switch, repeat, for, break
Instructiunea if
O instr if este o modalitate de a controla execuţia unui script, prin evaluarea unei expresii.
Forma generală este:
if ( expression ) {
// code to execute if the expression evaluates to true
}
<html>
<head>
<title>Listing 5.1</title>
</head>
<body>
<?php
$mood = "happy";
if ( $mood == "happy" ) {
print "Hooray, I'm in a good mood";
}
?>
</body>
</html>
output-ul va fi:
if ( expression ) {
// code to execute if the expression evaluates to true
} else {
// code to execute in all other cases
}
<html>
<head>
<title>Listing 5.2</title>
</head>
<body>
<?php
$mood = "sad";
if ( $mood == "happy" ) {
print "Hooray, I'm in a good mood";
} else {
print "Not happy but $mood";
}
?>
</body>
</html>
output-ul va fi:
if ( expression ) {
// code to execute if the expression evaluates to true
} elseif (another expression) {
// code to execute if the previous expression failed
// and this one evaluates to true
} else {
// code to execute in all other cases
}
În loc de elseif se poate scrie şi else if -- nu este nici o diferenţă.
Salvaţi urm cod ca elseif.php
<html>
<head>
<title>Listing 5.3</title>
</head>
<body>
<?php
$mood = "sad";
if ( $mood == "happy" ) {
print "Hooray, I'm in a good mood";
} elseif ( $mood == "sad" ) {
print "Awww. Don't be down!";
} else {
print "Neither happy nor sad but $mood";
}
?>
</body>
</html>
output-ul va fi:
utiliz instructiunii if împreună cu instr elseif permite evaluarea unor expresii multiple,
spre deosebire de instructiunea switch care evalueaza numai o expresie
rezultatul evaluarii unei expresii a instructiunii if este Boolean, iar rezultatul evaluarii
unei instructiuni switch este o valoare ce va fi comparată cu alte valori
switch ( expression ) {
case result1:
// execute this if expression results in result1
break;
case result2:
// execute this if expression results in result2
break;
default:
// execute this if no break statement
// has been encountered hitherto
}
<html>
<head>
<title>Listing 5.4</title>
</head>
<body>
<?php
$mood = "sad";
switch ( $mood ) {
case "happy":
print "Hooray, I'm in a good mood";
break;
case "sad":
print "Awww. Don't be down!";
break;
default:
print "Neither happy nor sad but $mood";
}
?>
</body>
</html>
output-ul va fi:
<html>
<head>
<title>Listing 5.5</title>
</head>
<body>
<?php
$mood = "sad";
$text = ($mood=="happy") ? "I'm in a good mood" : "Not happy but $mood";
print "$text";
?>
</body>
</html>
output-ul va fi:
while ( expression ) {
// do something
}
<html>
<head>
<title>Listing 5.6</title>
</head>
<body>
<?php
$counter = 1;
while ( $counter <= 12 ) {
print "$counter times 2 is ".($counter*2)."<br>";
$counter++;
}
?>
</body>
</html>
output-ul va fi:
1 times 2 is 2
2 times 2 is 4
3 times 2 is 6
4 times 2 is 8
5 times 2 is 10
6 times 2 is 12
7 times 2 is 14
8 times 2 is 16
9 times 2 is 18
10 times 2 is 20
11 times 2 is 22
12 times 2 is 24
Instructiunea do...while
Diferenta dintre aceasta instructiune şi instructiunea while constă în faptul că mai întâi
este executat codul şi apoi este verificată condiţia;
forma general a instructiunii do … while este:
do {
// code to be executed
} while ( expression );
output-ul va fi:
Execution number: 1
Instructiunea for
Forma generală este:
<html>
<head>
<title>Listing 5.8</title>
</head>
<body>
<?php
for ( $counter=1; $counter<=12; $counter++ ) {
print "$counter x 2 = ".($counter*2)."<br>";
}
?>
</body>
</html>
output-ul va fi:
1 times 2 is 2
2 times 2 is 4
3 times 2 is 6
4 times 2 is 8
5 times 2 is 10
6 times 2 is 12
7 times 2 is 14
8 times 2 is 16
9 times 2 is 18
10 times 2 is 20
11 times 2 is 22
12 times 2 is 24
Ieşirea din bucle cu instr break
- se utilizeaza de regulă pentru a ieşi din buclă, pe baza unor date adiţionale―de
exemplu pentru a preveni erori
Salvaţi codul urm ca break1.php
<html>
<head>
<title>Listing 5.9</title>
</head>
<body>
<?php
for ( $counter=1; $counter <= 10; $counter++ ) {
$temp = 4000/$counter;
print "4000 divided by $counter is... $temp<br>";
}
?>
</body>
</html>
Salvaţi codul urm ca break2.php
<html>
<head>
<title>Listing 5.10</title>
</head>
<body>
<?php
$counter = -4;
for ( ; $counter <= 10; $counter++ ) {
if ( $counter == 0 )
break;
$temp = 4000/$counter;
print "4000 divided by $counter is... $temp<br>";
}
?>
</body>
</html>
<html>
<head>
<title>Listing 5.11</title>
</head>
<body>
<?php
$counter = -4;
for ( ; $counter <= 10; $counter++ ) {
if ( $counter == 0 ) {
continue;
}
$temp = 4000/$counter;
print "4000 divided by $counter is... $temp<br>";
}
?>
</body>
</html>
output-ul va fi:
Obs slash-ul din faţa ghilimelelor! -- lucru necesar pt a tipari ghilimelele şi nu a marca începutul unui string
\n semnifică newline, iar \t reprezintă tab
Salvaţi codul următor ca imbricate2.php
<html>
<head>
<title>Imbricate2</title>
</head>
<body>
<?php
$display_prices = true;
if ( $display_prices ) {
print "<table border=\"1\">";
print "<tr><td colspan=\"3\">";
print "today's prices in dollars";
print "</td></tr>";
print "<tr><td>14</td><td>32</td><td>71</td></tr>";
print "</table>";
}
?>
</body>
</html>
Salvaţi codul urm ca imbricate3.php
<html>
<head>
<title>Imbricate3</title>
</head>
<body>
<?php
$display_prices = true;
if ( $display_prices ) {
?>
<table border="1">
<tr><td colspan="3">today's prices in dollars</td></tr>
<tr><td>14</td><td>32</td><td>71</td>
</table>
<?php
}
?>
</body>
</html>