Website Development Prices

Search Blog

Wednesday, May 13, 2015

Operatori dodele (Assignment operators)

Operatori dodele:

  • osnovni operator
  • kombinovani operatori
Osnovni operator dodele je =. On promenljivoj dodaje vrednost.

$motori = 10;

Nesto slozeniji primer je podesavanje tri promenljive na istu vrednost. Ovaj kod sacuvajte kao operatori-dodele.php.

<?php
$motor = $kaciga = $rukavice = 1;

echo $motor, ", ", $kaciga, ", ", $rukavice; // rezultat: 1, 1, 1
?>


Kombinovani operatori dodele su oni koji se kombinuju sa operatorom =

+=, -=, *=, /=, %=, .=, &=, |=, ^=, <<=, >>=

Ako zelite da promenljivu $moji_motori povecate za 10

$moji_motori = $moji_motori + 10;

Pomocu kombinovanog operatora dodele += ovo mozete da skratite.

$moji_motori += 10;

Primer upotrebe operatora spajanja (.) i operatora deljenja (/).
<?php
$tekst = "Volim ";
$ukupno = 150;

echo $tekst .= "motore.<br>"; // rezultat : Volim motore.
echo "Prosek = ", $ukupno /=3, "<br>"; // rezultat: Prosek = 50
?>

Assignment Operators:

  • basic operator
  • combined operators

The basic assignment operator is =. It adds value to a variable.

$motobikes = 10;


Something more complex example is setting three variables to the same value. This code save as assignment-operators.php.

<?php
$motobike = $helmet = $gloves = 1;

echo $motobike, ", ", $helmet, ", ", $gloves; // R: 1, 1, 1
?>

Combined assignment operators are the ones that are combined with the operator =

+ =, - =, * =, / =,% =,. =, & =, | =, ^ =, << =, >> =

If you want the variable $my_motobikes increase by 10

$my_motobikes = $my_motobikes + 10;

With combined assignment operator + = you can shorten this.

$my_motobikes + = 10;


Example of usage concatenation operator (.) and the division operator (/).

<?php
$text = "I love ";
$total = 150;

echo $text .= "motobikes.<br>"; // R: I love motobikes.
echo "Average = ", $total /=3, "<br>"; // R: Average = 50
?>

No comments:

Post a Comment

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