将PHP变量与字符串文字混合 [英] Mixing a PHP variable with a string literal

查看:96
本文介绍了将PHP变量与字符串文字混合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有一个变量$test,它的定义为:$test = 'cheese'

Say I have a variable $test and it's defined as: $test = 'cheese'

我想输出cheesey,我可以这样:

I want to output cheesey, which I can do like this:

echo $test . 'y'

但是我宁愿将代码简化为类似这样的代码(这是行不通的):

But I would prefer to simplify the code to something more like this (which wouldn't work):

echo "$testy"

是否有办法将y视为与变量分开?

Is there a way to have the y be treated as though it were separate from the variable?

推荐答案

echo "{$test}y";

直接在字符串中插值变量时,可以使用花括号消除歧义.

You can use braces to remove ambiguity when interpolating variables directly in strings.

此外,这不适用于单引号.所以:

Also, this doesn't work with single quotes. So:

echo '{$test}y';

将输出

{$test}y

这篇关于将PHP变量与字符串文字混合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆