Website Development Prices

Search Blog

Thursday, January 28, 2016

Alternativna sintaksa za iskaze (Alternative syntax for the statements)

U PHP-u postoji i alternativna sintaksa za iskaze if, while, for, foreach i switch. Ona menja otvorenu viticastu zagradu u dvotacku (:), a zatvorenu zagradu u endif;, endwhile; endfor;, endforeach; i endswitch;.

Primer: upotreba alternativne sintakse sa iskazom if.

<?php

$vrednost = 10;
if ($vrednost == 1): 
echo "\$vrednost sadrzi 1";
elseif ($vrednost == 10):
echo "\$vrednost sadrzi 10.";
else:
echo "\$vrednost ne sadrzi ni 1 ili 10.";
endif;


?>


Sacuvajte ovaj fajl kao alertnativna-sintaksa.php.

Rezultat:

$vrednost sadrzi 10.

Primer: upotreba alternativne sintakse sa petljom for.

<?php

for ($brojac_petlje = 0; $brojac_petlje < 4; 
$brojac_petlje++) :
echo "Ovo cu uraditi 4 puta.<br>";

endfor; 

?>

Rezultat:

Ovo cu uraditi 4 puta.
Ovo cu uraditi 4 puta.
Ovo cu uraditi 4 puta.
Ovo cu uraditi 4 puta.


In PHP there is an alternative syntax for the statements if, while, for, foreach, and switch. It changes the open curly brace in a colon (:) and the closing curly brace to endif;, endwhile; endfor;, endforeach; and endswitch;.

Example: use of alternate syntax with the statement if.

<?php

$value = 10;
if ($value == 1): 
echo "\$value contains 1";
elseif ($value == 10):
echo "\$value contains 10.";
else:
echo "\$value does not contain either 1 or 10.";

endif;

?>

Save this file as alternative-syntax.php.

Result:

$value contains 10.

Example: use of alternate syntax with the loop for.

<?php

for ($loop_counter = 0; $loop_counter < 4; 
$loop_counter++) :
echo "I will do this 4 times.<br>";
endfor; 


?>

Result:

I will do this 4 times.
I will do this 4 times.
I will do this 4 times.
I will do this 4 times.


No comments:

Post a Comment

Note: Only a member of this blog may post a comment.