Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    Regular Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    308
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Need help with if, else

    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

  • #2
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,266
    Thanks
    6
    Thanked 48 Times in 48 Posts
    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:

    PHP Code:
    if(isset($pageTitle)){
          
    $pageSubTitle "";


  • #3
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Maybe:
    PHP Code:
    <?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>
    Last edited by raf; 01-26-2004 at 11:00 PM.

  • #4
    Regular Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    308
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I came up with this (which works fine):

    PHP Code:
     <?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 Code:
    <?php
    $pageTitle 
    "Products";
    ?>
    2.

    PHP Code:
    <?php
    $pageTitle 
    "Products";
    $pageSubTitle "Product One";
    ?>
    I want to use code that will render the fastest. What do you guys recommend?

  • #5
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I think that would throw an error but if you say it works fine, then i suppose i thought wrong.

    I would use
    PHP Code:
    <?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
    Last edited by raf; 01-26-2004 at 11:18 PM.

  • #6
    New Coder
    Join Date
    Jan 2004
    Location
    UK
    Posts
    62
    Thanks
    0
    Thanked 0 Times in 0 Posts
    i'd pay to see that

  • #7
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Originally posted by sitami
    i'd pay to see that
    Start coding then and include a few timers It's a cordless one so a few bites and it's gone (i hope)

  • #8
    Regular Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    308
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #9
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #10
    Regular Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    308
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #11
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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 (although you sometimes better make deliberated sacrifices to enhance clearity or maintenance of your code)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •