variable substitution
Moderator: General Moderators
-
asif_phpdn
- Forum Commoner
- Posts: 28
- Joined: Sun Aug 26, 2007 8:50 pm
variable substitution
I can't understand variable substitution. Would anybody explain that with an easy and simple example?

- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Or do you mean variable variables?
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
-
asif_phpdn
- Forum Commoner
- Posts: 28
- Joined: Sun Aug 26, 2007 8:50 pm
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
- CoderGoblin
- DevNet Resident
- Posts: 1425
- Joined: Tue Mar 16, 2004 10:03 am
- Location: Aachen, Germany
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
Strings (including inserting variable values into strings)
Variable Variables
-
asif_phpdn
- Forum Commoner
- Posts: 28
- Joined: Sun Aug 26, 2007 8:50 pm
- CoderGoblin
- DevNet Resident
- Posts: 1425
- Joined: Tue Mar 16, 2004 10:03 am
- Location: Aachen, Germany
-
asif_phpdn
- Forum Commoner
- Posts: 28
- Joined: Sun Aug 26, 2007 8:50 pm
Why variable parsing??? When declaring a variable as string then why parsed???
Here using heredocs. But what'is the benefit? in the above code declaring the heredocs where it is used??? 
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;?>- CoderGoblin
- DevNet Resident
- Posts: 1425
- Joined: Tue Mar 16, 2004 10:03 am
- Location: Aachen, Germany
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.
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.
-
asif_phpdn
- Forum Commoner
- Posts: 28
- Joined: Sun Aug 26, 2007 8:50 pm
Thanks CoderGoblin. Would you [s]plz[/s] please give me an example? 
[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.
-
asif_phpdn
- Forum Commoner
- Posts: 28
- Joined: Sun Aug 26, 2007 8:50 pm
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;-
asif_phpdn
- Forum Commoner
- Posts: 28
- Joined: Sun Aug 26, 2007 8:50 pm
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;
That is substitution, as you asked, "little" is being substituted for $i. Concatenation.. yes. But also it could be done like this.
Same thing. "little" is being substituted for the variable $i in the string.
Code: Select all
<?php
$i = 'little';
$s = "Mary had a $i lamb.";
echo $s;Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.