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?

WBOY
Release: 2016-07-06 13:52:29
Original
1210 people have browsed it

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>
Copy after login
Copy after login

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>
Copy after login
Copy after login

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>
Copy after login
Copy after login

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>
Copy after login
Copy after login

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>
Copy after login

Specially handle the first or last item.

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

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template