are if-else statements slow to parse

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

Post Reply
doug_dev
Forum Newbie
Posts: 3
Joined: Tue Jul 08, 2003 4:24 pm

are if-else statements slow to parse

Post by doug_dev »

I am new to PHP and have the following code to determine what style to give a particular menu item. Is my if-else structure as effecient as possible? and are the if statements time consuming? Is there a better way? - appreciate your insights.

the style reference needs to change for each new nav presented. $p is the indicator for which page is being viewed.

Code: Select all

<?php

if ($p == "news")
	$style2 = "linkonnav";
	else $style2 = "linkoffnav";
	
	echo "<font class="$style2"><a href="main.php?p=news" class="$style2">What's New</a></font>

if ($p == "online")
	$style2 = "linkonnav";
	else $style2 = "linkoffnav";
	
	echo "<font class="$style2"><a href="main.php?p=online" class="$style2">Online Books & Essays</a></font>";

?>
User avatar
trollll
Forum Contributor
Posts: 181
Joined: Tue Jun 10, 2003 11:56 pm
Location: Round Rock, TX
Contact:

Post by trollll »

For something like that, you don't really have to worry about the speed of if/then statements and the statement you wrote wouldn't noticably run any faster most likely.

If you had to loop through a section of code a few thousand times which included comparisons then it would run a bit faster if you could use switch/case instead. But even that depends on who you ask. Also, strings generally take up more memory and more crunching time to compare than, say, integers or boolean.

Long story short, it will run fast enough and won't run that much faster for your example. :)
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

Looks OK (but would be even nicer with braces :wink: ).

If you ever want to speed test some code try this:

Code: Select all

<?php
function getmicrotime(){

    list($usec, $sec) = explode(" ",microtime());
    return ((float)$usec + (float)$sec);
}

$start = getmicrotime();

// the code to test

$end = getmicrotime();
$time = $end - $start;
echo '<p>time = ' . $time . '</p>';
?>
Last edited by McGruff on Thu Aug 11, 2005 2:25 am, edited 1 time in total.
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Re: are if-else statements slow to parse

Post by m3rajk »

doug_dev wrote:I am new to PHP and have the following code to determine what style to give a particular menu item. Is my if-else structure as effecient as possible? and are the if statements time consuming? Is there a better way? - appreciate your insights.

the style reference needs to change for each new nav presented. $p is the indicator for which page is being viewed.

Code: Select all

<?php

if ($p == "news")
	$style2 = "linkonnav";
	else $style2 = "linkoffnav";
	
	echo "<font class="$style2"><a href="main.php?p=news" class="$style2">What's New</a></font>";

if ($p == "online")
	$style2 = "linkonnav";
	else $style2 = "linkoffnav";
	
	echo "<font class="$style2"><a href="main.php?p=online" class="$style2">Online Books & Essays</a></font>";

?>
was that second if statement suppossed to be in the echo?
doug_dev
Forum Newbie
Posts: 3
Joined: Tue Jul 08, 2003 4:24 pm

braces?

Post by doug_dev »

What do you mean by braces? I am new to this and want to build as clean as possible.

Thanks again for your insights. I tried the speed test, but I am not sure what sort of speed I am looking for.

0.004 was the time on one particular full page I had built. Good? bad?

Thanks.
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

With braces:

Code: Select all

<?php

IF ($p == 'news') {

   $style2 = 'linkonnav';

} ELSE { 

    $style2 = 'linkoffnav';
}
   
echo '<font class="' . $style2 . '"><a href="main.php?p=news" class="' . $style2 . '">What''s New</a></font>';

IF ($p == 'online') {

   $style2 = 'linkonnav';

} ELSE {

    $style2 = 'linkoffnav';
}   
  
echo '<font class="' . $style2 . '"><a href="main.php?p=online" class="' . $style2 . '">Online Books & Essays</a></font>'; 
?>
That's not meant to be a functioning script: I couldn't quite work out what you're logic is. For example I started to re-write it like this..

Code: Select all

<?php

IF ($p == 'news' OR $p == 'online') {

   $style2 = 'linkonnav';

} ELSE { 

    $style2 = 'linkoffnav';
}
   
IF ($p == 'news') {

    echo '<font class="' . $style2 . '"><a href="main.php?p=news" class="' . $style2 . '">What''s New</a></font>';

} ELSEIF ($p == 'online') {

    echo '<font class="' . $style2 . '"><a href="main.php?p=online" class="' . $style2 . '">Online Books & Essays</a></font>'; 
}
?>
.. but couldn't see how $style = 'linkoffnav'; fits into the picture.

Braces around IF / ELSE make the code easier to read. I personally like to use whitespace (ie newlines) liberally as well for the same reason - I always add a newline after an IF or ELSE for example. The block of code inside the IF leg stands out much better - see m3rajk's question about the echo, above. One day you may be working in a team...

Upper case "IF" and "ELSE" rather than "if" or "else" possibly makes these items stand out better again enhancing readability.

I generally follow the PEAR coding standards http://pear.php.net/manual/en/standards.php (apart from the "one true brace" style). Style might not seem to matter much when you are starting out (compared to scarier issues like inheritance or passing by reference) but it really does help to work out a good system. Easy-to-read code is much easier to work with.

Naming conventions are worth a mention in this regard. Descriptive names for variables and functions make life a lot easier - the code can almost read like pure English. For example $first_name is trivial to understand but $fn is not. Same applies to functions: listAddresses($string) or addlst($string)? In your own code it's not clear what $p stands for.

Commenting the code also helps to make it easier to understand. In a few months time you'll have forgotten all the script logic.

Note how I rewrote the strings with single quotes. Strings can be defined with either single or double quotes: $vars won't be parsed inside single quotes however so you jump out the string and concatenate with a "." (again some spaces help to make this more readbale but aren't essential). There are rare occasions when vars aren't parsed inside double quotes so I almost always take the time to concatenate a string. That doesn't sound quite right - but I have had the odd difficulty.

Also, notice how the syntax highlighting helps to flag up vars inside a string when you concatenate.

The advantage of single quotes are that you don't have to escape " in the html (eg in the <a href="etc">). It's also "faster" since a single-quoted string isn't parsed - but we're talking tiny fractions of a second so it doesn't make any real difference in practice. Don''t forget to escape any '' in the string: it''s easy to forget.

0.004 sounds fine. I normally get 0.2s - 0.5s or so on my creaky old 700 Duron desktop for forum browsing scripts or similar. The above script is of course much simpler - no db queries, BBcode processing etc.

The main speed issue with a page load on a live site is almost always going to be from graphics used in the page. As a rough rule of thumb, I'm happy if my scripts come in at under half a second (site search scripts excluded!) since that isn't going to create any noticable extra lag.

I've also found that with Zone Alarm on, scripts on my local server take almost ten times as long to run.
Last edited by McGruff on Thu Aug 11, 2005 2:23 am, edited 1 time in total.
doug_dev
Forum Newbie
Posts: 3
Joined: Tue Jul 08, 2003 4:24 pm

thanks

Post by doug_dev »

appreciate the insights. better to learn to do it right from the beginning!
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

I don't think upper-cases languange keywords is a good idea. (IF vs if), almost everyone uses lower-case for languange constructs and uppercase for constants. So by making your language constructs uppercase you are likely to confuse people reading/skimming the code.

The single/double quote arguement isn't that important and really just comes down to user-preference. I find I rather have variable interpolation and have to escape symbols than note. I also find that heredoc's are more useful than either most of the time.
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

Rgr. There are different opinions about best coding style but it is best to comply with the norms (a search should turn up quite a long thread on the subject from a month or two back).
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

Agreed. However, I don't think your UPPERCASE suggestion is a norm and I don't think that single/double/heredoc is a norm either. I've never seen a debate thread on this site that wasn't just people saying "my way is better" on the quoting issue.
Post Reply