views:

105

answers:

3

I changed the single quotes to doubles quotes after I faced th following problem:

 $lang = array(
        'let's do something...'
    );

Now I have this problem:

$lang = array(
    "tagline_h2" => "A paragraph...<a href="#">this is a link</a>"
);

What should I do?

+4  A: 

As you are having double-quotes in a double-quoted string, you have to escape the double-quotes inside the string, using a backslash :

"A paragraph...<a href=\"#\">this is a link</a>"

See Double quoted string in the manual : it states that PHP will interpret the \" sequence as a double-quote (and \n will interpreted as a newline, and there are a couple of other of those sequences)

Pascal MARTIN
A: 

I like to use urlencode() and urldecode() to store strings temporarily. That way I don't have to think about double-quotes, quotes, ampersands, newlines or tabs or in fact anything.

Two caveats:

  • The academics will point out that it performs worse, and I would respond by saying that more time is wasted thinking about it than dealing with it, since it's a PHP website and not a particle accelerator. (Or is it?)

  • You have to urldecode() it when you're going to show it.

PLEASE NOTE that for the problem you describe, urlencode()/urldecode() would be overkill. I only mentioned this for future reference, but in your particular case, the escaping is far more appropriate as described in the accepted solution.

Helgi Hrafn Gunnarsson
What problem will this solve? It does not do anything.
Goran Rakic
Bloody hell, you're right, I completely misunderstood the problem. ;) Sorry about that.
Helgi Hrafn Gunnarsson
A: 

Even better, if you know that you are going to be dealing with quotes, why not use the heredoc and nowdoc syntaxes?

That way, you can do:

$lang = array(
    "tagline_h2" => <<<LINK A paragraph...<a href="#">this is a link</a>
LINK
);
Sean Vieira