PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
<?php
// initialize a session
session_start();
if(isset($_SESSION['counter'])){
//echo "<em><b>You have viewed this page</b></em> " . $_SESSION['counter']++ . " <em><b>times</b></em>";
echo "<em><b>You have viewed this page</b></em> ". $_SESSION['counter']+=1 ." <em><b>times</b></em>";
}else{
echo "<em><b>You have only viewed this page</b></em> " . $_SESSION['counter']=1 . " <em><b>times</b></em>";
}
?>
Ok! question: Why is it that when I echo $_SESSION['counter']+=1, idon't get to see the following line: <em><b>times</b></em>"; , on my browser but , echo "<em><b>You have viewed this page</b></em> " . $_SESSION['counter']++ . " <em><b>times</b></em>"; works just fine.
Thanks
Last edited by Benjamin on Thu Oct 14, 2010 2:13 pm, edited 1 time in total.
Reason:Added [syntax=php] tags.
It's operator precedence. You know how, in math, multiplication and division are more "important" then addition and subtraction? 1 + 2 * 3 = 1 + (2 * 3) and not (1 + 2) * 3.
Same in PHP. Normal addition (+) and string concatenation (.) have a higher precendece than normal assignment (=) and additive assignment (+=). So