PHP and JavaScript to implement variable interaction sample code _php Tips

Source: Internet
Author: User
This example is the example of PHP and JavaScript interaction, PHP in the value assigned to the JS variable, if the PHP variable must have a value to do, even if it is a false branch.

For example, $flags in PHP is true in this example, if JS false statement is written: $title _1.innerhtml = "<?php echo $title;? > "There will be an error because $title is assigned to an array in PHP

Although the flags== in PHP is "false"; $title is a string, the case is $title is an array, and the array is not directly displayed in the string.
Copy Code code as follows:

<?php
$string = "heaven and earth";
$flags = "";
if (STRPBRK ($string, "and")!=false) {
$title = Explode ("and", $string);
$title _1 = $title [0];
$title _2 = $title [1];
$flags = "true";
}
else{
$title = $string;
$flags = "false";
}
?>

->html
Copy Code code as follows:



<meta http-equiv=content-type content= "Text/html;charset=utf-8" >
<title></title>
<script type= "Text/javascript" >
var display = function () {
var flags = "<?php echo $flags;?>";
var $title _1 = document.getElementById ("Title_1");
var $title _2 = document.getElementById ("title_2");
if (flags== "true") {
$title _1.innerhtml = "<?php echo $title [0];? > ";
$title _2.innerhtml = "<?php echo $title [1];? > ";
Return
}
if (flags== "false") {
$title _1.innerhtml = "<?php echo $string;? > ";
Although this is a false branch, logically this $string can be replaced with $title, but although only the real branch of the top, but the compilation of
This bogus branch must also be compiled, so the echo $title is wrong, because $title is an array, only $string;
}
}
</script>

<body onload= "display ()" >
<div id= "Title_1" ></div>
<div id= "title_2" ></div>
</body>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.