Hi
my php code is not working on my computer, i cannot see any problem with my code either.
when loading my code, it just shows my code printed onto the browser

<?php
$yourname = check_input($_POST);
$email = check_input($_POST);
$likeit = check_input($_POST);
$comments = check_input($_POST);
?>
<html>
<body>
Your name is: <?php echo $yourname; ?><br />
Your e-mail: <?php echo $email; ?><br />
<br />
Do you like this website? <?php echo $likeit; ?><br />
<br />
Comments:<br />
<?php echo $comments; ?>
</body>
</html>

<?php
function check_input($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>

thats my code

Dani AI

Generated

Seeing your PHP printed in the browser means the server is not parsing it — the browser is just showing the file contents. As suggested, the usual causes are: no PHP-enabled web server running, the file is being opened directly with the file:// protocol, or the file does not have a .php extension or isn’t in the server’s document root.

Quick checks and fixes:

  • Put the file into your server’s web root (for WAMP/XAMPP/MAMP that’s htdocs/www) and open it with a URL like http://localhost/yourfile.php (not file://...).
  • Create a tiny test file named test.php in the same folder and visit it. If PHP is working you should see PHP info:
    <?php
    phpinfo();
    ?>

    If that prints your PHP configuration, the server is parsing PHP. If you still see source, the server isn’t set up to handle PHP.

A second bug in your script (once PHP is running): you’re passing the whole $_POST array into check_input. That function expects a string. Use the specific form keys and guard against missing fields, for example:

$yourname = isset($_POST['yourname']) ? check_input($_POST['yourname']) : '';
$email    = isset($_POST['email'])    ? check_input($_POST['email'])    : '';

Also prefer htmlspecialchars(..., ENT_QUOTES, 'UTF-8') when outputting form data.

If phpinfo() doesn’t work, confirm Apache/Nginx is started, PHP is installed/loaded, and the .php handler is enabled. For a quick local server you can also run:

php -S 127.0.0.1:8000

from the folder containing your file and visit http://127.0.0.1:8000/yourfile.php. Enable error reporting only for development, and check server error logs if problems persist.

Since you are trying it on your computer do you have a server and PHP installed on your computer? PHP is an interpreted language and needs the PHP on the machine trying to run the code. "<?php" tells the interpreter that it is supposed to run the following code up until "?>". Trying to run PHP files without a server and PHP installed means there's nothing that knows that it is supposed to run your code.

You can either sign up for an account on a free server host that supports PHP (which will be most of them these days) or you can install something like WAMP Server (if you run Windows on a PC). WAMP is free software which runs a server, PHP and MySQL and will allow you to test all of your PHP from your own computer.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.