PHP require_once() .js file

Charles John Thompson III picture Charles John Thompson III · Jun 24, 2014 · Viewed 11.6k times · Source

Okay, so I'm using require_once to load the contents of a .js file. However, there seems to be a 1 added to the end of the file after the require is done.

PHP:

echo require_once("test.js");

JS:

var newFunc = function() {
    console.log('yay!');
};

rendered html when the PHP file is loaded:

var newFunc = function() {
    console.log('yay!');
};1

Answer

i-CONICA picture i-CONICA · Jun 24, 2014

require_once() returns 1 if the include was successful. So you're echoing the return value, which is not what you thought it was.

Use file_get_contents() instead, although this is odd usage, you're probably heading in the wrong direction with whatever you're trying to do with this...