Two ways to pass a multiline string in PHP to JavaScript _php tutorial

Source: Internet
Author: User
Both PHP and JavaScript are beginners. There is a recent need:

Let's say there's a multi-line string for PHP:

$a = <<
 
  

After passing to JavaScript is equivalent to:

var c= ' thy38\n\ csdn\n\ blog ';

Because the understanding of these two languages is low to do not know how Google, had to find out the method of two ways:

1. First escape PHP, then split, then go to JSON, then JavaScript parse, and then use \ n stitching.

2. Arrange a hidden input on the page, then give the value to it by PHP, and then JavaScript reads from it to implement the multi-line value delivery

PS: The above method is in the discussion with a PHP and JavaScript programmer students gradually drawn.
After writing the article back to think about, in fact, these two very much around the method is also derived from the PHP and JavaScript multi-line string understanding is not transparent.
Understanding is simple, far from being so complicated, the direct string substitution is the line:

var d=<?php echo ' \ '. Str_replace ("\ r \ n", "\\n\\\n", $a). ' \ ";?>; alert (D==C);

http://www.bkjia.com/PHPjc/825410.html www.bkjia.com true http://www.bkjia.com/PHPjc/825410.html techarticle both PHP and JavaScript are beginners. There is a need for this recently: let's say there's a multi-line string for PHP: $a = EOF thy38 CSDN blog EOF; After passing to JavaScript is equivalent to: Var c= ' thy ...

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