I have to execute one php file via commandline in my linux docker and I am able to execute it but the issue is that js code written in the same file is not getting executed and I am not able to achieve the desired functionality. So please direct with me in correct direction.
3
-
1JavaScript runs in the user's browser. If there's no browser it doesn't run. Can you rewrite the JS functionality in PHP?Chris– Chris2018-02-02 13:14:47 +00:00Commented Feb 2, 2018 at 13:14
-
Javascript need to be interpreted. PHP cannot do this. It only interprets PHP sources.Syscall– Syscall2018-02-02 13:14:56 +00:00Commented Feb 2, 2018 at 13:14
-
php can certainly start the js interpretter though'I wrestled a bear once.– I wrestled a bear once.2018-02-02 13:27:59 +00:00Commented Feb 2, 2018 at 13:27
Add a comment
|
2 Answers
You have to use exec or passthru and execute the js with Node in PHP to start the JS interpretter...
I just ran this test from the Mac command line
# Made a directory to test in
$ mkdir jsphptest && cd jsphptest
# made a test php file
$ touch test.php
# put some PHP code in the PHP file that tells it to run a javascript file
$ echo '<?php passthru("node test.js"); ?>' > test.php
# create the above references js file
$ touch test.js
# Put some JS code in the new JS file that logs some stuff into the console
$ echo 'console.log("this is javascript");' > test.js
# run the php file
$ php test.php
#outputs: this is javascript
Comments
javascript is client based language so you can not execute it this way. Javascript is being executed by browser
If you persist to execute it . Look at this link How do you run JavaScript script through the Terminal?
2 Comments
Syscall
Not browsers, by javascript engines, usually used in web browsers.
Terlan Abdullayev
I said it generally