I've been programming in PHP for years, and I've always wondered if there is a way to 'pre-concatenate' a string. Example:
$path = '/lib/modules/something.php';
$server = $_SERVER['DOCUMENT_ROOT'];
I've been doing this for years in order to append a value to the beginning of a string:
$path = $server . $path;
// returns: /home/somesite.com/public_html/lib/modules/something.php
Is there a shorthand for this? Just curious.
PHP Compensation Operator is used to combine character strings. The PHP concatenation operator (.) is used to combine two string values to create one string.
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 ('.
' (dot) operator is PHP's concatenation operator. Two string operands are joined together (characters of right hand string appended to left hand string) and returns a new string. PHP also has .
Concatenation operators join multiple strings into a single string. There are two concatenation operators, + and & . Both carry out the basic concatenation operation, as the following example shows.
A not so serious answer (I know it's longer):
$path = strrev($path);
$path .= strrev($_SERVER['DOCUMENT_ROOT']);
$path = strrev($path);
There is no limit to creativity! ;)
a shorthand for concatenation is interpolation:
$path = "{$_SERVER['DOCUMENT_ROOT']}/lib/modules/something.php";
No, but you could write your own function:
function pc(&$a, &$b) {
$a = $b . $a;
}
pc($path, $server);
The above call to pc
will set $path
to $server . $path
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With