Variable Parsing in PHP

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

here1.jpg

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

here2.jpg

Example 3

Complex (Curly) Syntax

This is not called complex because the syntax is complex, it is called complex because it allows the use of complex expressions. This example show all errors.  

<?php

// Show all errors

error_reporting(E_ALL);

$invititation = 'hello sir!'."<br>";

echo "This is {$invititation}"."<br>";

echo "This is {$invititation}"."<br>";

echo "This is ${$invititation}"."<br>";

echo "This square is {$square->width}00 centimeters broad."."<br>";

echo "This works: {$arr['key']}"."<br>";

echo "This works: {$arr[4][3]}"."<br>";

echo "This is wrong: {$arr[name][3]}"."<br>";

echo "This works: {$arr['name'][3]}"."<br>";

echo "This works: " . $arr['name'][3]."<br>";

echo "This works too: {$obj->values[3]->name}"."<br>";

echo "This is the value of the var named $name: {${$name}}"."<br>";

echo "This is the value of the var named by the return value of getName(): {${getName()}}"."<br>";

echo "This is the value of the var named by the return value of \$object->getName(): {${$object->getName()}}"."<br>";

echo "This is the return value of getName(): {getName()}"."<br>";

?>

It's also possible to access class properties using variables within strings like using this syntax.

<?php

class name

{

    var $man = 'I am man.';

}

$name = new name();

$man = 'man';

$sharas = array('Nitin', 'man', 'sharas', 'rahul');

echo "{$name->$man}"."<br>";

echo "{$name->$sharas[1]}";

?>

 

Output

here3.jpg

Up Next
    Ebook Download
    View all
    Learn
    View all