4

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!

3 Answers 3

10

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));
Sign up to request clarification or add additional context in comments.

6 Comments

I know, I recognized that within seconds and fixed it (in fact you can't even see it in the edit history). You guys were quick! LOL -- well deserved down votes though, I'm still waking up :)
@KA_lin he keeps changing his answer it was incorrect
Ahh be the first no matter what I guess
I've removed my downvote, since the error is fixed. However, I would remove the implode() alternative since I don't see a benefit from it. But, yes; it does the job.
@hek2mgl the only reason I put the alternative is because sometimes SO questions are really basic and they actually want to do something different. In some cases, implode() is a better way to concatenate strings. And at that point, if I remove it, I might as well just delete my question. Thanks people for removing the downvotes, that was a total lapse in memory for me...
|
9

What you're looking for is called concatenation.

In PHP you have ms to do that :

$concat = $string . $AddToEnd;

6 Comments

Yeah my bad on that indeed, didn't that work years ago?
Indeed, it was echo. Well, deserve a small down point for that !
Fun fact: using seperators to echo multiple values is faster than to concatenate them!
@TimDev - not to mention that it eliminates the memory overhead of the concatenation as well
@MarkBaker Agree! Just to be off-topic a little bit. thanks a lot for PHPOffice, I love PHPExcel ;)
|
0

This is the way you concatenate them

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

echo $string;

output

I have a ]

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.