soniccool
soniccool

Reputation: 6058

PHP Doesnt Work with ( ' ) symbols

I have a php script that uses something like this. But for some reason if i have any code with ' symbols in it, i get php errors when loading the page. So for some reason because of this i need to move all my javascripts to where html .='' doesnt exist.

But i need this code inside my site to work. How can i get codes with ' symbols into here?

For example below in this code we have 'linkText' which is the issue because of ' << this symbol

  $html .= '<input onClick="SelectAll('linkText');" id="linkText" class="sharelinkboxes" />
    ';

Upvotes: 1

Views: 140

Answers (4)

Salman
Salman

Reputation: 1380

You can do it like

$html .= <<< HTML
     <input onClick="SelectAll('linkText');" id="linkText" class="sharelinkboxes" />
HTML;

but 1 important thing do not put space after <<< HTML also do not put space before or after HTML;

or you can use

$html .= '<input onClick="SelectAll(\'linkText\');" id="linkText" class="sharelinkboxes" />';

Upvotes: 0

Rasmus
Rasmus

Reputation: 8476

Let us take a deeper look at what you are doing:-

  $html= '<input onClick="SelectAll('linkText');" id="linkText" class="sharelinkboxes" />';

When php encounters the second single quote just after the first bracket (' , it says "oh! this is the closing quote for the first single quote. That is where the bug begins. So you have to escape it just like below :-

$html= '<input onClick="SelectAll(\'linkText\');" id="linkText" class="sharelinkboxes" />';

Upvotes: 0

David
David

Reputation: 4117

you have to escape the ' in a string

$html .= '<input onClick="SelectAll(\'linkText\');" id="linkText" class="sharelinkboxes" />';

Upvotes: 7

hsz
hsz

Reputation: 152206

You have to escape ' characters with \:

$html .= '<input onClick="SelectAll(\'linkText\');" id="linkText" class="sharelinkboxes" />';

Upvotes: 7

Related Questions