PHP Listbox



  • This a external consultant way of implementing a listbox in PHP (the rest of the code is worse)


     echo "Duration: ";
                    echo 
    "<br>";
                    echo 
    "<select name='duration'>";
                    echo 
    "<option value='0'";
                    if (
    $pfp_duration == '0') {
                        
    $pselected "selected";
                    } else {
                        
    $pselected "";
                    }
                     echo 
    " $pselected>0</option>";
                    echo 
    "<option value='1'";
                    if (
    $pfp_duration == '1') {
                        
    $pselected "selected";
                    } else {
                        
    $pselected "";
                    }
                     echo 
    " $pselected>1</option>";
                    echo 
    "<option value='2'";
                    if (
    $pfp_duration == '2') {
                        
    $pselected "selected";
                    } else {
                        
    $pselected "";
                    }

     
                    <---- SNIP MANY LINES ---->

                    echo "<option value='400'";
                    if (
    $pfp_duration == '400') {
                        
    $pselected "selected";
                    } else {
                        
    $pselected "";
                    }
                     echo 
    " $pselected>400</option>";
                    echo 
    "</select>";
                    echo 
    "<br><br>";

     
     



  • When will those lazy PHP developers start introducing loops?



  • [quote user="Manni"]When will those lazy PHP developers start introducing loops?[/quote]

     

    You don't need to use loops. L00ps are for n00bs. Just add a comment stating "loops unrolled for greater performance" and you'll become a hax0r.



  • I was thinking of becoming a PHP consultant, but I don't know if my wrists can take that much typing.



  • The real wtf is that the single quotes and double quotes are reversed everywhere but the echo " $pselected>x</option>"; line.  He's wasting as many as two cpu ticks per line!



  • [quote user="Pap"]The real wtf is that the single quotes and double quotes are reversed everywhere but the echo " $pselected>x</option>"; line.  He's wasting as many as two cpu ticks per line![/quote]

     

    which, technically speaking, isn't really an issue with php or html (that is being outputted) 

     --

    and the proper way to do it   (hopefully it preverses my & gt ; and & lt ; entities)

     

    // I always buffer my output for sanity and cookie's/header's sake
    $html = "Duration: <br><select name=\"duration\">";
    for ($i = 0; $i <= 400; $i++)
    {
    	if ($pselected == $i)
    		$html .= "<option selected value=\"$i\">$i</option>";
    	else
    		$html .= "<option value=\"$i\">$i</option>";
    }
    
    $html .= "</select><br><br>";
    


  • Just for good measure:

    The PHP manual advises you to use single quotes unless you really need the double quotes around your text.

    $html = "Duration: <br><select name=&quot;duration&quot;>";

    Should be written as:

    $html = 'Duration: <br><select name="duration">';

    Not only do you avoid the ugly backslashes that can often make your code hard to read and/or debug, but PHP parses it faster because it knows it doesn't have to search for any variables.  The benefit can be minimal, but then again so is the effort.  I think an actually significant speed optimization might be:

    $html .= '<option value="' . $i .'">' . $i .'</option>';

    instead of
    $html .= "<option value=&quot;$i&quot;>$i</option>";

    yes, I nitpick, but in a loop that gets run several hundred times per page access on a busy site, every little bit helps.



  • [quote user="Pap"]yes, I nitpick, but in a loop that gets run several hundred times per page access on a busy site, every little bit helps.[/quote]

     

    At that point I'd be more worried about memory use.

    One example that I think is ridiculous in terms of memory use is this random PHP Template class called PHPTemplate. It has this method called render that wraps the "include $this->tpl_file" in ob_start(9 and ob_end_flush(), and then returns the latter. The way you call it is "echo $tpl->render()". How silly is that?
     



  • [quote user="Pap"]The benefit can be minimal, but then again so is the effort.  I think an actually significant speed optimization might be:

    $html .= '<option value="' . $i .'">' . $i .'</option>';

    instead of
    $html .= "<option value=\"$i\">$i</option>";

    yes, I nitpick, but in a loop that gets run several hundred times per page access on a busy site, every little bit helps.
    [/quote]

     

    Actually, the second way might be the slower of the two. I've run some basic benchmarking, and many concatenations was actually slower than one double-quoted string. 



  • @Kazan said:

    [quote user="Pap"]The real wtf is that the single quotes and double quotes are reversed everywhere but the echo " $pselected>x</option>"; line.  He's wasting as many as two cpu ticks per line!

     

    which, technically speaking, isn't really an issue with php or html (that is being outputted) 

     --

    and the proper way to do it   (hopefully it preverses my & gt ; and & lt ; entities)

     

    // I always buffer my output for sanity and cookie's/header's sake
    $html = "Duration: <br><select name=\"duration\">";
    for ($i = 0; $i <= 400; $i++)
    {
    	if ($pselected == $i)
    		$html .= "<option selected value=\"$i\">$i</option>";
    	else
    		$html .= "<option value=\"$i\">$i</option>";
    }
    
    $html .= "</select><br><br>";
    
    [/quote]

    Shouldn't you be using an array of strings and concatenate it at the end?

    i don't think PHP strings are mutable, so your cat here is creating a few hundreds temporary strings.

    Wouldn't that fare better:

    $output = array();
    $output[0] = "Duration: <br><select name=\"duration\">";
    for($i=0; $i <= 400; $i++) {
        if($pselected == $i)
            $output[$i+1] = "<option selected value=\"$i\">$i</option>";
        else
            $output[$i+1] = "<option value=\"$i\">$i</option>";
    }
    $output[count($output)] = "</select><br><br>";
    $html = implode("", $output);

    ?



  • [quote user="Dragnslcr"]

    [quote user="Pap"]The benefit can be minimal, but then again so is the effort.  I think an actually significant speed optimization might be:

    $html .= '<option value="' . $i .'">' . $i .'</option>';

    instead of
    $html .= "<option value=&quot;$i&quot;>$i</option>";

    yes, I nitpick, but in a loop that gets run several hundred times per page access on a busy site, every little bit helps.
    [/quote]

     

    Actually, the second way might be the slower of the two. I've run some basic benchmarking, and many concatenations was actually slower than one double-quoted string. 

    [/quote]
    I keep hearing this. Every time I've done benchmarking, there's been no difference.

    What were your benchmarking standards? 



  • [quote user="wing"][quote user="Dragnslcr"]

    [quote user="Pap"]The benefit can be minimal, but then again so is the effort.  I think an actually significant speed optimization might be:

    $html .= '<option value="' . $i .'">' . $i .'</option>';

    instead of
    $html .= "<option value=&quot;$i&quot;>$i</option>";

    yes, I nitpick, but in a loop that gets run several hundred times per page access on a busy site, every little bit helps.
    [/quote]

     

    Actually, the second way might be the slower of the two. I've run some basic benchmarking, and many concatenations was actually slower than one double-quoted string. 

    [/quote]
    I keep hearing this. Every time I've done benchmarking, there's been no difference.

    What were your benchmarking standards? 

    [/quote]

     

    Ignore the above, I've just re-run my benchmarking.

    Serious differences.

    Interpolation is 150% as lengthy 



  • [quote user="masklinn"][quote user="Kazan"]

    [quote user="Pap"]The real wtf is that the single quotes and double quotes are reversed everywhere but the echo " $pselected>x</option>"; line.  He's wasting as many as two cpu ticks per line![/quote]

     

    which, technically speaking, isn't really an issue with php or html (that is being outputted) 

     --

    and the proper way to do it   (hopefully it preverses my & gt ; and & lt ; entities)

     

    // I always buffer my output for sanity and cookie's/header's sake
    $html = "Duration: <br><select name=\"duration\">";
    for ($i = 0; $i <= 400; $i++)
    {
    if ($pselected == $i)
    $html .= "<option selected value=\"$i\">$i</option>";
    else
    $html .= "<option value=\"$i\">$i</option>";
    }

    $html .= "</select><br><br>";
    [/quote]

    Shouldn't you be using an array of strings and concatenate it at the end?

    i don't think PHP strings are mutable, so your cat here is creating a few hundreds temporary strings.

    Wouldn't that fare better:

    $output = array();
    $output[0] = "Duration: <br><select name=\"duration\">";
    for($i=0; $i <= 400; $i++) {
    if($pselected == $i)
    $output[$i+1] = "<option selected value=\"$i\">$i</option>";
    else
    $output[$i+1] = "<option value=\"$i\">$i</option>";
    }
    $output[count($output)] = "</select><br><br>";
    $html = implode("", $output);

    ?

    [/quote]

     

    you array loop is naughty - you are explicitly using the indexes - that's unneccesary, and you are using count($output) - again unneccesary - just $output[] = 'foo' - the array loop and the .= loop work about the same.  




  • [quote user="wing"][quote user="wing"]


    I keep hearing this. Every time I've done benchmarking, there's been no difference.What

    What were your benchmarking standards? 

    [/quote]

     

    Ignore the above, I've just re-runbenchmarking.Serious

    idifferences.Interpolation

    tion is 150% as lengthy 

    [/quote]

     

    I tested it with a fairly large number of concatenations. I think it was about 12 total, using about 6 variables and 6 escape characters that don't work in single-quoted strings (\n, \t, etc.). Basically, 3 concatenations < 1 double-quoted string < 20 concatenations. Of course, the difference is pretty much negligible anyway; I think it was on the order of half a second for a million iterations. If anyone asks me which is better, I usually just say "whichever one you think is prettier".


Log in to reply