我想在同一个函数中返回一个变量和文本


I would like to return a variable and text in the same function

我写了一个函数:

public function title()
{
return ($location->cityName);
}

我希望函数返回城市名称,但也有文本的两侧。

e。g 伦敦信息今日

我试着写:

return (('Information in'))$location->cityName('Today'));

但是这不起作用,我还尝试了在返回前后使用echo语句

我认为你正在寻找的是所谓的字符串连接。下面的代码可以满足您的要求:

public function title()
{
  return 'Information in ' . $location->cityName . ' Today';
} 

对于PHP中的两个字符串的连接,我们使用a '。'运算符,就像我们在JavaJavascript中使用'+'一样。

像这样改变行

 return "Information in ".$location->cityName." Today";

EDIT: Thanks to Patrick

当我们使用双引号时,我们可以像这样用out操作符

 return "Information in $location->cityName Today";