0

I am confused about using single and double quotes and back slash while using java script and html tags in php can any one please clarify i googled it but still not clear about it. i am confused for this small thing.i am new to programming - here is my code

 <?php
if(isset($_GET['id'])) {
echo '<div id="d2">';
include "test2.php";
 echo '</div>'; }
 else
 { 
  echo '<div id="d1">';
 include "insert.php";
 print "<script type=javascript>"
 print "document.getEelementById('alertdiv1').innerHTML='hi' ;"
 print "</script>"
echo '</div>';
     }
?>
4
  • in deam veiwer it shows syntax error but program runs but inner.html is not working @rlatief Commented Feb 1, 2013 at 11:41
  • You didn't put semicolons on the print part. Commented Feb 1, 2013 at 11:42
  • 2
    Spell error in Element. You have given Eelement Commented Feb 1, 2013 at 11:43
  • Good catch @Edwin Alex :-) Commented Feb 1, 2013 at 11:44

5 Answers 5

1

In PHP, you can enclose a string in either single quotes or double quotes. Both are valid:

$var = "this is a string";
$var2 = 'this is also a string';

The main difference is that if your string contains a variable, and you want the variable content to be treated as part of the string, you need to use double quotes:

echo "$var which I made";

will return:

this is a string which I made

When you are manipulating html, css and JavaScript strings, you need to make sure that you don't accidentally close your PHP string. For example:

echo "<h1 class='myheading'>Heading Text</h1>";

Notice how I used double quotes to enclose my string? Because I did that, I was able to use single quotes in the html, without escaping them.

If I'd wanted to use double quotes in my string, I would have had to escape them, like this:

echo "<h1 class=\"myheading\">Heading Text</h1>";

The \ tells PHP that the double quote which immediately follows is to be treated as a literal, and not used to terminate the string.

Sign up to request clarification or add additional context in comments.

Comments

1

I can't see any problems relating to quotes in your code.

<script type=javascript> — That is not a valid value of the type attribute (which is optional anyway now). Get rid of the type attribute.

document.getEelementById — Element only has 3 es in it, not 4.

alertdiv1 — There is no element with that id in your code

Comments

0

hi as far as concerned single quotes and double quotes doesnt matters when its a string.but when you use any variable inside

$a = 'hi';
echo '$a' ;

will output

$a

but when you use " "

$a = 'hi';
    echo "$a" ;

it will print

hi

Comments

0

Basically, if you're using "" (quotation marks) as your delimiter and you then use a quotation mark as part of the string you must escape it by putting a backslash in front of it.

For example:

$string = "This is my string"; // This is fine as the value of the string doesn't contain any quotation marks (")

$string = "I am including a quote in my string \"To Be Or Not To Be\"."; // This is also fine, as I have escaped the quotation marks inside the string

The same applies if you're using '' (apostrophes) as your delimiter and you then want to use them as part of the string, you must escape them using back slash ().

Hope that helps.

Comments

0

$var = "AAA";
echo 'This costs a lot of $var.'; // This costs a lot of $s.
echo "This costs a lot of $var."; // This costs a lot of AAA.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.