简体   繁体   中英

JavaScript file line breaks with PHP

I'm working on a PHP script that compiles a JavaScript framework. Right now I'm using the following code to create line breaks in JavaScript files, but I'm guessing there is a better way to do this?

// Line break, can this be written better?
$line_break = '

';

// Get JavaScript files
$js_file_core = file_get_contents('js/core.js', true);
$js_file_timers = file_get_contents('js/timers.js', true);

// Add a line break between files and combine them
$compiled_js = $js_file_core . $line_break . $js_file_timers;

Use

$line_break = "\n";

for a line break. Note the double quotes and not single. the \\n must be in double quotes for it to work.

PHP具有常数PHP_EOL ,以帮助跨平台支持换行符。

People above told you about using "\\n" already. I will point out the quotes. Many people may try this with single quotes ('). If you try this with single quotes like '\\n' you will just print out \\n. Use double quotes instead: "\\n"

Makes difference.

Maybe '\\n' is better for linux users. people always use '\\n' to break lines, not '\\r\\n' which used in dos and not '\\r' in mac-os.

// Line break, can this be written better? Yes!, use \\n In PHP "\\n" forces a new line character!

$line_break = "\n";// prints a newline!

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