如何使用php函数在引号中打印字符变量


How do I print a character variable within quotes using php function

我需要得到这个输出。

 the result is"random"safdsaf

我正在使用这段代码

<?php
$x = "random";
echo 'the result is' .$x. 'safdsaf';
?>

但我得到了这个

the result israndomsafdsaf

在打印之前我必须定义随机。

即我不想更改这段代码

<?php
$x = "random";

我应该在echo内部进行什么更改才能获得所需的输出?

如果使用相同类型的引号,则将字符串中的引号分隔如下:

echo "The result is'"" .$x. "'"safdsaf";

或者简单地使用两组不同的引号:

echo 'the result is"' .$x. '"safdsaf';

任一代码行的输出:

The result is"random"safdsaf

试试这个

添加了之前的一点点空间,并添加了",它会根据您的需要提供一些输出

echo 'the result is "' .$x. '" safdsaf';

结果将是

The result is "random" safdsaf

如果要打印双引号,可以将其包含在单引号中这样的东西就可以了。

$x = '"random"';

如果出于任何原因你不想使用单引号,你也可以像一样转义它们

$x = "'"random'"";

由于你想保留字符串,我建议你更改你放它的原始行:

echo 'the result is"' .$x. '"safdsaf';

原理保持不变以下是一些阅读材料:http://php.net/manual/en/language.types.string.php

您可以简单地使用:-

echo 'the result is "' .$x. '" safdsaf';

或者你可以使用。

echo "the result is '" $x'"  safdsaf";

在引号前使用',如下所示:the result is '"random'" safdsaf。如果字符串中有很多引号之类的内容,我建议使用addslashes()。这种方法将为你做工作。有关更多信息,请查看此处-http://www.w3schools.com/php/func_string_addslashes.asp