Javascript runs on the client, PHP on the server, so you can't incorporate PHP code inside JS.
What you can do is submit an AJAX request from JS to the server, process it with PHP and send back the results as a response.
In JQuery, look at
$.ajax()
$.get()
$.post()
Here's a very basic example
<?php
if (isset($_GET['ajax'])) { // I like to tell my script it's reciving AJAX
$x = $_GET['x'] ?? 0;
exit("$x squared is " . ($x**2)); // when process an AJAX request, anything that would normally be sent to the screen
} // is sent back in a response message
// rest of php code here
?>
<html>
<head>
<title>Example</title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js"></script>
<script type='text/javascript'>
$().ready( function() {
$("#btnSub").click( function() {
$.get (
"", // target script is "self"
{"ajax":1, "x":$("#x").val() }, // data to send
function(resp) { // process response
$("#result").html(resp);
},
"TEXT" // response type
)
})
})
</script>
</head>
<body>
Input a number <input type='text' id='x' value='0'>
<br>
<button id='btnSub'>Get Square</button>
<hr>
<div id='result'></div>
</body>
</html>