3

I have bits of code I want to throw in to my site, and provisioned a space right after <body> using 'flairs' (divs) that sit outside the design. Here's the code:

//Add Flair Containers as needed
if($flairs>0){
  echo "<!--Flair Graphics (if needed)-->\n";
    while($fQty = --$flairs+1){ //-- subracts 1, +1 accounts for 1 being 0
       $flair = array($flair1, $flair2, $flair3);
        foreach($flair as $flairCode){
          echo "<div id=\"flair-".$fQty++."\">".$flairCode."</div>\n";
        };
    };
};

It prints correctly, where content = $flair1, $flair2, and so on.

<div id="flair-1">Content1</div>
<div id="flair-2">Content2</div>
<div id="flair-3">Content3</div>

But if $flair2/$flair3 is empty, it still prints a div. How can I fix this?

3
  • 1
    well what $fQty = --$flairs+1 does isnt it make $flairs always same for example if $flairs is 5 -- makes it 4 and +1 five ? Commented Feb 22, 2013 at 3:22
  • And to your question, even if $flair2/$flair3 is empty, $flair is still an array with elements (at least with $flair1, not to mention null still makes up an element), so foreach still runs. Commented Feb 22, 2013 at 3:27
  • @passerby Yes, I knew it would I just couldn't figure out a way to check if it exists. @ nullpointer the +1 simply makes the value print correctly. There's probably a better way, but that worked. Commented Feb 22, 2013 at 3:46

2 Answers 2

1

Within your foreach loop you can check if the value is empty and continue (i.e. skip) to the next value if it is.

Like so:

if($flairs>0){
  echo "<!--Flair Graphics (if needed)-->\n";
    while($fQty = --$flairs+1){ //-- subracts 1, +1 accounts for 1 being 0
       $flair = array($flair1, $flair2, $flair3);
        foreach($flair as $flairCode){
          if (empty($flairCode)) continue;
          echo "<div id=\"flair-".$fQty++."\">".$flairCode."</div>\n";
        };
    };
};
Sign up to request clarification or add additional context in comments.

Comments

1

I suspect that you could simply prepend if($flairCode) to your echo statement. That would make your inner loop:

foreach($flair as $flairCode){
    if($flairCode) echo "<div id=\"flair-".$fQty++."\">".$flairCode."</div>\n";
};

Some points to note:

  • Since the $flair array will always be the same, construct it outside of the loop (this will let you evaluate the condition only once too.
  • Using $fQty++ is not enough to guarantee unique ID's, especially since every time it hits the while the value is reset. I suggest $fQty should not be part of the while condition and simply stay as an independent tally.
  • Stop using double-quotes. They're slow.

4 Comments

You'll need the double quotes if you want to include line breaks (\n). Also, the performance hit for double vs single quotes is negligible if it exists at all. See Disproving the Single Quotes Performance Myth.
Cool. I use single quote whenever possible, but I want my final code to look good. I was a designer before I was a programmer, so it matters to me.
Line breaks can be done with PHP_EOL (which gives the platform specific line ending so I would argue that it is better anyway). As to single vs. double those numbers are new to me. The last time I ran a benchmark had single quotes running about 1-2% faster consistently. I'll see if I can't re-run his application both from a server and from the command line and see what pops up.
I will say that since he is concatenating manually anyway, and since he is escaping double-quotes to do that, I think his code would still be better formatted if he were to use single-quotes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.