我有一个php变量
$objOrdd['delivery_address']
我想用引号发送它的值,即字符串。我怎样才能做到这一点?
我已尝试"{$objOrdd['delivery_address']}"
和". $objOrdd['delivery_address'] ."
,但它不起作用!
答案 0 :(得分:4)
interpolate中有多种方法可以解决 concatenate / PHP 变量:
// the test
$arr['fox'] = "fox jumps over the ";
// ----------------------
// double quotes: different ways
$str = "The quick brown ".$arr['fox']."lazy dog"; // concat
echo $str."<br>";
# output: The quick brown fox jumps over the lazy dog
$str = "The quick brown {$arr['fox']}lazy dog"; // interp.
echo $str."<br>";
# output: The quick brown fox jumps over the lazy dog
$str = "The quick brown ${arr['fox']}lazy dog"; // interp.
echo $str."<br>";
# output: The quick brown fox jumps over the lazy dog
// ----------------------
// for single quotes concat
$str = 'The quick brown '.$arr['fox'].'lazy dog';
echo $str."<br>";
# output: The quick brown fox jumps over the lazy dog
// ----------------------
// or even use sprintf
$str = sprintf('The quick brown %slazy dog', $arr['fox']);
echo $str."<br>";
# output: The quick brown fox jumps over the lazy dog
当然还有不止这些。我希望尽可能连接。
答案 1 :(得分:1)
Suposse $objOrdd['delivery_address'] = "foo"
否则
$myVariable = "'" . $objOrdd['delivery_address'] . "'"
如果您执行echo $myVariable
,则会输出'foo'
您可以将{char替换为{或任何您想要的...
答案 2 :(得分:1)
你的意思是这样的:
$var = "'".$objOrdd['delivery_address']."'";
或者
$var = '"'.$objOrdd['delivery_address'].'"';
答案 3 :(得分:0)
您确定不是在寻找json_encode
($objOrdd['delivery_address'])
吗?
答案 4 :(得分:0)
addslashes - 使用斜杠引用字符串
// Outputs: Is your name O\'reilly?
echo addslashes($str);
?>
答案 5 :(得分:0)
您可以使用类型转换以字符串形式发送数据。例如:$ myText =(string)$ myVar;
如果要为变量添加引号,则需要将其连接起来。