Page 1 of 2

Which is better

Posted: Fri Oct 20, 2006 4:41 pm
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

Posted: Fri Oct 20, 2006 4:52 pm
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.

Posted: Fri Oct 20, 2006 5:06 pm
by feyd
I'd go with floor() as well.

Posted: Fri Oct 20, 2006 5:12 pm
by amir
Thanks!
I am asking which is better and why?
Any sujjestion would be very precious for me.

Posted: Fri Oct 20, 2006 5:15 pm
by feyd
"Better" is pretty subjective. How do you qualify better in this case?

Posted: Fri Oct 20, 2006 5:26 pm
by amir
You are right!
But some are obviously fast and others may be efficient.
Thats what i am asking about?

Posted: Fri Oct 20, 2006 5:29 pm
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.

Posted: Fri Oct 20, 2006 5:31 pm
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.

Posted: Fri Oct 20, 2006 5:31 pm
by amir
Thanks All!

Posted: Fri Oct 20, 2006 5:34 pm
by Cameri
Well, since type casting is faster than a function call, in this case, floor(), I'd go with type casting.

Posted: Fri Oct 20, 2006 5:40 pm
by s.dot
the purpose of typecasting is to cast to a type, not to floor

Posted: Fri Oct 20, 2006 5:45 pm
by RobertGonzalez
scottayy wrote:the purpose of typecasting is to cast to a type, not to floor
Well said scottayy.

Posted: Fri Oct 20, 2006 5:55 pm
by amir
Thanks for this healthy discussion but if you all dont mind, please also see my this post.
viewtopic.php?t=57419

Posted: Fri Oct 20, 2006 6:08 pm
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.

Posted: Fri Oct 20, 2006 6:22 pm
by amir
I am Sorry!
Thanks for informing me.