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.
Page 1 of 2 12 LastLast
Results 1 to 15 of 21
  1. #1
    Regular Coder
    Join Date
    Nov 2010
    Location
    Washington DC
    Posts
    338
    Thanks
    22
    Thanked 1 Time in 1 Post

    Proper way to construct a complex if and else statements

    Hello!

    I was wondering if someone can take a look at the following if and else statement and point out what I have written incorrectly. In its current form, I cannot get it to work. So, essentially, I'm trying to construct an if conditional statement within another if statement.


    PHP Code:
                $var_test ="25";
                
               
    $play_envir ="Open Play"
               
               
    //In my complete code; $play_envir variable could be any of the following:"Open Play, Robotic Training, Coaching Session "
                
        
    if($var_test >'0' )
         
    //if credit exists, payment will be deducted from credit
         
    {
                if ( 
    $play_envir == "Open Play"  ) {
                echo 
    "Your fee for today is <font color='white'>$$open_play.00</font> <br /><img border='0' src='images/form-flashing-arrow.gif' width='45' height='26'><span style='background-color:white'><font color='green' size='5'>Payment Will be Deducted from your Credit on file!</font></span><br /> <font color='white' size='3'>Thank you $firstname, and have a great work-out!</font>";
                }
                elseif ( 
    $play_envir == "Robotic Training" ) {
                echo 
    "Your fee for today is <font color='white'>$$robotic_play.00 per 30 minute block</font> <br /><img border='0' src='images/form-flashing-arrow.gif' width='45' height='26'><span style='background-color:white'><font color='green' size='5'>Payment Will be Deducted from your Credit on file!</font></span><br /><font color='white' size='3'>Thank you $firstname, and have a great work-out!</font>";
                }
                elseif ( 
    $play_envir == "Coaching Session" ) {
                echo 
    "<font color='red'>Please see Club Management for today's  coaching fee</font><br /><font color='white' size='3'>Thank you $firstname, and have a great work-out!</font>";
                }
                elseif ( 
    $play_envir == "Open Play and Coaching Session" ) {
                echo 
    "<font color='red'>Please see Club Management for today's fee</font><br /><font color='white' size='3'>Thank you $firstname, and have a great work-out!</font>";
                }
                elseif ( 
    $play_envir == "Robotic Training and Coaching Session" ) {
                echo 
    "<font color='red'>Please see Club Management for today's fee</font><br /><font color='white' size='3'>Thank you $firstname, and have a great work-out!</font>";
                }
                elseif ( 
    $play_envir == "Open Play and Robotic Training" ) {
                echo 
    "For these selections, please pay a <font color='white'>$$play_combo.00 deposit</font>, the balance after playing!<br /><font color='red' size='8'><img border='0' src='images/form-flashing-arrow.gif' width='65' height='36'>Please Pay First!</font><br /><font color='white' size='3'>Thank you $firstname, and have a great work-out!</font>";
                }
                elseif ( 
    $play_envir == "Open Play and Robotic Training and Coaching Session" ) {
                echo 
    "<font color='red'>Please see Club Management for today's  fee</font><br /><font color='white' size='3'>Thank you $firstname, and have a great work-out!</font>";
                }
                
                }
               
    // else we need a payment amount indicated
          
    else
                     {
                          
                if (
    $play_envir == "Open Play" ) {
                
                echo 
    "Your fee for today is <font color='white'>$$open_play.00</font> <br /><font color='red' size='8'><img border='0' src='images/form-flashing-arrow.gif' width='65' height='36'>Please Pay First!</font><br /> <font color='white' size='3'>Thank you $firstname, and have a great work-out!</font>";
                }
                if ( 
    $play_envir == "Robotic Training" ) {
                echo 
    "Your fee for today is <font color='white'>$$robotic_play.00 per 30 minute block</font> <br /><font color='red' size='8'><img border='0' src='images/form-flashing-arrow.gif' width='65' height='36'>Please Pay First!</font><br /><font color='white' size='3'>Thank you $firstname, and have a great work-out!</font>";
                }
                elseif ( 
    $play_envir == "Coaching Session" ) {
                echo 
    "<font color='red'>Please see Club Management for today's  coaching fee</font><br /><font color='white' size='3'>Thank you $firstname, and have a great work-out!</font>";
                }
                elseif ( 
    $play_envir == "Open Play and Coaching Session" ) {
                echo 
    "<font color='red'>Please see Club Management for today's fee</font><br /><font color='white' size='3'>Thank you $firstname, and have a great work-out!</font>";
                }
                elseif ( 
    $play_envir == "Robotic Training and Coaching Session" ) {
                echo 
    "<font color='red'>Please see Club Management for today's fee</font><br /><font color='white' size='3'>Thank you $firstname, and have a great work-out!</font>";
                }
                elseif ( 
    $play_envir == "Open Play and Robotic Training" ) {
                echo 
    "For these selections, please pay a <font color='white'>$$play_combo.00 deposit</font>, the balance after playing!<br /><font color='red' size='8'><img border='0' src='images/form-flashing-arrow.gif' width='65' height='36'>Please Pay First!</font><br /><font color='white' size='3'>Thank you $firstname, and have a great work-out!</font>";
                }
                elseif ( 
    $play_envir == "Open Play and Robotic Training and Coaching Session" ) {
                echo 
    "<font color='red'>Please see Club Management for today's  fee</font><br /><font color='white' size='3'>Thank you $firstname, and have a great work-out!</font>";
                }
                
                } 

    I would appreciate any thoughts on this.

    Mossa

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    3,761
    Thanks
    23
    Thanked 546 Times in 545 Posts
    When your working with money AND names you are not inserting your php string properly.
    PHP Code:
    echo "Your fee for today is <font color='white'>$$open_play.00</font> <br /><font color='red' size='8'><img border='0' src='images/form-flashing-arrow.gif' width='65' height='36'>Please Pay First!</font><br /> <font color='white' size='3'>Thank you $firstname, and have a great work-out!</font>"
    Should be
    PHP Code:
    <?php
    $open_play 
    "35"//SO WE HAVE A VALUE
    $firstname "Tommy"//SO WE HAVE A VALUE
    // Also changed color to red so I could see it. Change back.
    echo "Your fee for today is <font color='red'>$".$open_play.".00</font> <br /><font color='red' size='8'><img border='0' src='images/form-flashing-arrow.gif' width='65' height='36'>Please Pay First!</font><br /> <font color='red' size='3'>Thank you ".$firstname.", and have a great work-out!</font>"

    ?>
    Evolution - The non-random survival of random variants.

  • #3
    Regular Coder
    Join Date
    Nov 2010
    Location
    Washington DC
    Posts
    338
    Thanks
    22
    Thanked 1 Time in 1 Post
    sunfighter,

    I appreciate the reply and the advice. Would you deduce that be the reason why the conditional statement is not responsive?

    I have corrected as you have suggested, but still unable to get it to work! I believe my issue is with the first if statement:

    PHP Code:
    $var_test ="25";  
    if(
    $var_test >'0' 
         
    //if credit exists, payment will be deducted from credit 

     if ( 
    $play_envir == "Open Play"  ) { 
                echo 
    "Your fee for today is <font color='white'>$".$open_play.".00</font> <br /><img border='0' src='images/form-flashing-arrow.gif' width='45' height='26'><span style='background-color:white'><font color='green' size='5'>Payment Will be Deducted from your Credit on file!</font></span><br /> <font color='white' size='3'>Thank you".$firstname.", and have a great work-out!</font>"
                } 
                elseif ( 
    $play_envir == "Robotic Training" ) { 
                echo 
    "Your fee for today is <font color='white'>$".$robotic_play.".00 per 30 minute block</font> <br /><img border='0' src='images/form-flashing-arrow.gif' width='45' height='26'><span style='background-color:white'><font color='green' size='5'>Payment Will be Deducted from your Credit on file!</font></span><br /><font color='white' size='3'>Thank you".$firstname.", and have a great work-out!</font>"
                } 
    }
    else
    {
    //somthing here!

    Any thoughts!

  • #4
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    709
    Thanks
    20
    Thanked 84 Times in 84 Posts
    i would use the switch() statement instead:

    i done the 1st 2 options

    PHP Code:
    $var_test ="25";
    $play_envir ="Open Play";           
       
                switch(
    $play_envir) {
                    case 
    "Open Play": {
                 if(
    $var_test >'0' ){            //if credit exists, payment will be deducted from credit
                     
    echo "Your fee for today is <font color='white'>$$open_play.00</font> <br /><img border='0' src='images/form-flashing-arrow.gif' width='45' height='26'><span style='background-color:white'><font color='green' size='5'>Payment Will be Deducted from your Credit on file!</font></span><br /> <font color='white' size='3'>Thank you $firstname, and have a great work-out!</font>";
                    break;
                 }else{                            
    // else we need a payment amount indicated 
                     
    echo "Your fee for today is <font color='white'>$$open_play.00</font> <br /><font color='red' size='8'><img border='0' src='images/form-flashing-arrow.gif' width='65' height='36'>Please Pay First!</font><br /> <font color='white' size='3'>Thank you $firstname, and have a great work-out!</font>";
                     break;
                 }
                }
                case 
    "Robotic Training": {
                if(
    $var_test >'0' ){
                    echo 
    "Your fee for today is <font color='white'>$$robotic_play.00 per 30 minute block</font> <br /><img border='0' src='images/form-flashing-arrow.gif' width='45' height='26'><span style='background-color:white'><font color='green' size='5'>Payment Will be Deducted from your Credit on file!</font></span><br /><font color='white' size='3'>Thank you $firstname, and have a great work-out!</font>";
                    break;
                    }else{    
                    echo 
    "Your fee for today is <font color='white'>$$robotic_play.00 per 30 minute block</font> <br /><font color='red' size='8'><img border='0' src='images/form-flashing-arrow.gif' width='65' height='36'>Please Pay First!</font><br /><font color='white' size='3'>Thank you $firstname, and have a great work-out!</font>";
                    break;
                    }
                    
                }
                case 
    "Coaching Session" : {
                echo 
    "<font color='red'>Please see Club Management for today's  coaching fee</font><br /><font color='white' size='3'>Thank you $firstname, and have a great work-out!</font>";
                break;
                }
                case 
    "Open Play and Coaching Session" : {
                echo 
    "<font color='red'>Please see Club Management for today's fee</font><br /><font color='white' size='3'>Thank you $firstname, and have a great work-out!</font>";
                break;
                }
                case 
    "Robotic Training and Coaching Session" : {
                echo 
    "<font color='red'>Please see Club Management for today's fee</font><br /><font color='white' size='3'>Thank you $firstname, and have a great work-out!</font>";
                break;
                }
                case 
    "Open Play and Robotic Training" : {
                echo 
    "For these selections, please pay a <font color='white'>$$play_combo.00 deposit</font>, the balance after playing!<br /><font color='red' size='8'><img border='0' src='images/form-flashing-arrow.gif' width='65' height='36'>Please Pay First!</font><br /><font color='white' size='3'>Thank you $firstname, and have a great work-out!</font>";
                break;
                }
                case 
    "Open Play and Robotic Training and Coaching Session" : {
                echo 
    "<font color='red'>Please see Club Management for today's  fee</font><br /><font color='white' size='3'>Thank you $firstname, and have a great work-out!</font>";
                break;
                }
                
                default:{
                
    //what to do if $play_envir doesn't equal any of the above
                
    }
            } 
    There are 10 types of people on CodingForums,
    Those who understand Binary and those who dont.
    Get Cloud Hosting now from only£59 / month

  • #5
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    3,761
    Thanks
    23
    Thanked 546 Times in 545 Posts
    It did work for me mbarandao. I threw up warnings but I did get some output.{After I changed the font color, white against my white page did not show that's why I changed it to red.} You need to define $open_play = "35"; and $firstname = "Tommy"; but the code should work, because your if statements are correct. Did you change every echo line?

    See if this works for you:
    PHP Code:
    <?php
    // Notice how many variables need to be defined for the if states to work
    $play_envir "Open Play";
    $open_play 35;
    $firstname "Tommy"
    $var_test 35;  // YOU CAN MAKE THIS ZERO TO GET THE ELSE TO WORK
                
    if($var_test >'0' ){
        if ( 
    $play_envir == "Open Play"  ) {
            echo 
    "Your fee for today is <font color='red'>$".$open_play.".00</font> <br /><img border='0' src='images/form-flashing-arrow.gif' width='45' height='26'><span style='background-color:white'><font color='green' size='5'>Payment Will be Deducted from your Credit on file!</font></span><br /> <font color='red' size='3'>Thank you ".$firstname.", and have a great work-out!</font>";
        }
    }else{
        if (
    $play_envir == "Open Play" ) {
            echo 
    "Your fee for today is <font color='red'>$".$open_play.".00</font> <br /><font color='red' size='8'><img border='0' src='images/form-flashing-arrow.gif' width='65' height='36'>Please Pay First!</font><br /> <font color='red' size='3'>Thank you ".$firstname.", and have a great work-out!</font>";
        }
     }
    ?>
    Evolution - The non-random survival of random variants.

  • #6
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    709
    Thanks
    20
    Thanked 84 Times in 84 Posts
    or further to my previous post you could store your options in an array and use a simple if at the end.

    PHP Code:
    <?php
        
    $var_test 
    ="25";
    $play_envir "Open Play";           
    $playarray =     array(
                        array(
                        
    "Open Play" => "Your fee for today is <font color='white'>$$open_play.00</font> <br /><img border='0' src='images/form-flashing-arrow.gif' width='45' height='26'><span style='background-color:white'><font color='green' size='5'>Payment Will be Deducted from your Credit on file!</font></span><br /> <font color='white' size='3'>Thank you $firstname, and have a great work-out!</font>",
                        
    "Robotic Training" => "Your fee for today is <font color='white'>$$robotic_play.00 per 30 minute block</font> <br /><img border='0' src='images/form-flashing-arrow.gif' width='45' height='26'><span style='background-color:white'><font color='green' size='5'>Payment Will be Deducted from your Credit on file!</font></span><br /><font color='white' size='3'>Thank you $firstname, and have a great work-out!</font>",
                        
    "Coaching Session" => "<font color='red'>Please see Club Management for today's  coaching fee</font><br /><font color='white' size='3'>Thank you $firstname, and have a great work-out!</font>",
                        
    "Open Play and Coaching Session" => "<font color='red'>Please see Club Management for today's fee</font><br /><font color='white' size='3'>Thank you $firstname, and have a great work-out!</font>",
                        
    "Robotic Training and Coaching Session" => "<font color='red'>Please see Club Management for today's fee</font><br /><font color='white' size='3'>Thank you $firstname, and have a great work-out!</font>",
                        
    "Open Play and Robotic Training" => "For these selections, please pay a <font color='white'>$$play_combo.00 deposit</font>, the balance after playing!<br /><font color='red' size='8'><img border='0' src='images/form-flashing-arrow.gif' width='65' height='36'>Please Pay First!</font><br /><font color='white' size='3'>Thank you $firstname, and have a great work-out!</font>",
                        
    "Open Play and Robotic Training and Coaching Session" => "<font color='red'>Please see Club Management for today's  fee</font><br /><font color='white' size='3'>Thank you $firstname, and have a great work-out!</font>"
                        
                        
    ),
                        array(
                        
    "Open Play" => "Your fee for today is <font color='white'>$$open_play.00</font> <br /><font color='red' size='8'><img border='0' src='images/form-flashing-arrow.gif' width='65' height='36'>Please Pay First!</font><br /> <font color='white' size='3'>Thank you $firstname, and have a great work-out!</font>",
                        
    "Robotic Training" => "Your fee for today is <font color='white'>$$robotic_play.00 per 30 minute block</font> <br /><font color='red' size='8'><img border='0' src='images/form-flashing-arrow.gif' width='65' height='36'>Please Pay First!</font><br /><font color='white' size='3'>Thank you $firstname, and have a great work-out!</font>",
                        
    "Coaching Session" =>"<font color='red'>Please see Club Management for today's  coaching fee</font><br /><font color='white' size='3'>Thank you $firstname, and have a great work-out!</font>",
                        
    "Open Play and Coaching Session" => "<font color='red'>Please see Club Management for today's fee</font><br /><font color='white' size='3'>Thank you $firstname, and have a great work-out!</font>",
                        
    "Robotic Training and Coaching Session" => "<font color='red'>Please see Club Management for today's fee</font><br /><font color='white' size='3'>Thank you $firstname, and have a great work-out!</font>",
                        
    "Open Play and Robotic Training" => "For these selections, please pay a <font color='white'>$$play_combo.00 deposit</font>, the balance after playing!<br /><font color='red' size='8'><img border='0' src='images/form-flashing-arrow.gif' width='65' height='36'>Please Pay First!</font><br /><font color='white' size='3'>Thank you $firstname, and have a great work-out!</font>",
                        
    "Open Play and Robotic Training and Coaching Session" => "<font color='red'>Please see Club Management for today's  fee</font><br /><font color='white' size='3'>Thank you $firstname, and have a great work-out!</font>"
                        
    )
                    );

                 if(
    $var_test '0' ){            //if credit exists, payment will be deducted from credit
                     
    echo $playarray[0][$play_envir];
                 }else{                            
    // else we need a payment amount indicated 
                    
    echo $playarray[1][$play_envir];
                 }
    ?>
    There are 10 types of people on CodingForums,
    Those who understand Binary and those who dont.
    Get Cloud Hosting now from only£59 / month

  • #7
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,090
    Thanks
    51
    Thanked 506 Times in 493 Posts
    Quote Originally Posted by sunfighter View Post
    Should be
    PHP Code:
    <?php
    $open_play 
    '35'//SO WE HAVE A VALUE
    $firstname 'Tommy'//SO WE HAVE A VALUE
    // Also changed color to red so I could see it. Change back.
    echo 'Your fee for today is <font color="red">$' .$open_play .'.00</font> <br /><font color="red" size="8"><img border="0" src="images/form-flashing-arrow.gif" width="65" height="36">Please Pay First!</font><br /> <font color="red" size="3">Thank you ' .$firstname .', and have a great work-out!</font>'

    ?>
    Just changed all the unneeded double quotes to single quotes and corrected the wrong html single quotes back into doubles

    Quote Originally Posted by Arcticwarrio View Post
    PHP Code:
          switch($play_envir) {
                    case 
    "Open Play": {
                 if(
    $var_test >'0' ){            //if credit exists, payment will be deducted from credit
                     
    echo "Your fee for today is <font color='white'>$$open_play.00</font> <br /><img border='0' src='images/form-flashing-arrow.gif' width='45' height='26'><span style='background-color:white'><font color='green' size='5'>Payment Will be Deducted from your Credit on file!</font></span><br /> <font color='white' size='3'>Thank you $firstname, and have a great work-out!</font>";
                    break;
                 }else{                            
    // else we need a payment amount indicated 
                     
    echo "Your fee for today is <font color='white'>$$open_play.00</font> <br /><font color='red' size='8'><img border='0' src='images/form-flashing-arrow.gif' width='65' height='36'>Please Pay First!</font><br /> <font color='white' size='3'>Thank you $firstname, and have a great work-out!</font>";
                     break;
                 }
            } 
    See I couldn't do that, I don't like if conditionals within switches.. this is what I'd do myself:
    PHP Code:
    switch(true)
       {
       
    //if credit exists, payment will be deducted from credit
       
    case (($play_envir == 'Open Play') and ($var_test >'0')):
          echo 
    "Your fee for today is <font color='white'>$$open_play.00</font> <br /><img border='0' src='images/form-flashing-arrow.gif' width='45' height='26'><span style='background-color:white'><font color='green' size='5'>Payment Will be Deducted from your Credit on file!</font></span><br /> <font color='white' size='3'>Thank you $firstname, and have a great work-out!</font>";
          break;
       
       
    // else we need a payment amount indicated 
       
    case (($play_envir == 'Open Play') and ($var_test == '0')):
          
    // else we need a payment amount indicated 
          
    echo "Your fee for today is <font color='white'>$$open_play.00</font> <br /><font color='red' size='8'><img border='0' src='images/form-flashing-arrow.gif' width='65' height='36'>Please Pay First!</font><br /> <font color='white' size='3'>Thank you $firstname, and have a great work-out!</font>";
          break;
       } 
    By using true in your switch instead of a variable, you can test for many different cases without all of the if () / elseif () and braces junk that takes an age to write Ok so using if () / elseif () would do the same thing but this looks way cooler and will get you a bit more respect as a coder

    Also whats with the extra { and } symbols? - eg: case "Open Play":{
    You only need the : and then the break; statement at the end
    My helpful sig is on vacation trying to loose some weight. It got a bit fat and caused a few problems but it will be back at some point!

  • Users who have thanked tangoforce for this post:

    Arcticwarrio (03-27-2013)

  • #8
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    709
    Thanks
    20
    Thanked 84 Times in 84 Posts
    i like the switch true idea, will have a go with that next time i can
    There are 10 types of people on CodingForums,
    Those who understand Binary and those who dont.
    Get Cloud Hosting now from only£59 / month

  • #9
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,090
    Thanks
    51
    Thanked 506 Times in 493 Posts
    It's very handy when you need one function that can do multiple things - eg a recursive function that has to decide whether to do something or call itself again.
    My helpful sig is on vacation trying to loose some weight. It got a bit fat and caused a few problems but it will be back at some point!

  • #10
    Regular Coder
    Join Date
    Nov 2010
    Location
    Washington DC
    Posts
    338
    Thanks
    22
    Thanked 1 Time in 1 Post
    Thank you all for having provided a wealth of information concerning my post.
    I'm testing all of the possibilities suggested here and will report back soon after.

    Again, Thank you all!

    Best,
    Mossa

  • #11
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    3,761
    Thanks
    23
    Thanked 546 Times in 545 Posts
    Quote Originally Posted by tangoforce View Post
    Just changed all the unneeded double quotes to single quotes and corrected the wrong html single quotes back into doubles
    A joke right?? And I don't know how all those got switched around. I copied from mbarandao first post and the quotes are reversed from what I had.
    I didn't do it. Must have come from my beta test editor.

    A question, when someone asks a question in a if/else if format why answer him in a switch? I like the switch myself, it's cleaner, but why confuse mbarandao with something he may know nothing about?
    Evolution - The non-random survival of random variants.

  • #12
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,090
    Thanks
    51
    Thanked 506 Times in 493 Posts
    Quote Originally Posted by sunfighter View Post
    A joke right??
    Only if you intentionally want to waste cpu cycles examining all those double quoted strings for variables to replace. If that was the intention, yes it's a joke

    Quote Originally Posted by sunfighter View Post
    I didn't do it. Must have come from my beta test editor.
    I never said you did it. All I did was to make a minor adjustment and share the tip with you and the op. Some people see the difference in quotes as being a minor issue however if you start writing larger scripts with several thousand lines, you need every biut of CPU power available to you and thus using single quotes where possible is beneficial.

    On a side note, I use notepad++ for writing code samples and checking the quotes are all opened and terminated etc. Much easier than writing a custom editor from scratch!

    Quote Originally Posted by sunfighter View Post
    A question, when someone asks a question in a if/else if format why answer him in a switch? I like the switch myself, it's cleaner, but why confuse mbarandao with something he may know nothing about?
    You've just answered your own question. You like the idea, the op is wanting to also improve their knowledge of programming and that is why they've come here. Not only did Arctic like the tip, so did you and so therefore the op may find it useful too. THAT is why

    You have to learn something sunfighter.. Remember all those times I helped you when you first came here? I do that for others too you know. When I say something about code you've posted, I'm not trying to be insulting, I'm merely trying to point out an improvement. Don't take that personally or think I'm trying to put you down because I'm not.

    Out there on the big wide interweb, you're not an expert - there is always someone bigger, better and smarter than you. Look how many times I answer things and Fou or Inigoesdr jumps in and adds a far superior answer than mine - do I complain or take it personally (even when one of them point out I've made a glaring error)? - no! I'm not an expert but I have a lot of practical experience that I am happy to share where possible. If that means pointing out more efficient ways of doing something..
    My helpful sig is on vacation trying to loose some weight. It got a bit fat and caused a few problems but it will be back at some point!

  • #13
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    3,761
    Thanks
    23
    Thanked 546 Times in 545 Posts
    OK something to be learned here. A double quoted string or section of code is replaced by a variable and that takes up cpu time. But not so much for a single quoted string. Is this right? Never knew that. How are single quoted handled?

    And I wasn't ragging or raging just trying to say the code is written already, we just have to correct it. It is a big project to re-write this into a switch, giving mbarandao the entire switch would be nice,not something I'd do, but my question was "does that help him now?" Sure knowing about another way that looks better is good, but finding his/her error in the code he already has may be better. That was the question, or at lest that was what I intended to ask. And maybe I should have told him about the switch, my failure there. And Tango, I know your a better programmer then me and mean you no disrespect. I'd take you out for a beer if you ever climb out of my monitor.
    Evolution - The non-random survival of random variants.

  • #14
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,090
    Thanks
    51
    Thanked 506 Times in 493 Posts
    Quote Originally Posted by sunfighter View Post
    OK something to be learned here. A double quoted string or section of code is replaced by a variable and that takes up cpu time. But not so much for a single quoted string. Is this right? Never knew that. How are single quoted handled?
    There is a link in my signature about quotes however here is a quick demo:
    PHP Code:
    $Name 'Tangoforce';

    print 
    'My name is $Name'//My name is $Name

    Print "My name is $Name"// My name is Tangoforce 
    Thats what the difference is. If its single quoted, php just prints it as-is. If its double quoted, PHP has to slow down a little, examine the string, check for variables and then replace them (of course in the process it has to check they are set and output an error if not). This of course takes up more CPU time and while it may not be noticeable to you with a small script, on a large script it will make a difference.

    Quote Originally Posted by sunfighter View Post
    trying to say the code is written already, we just have to correct it. It is a big project to re-write this into a switch
    But thats what being a programmer is all about. You make amendments, rewrite bits of code to be better, tidy things up.. thats what its about!

    Quote Originally Posted by sunfighter View Post
    giving mbarandao the entire switch would be nice
    I've given a big tip. I think thats more than enough. Most coders are grateful for that and find it rewarding to finish it off themselves and call it their own work

    Quote Originally Posted by sunfighter View Post
    my question was "does that help him now?"
    It shows them an alternative way of doing something that they may find more convenient in the future.

    Quote Originally Posted by sunfighter View Post
    Sure knowing about another way that looks better is good, but finding his/her error in the code he already has may be better. That was the question, or at lest that was what I intended to ask.
    Well as I understand it you'd pretty much solved the problem. I just noticed the quotes needed fixing and that Arctic had suppled a switch solution and though I would expand on it a little.

    Quote Originally Posted by sunfighter View Post
    And maybe I should have told him about the switch, my failure there.
    Not really, thats what forums are about - swapping tips and bits of code. Not everyone gets things right all the time - I make blunders that the mods pick me up on too you know.

    Quote Originally Posted by sunfighter View Post
    And Tango, I know your a better programmer then me and mean you no disrespect. I'd take you out for a beer if you ever climb out of my monitor.
    Lol If we ever meet
    My helpful sig is on vacation trying to loose some weight. It got a bit fat and caused a few problems but it will be back at some point!

  • #15
    Regular Coder
    Join Date
    Nov 2010
    Location
    Washington DC
    Posts
    338
    Thanks
    22
    Thanked 1 Time in 1 Post
    Tangoforce and Sunfighter,

    I appreciate the spirited debate and I'm very glad that there seems to be a respectful consensus that the purpose of this forum and its community is to engage each other in the quest of broadening knowledge.

    As one who is always eager to learn new things, new perspectives and more efficient ways to code, I'm truly indebted to experience coders such as you two. I appreciate both of your suggestions as it relates to my question of this post:

    PHP Code:
    Proper way to construct a complex if and else statements 
    At the writing of this reply, I have not had a chance to fully test all the suggestions, but I trust within these suggestions, I will find a resolution to my issue and I'll be sure to report back.

    I believed it to be important to express my appreciation to you both for having taken the time to come to my call for help.

    Again, thank you!

    Best,
    Mossa
    Last edited by mbarandao; 03-28-2013 at 04:05 AM.

  • Users who have thanked mbarandao for this post:

    tangoforce (03-28-2013)


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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