Which is better

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!

Moderator: General Moderators

amir
Forum Contributor
Posts: 287
Joined: Sat Oct 07, 2006 4:28 pm

Which is better

Post by amir »

these are items
1.2
1.56
1.765
and i can take the first part i mean before '.' in several ways like

1;

Code: Select all

$res = floor ($value)
2;

Code: Select all

echo (int)'1.75';
3;

Code: Select all

$arr = explode(".", "1.2");
    $val = arr[0];
i want to know which is better and why

i know first and 3rd having calls, so they are slow

thanks
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

It really depends on what you are trying to do with the returned value and what is being passed (Is it always of var type double?)

Generally I'd use floor() for this.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I'd go with floor() as well.
amir
Forum Contributor
Posts: 287
Joined: Sat Oct 07, 2006 4:28 pm

Post by amir »

Thanks!
I am asking which is better and why?
Any sujjestion would be very precious for me.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

"Better" is pretty subjective. How do you qualify better in this case?
amir
Forum Contributor
Posts: 287
Joined: Sat Oct 07, 2006 4:28 pm

Post by amir »

You are right!
But some are obviously fast and others may be efficient.
Thats what i am asking about?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

floor(). It returns a float back from the call so your types are the same. You are not changing the core data (as with type casting) and explode() requires the initial call (with overhead) plus reading an var into the reurn.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

floor is better because that's the purpose of the function! Typecasting and splitting into an array are just workarounds to do what floor already does.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
amir
Forum Contributor
Posts: 287
Joined: Sat Oct 07, 2006 4:28 pm

Post by amir »

Thanks All!
User avatar
Cameri
Forum Commoner
Posts: 87
Joined: Tue Apr 12, 2005 4:12 pm
Location: Santo Domingo, Dominican Republic

Post by Cameri »

Well, since type casting is faster than a function call, in this case, floor(), I'd go with type casting.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

the purpose of typecasting is to cast to a type, not to floor
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

scottayy wrote:the purpose of typecasting is to cast to a type, not to floor
Well said scottayy.
amir
Forum Contributor
Posts: 287
Joined: Sat Oct 07, 2006 4:28 pm

Post by amir »

Thanks for this healthy discussion but if you all dont mind, please also see my this post.
viewtopic.php?t=57419
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

amir wrote:Thanks for this healthy discussion but if you all dont mind, please also see my this post.
viewtopic.php?t=57419
Please refrain from bumping other threads within a thread.
[url=http://forums.devnetwork.net/viewtopic.php?t=30037]Forum Rules[/url] Section 1.1 wrote:4. All users of any level are restricted to bumping (as defined here) any given thread within twenty-four (24) hours of its last post. Non-trivial posts are not considered bumping. A bump post found in violation will be deleted, and you may or may not recieve a warning. Persons bumping excessively be considered as spammers and dealt with accordingly.
amir
Forum Contributor
Posts: 287
Joined: Sat Oct 07, 2006 4:28 pm

Post by amir »

I am Sorry!
Thanks for informing me.
Post Reply