Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Shorthand for Pre-Concatenation?

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.

like image 892
mwieczorek Avatar asked Sep 18 '10 08:09

mwieczorek


People also ask

What is the symbol for concatenation in PHP?

PHP Compensation Operator is used to combine character strings. The PHP concatenation operator (.) is used to combine two string values to create one string.

What is used for concatenation 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 ('.

Why do we use dot operator in PHP?

' (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 .

Which operator is used for string concatenation?

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.


3 Answers

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! ;)

like image 180
2 revs, 2 users 92% Avatar answered Nov 03 '22 04:11

2 revs, 2 users 92%


a shorthand for concatenation is interpolation:

  $path = "{$_SERVER['DOCUMENT_ROOT']}/lib/modules/something.php";
like image 30
user187291 Avatar answered Nov 03 '22 03:11

user187291


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.

like image 27
Delan Azabani Avatar answered Nov 03 '22 03:11

Delan Azabani