Without more information about what you want to do exactly I can not assure you, but in principle you can not do what you want.
PHP runs on the server side, and JS on the client side. The sequence would be more or less like this:
The client requests the server to view a page .php
The server executes the PHP code to generate a plain text file. This plain text will normally be HTML, which may contain JS code between "script" tags
The server sends the generated text and the client receives it (the client will normally be a browser, such as firefox or chrome)
The client will interpret the HTML and will show it properly, also executing the javascript code it contains, but it is executed ON THE CUSTOMER
As you can see, the execution of the PHP code has already finished completely in step 2, but the execution of the JS code is done in step 4 and on a different machine (the PHP has been executed on the server, the JS has been executed on the client).
Through AJAX you can send information through JS to be processed on the server using PHP, but I do not think it is the solution you are looking for. It works in the following way:
The client receives the HTML + JS text from the server according to the previous steps. Show the HTML content and execute the JS code.
In the JS code, an asynchronous request is made to the server using AJAX. In this request, the location is sent as a variable.
The server receives the request, processes it and sends data back
The JS code continues to run, and you can use the received data to modify the current page
As you can see, it is possible to use data obtained by javascript to execute PHP code, but it implies a more complex design. It requires a more complex JS code that makes the AJAX call and modify the page according to the received data and requires that the PHP code be executed in 2 different moments.
If you give more information about what you want to achieve, we can suggest how to do it.