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

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

问题描述

假设我有一个变量 $test,它被定义为:$test = 'cheese'

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

我想输出cheese,我可以这样做:

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天全站免登陆