Page 1 of 2

variable substitution

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

Posted: Tue Aug 28, 2007 11:47 pm
by Christopher
Do you mean like:

Code: Select all

$i = 7;
echo "i=$i";
// prints i=7

Posted: Wed Aug 29, 2007 2:01 am
by s.dot
Or do you mean variable variables?

Posted: Thu Aug 30, 2007 8:45 pm
by asif_phpdn
Actually that is variable substitution in string. But I can't understand that. Help me to understand with an easy example.

Posted: Thu Aug 30, 2007 8:46 pm
by John Cartwright
asif_phpdn wrote:Actually that is variable substitution in string. But I can't understand that. Help me to understand with an easy example.
Help us by explaining what you mean, please.

Posted: Thu Aug 30, 2007 8:53 pm
by CoderGoblin
Without understanding exactly what you are after I can only really point you to a couple of manual pages...

Strings (including inserting variable values into strings)
Variable Variables

Posted: Thu Aug 30, 2007 8:53 pm
by asif_phpdn

Posted: Thu Aug 30, 2007 8:58 pm
by CoderGoblin
First link I posted... Read the section "Variable parsing".

Posted: Thu Aug 30, 2007 9:12 pm
by asif_phpdn
Why variable parsing??? When declaring a variable as string then why parsed???

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;?>
Here using heredocs. But what'is the benefit? in the above code declaring the heredocs where it is used??? 8O

Posted: Thu Aug 30, 2007 9:24 pm
by CoderGoblin
Parsing in this instance is converting the variable to it's value.

Heredoc syntax can aid readability, especially in cases of outputting a form with lots of elements and variables. Using double quote means escaping all " used in html (<input name=\"myname\" ...) etc and you can use \n to produce new line (not the same as html <br /> as newline is source only). Single quotes can also be confusing as you have to concatenate variables together with the string and cannot use the \n to produce a new line (it is quicker to process but to be honest not enough to make a difference). With heredoc syntax you can use both " and ' as text, where needed. Insert variables without concatenation and you do not need to use \n for a new line which makes the whole thing more readable.

I use all three in my code depending on circumstances.

Posted: Thu Aug 30, 2007 9:28 pm
by asif_phpdn
Thanks CoderGoblin. Would you [s]plz[/s] please give me an example? :roll:
[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.

Posted: Fri Aug 31, 2007 9:31 pm
by asif_phpdn
Why a variable need to parse to it's value especially for string???

Posted: Fri Aug 31, 2007 9:50 pm
by volka
asif_phpdn wrote:Why a variable need to parse to it's value especially for string???
Let's try it the other way round, shall we?
What happens here

Code: Select all

<?php
$i = 'little';
$s = 'Mary had a ' . $i . ' lamb.';
echo $s;
?

Posted: Sat Sep 01, 2007 10:32 pm
by asif_phpdn
volka wrote:
asif_phpdn wrote:Why a variable need to parse to it's value especially for string???
Let's try it the other way round, shall we?
What happens here

Code: Select all

<?php
$i = 'little';
$s = 'Mary had a ' . $i . ' lamb.';
echo $s;
?
just concatenate. Anything more?

Posted: Sat Sep 01, 2007 11:03 pm
by s.dot
That is substitution, as you asked, "little" is being substituted for $i. Concatenation.. yes. But also it could be done like this.

Code: Select all

<?php
$i = 'little';
$s = "Mary had a $i lamb.";
echo $s;
Same thing. "little" is being substituted for the variable $i in the string.