开发者

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.


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.


try out the sprintf function as it has worked for me - information on it here: http://docs.php.net/sprintf

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜