note 58353 added to language.types.string

From: Date: Tue, 01 Nov 2005 16:05:59 +0000
Subject: note 58353 added to language.types.string
Groups: php.notes 
Request: Send a blank email to php-notes+get-97933@lists.php.net to get a copy of this message
Just some quick observations on variable interpolation: Because PHP looks for {? to start a complex variable expression in a double-quoted string, you can call object methods, but not class methods or unbound functions. This works: <?php class a { function b() { return "World"; } } $c = new a; echo "Hello {$c->b()}.\n" ?> While this does not: <?php function b() { return "World"; } echo "Hello {b()}\n"; ?> Also, it appears that you can almost without limitation perform other processing within the argument list, but not outside it. For example: <? $true = true; define("HW", "Hello World"); echo "{$true && HW}"; ?> gives: Parse error: parse error, unexpected T_BOOLEAN_AND, expecting '}' in - on line 3 There may still be some way to kludge the syntax to allow constants and unbound function calls inside a double-quoted string, but it isn't readily apparent to me at the moment, and I'm not sure I'd prefer the workaround over breaking out of the string at this point. ---- Manual Page -- http://www.php.net/manual/en/language.types.string.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+58353 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+58353&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+58353&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+58353&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+58353&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+58353&report=yes&reason=non-english Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+58353&report=yes&reason=already+in+docs Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+58353&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+58353&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#97933) next »