Complex (Curly) Syntax PHP

1k views Asked by At

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

There are 1 answers

0
akrys On BEST ANSWER

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.