TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 02-06-2010, 07:47 AM   #1 (permalink)
The Wanderer
 
Join Date: Feb 2010
Posts: 6
Thanks: 1
aquastream is on a distinguished road
Default Retaining Value of a Ceil-ed Function

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.
aquastream is offline  
Reply With Quote
Old 02-06-2010, 08:10 AM   #2 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

The reason it's not acting like the first example is because you're not assigning the result of ceil().

php Code:
$var = 3.14;
echo ceil( $var ); // This just returns the result to STDOUT
$var = ceil( $var ); // This is equiv to what you were doing in the first example
 
delayedinsanity is offline  
Reply With Quote
The Following User Says Thank You to delayedinsanity For This Useful Post:
aquastream (02-06-2010)
Old 02-06-2010, 08:59 AM   #3 (permalink)
The Wanderer
 
Join Date: Feb 2010
Posts: 6
Thanks: 1
aquastream is on a distinguished road
Default

Quote:
Originally Posted by delayedinsanity View Post
The reason it's not acting like the first example is because you're not assigning the result of ceil().

php Code:
$var = 3.14;
echo ceil( $var ); // This just returns the result to STDOUT
$var = ceil( $var ); // This is equiv to what you were doing in the first example
 
Simple solution to a simple problem. Why didn't I think of that! Thanks DelayedInsanity.
aquastream is offline  
Reply With Quote
Old 02-06-2010, 10:57 AM   #4 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

Simple solutions are sometimes the hardest to come by. I spent 20 minutes the other day writing a function for an application I'm developing, only to find out that it was a new function already introduced into PHP > 5.2 - the reason I found out? I gave it the same name they did.
delayedinsanity is offline  
Reply With Quote
Old 02-06-2010, 01:51 PM   #5 (permalink)
The Addict
 
Enfernikus's Avatar
 
Join Date: Jun 2008
Posts: 335
Thanks: 2
Enfernikus is on a distinguished road
Default

This is why you should read the docs :D
__________________
My Blog
Enfernikus is offline  
Reply With Quote
Old 02-06-2010, 08:33 PM   #6 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

I actually do read the docs, religiously. I have a tab open with us.php.net almost 100% of the time when I'm coding, even though I've been doing this for years.

What I don't read but probably should start, is the changelog. ;)
delayedinsanity is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Timezone Class: Dealing with Timezones the Proper Way Wildhoney General 2 01-10-2011 11:01 PM
How to create a gallery class Tanax Advanced PHP Programming 22 06-24-2009 12:36 AM
Next class project? allworknoplay The Lounge 6 04-18-2009 09:33 PM
Part 2: Giving our Currency Conversion Script some Responsibility Wildhoney General 15 03-17-2009 02:53 PM
[Tutorial] How to organize your classes | Part 1 Tanax Advanced PHP Programming 10 03-01-2009 10:08 PM


All times are GMT. The time now is 07:06 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design