Thursday, 11 March 2021

Php: in place substitution of variables in a string

Syntax

"$variable_name"

"{$variable_name}"

 

Example

echo "Hi $first_name, you are $age years old\n";

echo "Hi {$first_name}, you are {$age} years old\n";

 

in_place_substitution_demo.php

#!/usr/bin/php

<?php

    $first_name = 'Krishna';
    $age = '32';

    echo "Hi $first_name, you are $age years old\n";
    echo "Hi {$first_name}, you are {$age} years old\n";

?>

 Output

$./in_place_substitution_demo.php 

Hi Krishna, you are 32 years old
Hi Krishna, you are 32 years old

 

 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment