variable string php code example

Example 1: php heredoc

$output = <<<HTML
	<p>Lorem ipsum dolor sit amet consectetur<p>
	<a href="{$foobar}">click here</a>
HTML;

Example 2: php variable in string

$a = '12345';

// This works:
echo "qwe{$a}rty"; // qwe12345rty, using braces
echo "qwe" . $a . "rty"; // qwe12345rty, concatenation used

// Does not work:
echo 'qwe{$a}rty'; // qwe{$a}rty, single quotes are not parsed
echo "qwe$arty"; // qwe, because $a became $arty, which is undefined

Example 3: php heredoc variable substitution

echo <<<EX
    <p>Game: {$data['game_name']}<br/>
    the owner of the game is {$data['game_owner']}
    </p>
EX;

Tags:

Php Example