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

Z3RO21
Forum Contributor
Posts: 130
Joined: Thu Aug 17, 2006 8:59 am

Post by Z3RO21 »

And yet another way it can be done:

Code: Select all

$str = 'little';
printf('Mart had a %s lamb.', $str);
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

and just to put it all together

Code: Select all

// when code execution reaches this statement php concatenates the three strings
$s = 'Mary had a ' . $i . ' lamb.'; 

// when code execution reaches this statement php executes printf
// and printf "replaces" %s with the value of its second parameter
printf('Mart had a %s lamb.', $str);

// when code execution reaches this statement php "replaces" $i
// by the contents of the variable i
$s = "Mary had a $i lamb.";
not too strange, is it?
asif_phpdn
Forum Commoner
Posts: 28
Joined: Sun Aug 26, 2007 8:50 pm

Post by asif_phpdn »

:D Thanks everybody. I've almost done. Thanks again for replies. But I've another question.

In every example everybody used single quote. But what about double or heredocs (still I'm not clear about this 8O
) ???
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

try

Code: Select all

<?php
$i = 1234;
echo <<< dfh
- $i -
dfh;
echo '- $i -';
echo "- $i -";
see also: http://de2.php.net/string
asif_phpdn
Forum Commoner
Posts: 28
Joined: Sun Aug 26, 2007 8:50 pm

Post by asif_phpdn »

volka wrote:try

Code: Select all

<?php
$i = 1234;
echo <<< dfh
- $i -
dfh;
echo '- $i -';
echo "- $i -";
see also: http://de2.php.net/string
OK. I'll try and tell you immediately.
asif_phpdn
Forum Commoner
Posts: 28
Joined: Sun Aug 26, 2007 8:50 pm

Post by asif_phpdn »

volka wrote:try

Code: Select all

<?php
$i = 1234;
echo <<< dfh
- $i -
dfh;
echo '- $i -';
echo "- $i -";
see also: http://de2.php.net/string
heredocs and double quote print the value of i.
single quote print what is between the quotes not the value.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Yes.
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