Re: [PHP] php string syntax question with html

[email protected] (Paul M Foster)
Newsgroups php.general
Message-ID <[email protected]>
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
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.