Before you start
You need a working PHP command-line installation and a plain-text code editor. In a terminal, run php -v; it should print the version of the PHP executable on your PATH. If the command is unavailable, follow the installation guide for your operating system first. No database, framework or web-hosting account is needed for this exercise.
This tutorial uses PHP’s built-in web server only on your own computer. It is for local development and testing, not a public website. The PHP Playground in Codectionary does not execute PHP, so run these steps with your local PHP installation.
Check PHP in a terminal
php -vCreate a project folder and index.php
Make a new folder named php-first and open that folder in your editor. Create a plain-text file named index.php inside it. The .php extension matters: it tells a PHP-enabled server to process this file as PHP. Check that your editor did not save index.php.txt instead.
Save the first example below in index.php. If your editor is already open, use its Save command before running the server. In the terminal, change into the same php-first folder; the server will use that folder as its document root unless you provide a different one.
- Create a folder called php-first and open it in a code editor.
- Create index.php inside php-first, paste the first example and save it.
- Open a terminal in php-first. Use pwd on macOS/Linux or Get-Location in PowerShell if you need to check the current folder.
macOS or Linux — create and enter the folder
mkdir php-first
cd php-firstWindows PowerShell — create and enter the folder
New-Item -ItemType Directory -Path .\php-first
Set-Location .\php-firstUnderstand the first three lines
The <?php opening tag tells the PHP interpreter where PHP code begins. The empty line separates the tag from the statement for readability. echo writes a value into the response body. Here the value is a string: text enclosed in quotation marks. The quotation marks mark the string’s boundaries and are not themselves displayed.
The semicolon ends the echo statement. There is no ?> closing tag because the file contains only PHP code. PHP’s manual recommends leaving the final closing tag out of PHP-only files so accidental whitespace after it cannot become unwanted output. When PHP is embedded within an HTML document, a closing tag can switch back to ordinary HTML.
Start the server and open the page
From the php-first folder, run php -S localhost:8000. Leave that terminal open while you use the browser. The server listens on your computer at port 8000. Open http://localhost:8000/; the server finds index.php, asks PHP to execute it, and sends the resulting response to the browser. The browser should show Hello, Codectionary! as plain page text.
The URL starts with http:// because this is a local HTTP server. The browser does not execute the PHP statements: it receives their output. The server terminal also shows request messages. Press Ctrl+C there when you finish. If port 8000 is in use, choose 8001 in both the command and browser URL.
Start the local development server from php-first
php -S localhost:8000If port 8000 is already in use
php -S localhost:8001Change the message and refresh
Replace the string with “Hello from my first PHP page!”, save index.php, then refresh http://localhost:8000/. The message changes because each page request makes the server execute the saved script again. If you still see the old text, confirm you saved the file in the folder from which the server was started and that the browser is using the same port.
Do not open index.php with a file:// URL. That asks the browser to read a local file, bypassing the PHP server, so the PHP code is not processed. Depending on the browser, the source may be displayed, downloaded or handled another way. Use http://localhost:8000/ for this exercise.
Generate HTML with PHP
Now replace index.php with the second example. Most of the file is ordinary HTML. Inside the <h1>, <?php enters PHP mode, echo produces the heading text, and ?> returns to HTML. The browser receives an HTML document with the heading text already inserted; it renders the heading and paragraph as a webpage.
This static greeting does not require PHP in a real project, but the pattern becomes useful when the heading depends on application data. Whenever that data comes from a user or another untrusted source, validate it and escape it appropriately before inserting it into HTML. Save and refresh to see the page. Use View Page Source to compare the generated HTML with the PHP source in your editor.
Fix common first-program problems
If php is not recognised, the PHP executable is not installed or is not on this terminal’s PATH; use your operating-system setup guide and reopen the terminal. If the browser says it cannot connect, check that the server is still running and that the URL has the same host and port shown in the terminal. A 404 usually means you started the server in the wrong folder or saved the file under a different name.
A PHP parse error often points to a missing semicolon, quote or tag. The error includes a file and line number; read that line and the one before it. You can check syntax with php -l index.php without starting the server. If the browser shows literal PHP code, check that you used an http://localhost URL and saved a .php file inside the server’s document root.
Check index.php for PHP syntax errors
php -l index.phpWhat to learn next
You have created a project, run PHP in a local server and seen the difference between source code and the HTTP response. Next, learn variables and strings so that the output can change, then learn how requests provide input. Keep the development server local while you practise.
Syntax
<?php
echo "Hello, Codectionary!";
Examples
A complete index.php for the first request
Save this code as php-first/index.php, start php -S localhost:8000 in php-first, then open http://localhost:8000/.
<?php
echo "Hello, Codectionary!";
- The opening tag tells PHP to interpret the rest of this PHP-only file. No final closing tag is needed.
- echo writes the quoted string to the response body. The semicolon completes that statement.
- The server sends the output, Hello, Codectionary!, to the browser. The browser never needs the PHP source to display that text.
Place PHP output inside an HTML document
Replace the same index.php with this page and refresh the local browser URL.
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>My first PHP page</title>
</head>
<body>
<h1><?php echo "Hello, Codectionary!"; ?></h1>
<p>PHP generated the heading before the browser received this page.</p>
</body>
</html>- The document declares HTML and identifies its language, character encoding, viewport and tab title. The body contains a heading and paragraph.
- PHP runs only between <?php and ?> inside the heading. echo supplies its text; ?> then switches back to HTML.
- The browser receives generated HTML containing <h1>Hello, Codectionary!</h1> and renders the heading. It does not interpret PHP tags.
Best practices
- Save PHP files as plain text with a .php extension and use a PHP server URL to preview generated pages.
- Keep the built-in server on localhost for learning; stop it with Ctrl+C when finished.
- Read parse-error file names and line numbers, and run php -l index.php when syntax is unclear.
Runtime & versions
PHP runtime on a server or CLI. Feature availability depends on the language and runtime version used by your project; consult the official reference below.
PHP Manual