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
Old 10-22-2012, 08:25 AM   #7 (permalink)
The Addict
 
Join Date: Oct 2012
Posts: 244
Thanks: 0
dashixiong is on a distinguished road
Default Coach Outlet

You’ve relativelyCoach Outlet recently arrived in New Delhi after living in two of Asia’s other great cities,Coach Outlet Store Online Tokyo and Hong Kong, for several years. Do these cities feel like they’re part of the same continent? Yes, and no. In terms Coach Factory Onlineof infrastructure, they couldn’t be more different. Getting regularCoach Outlet power and water at my house in New Delhi is never a sure thing, even though Coach Purse Outlet OnlineI’m paying the same rent that I paid in Tokyo and almost the same electricity prices. Both Hong Kong and Tokyo are also crowded places,Coach Factory Outlet Online but both cities are incredibly well planned and efficiently run. Efficient is not a word I would use to describe my Coach Bags Outlet Onlineday-to-day life in New Delhi. On the other hand, one thing that I think Hong Kong and New Delhi have in common isCoach Handbags Outlet a shared sense of optimism — a feeling that the best is yet to come. That’s definitely not the feeling you get in Tokyo,Coach Outlet Online or in the U.S. when I go home. It’s a big part of what I find addictive about living and working in this part of the world. You feel like you’re watching the future unfold.
dashixiong 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
How to create a gallery class Tanax Advanced PHP Programming 25 02-19-2013 04:25 AM
Timezone Class: Dealing with Timezones the Proper Way Wildhoney General 2 01-10-2011 11:01 PM
Next class project? allworknoplay The Lounge 6 04-18-2009 08:33 PM
Part 2: Giving our Currency Conversion Script some Responsibility Wildhoney General 15 03-17-2009 01: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 03:22 PM.

 
     

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