2

I'm pretty sure I found no answer here or elsewhere or maybe because it's my fault not being able to ask the right question, but anyway, say I have a for-loop in my PHP that goes something like this:

for($i=0; $i<3; $i++){
echo "<a href=\"\">$i</a> | ";
}

How do I make the last call for " | " to not appear in order to have a nicely output:

<a href="">0</a> | <a href="">1</a> | <a href="">2</a>

INSTEAD OF

<a href="">0</a> | <a href="">1</a> | <a href="">2</a> | <-- remove this! :(

You guys are fabulous! Thanks!!!

What if $i < $undefinedNumber ?

9 Answers 9

2

Keep the flow, you only need that if it's not the first:

for ($i = 0; $i < 3; $i++) {
    echo $i ? " | " : '', "<a href=\"\">$i</a>";
}

And for the first $i is 0 which means it's false. Otherwise $i is a positive integer, which means true.

Sometimes finding an alternative way to describe the problem helps to formulate a simpler answer. Instead of:

<a href="">0</a> | <a href="">1</a> | <a href="">2</a> |  /* <-- remove this! :( */

It's that problem:

/* :( remove this! --> */ | <a href="">0</a> | <a href="">1</a> | <a href="">2</a>

The same form of solution works for CSS, too:

a:not(:first-child):before {content: ' | '}

sometimes that's easier to integrate than fiddling with PHP and HTML (Example) - however that adds the | to the link-text or style (at least in chrome where I tested it, looks buggy).

Sign up to request clarification or add additional context in comments.

Comments

1
for($i=0; $i<3; $i++){
   echo "<a href=\"\">$i</a>"
   if ($i != 2) {
      echo " | ";
   }
}

or

echo "<a href=\"\">0</a>";
for($i=1; $i<3; $i++){
   echo " | <a href=\"\">$i</a>"
}

Comments

1
for($i=0; $i<3; $i++) {
    if ($i == 0) {
       echo "<a href=\"\">$i</a>;
    }
    else {
       echo " | <a href=\"\">$i</a>"
    }
}

Comments

1

So you just need to remove the last bar from the output? Why not skip it as you loop? Something like this:

for($i=0; $i<3; $i++)
{
    echo "<a href=\"\">$i</a>";

     if($i < 2)
         echo "|";
}

This would ensure that the bar only shows after the first two links, and not after the third.

Comments

1
$output=array();
for($i=0; $i<3; $i++){
    $output[]="<a href=\"\">$i</a>";
}
$ouput=implode(' | ', $ouput);

Comments

1

You can also do this:

for($i=0; $i<3; $i++) {
    $arr[] = "<a href=\"\">$i</a>";
}
echo implode(' | ',$arr);

Comments

1

This is a commonly found situation in programming known as the Special Case pattern. There is no built in way of solving this other than using an if statement or the ternary operator

for($i=0; $i<3; $i++){
    echo "<a href=\"\">$i</a>" . ($i != 2) ? " | " : "";

Comments

1

Don't do it in the php, do it in the stylesheet.

First build your HTML

<ul class="navlinks">
     <?php foreach($links as $linkName => $linkUrl): ?>
     <li><a href="<?php echo $linkUrl; ?>"><?php echo $linkName; ?></a></li>
     <?php endforeach; ?>
</ul>

Apply the CSS.

.navlinks > li:after
{ 
    content:"<li>|</li>";
}

.navlinks > li:last-child:after
{ 
    content:"";
}

You should never style your HTML in php, this is the correct wayt to do it.

Comments

1
$arr = array();

for ($i=0; i<3; i++)
    $arr[] = "<a href=''>$i</a>";

echo implode(" | ",$arr);

More compact version:

echo implode(" | ", array_map(function($x){return "<a href=''>$x</a>";},range(1,3)));

Comments

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.