Double click and run the Php script on mac

Double click and run the Php script on mac

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

Stefan Bogdanescu

Founder & Senior Architect

Why Double-Clicking PHP Files Doesn't Work on macOS

When you double-click an HTML file on a Mac, it opens in your default browser immediately. This works because the operating system associates the .html extension with a web server that can render static text and images directly to the user. However, clicking a .php file simply launches your preferred text editor, such as TextEdit or VS Code, displaying raw source code instead of executing the logic contained within.

This behavior occurs because PHP is not a client-side language like JavaScript or CSS. It is a server-side scripting language. The browser never sees the PHP tags (<?php ... ?>); it only receives the final output generated by the server after processing. Without an active web server listening on a local port, there is no entity capable of interpreting and executing those instructions.

The Role of the Web Server

To run PHP code locally on macOS, you must provide a runtime environment that mimics a production server. This usually involves installing a web server software like Apache or Nginx, or using an embedded server provided by your development stack.

If you are developing with Laravel or modern PHP frameworks, the industry standard is to use PHP Built-in Server (php artisan serve) or Valet. These tools spin up a lightweight HTTP server that parses PHP files and sends the resulting HTML back to the browser. Without these running in the background, the operating system treats the PHP file purely as data—a text file with a specific extension—hence why your text editor is the only application that responds to the double-click action.

Configuring Associations via open-url

If your goal is specifically to avoid typing commands like php artisan serve every time you want to view a page, you can configure macOS to associate PHP files with a specific command. This allows you to double-click a file and have it open in your browser, provided the underlying server process is running.

You can achieve this using the open-url application or by modifying the file associations directly. However, the most robust method involves creating a shell script that acts as a proxy for opening PHP files.

  1. Create a new file named open.php.sh in your home directory (~).
  2. Add the following content to the file:
#!/bin/bash
# Locate the PHP binary, usually installed via Homebrew or XAMPP
PHP_BIN=$(which php)

# Check if a web server is running (simple heuristic)
# Note: This requires specific setup depending on your stack (Laravel Valet, MAMP, etc.)
# For a generic solution, you often just need to launch the file in your browser
# assuming the development server is active on localhost.

if [ -z "$1" ]; then
    echo "Usage: open.php.sh <path_to_php_file>"
    exit 1
fi

FILE_PATH="$1"
OPEN_URL="http://localhost:8000/${FILE_PATH##*/}" # Adjust port based on your setup (e.g., Valet uses 80, Laravel serve varies)

open "$OPEN_URL"

Note: The logic above assumes a specific local server configuration. If you are using Laravel Valet, the domain might be project.test. If you are using MAMP, it is likely localhost:8888.

A more dynamic approach involves modifying the /Applications/Utilities/TextEdit.app or your browser's properties if you want to force a specific behavior, but this often breaks with file updates. A cleaner solution for developers who prefer double-clicking is to ensure their local development environment (like Valet) has its DNS resolution configured correctly in /etc/hosts.

Why Not Just Use php -f?

You might wonder if running the command php -f path/to/file.php from the terminal achieves the same result. While this executes the script, it outputs the results to the standard output stream (the terminal window) rather than sending an HTTP response to a browser. PHP scripts do not automatically serve content over HTTP unless they are invoked by a web server daemon or the built-in server. The CLI (Command Line Interface) mode is designed for scripting tasks, logging, and data processing, not for rendering interactive web pages.

The Trade-Off: Convenience vs. Context

Relying on double-click convenience comes with a significant trade-off. When you open a file via double-click, you lose the context of your development environment. You are no longer running in the same directory where your .env files, configuration paths, and dependencies reside. This can lead to subtle bugs if the script relies on relative paths that differ between the file system root and the web server's document root.

For serious development work, always run your application from the terminal or an IDE that manages the process state. Use double-clicking only for quick visual checks of static assets or simple scripts where path resolution is not critical. If you are working on a Laravel project specifically, tools like Laravel Valet handle this association seamlessly by registering the correct domains and ports, allowing you to navigate to your projects without needing to manually configure file associations or guess server paths.

Conclusion

The inability to double-click and run PHP directly is a feature of how server-side languages operate, not a limitation of macOS. To bridge the gap between user convenience and technical reality, you must either maintain a running local web server or configure file associations to launch that server's URL. Understanding the distinction between CLI execution and HTTP serving ensures you avoid common pitfalls regarding environment context and path resolution.

Our network