Introduction

PHP is a server-side scripting language that runs on the web server to generate the HTML pages sent to a visitor's browser, powering dynamic, database-driven websites like WordPress and Wikipedia.

Learn Python, JavaScript, Java and more with free interactive lessons, real projects and AI-powered help. Beginner-friendly.

Part of the free Php course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

By the end of this lesson you'll know what PHP is, how it runs on a web server, and how to write and output your first lines of PHP — the foundation of every dynamic page you'll ever build.

What You'll Learn in This Lesson

1️⃣ What is PHP?

PHP (it stands for "PHP: Hypertext Preprocessor") is a server-side scripting language — meaning the code runs on the web server , not in the visitor's browser. The server runs your PHP, the PHP produces HTML, and only that finished HTML is sent to the browser. This is the opposite of JavaScript, which runs in the browser. Created by Rasmus Lerdorf in 1995, PHP powers roughly 75% of all websites whose server language is known — including WordPress, Wikipedia, and apps built with frameworks like Laravel . Here is the very first thing every PHP programmer writes.

Notice three things already: code lives between ?php and ? , echo sends text to the output, and every statement ends with a semicolon . The \n inside the quotes is a newline, so each message lands on its own line.

2️⃣ The ?php Tags, echo & print

PHP only runs the code wrapped in tags . You open with ?php and close with ? ; everything between them is executed on the server. To produce output you have two near-identical tools: echo and print . Use echo almost all of the time — it's a touch faster and can print several values at once ( echo "a", "b"; ). print behaves the same for everyday text but only takes one value, so it's rarely needed. The first script above already showed both in action.

3️⃣ Comments

A comment is a note for humans that PHP completely ignores — it never reaches the browser. Comments explain why code does something and let you switch a line off without deleting it. PHP gives you three styles: // and # both comment out the rest of a single line, and /* ... */ wraps a block across many lines.

4️⃣ Embedding PHP in HTML

PHP's superpower is that it slots straight into an HTML page. Anything outside the ?php ... ? tags is sent to the browser exactly as written; anything inside is run first, and its output is dropped into that spot. That's how a static page becomes dynamic — the $name below could just as easily come from a login or a database. (A $variable is a named box for a value — that's the next lesson; here just notice it.)

The browser receives the Output panel above — pure HTML. It never sees the word echo or your variables. That's why PHP is safe for hiding business logic and database passwords: the source stays on the server.

5️⃣ How PHP Runs

PHP follows a simple loop: browser requests a page → web server runs the PHP → PHP returns HTML → browser displays it . There are two everyday ways to run a file. For a quick script, php hello.php runs it through the PHP command-line tool and prints the result in your terminal. To serve a real page, run php -S localhost:8000 in your project folder and open http://localhost:8000 — PHP's built-in web server executes your .php files. (Tools like XAMPP or Docker bundle PHP with the Apache server for bigger projects.)

Now you try. The script below is almost complete — fill in each ___ using the 👉 hint, then run it and check it against the Output panel.

One more. Here a line needs to be commented out so PHP skips it. Add a // where the hint points.

📋 Quick Reference — PHP Basics

No code is filled in this time — just a brief and an outline. Write it yourself, run it on onecompiler.com/php or your own machine, then check your result against the expected output in the comments. This is exactly the write-run-check loop you'll use on every real script.

Practice quiz

What does PHP stand for?

  • Personal Home Page Tool
  • PHP: Hypertext Preprocessor
  • Private Hosted Pages
  • Preprocessed Hyperlink Producer

Answer: PHP: Hypertext Preprocessor. PHP is a recursive acronym for 'PHP: Hypertext Preprocessor'.

Where does PHP code run?

  • In the visitor's browser
  • On the web server
  • Inside the database
  • On the user's graphics card

Answer: On the web server. PHP is a server-side language: it runs on the server and only the resulting HTML is sent to the browser.

Which tags open and close a block of PHP code?

  • {php} ... {/php}
  • <script> ... </script>
  • <?php ... ?>
  • <% ... %>

Answer: <?php ... ?>. PHP code lives between the <?php opening tag and the ?> closing tag.

How must every PHP statement end?

  • With a semicolon ;
  • With a colon :
  • With a period .
  • With a newline only

Answer: With a semicolon ;. Every PHP statement ends with a semicolon. A missing one is the most common parse error.

Which keyword is the standard way to output text in PHP?

  • write
  • echo
  • display
  • console.log

Answer: echo. echo sends text to the output and is the everyday tool for printing in PHP.

Which of these is NOT a valid PHP comment?

  • // a comment
  • # a comment
  • /* a comment */
  • <!-- a comment -->

Answer: <!-- a comment -->. <!-- --> is an HTML comment. PHP uses //, #, and /* */.

What appears in the browser for the line: echo "Hi"; // greeting

  • Hi // greeting
  • Hi
  • // greeting
  • Nothing

Answer: Hi. Comments are ignored by PHP and never reach the browser, so only 'Hi' is output.

Inside double quotes, what does echo "Hello, $name!" do with $name (where $name is "Alice")?

  • Prints the literal text $name
  • Replaces it with its value: Hello, Alice!
  • Causes a syntax error
  • Prints Hello, !

Answer: Replaces it with its value: Hello, Alice!. Double quotes interpolate variables, so $name is swapped for its value.

Which command runs a PHP file from the terminal?

  • run file.php
  • php file.php
  • node file.php
  • exec file.php

Answer: php file.php. php file.php runs the script through the PHP command-line tool and prints the result.

What is true about plain HTML written outside the <?php ... ?> tags?

  • It is executed as PHP
  • It causes an error
  • It is sent to the browser as-is
  • It is ignored completely

Answer: It is sent to the browser as-is. Anything outside the PHP tags is sent to the browser untouched; only code inside the tags runs.