Irshad Babar
Irshad Babar

Reputation: 1419

Debugging Javascript inside php file possible?

I am trying to find the way to debug javascript code inside the php file. I have a lot of javascript code embedded inside the php file.

I can debug php code using netbeans with the help of XDebug. I can also debug javascript separately inside html or js file with browser like chrome or firefox.

What I want is to debug javascript code inside the php file if it is possible. I am sure a lot of people will be using javascript embedded with php file. I don't like it personally, but I have to work it on. I know I can write the code separately in js file and then can debug with browsers, but it's lot of code, take time for the separation.

Can anybody here suggest me a way if it's possible what I am asking.

Upvotes: 4

Views: 4618

Answers (5)

Soufian
Soufian

Reputation: 161

In the Chrome DevTools inspector, when you navigate to the 'Sources' section, you can find the files being used, including the open PHP page. Simply set a breakpoint on the line you want, and it should work.

However, there might be cases where your JS code is in a different PHP file than the currently open page (I faced this issue myself 😄). Nevertheless, I found a little trick that might interest you: I set an event (for example on a button click) to trigger the execution of my JS code, and when I go to the Chrome 'Event Listeners', I have a link to my JS code, allowing me to debug.

I hope this helps you too 😉

Upvotes: 0

Cyrus Joudieh
Cyrus Joudieh

Reputation: 31

I don't know if I am late, but, I came across the following website and was able to setup the debugger for Javascript and PHP which supports to debug embedded JavaScript in PHP scripts.

source: https://abcmemo.com/2017/04/20/debug-php-and-javascript-in-visual-studio-code/

The website uses PHP.exe as a web service.

It is also possible to use IIS, Apache or others.

Requirements:

  1. IDE: Visual studio code
  2. Extension: PHP debug
  3. Extension: Debugger for (Chrome, Firefox or Edge)
  4. Xdebug set in php.ini
  5. Extension in browser (optional): PHP xdebug if you want to debug on trigger.

You need to have two debuggers running at the same time one for PHP and one for JavaScrpit.

PHP sample

JavaScript sample

Upvotes: 3

Irshad Babar
Irshad Babar

Reputation: 1419

I helped my self with the following steps if it could help any body else

Note:- If your rendered code is inline specific to java-script then it would hard to debug like this.

  1. Run the required page of your application using browser like chrome, edge etc

  2. Open the inspection page by pressing f12 or (Ctrl+Shift+I), Or you can right click on the page see the option for the inspection and click on it.

  3. Goto sources and double click on the source file(this will be probably your php file).
  4. If code not loaded then reload the page by pressing f5 or Ctrl+R, you will see your java-script code there embedded with the html after rendering, then you can put the break point wherever you want and debug through the browser tools(you can see some buttons there to guide you about debugging like step-over,step-into etc).

  5. You will also see errors there regarding java-script,

Upvotes: 1

Vladimir Mujakovic
Vladimir Mujakovic

Reputation: 680

I worked with a lot of wordpress templates where I had to deal with some js inside my php files. Assuming that you can run your code on a server, you can easily debug your output in dev tools in chrome (if using chrome). Chrome also allows for setting of breakpoints so you can go line by line, and since your browser ultimately runs the js, you can monitor your code behavior without dealing with the php. That was my main way of dealing with this issue.

I also recommend separating as much js as possible into separate files in your assets folder. Depending on your project, you rarely have to inline your code, In my opinion it's messy to include a lot of JS right in your php, unless you use onclick="" or onchange="" attributes (which can also be handled with event listeners.

Aside from that, console.log() the crap out of everything.

Upvotes: 2

decksterr
decksterr

Reputation: 143

IMHO, without even looking it up, i don't think that it is (nor should) be feasible.

Here's why:
Your PHP gets processed on the server side, that's when XDebug kicks in and enables you to breakpoint all your PHP code. Then the server output gets to the client, that's when the actual JS is processed inline in the parsed HTML. Which means you would have to intercept the HTML in some way, parse it, detect eventual inlined JS scripts... and set your breakpoint(s) at that time (yes on each run), then output to client, which parses the HTML yet again to render it and process eventual breakpoints. It would be a tedious process and even more tedious to get to work i guess and that is why nobody even tried making an extension for that.
To my knowledge, inlined JS is also a lot harder to debug and i never saw an actual setup that would allow breakpointing embedded tags in a static HTML document directly from the IDE, which would've been somewhat a little easier to achieve than breakpointing JS in PHP...

Your best shot i guess would be to externalise your JS in separate files and only hard code <script src="path/to/your/app.js"></script> in your PHP templates, which indeed would be much more comfortable to work with on the long run anyways.

Then you would be able to breakpoint all the stuff in app.js, plus have an actual front-end architecture, syntax-highlighting, impress your boss, make your life a lot easier, the world a better place, etc.

Also, for reference: How to debug JavaScript code with Netbeans? (answer #45515159)
And read on: https://netbeans.org/kb/docs/webclient/html5-js-support.html

Edit: seems like setting JS breakpoints in static HTML tags is feasible in Visual Code for example -> https://github.com/Microsoft/vscode-chrome-debug/issues/122

Upvotes: 6

Related Questions