How to get PHP script to run in browser?

How to get PHP script to run in browser?

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

The Server-Side Reality of PHP

The core issue preventing your code from executing is a fundamental misunderstanding of how server-side languages like PHP operate compared to client-side technologies like HTML and JavaScript. You are attempting to execute <?php echo "hello world"; directly within an HTML file opened via the file:// protocol in your browser, or by linking to a .php file that resides on your local machine without being served through a web server.

Browsers do not natively understand PHP. When you open an HTML file containing PHP code in Chrome, the browser reads the raw text of the file. It sees the string <?php echo "hello world"; as plain text because it has no mechanism to parse or execute that syntax. The result is exactly what you are seeing: the browser renders the HTML tags around the PHP block, but leaves the content inside the delimiters untouched.

Even if you create a file named helloworld.php on your desktop and try to access it via a local URL (e.g., file:///C:/Users/YourName/helloworld.php), the behavior remains identical. The browser still treats the response as static text unless the HTTP server explicitly processes the PHP tags before sending the response back to the client.

To get PHP code to run, you must route it through a web server that has the PHP interpreter installed and configured. This is why simple local file opening fails; there is no intermediary processing layer performing the translation from PHP source code to HTML output.

Setting Up the Environment

You cannot run standard PHP scripts directly from your hard drive without a server environment. While some online IDEs or specific development tools offer "live preview" features that simulate a server, these are exceptions that hide the underlying requirement for a server context. For legitimate development, you need a local server stack.

The most common approach on Windows is XAMPP or MAMP (for macOS), which bundles an Apache web server with PHP and MySQL. Alternatively, modern developers often prefer using Laravel Valet or a Docker-based setup. If you are already familiar with the Laravel ecosystem, you might consider running your local projects via laravel serve, which spins up a built-in development server configured to handle .php routes correctly.

Once you have a server running (such as Apache on port 80 or Nginx on port 80), you must place your files in the correct directory. For XAMPP, this is typically C:\xampp\htdocs. If you simply save your file elsewhere and try to access it via localhost, the server will not find the resource, resulting in a 404 error or, if the path is wrong, potentially a permission issue where the server refuses to execute the script.

Correct Implementation Example

Assuming you have installed XAMPP (or a similar stack) and your file is located at C:\xampp\htdocs\helloworld.php, your workflow changes significantly. You do not open the file in Chrome by double-clicking it. Instead, you launch your browser and navigate to:

http://localhost/helloworld.php

The request travels from your browser to the local Apache server. Apache detects the .php extension, passes the raw file content to the PHP interpreter. The interpreter executes <?php echo "hello world";, which outputs the string hello world. Finally, Apache sends this resulting HTML back to the browser, which renders it correctly.

Why the Form Approach Failed

Your second attempt involved creating a form pointing to helloworld.php. Even if the server were configured correctly, submitting a form via POST does not inherently change how the browser handles the initial request or why the file needs a server environment. The URL in the source code (action="helloworld.php") is treated as a relative path. Without a base URL defined by an active web server (like http://localhost), the browser cannot resolve where that script lives to send the data.

If you are looking to build robust applications beyond simple echo statements, integrating with frameworks can streamline this setup. For instance, LaravelCompany offers resources on how to structure projects that handle routing and execution environments efficiently, ensuring your PHP logic is always executed within a proper server context rather than relying on ad-hoc file paths.

Common Pitfalls

A frequent mistake for beginners is assuming that saving a file with a .php extension makes it executable. The filename extension only tells the client (browser) or the server what type of content to expect; it does not grant execution rights in a vacuum. Without an HTTP listener parsing the request and invoking the PHP CLI or SAPI (Server API), the code remains dormant text.

Another subtle issue is file permissions. If you install XAMPP, ensure your user account has read access to the htdocs folder. Similarly, if you are using a Docker container, forgetting to mount the volume correctly will result in the server reading an empty or inaccessible directory, leading to silent failures where the script simply doesn't run.

The Bottom Line

You cannot bypass the web server requirement for PHP. The "hello world" test is not about writing code; it is about establishing a pipeline between your browser and a processing engine. Once you configure a local server and point your browser to http://localhost, your PHP scripts will execute as intended, allowing you to move on to server-side logic, database interactions, and dynamic content generation.

Our network