简体   繁体   中英

using html and java script tags in php

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>';
     }
?>

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

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.

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

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.

$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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM