Forum Moderators: coopster

Message Too Old, No Replies

What is faster? IF / ELSE or CASE

         

onlineleben

9:46 am on Apr 12, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hello all,
just a quick question regarding processing speed.

I use a short script to read information from the referrer and include files and images according to these results.
There are 7 different conditons that can occor (keyords in referrer) including the default.
The question is: is it faster to use the if / else if statements or the case statement to make the appropriate selections.
Code snippet see below.

Thanks for your kind advice.

$referer = strtolower($_SERVER["HTTP_REFERER"]);
if (strstr($referer,"keyword1"))
{
include ("www.example.com/includefile1.inc");
}
else if (strstr($referer,"keyword2"))
...

Matthew1980

10:08 am on Apr 12, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there onlineleben,

>>is it faster to use the if / else if statements or the case statement

This was a question asked a while back on this forum, but I can't find the thread. If/elseif/else and switch/case statements have little difference in operational speed - at least non that I have come across :)

It all depending on preference anyway; whichever method you find the easiest to maintain, just remember to have the default/else clause there to catch an errors though.

Have a read of this thread [webmasterworld.com] to see what I suggested for a similar question.

Cheers,
MRb

onlineleben

7:52 pm on Apr 12, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Matthew,
thanks for the quick reply this morning.
Reading your above advice and also the post mentioned above, I came to the conclusion that the switch/case statements are a little easier to maintain.

Would there be a chance to optimize more? Like finding the keyword string in the referer? Any ideas are much appreciated.

Matthew1980

8:19 pm on Apr 12, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



>>Would there be a chance to optimize more? Like finding the keyword string in the referer?

This sounds like something that can be achieved through the use of preg_match() or the like; isolate a string pattern & if the criteria is matched use the switch/case to direct the user to the correct place...

This is only a suggestion, and my knowledge of regex/preg_ functions is very limited; though there may be others who could suggest an alternative that may be a better course of action.

Yup, I agree, I find switch statements easier to deal with, though they are logically the same flow as if/elseif/else clauses..

Cheers,
MRb

onlineleben

8:35 pm on Apr 12, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks for the preg_match idea. Checked it out on php.net which suggested to use strpos which looks for the first occurrence of a string. They say it is faster than preg_match and strstr.
I could have sped it up by using stristr which is not case sensitive.

anyway, lots to figure out over the next days.
Thanks again for your feedback - good night

Matthew1980

7:25 am on Apr 13, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there onlineleben,

Well, intrigue has the better of me now, could you post an example of what you're looking at as the incoming string, and how/where it is getting set. Then I will have a look and see if there is a more elegant approach to this issue.

Other than that, have fun on your learning curve - and when you have a workable solution, let us know so that others may benefit from it too.

Cheers,
MRb

onlineleben

11:25 am on Apr 13, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Matthew,
thanks for the kind offer.
What I do is
a) take the referrer (including query string) from a searchengine and convert it to lower case using strtolower
b) then check if the full referrer string contains a certain keyword by use of strstr
c) based on occurrence of keword, it runs through an if/else (or switch/case) cascade to serve adds related to keyword

From what I found on php.net last night I probably could shorten the process by using the stristr statement (instead of strtolower + strstr) which is case-insensitive.
PHP version I use is 4.x

Looking forward to read your ideas.