Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - How to add a specific character at the end of a string?

Tags:

php

I am looking for hours how can i add an specific character at the end of a string.

Here is my code:

$string = "I have a";
$AddToEnd = "]";

I want to make AddToEnd to appear after the last character in $string.

How it is possible, thanks in advance!

like image 799
Venelin Avatar asked Nov 30 '22 17:11

Venelin


2 Answers

What you're looking for is called concatenation.

In PHP you have ms to do that :

$concat = $string . $AddToEnd;
like image 40
Michaël Garrez Avatar answered Dec 04 '22 04:12

Michaël Garrez


See PHP's String Operators to see how to concatenate strings:

$finalString = $string . $AddToEnd;
// OR
$string .= $AddToEnd

You can also use a function like implode():

$finalString = implode(array($string, $AddToEnd));
like image 114
Sam Avatar answered Dec 04 '22 05:12

Sam