php echo 未按顺序回显

php echo not echoing in order

我试图按顺序回显这些变量

$caption = "Some random caption";
$url = " https://example.com";
$tags = " tag1, tag2, tag3, tag4 ";
$message = $caption.$url.$tags;

而且输出顺序不对。是这样的

tag1, tag2, tag3, tag4 Some random caption https://example.com

实际代码如下所示:

``

function hashtag($string){
   $newString = str_replace(' ', "", $string);
         $nString = str_replace(',', " #", $newString);
echo $nString;
}
$name =  "Full Name";
$hashName = " #".str_replace(' ', '', $name);
$string = ', tag1, tag2, tag3, tag4, tag5, tag6, tag7';
$tags = hashtag($string);
$url = ' http://example.com';
$caption = ' some random caption';
$message = $caption.$url.$tags;
echo $message;

echo 函数中的主题标签,以便在您调用它时显示该字符串。你的函数没有 return 任何东西,所以 $tags 的值为 NULL 并且标签没有出现在第二个 echo

在函数中使用 return 语句来解决您的问题:

function hashtag($string){
    $newString = str_replace(' ', "", $string);
    $nString = str_replace(',', " #", $newString);
    return $nString;
}