php回声未依次回声

问题描述 投票:0回答:1

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

$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;
php echo
1个回答
0
投票

echo您在函数中的主题标签,因此在您调用它时会显示该字符串。您的函数不返回任何内容,因此$tags的值为NULL,并且标记不会出现在第二个echo中。

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

function hashtag($string){
    $newString = str_replace(' ', "", $string);
    $nString = str_replace(',', " #", $newString);
    return $nString;
}
© www.soinside.com 2019 - 2024. All rights reserved.