Ako se u istom trenutku koristi vise operatora, koji se koristi prvi?
Primer:
<?php
echo "4 + 2 * 9";
?>
Ovaj fajl sacuvajte kao prioritet-operatora.php.
Da li ce prvo izracunati deo sa operatorom +, tako da se dobije 4+2*9=6*9=54 ili ce se prvo izracunati deo sa operatorom *, tako da se dobije 4+2*9=4+28=22?
Odgovor je 22. Zato sto operator * ima prioritet nad operatorom +.
Ako prioritet operatora nije u skladu sa onim sto zelite, mozete da upotrebite zagrade.
Ako izraz promenite na (4+2)*9, dobicete 6*9=54.
Primer:
<?php
echo "4 + 2 * 9 = ", 4 + 2 * 9, "<br>"; // R: 4 + 2 * 9 = 22
echo "(4 + 2) * 9 = ", (4 + 2) * 9, "<br>"; // R: (4 + 2) * 9 = 54
echo "4 + (2 * 9) = ", 4 + (2 * 9), "<br>"; //R: 4 + (2 * 9) = 22
?>
If at the same time you are using multi operator, which one to use first?
Example:
<?php
echo "4 + 2 * 9";
?>
This file save as priority-operators.php.
To calculate which operator will be first used, operator +, so that you get 4 + 2 = 6 * 9 * 9 = 54, or it will be used the first operator *, so that you get 4 + 2 = 4 * 9 + 28 = 22?
Answer is 22. Because the operator * has priority over the operator +..
If operator precedence is not in line with what you want, you can use parentheses.
If you change the expression (4 + 2) * 9, you will get 6 * 9 = 54.
Example:
<?php
echo "4 + 2 * 9 = ", 4 + 2 * 9, "<br>"; // R: 4 + 2 * 9 = 22
echo "(4 + 2) * 9 = ", (4 + 2) * 9, "<br>"; // R: (4 + 2) * 9 = 54
echo "4 + (2 * 9) = ", 4 + (2 * 9), "<br>"; //R: 4 + (2 * 9) = 22
?>
Primer:
<?php
echo "4 + 2 * 9";
?>
Ovaj fajl sacuvajte kao prioritet-operatora.php.
Da li ce prvo izracunati deo sa operatorom +, tako da se dobije 4+2*9=6*9=54 ili ce se prvo izracunati deo sa operatorom *, tako da se dobije 4+2*9=4+28=22?
Odgovor je 22. Zato sto operator * ima prioritet nad operatorom +.
Ako prioritet operatora nije u skladu sa onim sto zelite, mozete da upotrebite zagrade.
Ako izraz promenite na (4+2)*9, dobicete 6*9=54.
Primer:
<?php
echo "4 + 2 * 9 = ", 4 + 2 * 9, "<br>"; // R: 4 + 2 * 9 = 22
echo "(4 + 2) * 9 = ", (4 + 2) * 9, "<br>"; // R: (4 + 2) * 9 = 54
echo "4 + (2 * 9) = ", 4 + (2 * 9), "<br>"; //R: 4 + (2 * 9) = 22
?>
If at the same time you are using multi operator, which one to use first?
Example:
<?php
echo "4 + 2 * 9";
?>
This file save as priority-operators.php.
To calculate which operator will be first used, operator +, so that you get 4 + 2 = 6 * 9 * 9 = 54, or it will be used the first operator *, so that you get 4 + 2 = 4 * 9 + 28 = 22?
Answer is 22. Because the operator * has priority over the operator +..
If operator precedence is not in line with what you want, you can use parentheses.
If you change the expression (4 + 2) * 9, you will get 6 * 9 = 54.
Example:
<?php
echo "4 + 2 * 9 = ", 4 + 2 * 9, "<br>"; // R: 4 + 2 * 9 = 22
echo "(4 + 2) * 9 = ", (4 + 2) * 9, "<br>"; // R: (4 + 2) * 9 = 54
echo "4 + (2 * 9) = ", 4 + (2 * 9), "<br>"; //R: 4 + (2 * 9) = 22
?>
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.