Home >Backend Development >PHP Tutorial >When looping, the vertical bar '|' separator between each item is not required at the beginning and end. What needs to be done?

When looping, the vertical bar '|' separator between each item is not required at the beginning and end. What needs to be done?

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-07-06 13:52:291223browse

When looping, the vertical bar "|" separator between each item is not required at the beginning and end. What needs to be done?

The effect to be achieved: (no vertical lines at the beginning and end)

<code><a href="#">桃子</a> | <a href="" >苹果</a></span> | <a href="" >香蕉</a></span></code>

Loop like the following, either with a vertical line at the beginning or at the end:

<code>@foreach ($goods->fruits as $ fruit)
    <a href="#">{{$fruit->name}}</a> |
@endforeach</code>

Question:
What should I do so that there are no vertical lines at the beginning and end?

Reply content:

When looping, the vertical bar "|" separator between each item is not required at the beginning and end. What needs to be done?

The effect to be achieved: (no vertical lines at the beginning and end)

<code><a href="#">桃子</a> | <a href="" >苹果</a></span> | <a href="" >香蕉</a></span></code>

Loop like the following, either with a vertical line at the beginning or at the end:

<code>@foreach ($goods->fruits as $ fruit)
    <a href="#">{{$fruit->name}}</a> |
@endforeach</code>

Question:
What should I do so that there are no vertical lines at the beginning and end?

<code>@foreach ($goods->fruits as $key => $fruit)
    <a href="#">{{ $fruit->name }}</a>
    @if ($key + 1 !== count($goods->fruits))
        |
    @endif
@endforeach</code>

Specially handle the first or last item.

Use join() directly. This function can be realized perfectly.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn