PDA

View Full Version : Which is quicker?



Decode
11-07-2008, 07:40 PM
This;


if ( $id == "1" ) {
}
else {
if ( $id == "2" ) {
}
else {
if ( $id == "3" ) {
}
else {
}
}
}
or this;


if ( $id == "1" ) {
}
else if ( $id == "2" ) {
}
else if ( $id == "3" ) {
}
else {
}
Im using the 2nd one alot arround my site and i was wondering if the first way is quicker.

Trigs
11-07-2008, 08:19 PM
2nd one

Independent
11-07-2008, 10:37 PM
This;


if ( $id == "1" ) {
}
else {
if ( $id == "2" ) {
}
else {
if ( $id == "3" ) {
}
else {
}
}
}
or this;


if ( $id == "1" ) {
}
else if ( $id == "2" ) {
}
else if ( $id == "3" ) {
}
else {
}
Im using the 2nd one alot arround my site and i was wondering if the first way is quicker.
Second version, as I think if else's are slower than elseifs.

Source
12-07-2008, 01:46 AM
Surely the best way would be to use switch and cases....



$id = 2;

switch ($id) {
case 1:
echo "ID has the value of 1";
//add query/function/echo etc...
break;
case 2:
echo "ID has the value of 2";
//add query/function/echo etc...
break;
case 3:
echo "ID has the value of 3";
//add query/function/echo etc...
break;
}

That would bring up case 2 as the number of $id is 2....

Hope that helps, seems a more logical way of doing it to me. However from the 2 that you posted, 2 wouldn't be any faster but the better way todo it.

Jahova
12-07-2008, 06:36 AM
Second one, I'm sure =)

Independent
12-07-2008, 10:56 AM
Second one, I'm sure =)
No, they both take the same time. Matt's solution is a lot cleaner code and faster.

Switches are a lot better than 100 else if/if else's.

Source
12-07-2008, 11:23 AM
Note my user tag "$learning::php( "slowly" );" - thus meaning I am learning php slowly. So the switch option would be the best option for me personally, but I may be wrong in terms of it been better, though im 90% sure.

Invent
12-07-2008, 11:51 AM
Note my user tag "$learning::php( "slowly" );" - thus meaning I am learning php slowly. So the switch option would be the best option for me personally, but I may be wrong in terms of it been better, though im 90% sure.Meh. Sadly most of the php coders on this forum won't understand that that's valid php (your usertag).

Source
12-07-2008, 11:53 AM
haha, they will all learn soon enough surely?

I love the name : "Paamayim Nekudotayim" (the :: for none php coders)

Invent
12-07-2008, 11:57 AM
haha, they will all learn soon enough surely?

That's what I'm hoping for! :)

Agnostic Bear
12-07-2008, 02:12 PM
That's what I'm hoping for! :)

Doubtable.

Independent
12-07-2008, 03:37 PM
Doubtable.
I'm hoping that you'll stop trying to compete with Caleb over an image upload site.

Want to hide these adverts? Register an account for free!