php 是否可以将一个变量插入到另一个变量中?

发布于 2025-01-20 19:04:25 字数 544 浏览 3 评论 0原文

我正在从事一个个人项目,我会做一个爱好,我喜欢编程的世界,但我不是很好。因此,我想更多地了解我所处的问题。

我想知道是否可以将一个变量放入另一个变量。让我用一些例子来更好地解释。

我知道我可以做到这一点:

$var1 = $item->get_something();
$var2 = $var1->get_anything();

echo '<div class="custom"> '. $var2 .' </div>

但是我不能做这样的事情:

$var2 = ( $var1 = $item->get_something() ?? $var1->get_anything() );
echo '<div class="custom"> '. $var2 .' </div>

可以在$ var2内定义$ var1吗?因此,$ var2既包含$ var1东西又包含$ var1的东西。我知道这是一种恐怖,请原谅我的无知。但是这样的事情可能吗?

I'm working on a personal project, I do it as a hobby, I like the world of programming but I'm not very good. So I would like to understand more about the issue I am in.

I was wondering if it was possible to put one variable inside another. Let me explain better with some examples.

I know I can do this:

$var1 = $item->get_something();
$var2 = $var1->get_anything();

echo '<div class="custom"> '. $var2 .' </div>

But I can't do something like that:

$var2 = ( $var1 = $item->get_something() ?? $var1->get_anything() );
echo '<div class="custom"> '. $var2 .' </div>

Is it possible to define $ var1 inside $ var2? So that $ var2 contains both $ var1 something and $ var1 anything. I understand this is a horror, pardon my ignorance. But is something like this possible ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

2025-01-27 19:04:25

是否可以在$ var2内定义$ var1?因此$ var2包含
$ var1的东西和$ var1 nothing。

基本上,您正在尝试使用php中的一个变量中的两个函数返回的值,您可以使用PHP词典来实现这一目标,并遵循一个示例。

$temp = $item->get_something();

//store the values of both something and anything in a PHP associative array
$var= [
    'something' => $temp,
    'anything' => $temp.get_anything()
];

echo $var['something']; //prints the value of something
echo $var['anything']; //prints the value of anything

Is it possible to define $ var1 inside $ var2? So that $ var2 contains
both $ var1 something and $ var1 anything.

Basically you are trying to keep the values returned from both of the functions in one variable using php, you can achieve this by using php dictionaries, an example is following.

$temp = $item->get_something();

//store the values of both something and anything in a PHP associative array
$var= [
    'something' => $temp,
    'anything' => $temp.get_anything()
];

echo $var['something']; //prints the value of something
echo $var['anything']; //prints the value of anything
最美的太阳 2025-01-27 19:04:25

感谢安德里亚·奥利瓦托

不是 100% 确定我理解你的意思,但似乎你希望在一条指令中完成所有操作,你可以执行 $item->get_something()->get_anything() – Andrea Olivato

经过几次测试后我设法得到一个结果。我明白我也可以按照下面写的那样做。

$var2 = $var1 = $item->get_something()->get_anything();
echo '<div class="custom"> '. $var2 .' </div>

Thanks to Andrea Olivato.

Not 100% sure I understand what you mean but it seems you're looking to do everything in one instruction and you could do $item->get_something()->get_anything() – Andrea Olivato

After several tests I managed to get a result. I understood that I can also do as written below.

$var2 = $var1 = $item->get_something()->get_anything();
echo '<div class="custom"> '. $var2 .' </div>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文