Current Location: Home> Latest Articles> Mac PHP File Tutorial: How to Create and Run PHP Scripts from Scratch

Mac PHP File Tutorial: How to Create and Run PHP Scripts from Scratch

M66 2025-09-18

How to Create PHP Files on Mac

Open a Text Editor

The first step to creating a PHP file on Mac is to open the Terminal application. You can use common text editors like nano or vi to write PHP code.

Create a New File

In the text editor, type the following command to create a new file:

nano my_php_file.php

This will create a file named my_php_file.php.

Write PHP Code

Enter the following PHP code in the new file:

<?php
    echo "Hello, world!";
?>

This code will output "Hello, world!" in the Terminal.

Save the File

In nano, press Control + O to save the file, then press Enter to confirm the file name.

Exit the Text Editor

Press Control + X to exit the editor.

Run the PHP Script

Navigate to the directory where the file is saved in Terminal and run the following command:

php my_php_file.php

You will see "Hello, world!" displayed in the Terminal.

Summary

This guide explains how to create PHP files on Mac, including opening the editor, creating a file, writing code, saving it, and running the script. Following these steps, even beginners can start programming with PHP easily.