Re: php string syntax question with html

From: Date: Thu, 11 Mar 2010 03:06:15 +0000
Subject: Re: php string syntax question with html
References: 1 2 3  Groups: php.general 
Request: Send a blank email to php-general+get-302687@lists.php.net to get a copy of this message
On Thu, Mar 11, 2010 at 01:17:57AM +0000, Ashley Sheridan wrote: <snip> > > You're using single quotes in your string, so you can't have PHP parse > the string for variables to extend into their corresponding values. If > you wish to do that, use either double-quoted strings or heredoc/nowdoc > syntax: > > $styleSheets[0]["sheet"]="<link href= > \"{$_SERVER['DOCUMENT_ROOT']}/wp-content/themes/theme/style/white.css\" > rel=\"stylesheet\" type=\"text/css\" />"; > > or > > $styleSheets[0]["sheet"]= <<<EOS > <link > href="{$_SERVER['DOCUMENT_ROOT']}/wp-content/themes/theme/style/white.css" > rel="stylesheet" type="text/css" /> > EOS; > > In both cases note the {} surrounding the variable. This is because PHP > needs to be told that you are trying to access an array element, > otherwise it will match only as far as $_SERVER and think that the > [ character starts regular text. This also works with object properties > and method return values: > > echo "{$some_obect->some_value} and {$some_object->some_method()}"; Um, not exactly. "This will parse correctly: $_SERVER[DOCUMENT_ROOT]." You just can't use single quotes inside the brackets to denote the array index, when the whole string is surrounded by double quotes. A more pedestrian example: $message = "The value of foo is $_POST[bar]\n"; You are, however, right about object properties. I know of no other way to parse them inside a quoted string, other than using braces. Paul -- Paul M. Foster

« previous php.general (#302687) next »