...

View Full Version : Need help with if, else



AshleyQuick
01-26-2004, 10:39 PM
I have the following variables:

$pageTitle = "Products";
$pageSubTitle = "Product One";

I'm then echoing them in various places but how can I wrap the $pageSubTitle in an if statement so that it is NOT displayed is a Subtitle is not declared?

Here's an example of the echo:

<title>XYZ, Inc. - <?php echo $pageTitle ?> - <?php echo $pageSubTitle ?></title>

I'm sure this is an easy fix. For some reason, I have reached an impasse.

Ashley

Nightfire
01-26-2004, 10:57 PM
I'm missing something. If the subtitle isn't declared then it just won't show up. I don't understand why you need an if statement for it? Unless you mean the subtitle not to show if the pagetitle isn't set... if that's the case then this'll do it:



if(isset($pageTitle)){
$pageSubTitle = "";
}

raf
01-26-2004, 10:57 PM
Maybe:


<?php
$outpt = $pageTitle ;
if (strlen($pageSubTitle ) >= 1){
$outpt .= ' - ' . $pageSubTitle ;
}
?>
....
<title>XYZ, Inc. - <?php echo $outpt ?></title>

--> i like to first build the outputstrings and then just include them as one variable, specially if you use them at several places in your code.

But you can just as well slip in in your code.

<edit> posts crossed. I thought the problem was the "- " wich would be shown and would look weird if no subtitle was set.</edit>

AshleyQuick
01-26-2004, 11:06 PM
I came up with this (which works fine):


<?php
if(!isset($pageSubTitle)){
echo '<title>XYZ, Inc. - ',$pageTitle,' - blah, blah</title>';
} else {
echo '<title>XYZ, Inc. - ',$pageTitle,' - ',$pageSubTitle,' - blah, blah</title>';
}
?>

...for these two scenarios:

1.


<?php
$pageTitle = "Products";
?>

2.


<?php
$pageTitle = "Products";
$pageSubTitle = "Product One";
?>

I want to use code that will render the fastest. What do you guys recommend?

raf
01-26-2004, 11:16 PM
I think that would throw an error but if you say it works fine, then i suppose i thought wrong.

I would use


<?php
$outpt = ('<title>XYZ, Inc. - ' . $pageTitle) ;
if (strlen($pageSubTitle ) >= 1){
$outpt .= ' - ' . $pageSubTitle ;
}
$outpt .= ' - blah, blah</title>';
echo $outpt;
?>


If you can show me any differnce above 1 nanosecond in renderingtime, then i'll eat my mouse

sitami
01-26-2004, 11:28 PM
i'd pay to see that :D :thumbsup:

raf
01-27-2004, 12:09 AM
Originally posted by sitami
i'd pay to see that :D :thumbsup:
Start coding then and include a few timers :D It's a cordless one so a few bites and it's gone (i hope):p

AshleyQuick
01-27-2004, 02:56 AM
raf,

I like your iteration better. :) Just to clarify, I know a small script such as these wouldn't effect rendering time. It's just that I always strive to implement efficient code. :)

Oh well, I guess you no longer need to devour your mouse...a shame really. ;)

Ashley

raf
01-27-2004, 08:10 AM
Originally posted by AshleyQuick
Just to clarify, I know a small script such as these wouldn't effect rendering time. It's just that I always strive to implement efficient code. :)
You almost make it sound as if my code would not be efficient ...
For maintenance, i believe this codingstyle is more efficient.
For coding, to set up your 'control of flow' this is also more efficient since i don't need to print $outp there if i don't want to (for instance if i would like to redirect to an error-page further down the script. ) So it leaves you more flexability
Also, i would probably not include the title tags, and then i'd be able to include different titles for the different stages where the page is reloaded (i use a loot of multiputpose pages)
You could also run a performance check, and i believe that my code would come out on top, because i need less evaluationsteps. I'm just guessing here but i suppose that the page subtitle will be set in the vast majority of pages. If you write if then elses or switches or whatever, then you should always have the evaluations in such an order that the most occuring case comes first. Then the second most occuring and so on.
So my check would be more efficient if there are indeed subtitles on most pages (for this code the evaluation order wount make a different, but if you would process a large recordset or run a scrip in your db against a table of afew thousand records, and you have an inefficient evaluationorder, then you certainly would notice the difference (if you time it)

Originally posted by AshleyQuick
Oh well, I guess you no longer need to devour your mouse...a shame really. ;)
Ashley
I actually first concidered to write 'keyboard' but i got scared :p

AshleyQuick
01-27-2004, 02:26 PM
raf,

I hoped I wouldn't be misunderstood, and it looks as if I was. I just wanted to make certain I implemented the most efficient version...which I feel I did...yours. :)

Thanks again for all your help and time!

Ashley

raf
01-27-2004, 02:58 PM
No problem. I was just clarifying.

I work alott with SQL and SAS scripts on huge tables, and then it indeed matters which checks you place where in which order (specially since CPU-time becomes a cost-factor in most businesses and the 'we just buy a bigger machine and chuck in some extra RAM attitude' isn't appreciated anymore)

So it's a good attitude to try to code as efficient and defensive (i would probably use "if strlen()>=2" to avoid that a space or dot or so slipped in) as possible :thumbsup: (although you sometimes better make deliberated sacrifices to enhance clearity or maintenance of your code)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum