Library contents

PHP implode

Join two stops with a visible route delimiter.

A simple example

implode

  1. Start with a and b, separator -
  2. Do this Join using the separator
  3. Result a-b
The separator goes between the two pieces exactly once.
lesson.php
<?php
$value = "a";
$other = "b";
echo (implode("-", [$value, $other])), PHP_EOL;
Output
a-b

Syntax

PHP syntax
implode(" → ", [$value, $other])
Run these examples in PHP

Save one example at a time as lesson.php. Use PHP 8.2 or later.

php lesson.php

Print a campus shuttle route

A display receives an ordered list of items as separate values. Join them into one readable line with separators between neighboring values and no extra separator after the last one.

From the program$value = "Library"
ResultLibrary → Lab
lesson.php
<?php
$value = "Library";
$other = "Lab";
echo (implode(" → ", [$value, $other])), PHP_EOL;
Output
Library → Lab

The arrow preserves the order of the two stops.

Advertisements

Show a parcel handoff

A transit or dispatch system receives journey information as a sequence of stops or handoffs. Preserve their order when interpreting or displaying the route so the next destination is clear.

From the program$value = "Depot"
ResultDepot → Gate
lesson.php
<?php
$value = "Depot";
$other = "Gate";
echo (implode(" → ", [$value, $other])), PHP_EOL;
Output
Depot → Gate

The same delimiter separates the names.

Build a parcel shelf identifier

The shelf prefix already includes the hyphen. Combine the two fragments in order without adding another separator. What label goes to the printer?

Given'North-' · '12'
Worked resultNorth-12

What to watch for

Joining inserts a delimiter between elements. Include blank elements only if they represent a real stop.

Documentation for implode