Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

PHP Dictionary

  1. Home
  2. PHP Dictionary
  3. Creating and Running .php Files

Creating and Running .php Files

This page explains how to save PHP code as a text file and run it. The file itself is simply a plain text file saved with a .php extension.

How to write a .php file

Write your PHP code in a text editor and save the file with a .php extension. Save the file using the UTF-8 character encoding.

PHP code must always start with the <?php tag. The closing ?> tag at the end of the file can be omitted (omitting it is recommended for PHP-only files).

sample_hello.php
<?php

$name = 'Tsunemori Akane';
$age = 21;

echo 'Name: ' . $name . "\n";
echo 'Age: ' . $age . "\n";

if ($age >= 20) {
    echo "Adult.\n";
} else {
    echo "Minor.\n";
}

Write PHP code after the <?php tag like this.

How to write comments

You can write comments (notes) in a .php file. Comments are ignored by the PHP interpreter, so they are useful for leaving descriptions or notes about the code.

SyntaxDescription
// commentA single-line comment. Write it with one space after //. Everything from // to the end of the line is a comment.
# commentA single-line comment. Write it with one space after #. Everything from # to the end of the line is a comment.
/* comment */A multi-line comment. Everything from /* to */ is a comment.
<?php

// A script that displays user information.
# Shell script-style single-line comments also work.

function greet($name, $age) {
    // Receives name and age as arguments and displays the information.
    echo 'Name: ' . $name . "\n";
    echo 'Age: ' . $age . "\n";
}

greet('Kogami Shinya', 30);

// is commonly used for single-line comments. # is a notation from shell scripts that also works in PHP, but // is the standard in modern PHP.

How to run

Open a terminal (macOS / Linux) or command prompt (Windows), navigate to the directory where the .php file is saved, then run it.

php hello.php
Name: Tsunemori Akane
Age: 21
Adult.

If PHP is not installed, see Setup.

When running PHP via a web server (Apache, Nginx, etc.), place the .php file in the document root and access it with a browser. Unlike command-line execution, the main use case here is to output HTML and display it as a web page.

Summary

A .php file is simply a plain text file. You can create one by writing PHP code in a text editor and saving it with a .php extension. No special tools are needed.

PHP code must always start with the <?php tag. To run from the command line, use php filename.php; to run via a web server, access it with a browser.

Three types of comments are available: //, #, and /* */. // is most commonly used for single-line comments.

For recommended editors and PHP installation instructions, see Setup.

If you find any errors or copyright issues, please .