variable substitution
Posted: Tue Aug 28, 2007 9:14 pm
I can't understand variable substitution. Would anybody explain that with an easy and simple example?

A community of PHP developers offering assistance, advice, discussion, and friendship.
http://forums.devnetwork.net/
Code: Select all
$i = 7;
echo "i=$i";
// prints i=7Help us by explaining what you mean, please.asif_phpdn wrote:Actually that is variable substitution in string. But I can't understand that. Help me to understand with an easy example.
Code: Select all
<?php
$str = <<<EOD
Example of string
spanning multiple lines
using heredoc syntax.
EOD;
/* More complex example, with variables. */
class foo
{
var $foo;
var $bar;
function foo()
{
$this->foo = 'Foo';
$this->bar = array('Bar1', 'Bar2', 'Bar3');
}
}
$foo = new foo();
$name = 'MyName';
echo <<<EOT
My name is "$name". I am printing some $foo->foo.
Now, I am printing some {$foo->bar[1]}.
This should print a capital 'A': \x41
EOT;?>[url=http://forums.devnetwork.net/viewtopic.php?t=30037]Forum Rules[/url] Section 1.1 wrote:11. Please use proper, complete spelling when posting in the forums. AOL Speak, leet speak and other abbreviated wording can confuse those that are trying to help you (or those that you are trying to help). Please keep in mind that there are many people from many countries that use our forums to read, post and learn. They do not always speak English as well as some of us, nor do they know these aberrant abbreviations. Therefore, use as few abbreviations as possible, especially when using such simple words.
Some examples of what not to do are ne1, any1 (anyone); u (you); ur (your or you're); 2 (to too); prolly (probably); afaik (as far as I know); etc.
Let's try it the other way round, shall we?asif_phpdn wrote:Why a variable need to parse to it's value especially for string???
Code: Select all
<?php
$i = 'little';
$s = 'Mary had a ' . $i . ' lamb.';
echo $s;just concatenate. Anything more?volka wrote:Let's try it the other way round, shall we?asif_phpdn wrote:Why a variable need to parse to it's value especially for string???
What happens here?Code: Select all
<?php $i = 'little'; $s = 'Mary had a ' . $i . ' lamb.'; echo $s;
Code: Select all
<?php
$i = 'little';
$s = "Mary had a $i lamb.";
echo $s;