php string syntax question with html

Hello,
I've got what is probably a very simple question, probably something
having to do with quotes single vs. double, but the answer is
frustrating elusive, I keep getting a syntax error.
I'm trying to customize a wordpress theme a friend sent me. We're both
using apache as web server and php5, but his has got to be configed
differently than mine. The theme deals with multiple stylesheet
inclusion among other things. The original line is:

$styleSheets[0]["sheet"]='<link
href="/wp-content/themes/theme/style/white.css" rel="stylesheet"
type="text/css" />';

That code puts the <link in the head portion of the document. The
issue is his / is not where mine is, i'm using a virtual host and need
a line similar to this:

$styleSheets[0]["sheet"]='<link href=$_SERVER['DOCUMENT_ROOT'] .
"/wp-content/themes/theme/style/white.css" rel="stylesheet"
type="text/css" />';

I've tried this with both double quotes before the <link declaration,
but keep getting a parse error.
Help appreciated.
Thanks.
Dave.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
David Mehler [ Do, 11 März 2010 01:15 ] [ ID #2034702 ]

Re: php string syntax question with html

--001636c5a8de30693b04817b90b9
Content-Type: text/plain; charset=ISO-8859-1

Try this:

'<link href='.$_SERVER['DOCUMENT_ROOT']
..'/wp-content/themes/themestyle/white.css" rel="stylesheet" type="text/css"
/>';

On Wed, Mar 10, 2010 at 7:15 PM, David Mehler <dave.mehler [at] gmail.com> wrote:

> Hello,
> I've got what is probably a very simple question, probably something
> having to do with quotes single vs. double, but the answer is
> frustrating elusive, I keep getting a syntax error.
> I'm trying to customize a wordpress theme a friend sent me. We're both
> using apache as web server and php5, but his has got to be configed
> differently than mine. The theme deals with multiple stylesheet
> inclusion among other things. The original line is:
>
> $styleSheets[0]["sheet"]='<link
> href="/wp-content/themes/theme/style/white.css" rel="stylesheet"
> type="text/css" />';
>
> That code puts the <link in the head portion of the document. The
> issue is his / is not where mine is, i'm using a virtual host and need
> a line similar to this:
>
> $styleSheets[0]["sheet"]='<link href=$_SERVER['DOCUMENT_ROOT'] .
> "/wp-content/themes/theme/style/white.css" rel="stylesheet"
> type="text/css" />';
>
> I've tried this with both double quotes before the <link declaration,
> but keep getting a parse error.
> Help appreciated.
> Thanks.
> Dave.
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>


--
Nephtali: PHP web framework that functions beautifully
http://nephtaliproject.com

--001636c5a8de30693b04817b90b9--
Adam Richardson [ Do, 11 März 2010 01:33 ] [ ID #2034703 ]

Re: php string syntax question with html

--=-tWKPGyViPBtwIkXYTMZw
Content-Type: text/plain
Content-Transfer-Encoding: 7bit

On Wed, 2010-03-10 at 19:33 -0500, Adam Richardson wrote:

> Try this:
>
> '<link href='.$_SERVER['DOCUMENT_ROOT']
> .'/wp-content/themes/themestyle/white.css" rel="stylesheet" type="text/css"
> />';
>
> On Wed, Mar 10, 2010 at 7:15 PM, David Mehler <dave.mehler [at] gmail.com> wrote:
>
> > Hello,
> > I've got what is probably a very simple question, probably something
> > having to do with quotes single vs. double, but the answer is
> > frustrating elusive, I keep getting a syntax error.
> > I'm trying to customize a wordpress theme a friend sent me. We're both
> > using apache as web server and php5, but his has got to be configed
> > differently than mine. The theme deals with multiple stylesheet
> > inclusion among other things. The original line is:
> >
> > $styleSheets[0]["sheet"]='<link
> > href="/wp-content/themes/theme/style/white.css" rel="stylesheet"
> > type="text/css" />';
> >
> > That code puts the <link in the head portion of the document. The
> > issue is his / is not where mine is, i'm using a virtual host and need
> > a line similar to this:
> >
> > $styleSheets[0]["sheet"]='<link href=$_SERVER['DOCUMENT_ROOT'] .
> > "/wp-content/themes/theme/style/white.css" rel="stylesheet"
> > type="text/css" />';
> >
> > I've tried this with both double quotes before the <link declaration,
> > but keep getting a parse error.
> > Help appreciated.
> > Thanks.
> > Dave.
> >
> > --
> > PHP General Mailing List (http://www.php.net/)
> > To unsubscribe, visit: http://www.php.net/unsub.php
> >
> >
>
>

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/st yle/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()}";

Thanks,
Ash
http://www.ashleysheridan.co.uk



--=-tWKPGyViPBtwIkXYTMZw--
Ashley Sheridan [ Do, 11 März 2010 02:17 ] [ ID #2034704 ]

Re: php string syntax question with html

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/st yle/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

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Paul M Foster [ Do, 11 März 2010 04:06 ] [ ID #2034705 ]

Re: php string syntax question with html

$var = 'bla'.$var2.'doh'.$var3['index'].'argh'.$var4[$var4index];

is so much more readable in any editor that does syntax highlighting,
and parses quicker too.

On Thu, Mar 11, 2010 at 1:15 AM, David Mehler <dave.mehler [at] gmail.com> wrote:
> Hello,
> I've got what is probably a very simple question, probably something
> having to do with quotes single vs. double, but the answer is
> frustrating elusive, I keep getting a syntax error.
> I'm trying to customize a wordpress theme a friend sent me. We're both
> using apache as web server and php5, but his has got to be configed
> differently than mine. The theme deals with multiple stylesheet
> inclusion among other things. The original line is:
>
> $styleSheets[0]["sheet"]='<link
> href="/wp-content/themes/theme/style/white.css" rel="stylesheet"
> type="text/css" />';
>
> That code puts the <link in the head portion of the document. The
> issue is his / is not where mine is, i'm using a virtual host and need
> a line similar to this:
>
> $styleSheets[0]["sheet"]='<link href=$_SERVER['DOCUMENT_ROOT'] .
> "/wp-content/themes/theme/style/white.css" rel="stylesheet"
> type="text/css" />';
>
> I've tried this with both double quotes before the <link declaration,
> but keep getting a parse error.
> Help appreciated.
> Thanks.
> Dave.
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Rene Veerman [ Do, 11 März 2010 08:03 ] [ ID #2034709 ]

Re: php string syntax question with html

--=-DdxB9nxk7Nn5ei3+wHPQ
Content-Type: text/plain
Content-Transfer-Encoding: 7bit

On Thu, 2010-03-11 at 08:03 +0100, Rene Veerman wrote:

> $var = 'bla'.$var2.'doh'.$var3['index'].'argh'.$var4[$var4index];
>
> is so much more readable in any editor that does syntax highlighting,
> and parses quicker too.
>
> On Thu, Mar 11, 2010 at 1:15 AM, David Mehler <dave.mehler [at] gmail.com> wrote:
> > Hello,
> > I've got what is probably a very simple question, probably something
> > having to do with quotes single vs. double, but the answer is
> > frustrating elusive, I keep getting a syntax error.
> > I'm trying to customize a wordpress theme a friend sent me. We're both
> > using apache as web server and php5, but his has got to be configed
> > differently than mine. The theme deals with multiple stylesheet
> > inclusion among other things. The original line is:
> >
> > $styleSheets[0]["sheet"]='<link
> > href="/wp-content/themes/theme/style/white.css" rel="stylesheet"
> > type="text/css" />';
> >
> > That code puts the <link in the head portion of the document. The
> > issue is his / is not where mine is, i'm using a virtual host and need
> > a line similar to this:
> >
> > $styleSheets[0]["sheet"]='<link href=$_SERVER['DOCUMENT_ROOT'] .
> > "/wp-content/themes/theme/style/white.css" rel="stylesheet"
> > type="text/css" />';
> >
> > I've tried this with both double quotes before the <link declaration,
> > but keep getting a parse error.
> > Help appreciated.
> > Thanks.
> > Dave.
> >
> > --
> > PHP General Mailing List (http://www.php.net/)
> > To unsubscribe, visit: http://www.php.net/unsub.php
> >
> >
>


Good catch Paul with the quotes around the array element!

My editor highlights those strings even without me having to keep
breaking out with concatenation Rene

Thanks,
Ash
http://www.ashleysheridan.co.uk



--=-DdxB9nxk7Nn5ei3+wHPQ--
Ashley Sheridan [ Do, 11 März 2010 09:16 ] [ ID #2034710 ]

Re: php string syntax question with html

--0015173ff00ac997ba04818285b8
Content-Type: text/plain; charset=ISO-8859-1

Ah, ok..
Turns out mine does too ;)

So for light apps, it can be considered a coder's preference then..


On Thu, Mar 11, 2010 at 9:16 AM, Ashley Sheridan
<ash [at] ashleysheridan.co.uk>wrote:

>
> My editor highlights those strings even without me having to keep breaking
> out with concatenation Rene
>

--0015173ff00ac997ba04818285b8--
Rene Veerman [ Do, 11 März 2010 09:51 ] [ ID #2034712 ]
PHP » gmane.comp.php.general » php string syntax question with html

Vorheriges Thema: changing values deep in an array by reference, with some index vars
Nächstes Thema: Array Search Not Working?