Thanks for the replies peoples :-)

-----Original Message-----
From: Ford, Mike [LSS] [mailto:[EMAIL PROTECTED]]
Sent: donderdag 2 mei 2002 11:13
To: 'Ferry van Steen'; [EMAIL PROTECTED]
Subject: RE: [PHP] Hashes in strings


> -----Original Message-----
> From: Ferry van Steen [mailto:[EMAIL PROTECTED]]
> Sent: 02 May 2002 09:42
> 
> why do these work:
> $vars = "vars";
> $ar[0] = "arrays";
> $hash['vars'] = "hashes";
> str = "I can use $vars in strings";
> str = "I can use $ar[0] in string";
> 
> while this one doesn't:
> str = "I can NOT use $hash['vars'] in strings";

Because the string parser doesn't believe ' is a valid character as part of
the "variable name" following a $.  Fortunately, this is catered for by
using braces to delimit your $-specification, thusly:

    $str = "I CAN use {$hash['vars']} in strings";

The {} notation can be used to delimit any $ specification, even simple ones
in cases where you might otherwise have ambiguity.  For example, this will
work (even if ungrammatically!):

   echo "Today is the {$date}th of $month."

where this won't:

   echo "Today is the $dateth of $month."

I strongly recommend using {} if in any doubt -- it can't do any harm, and
may just save you tearing your hair out looking for that elusive bug!!

Cheers!

Mike

---------------------------------------------------------------------
Mike Ford,  Electronic Information Services Adviser,
Learning Support Services, Learning & Information Services,
JG125, James Graham Building, Leeds Metropolitan University,
Beckett Park, LEEDS,  LS6 3QS,  United Kingdom
Email: [EMAIL PROTECTED]
Tel: +44 113 283 2600 extn 4730      Fax:  +44 113 283 3211 

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to