Introduction
In this article I will explain Variable Parsing in PHP. When a string is in double quotes or in heredoc, the variables are parsed within it. It can be explained with two simple examples and a complex example and the complex syntax can be recognized by the curly braces. The simple one is most common and convenient, it is provides a way to variable.
Example 1
If a dollar sign ($) is encountered then the parser will take as many tokens as possible to form a valid variable name. Such as:
<?php
$name = "MCN";
echo "Company name is the $name solution.".PHP_EOL ."<br>";
//invalid. 's' is a valid character for a variable name, but the variable is $name.
echo "Company name is the $names.";
?>
Output

Example 2
<?php
$c_name = array("MCN", "TCS", "company" => "Acencher");
echo "Company name is the $c_name[0] .".PHP_EOL ."<br>";
echo "Company name is the $c_name[1] .".PHP_EOL ."<br>";
echo "Company name is the $c_name[0] .".PHP_EOL ."<br>";
echo "Company name is the $c_name[company] .".PHP_EOL ."<br>";
class people
{
public $manish = "manish sharma";
public $nitin = "nitin bhardwaj";
public $sharad = "sharad gupata";
public $rahul = "rahul sharma";
}
$emp = new people();
echo "$emp->manish works in $c_name[0] company.".PHP_EOL."<br>";
echo "$emp->nitin works in $c_name[1] company.".PHP_EOL."<br>";
echo "$emp->sharad works in $emp->nitin company.".PHP_EOL."<br>";
echo "$emp->nitin said hello to $emp->manish.".PHP_EOL."<br>";
?>
Output

Example 3
Complex (Curly) Syntax


Join the conversation! Your thoughts help the community grow.