如果我有这个字符串变量$ VAR: echo "Hello, there are many $vars";
PHP:在字符串变量无连接
PHP会为variable $vars
而不是$var
。
没有级联如:"Hello, there are many $var" . "s";
是否有另一种方法来做到这一点,就像某种逃避角色?
如果我有这个字符串变量$ VAR: echo "Hello, there are many $vars";
PHP:在字符串变量无连接
PHP会为variable $vars
而不是$var
。
没有级联如:"Hello, there are many $var" . "s";
是否有另一种方法来做到这一点,就像某种逃避角色?
在PHP中可以逃脱的变量,像这样
echo "Hello, ${var}'s head is big";
或类似
echo "Hello, {$var}'s head is big";
参考here下转义字符段
个人而言,我总是从这样的字符串分割变量: echo 'Hello, there are many '.$var.'s';
主要是因为的可读性,如果你通过你的代码,它立即清楚变量的位置。
如果在3个字符串中有3组转义引号和5个变量,比起听起来要困难 – dukevin
可读性是我更喜欢其他解决方案的确切原因 –
这实际上取决于您的IDE /编辑器。我的编辑器为我突出了这些变量,所以我总是有可读的代码。另外,使用'{}'来调用这些变量会产生接缝可读性效果。 –
您可以使用{}
正确地转义您的变量。请看下面的例子:
echo "There are many ${var}s"; #Or...
echo "There are many {$var}s";
或者你可能想看看的sprintf:http://php.net/manual/en/function.sprintf.php
sprintf函数允许你格式化任何类型的变量作为一个特定符号的字符串。
'{$ var}'也可以... –
有趣的是,我也知道这一点,但是当我在php文档中搜索参考时,找不到任何内容。我可能搜索错了,所以有人有吗? – Joubarc
然后当然我发现它 - 可能有用的参考:http://pa2.php.net/manual/en/language.types.string.php#language.types.string.parsing.complex – Joubarc