aquastream Posted February 6, 2010 Share Posted February 6, 2010 Okay, here's a newbie theoretical question. In other words, something I've come across which I have no idea why I would ever use but I'm curious about it anyway! If i add a number to a variable, the answer to this becomes the new value of that variable, and if I subtracted a number from the variable, it would then subtract it from the new value rather than the original value. For example: <?php $variable1 = 2; $variable1 += 4; echo $variable1."<br />"; $variable1 -= 1; echo $variable1; ?> However, when I use either a ceil or floor function to a float, the rounded value doesn't behave like the previous example. Instead, when I do a mathematical function to the variable, it takes it from the original value rather than the new ceil-ed or floor-ed value. For example: <?php $variable1 = 3.14; echo ceil($variable1)."<br />"; $variable1 += 1; echo $variable1 ?> Is the reason for this something to do with the fact that ceil and floor aren't pure mathematical functions like adding or squaring? If I wanted to in the previous example retain the value of the ceil (4) when adding to its value, how would I do this? PS: Brownie points to anyone who can come up with a reason as to why I would do this in a real project. Link to comment https://forums.phpfreaks.com/topic/191130-retaining-value-of-a-ceil-ed-function/ Share on other sites More sharing options...
jl5501 Posted February 6, 2010 Share Posted February 6, 2010 ceil etc are functions which output their results. The input could be a single variable or a whole expression. So if you need to do other than echo the output, you need to store it in a variable. $a = ceil($b); Link to comment https://forums.phpfreaks.com/topic/191130-retaining-value-of-a-ceil-ed-function/#findComment-1007813 Share on other sites More sharing options...
aquastream Posted February 6, 2010 Author Share Posted February 6, 2010 Thanks jl5501. That's so simple I probably should have thought of it, but I didn't and that's why I'm glad communities like this exist. Link to comment https://forums.phpfreaks.com/topic/191130-retaining-value-of-a-ceil-ed-function/#findComment-1007815 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.