...">

Php brackets and strings

can someone explain to me how to use curly braces {} in php lines? as

"this is a {$variable}"
"this is a {$user -> getName($variable);} name"

+3
source share
3 answers

If a dollar sign ($) is encountered, the parser will eagerly accept as many tokens as possible in order to form the correct variable name. Name the variable in braces to explicitly indicate the end of the name.

<?php
$beer = 'Heineken';
echo "$beer taste is great"; // works; "'" is an invalid character for variable names
echo "He drank some $beers";   // won't work; 's' is a valid character for variable names but the variable is "$beer"
echo "He drank some ${beer}s"; // works
echo "He drank some {$beer}s"; // works
?>

Source

+8
source

He used to indicate the end of a variable name, for example:

$var = "apple";

echo "I love $var!"; //I love apple!
echo "I love $vars!"; // I love !
echo "I love {$var}s!"; //I love apples!
echo "I love ${var}s!"; //I love apples! //same as above
+3
source

" {$ user → getName ($ variable);} name" . / . :

"this is a " . $user->getName($varaible) . " name"
+1

Source: https://habr.com/ru/post/1769187/


All Articles