Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any equivalent to .= for adding to beginning of string in PHP?

Tags:

php

Just wondering if there is something like .= for adding text to the beginning of a string, e.g.:

$foo =. 'bar'; 

which doesn't work.

Edit: example was originally $foo =. $bar; which can be achieved with $bar .= $foo;

like image 843
yuttadhammo Avatar asked Aug 18 '11 17:08

yuttadhammo


People also ask

How do you prepend a string?

Method 1: Using Concatenation Operator(“.”): The Concatenation operator is used to prepend a string str1 with another string str2 by concatenation of str1 and str2.

How do I add characters to a string in PHP?

There is no specific function to append a string in PHP. In order to do this task, we have the this operator in PHP: Using Concatenation assignment operator (“. =”): The Concatenation assignment operator is used to append a string str1 with another string str2.

How can I get the first 3 characters of a string in PHP?

To get the first n characters of a string, we can use the built-in substr() function in PHP. Here is an example, that gets the first 3 characters from a following string: <? php echo substr("Google", 0, 3); ?>

What is concatenating string in PHP?

There are two string operators. The first is the concatenation operator ('. '), which returns the concatenation of its right and left arguments. The second is the concatenating assignment operator (' . = '), which appends the argument on the right side to the argument on the left side.


1 Answers

Nope. But you can do

$foo = "bar" . $foo 
like image 134
Eric V Avatar answered Sep 20 '22 21:09

Eric V