Re: how call a variable in a text

From: Date: Thu, 22 Oct 2009 00:24:43 +0000
Subject: Re: how call a variable in a text
References: 1 2 3 4 5 6  Groups: php.general 
Request: Send a blank email to php-general+get-299176@lists.php.net to get a copy of this message
Ashley Sheridan wrote: > On Wed, 2009-10-21 at 22:54 +0200, Kim Madsen wrote: > >> Ashley Sheridan wrote on 2009-10-21 22:43: >> >>> The {} only become really useful when you're trying to reference arrays >>> within a string: >>> >>> $var = array('great', 'boring'); >>> >>> $text = "this is {$var[0]}."; >>> >>> Without the curly braces, PHP wouldn't be able to figure out whether you >>> wanted the end string to be 'This is great.' or 'This is [0].' >>> despite >>> the variable itself clearly being an array. >> Ehh what? This has never been a problem for me: >> >> $text = "this is $var[0]."; >> >> However this does give an error (or notice, don't recall, haven't seen >> the error in quite a while): >> >> $text = "this is $var['0']."; >> >> In that case the solution is the curly brackets: >> >> $text = "this is {$var['0']}."; >> >> -- >> Kind regards >> Kim Emax - masterminds.dk >> > > Try this though: > > <?php > > $var = array(array('great','alright'), 'boring'); > > print "This is $var[0][0]."; > > ?> > > Thanks, > Ash > http://www.ashleysheridan.co.uk > > > And since we are trying to cover all possible ways (works with double quotes also): $message = '<b> There is a text ' . $variable . ' trial. </b>'; -- Thanks! -Shawn http://www.spidean.com

« previous php.general (#299176) next »