Complex (Curly) Syntax PHP [duplicate]

房东的猫 提交于 2021-01-29 02:59:35

问题


Afternoon guys!

For some reason Complex (Curly) Syntax in my PHP code doesn't interpreter as variable

function view($name, $data = [])
{
    require __DIR__ . '/../app/views/{$name}.view.php';
}

Here is the code I am calling view('index'); the problem is I get this error:

Warning: require(./core/../app/views/{$name}.view.php): failed to open stream

By the way my PHP version before anyone asks is 7.0.13

Thanks for your help!


回答1:


Your string has to be placed in double quotes (") instead of single quotes ('). Otherwise variables won't be replaced.

Look here: https://secure.php.net/manual/en/language.types.string.php#language.types.string.syntax.single

Note: Unlike the double-quoted and heredoc syntaxes, variables and escape sequences for special characters will not be expanded when they occur in single quoted strings.



来源:https://stackoverflow.com/questions/41523946/complex-curly-syntax-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!