Sunteți pe pagina 1din 22

Instructiuni de control

Î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:

Hooray, I'm in a good mood


Dacă vom sch val variab $mood în "sad", nu se va mai intra în codul instr if, şi nu se va mai afişa nimic.
Instructiunea else
Are scopul de a gestiona alternative la condiţia instr if. Forma generală este:

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:

Not happy but sad


Instructiunea elseif

-se utilizeaza pe lânga if şi elseif pentru a testa mai multe expresii


Forma generală este:

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:

Awww. Don't be down!


Instructiunea switch
Instrucţiunea switch este o alternativă la instr if else;
există câteva diferente importante între instructiunile switch şi if:

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

Forma general a instructiunii switch este:

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:

Awww. Don't be down!


Operatorul ?
Operatorul ? este un op ternar, care are forma generală:

( expresie )?val_dacă expr_este_true: val _dacă expr_este_false;

Salvaţi codul urm ca tern.php

<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:

Not happy but sad


Bucle

Până acum am studiat deciziile luate de un cod relativ la ce urmareste să execute.


În continuare script-urile vor decide de câte ori să execute un block de cod.
În general, o buclă continuă să se execute până se îndeplineste o condiţie, sau se
specifică explicit ieşirea din buclă.
Instructiunea while
Instructiunea while are forma generală:

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 );

Întotdeauna se pune punct şi virgulă după expresia instr while.


<html>
<head>
<title>Listing 5.7</title>
</head>
<body>
<?php
$num = 1;
do {
print "Execution number: $num<br>\n";
$num++;
} while ( $num > 200 && $num < 400 );
?>
</body>
</html>

output-ul va fi:

Execution number: 1
Instructiunea for
Forma generală este:

for ( initialization expression; test expression; modification expression ) {


// code to be executed
}

Salvaţi codul urm ca for.php

<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>

Împărţirea unui număr la 0 nu cauzează o eroare fatală în PHP; aceasta împartire va


genera o avertizare şi executia va continua.
Ignorarea unei iteraţii prin utilizarea instrucţiunii continue
Înstr continue opreşte execuţia iteraţiei curente dar nu provoacă ieşirea din buclă, ci numai trecerea
la iteraţia imediat următoare.

Salvaţi codul următor ca continue.php

<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:

4000 divided by -4 is... -1000


4000 divided by -3 is... -1333.33333333
4000 divided by -2 is... -2000
4000 divided by -1 is... -4000
4000 divided by 1 is... 4000
4000 divided by 2 is... 2000
4000 divided by 3 is... 1333.33333333
4000 divided by 4 is... 1000
4000 divided by 5 is... 800
4000 divided by 6 is... 666.666666667
4000 divided by 7 is... 571.428571429
4000 divided by 8 is... 500
4000 divided by 9 is... 444.444444444

observam ca instructiunea continue este mai puţin “drastică” faţă de


instructiunea break.
Bucle imbricate
Buclele imbricate sunt utile în cazul lucrului cu tabele HTML dinamice.

Salvaţi codul urm ca imbricate.php


<html>
<head>
<title>Imbricate1</title>
</head>
<body>
<?php
print "<table border=\"1\">\n";
for ( $y=1; $y<=12; $y++ ) {
print "<tr>\n";
for ( $x=1; $x<=12; $x++ ) {
print "\t<td>";
print ($x*$y);
print "</td>\n";
}
print "</tr>\n";
}
print "</table>";
?>
</body>
</html>

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>

S-ar putea să vă placă și