What are the potential pitfalls of trying to execute PHP code from JavaScript in a web environment?

Executing PHP code from JavaScript in a web environment can be potentially dangerous as it exposes your server-side code to client-side manipulation and security vulnerabilities. A safer approach is to make AJAX requests to a server-side PHP script that handles the execution of PHP code and returns the result to the client-side JavaScript.

// server-side PHP script (example.php)
<?php
// retrieve the PHP code sent via POST request
$php_code = $_POST['php_code'];

// execute the PHP code
eval($php_code);
?>