Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 为变量赋值并在赋值后返回值_Php_String_Reference - Fatal编程技术网

Php 为变量赋值并在赋值后返回值

Php 为变量赋值并在赋值后返回值,php,string,reference,Php,String,Reference,我有以下几点 $basename = !empty($_POST['basename']) ? $_POST['basename'] : null; $upload_files = array( '/usr/path/to/dir/' . $basename, '/usr/path/to/dir/thumbs/' . $basename 稍后,我重新分配了变量$basename,如下所示 $basename = 'test.jpg'; 当echo像这样 echo $uplo

我有以下几点

$basename = !empty($_POST['basename']) ? $_POST['basename'] : null;

$upload_files = array(
    '/usr/path/to/dir/' . $basename,
    '/usr/path/to/dir/thumbs/' . $basename
稍后,我重新分配了变量
$basename
,如下所示

$basename = 'test.jpg';
echo
像这样

echo $upload_files[0];
我想输出这个

'/usr/path/to/dir/test.jpg'
但很明显,事实并非如此


php有没有一个技巧可以做到这一点,比如在变量之前添加
&
之类的东西?

一旦将
$basename
包含到字符串中,php将失去它作为变量的所有跟踪,因此后续更改没有任何效果。您可以将
$upload\u files
中的值设置为数组,必要时将其内插为字符串,并将第二个元素设置为对
$basename
的引用:

$basename = 'file1.gif';
$upload_files = array(
    array('/usr/path/to/dir/', &$basename),
    array('/usr/path/to/dir/thumbs/', &$basename)
);

echo implode('', $upload_files[0]) . PHP_EOL;

$basename = 'test.jpg';
echo implode('', $upload_files[0]). PHP_EOL;
输出:

/usr/path/to/dir/file1.gif
/usr/path/to/dir/test.jpg

一旦将
$basename
包含到字符串中,PHP将失去对它作为变量的所有跟踪,因此后续更改将无效。您可以将
$upload\u files
中的值设置为数组,必要时将其内插为字符串,并将第二个元素设置为对
$basename
的引用:

$basename = 'file1.gif';
$upload_files = array(
    array('/usr/path/to/dir/', &$basename),
    array('/usr/path/to/dir/thumbs/', &$basename)
);

echo implode('', $upload_files[0]) . PHP_EOL;

$basename = 'test.jpg';
echo implode('', $upload_files[0]). PHP_EOL;
输出:

/usr/path/to/dir/file1.gif
/usr/path/to/dir/test.jpg

您的预期用例是什么?如果需要访问每个pic字符串,可以将它们存储在数组中,只需使用“当前”项的最后一个条目。然后,如果需要,您可以访问搜索或处理每一项。你可以根据需要来做。我可能会编写一个函数来处理它

$basenames = [];
// However you're retrieving the filename
array_push($basenames, "file.gif");

// Current file
concatPic(count($basenames)-1); 

// To find what key you wants position
$fileIndex = array_search("what_you're_looking_for", $basenames); 

concatPic($fileIndex); // To process a specific index

function concatPic($indexValue) 
{ 
    return / echo (whatever) "usr/path/..." . $basenames[$indexValue] 
};

您还可以编写一个foreach来同时执行所有这些操作,或者如果需要,甚至可以在推送到数组之前连接它们。

您的预期用例是什么?如果需要访问每个pic字符串,可以将它们存储在数组中,只需使用“当前”项的最后一个条目。然后,如果需要,您可以访问搜索或处理每一项。你可以根据需要来做。我可能会编写一个函数来处理它

$basenames = [];
// However you're retrieving the filename
array_push($basenames, "file.gif");

// Current file
concatPic(count($basenames)-1); 

// To find what key you wants position
$fileIndex = array_search("what_you're_looking_for", $basenames); 

concatPic($fileIndex); // To process a specific index

function concatPic($indexValue) 
{ 
    return / echo (whatever) "usr/path/..." . $basenames[$indexValue] 
};

您还可以编写一个foreach来同时执行所有这些操作,或者如果需要,甚至可以在推送到数组之前连接它们。

除了您得到的好答案之外,这也是一个选项。除了你得到的好答案之外,这也是一个选择。