在echo引用字符串中使用PHP变量?

时间:2011-08-25 16:53:57

标签: php function variables

如何在echo ""中使用ucwords($ firstname)?即

<?php
$firstname = "john";
$surname = "smith";
echo "Welcome ucwords($firstname) ucwords($surname)";
?>

6 个答案:

答案 0 :(得分:3)

<?php
$firstname = "john";
$surname = "smith";
echo "Welcome " . ucwords($firstname) . " " .  ucwords($surname);
?>

喜欢这个。

答案 1 :(得分:3)

echo "Welcome" .  ucwords($firstname) . " " . ucwords($surname);

编辑:

替代模式:

$firstname = ucwords($firstname);
$surname = ucwords($surname);

echo 'Welcome $firstname $surname';

答案 2 :(得分:2)

像这样:

<?php
$firstname = "john";
$surname = "smith";
echo "Welcome " . ucwords($firstname) . " " . ucwords($surname);
?>

或者这个:

<?php
$firstname = "john";
$surname = "smith";
$format = "Welcome %s %s";
printf($format, ucwords($firstname), ucwords($surname));
?>

答案 3 :(得分:1)

echo "Welcome " . ucwords($firstname) . " " . ucwords($surname);

答案 4 :(得分:1)

这是极端的hacky,但你可以使用变量函数:

$uc = 'ucwords';
$firstname = 'fred';
$lastname = 'jones';

echo "Welcome {$uc($firstname)} {$uc($lastname)}";

答案 5 :(得分:0)

PHP脚本:

<?php
$firstname = 'john';
$surname = 'smith';
//Use of single quote instead double is an optimize way
echo 'Welcome '.ucwords($firstname).' '.ucwords($surname);
?>

<强>输出:

欢迎John Smith

<强> Reference