admin管理员组

文章数量:1290976

I'm working on a PHP script that piles 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 bine them
$piled_js = $js_file_core . $line_break . $js_file_timers;

I'm working on a PHP script that piles 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 bine them
$piled_js = $js_file_core . $line_break . $js_file_timers;
Share Improve this question edited Jun 25, 2012 at 0:19 Ash Blue asked Jun 25, 2012 at 0:10 Ash BlueAsh Blue 5,6025 gold badges31 silver badges37 bronze badges 8
  • 1 why cant you use "\n" to write line breaks? – Eswar Rajesh Pinapala Commented Jun 25, 2012 at 0:12
  • 2 why do you need linebreaks, anyway? – aurora Commented Jun 25, 2012 at 0:13
  • 2 Please use '\' not '/' :D .. if you do it wrong, it wont work :) – Eswar Rajesh Pinapala Commented Jun 25, 2012 at 0:18
  • 1 That's why you should use \n. – Fabrício Matté Commented Jun 25, 2012 at 0:23
  • 1 Can't you just use a regular white space character instead of line breaks? Pretty sure it'll have the same result (as long as your code is properly semi-colon'd) unless you're coding in coffeescript or something that relies on proper indenting. – Fabrício Matté Commented Jun 25, 2012 at 0:26
 |  Show 3 more ments

5 Answers 5

Reset to default 5

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 has a constant, PHP_EOL, to aid in cross-platform support of linebreaks.

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!

本文标签: JavaScript file line breaks with PHPStack Overflow