variable substitution

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

asif_phpdn
Forum Commoner
Posts: 28
Joined: Sun Aug 26, 2007 8:50 pm

variable substitution

Post by asif_phpdn »

I can't understand variable substitution. Would anybody explain that with an easy and simple example?
:(
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Do you mean like:

Code: Select all

$i = 7;
echo "i=$i";
// prints i=7
(#10850)
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

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

Post by asif_phpdn »

Actually that is variable substitution in string. But I can't understand that. Help me to understand with an easy example.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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.
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post 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
asif_phpdn
Forum Commoner
Posts: 28
Joined: Sun Aug 26, 2007 8:50 pm

Post by asif_phpdn »

User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

First link I posted... Read the section "Variable parsing".
asif_phpdn
Forum Commoner
Posts: 28
Joined: Sun Aug 26, 2007 8:50 pm

Post 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
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post 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.
asif_phpdn
Forum Commoner
Posts: 28
Joined: Sun Aug 26, 2007 8:50 pm

Post 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.
asif_phpdn
Forum Commoner
Posts: 28
Joined: Sun Aug 26, 2007 8:50 pm

Post by asif_phpdn »

Why a variable need to parse to it's value especially for string???
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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;
?
asif_phpdn
Forum Commoner
Posts: 28
Joined: Sun Aug 26, 2007 8:50 pm

Post 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?
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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.
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.
Post Reply