gpt4 book ai didi

php - ${ } 在 PHP 语法中是什么意思?

转载 作者:IT王子 更新时间:2023-10-28 23:45:35 25 4
gpt4 key购买 nike

我用PHP很长时间了,但我只是看到了类似的东西,

${  } 

确切地说,我是在一个 PHP Mongo 页面中看到的:

$m = new Mongo("mongodb://${username}:${password}@host");

那么,${ } 是做什么的?使用 Google 或在 PHP 文档中搜索 ${} 等字符非常困难。

最佳答案

${ }(美元符号大括号)被称为 Simple syntax .

It provides a way to embed a variable, an array value, or an objectproperty in a string with a minimum of effort.

If a dollar sign ($) is encountered, the parser will greedily take asmany tokens as possible to form a valid variable name. Enclose thevariable name in curly braces to explicitly specify the end of thename.

<?php
$juice = "apple";

echo "He drank some $juice juice.".PHP_EOL;
// Invalid. "s" is a valid character for a variable name, but the variable is $juice.
echo "He drank some juice made of $juices.";
// Valid. Explicitly specify the end of the variable name by enclosing it in braces:
echo "He drank some juice made of ${juice}s.";
?>

The above example will output:

He drank some apple juice.
He drank some juice made of .
He drank some juice made of apples.

关于php - ${ } 在 PHP 语法中是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5571624/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com