Introduction to PHP

Reviewed & published by Brayan K

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.

Part of the free PHP course at LearnCodingFast — hands-on lessons with worked examples and the output they print, 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.

<?php
/* Your very first PHP script. Everything PHP runs between <?php and ?>.
   Note this is a block comment, not a // one: a closing tag inside a //
   comment really does close PHP mode, and the rest of the file would then
   print as plain text instead of running. */

// 'echo' prints text to the page. Every statement ends with a semicolon ( ; ).
echo "Hello, World!\n";

// You can echo more than one line — each echo is its own statement.
echo "PHP is running on the server.\n";

// 'print' does almost the same job as echo (more on that below).
print "Goodbye for now!\n";
?>

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.

<?php
// This is a single-line comment. PHP ignores everything after the // .

# This is ALSO a single-line comment — the hash style works too.

/*
   This is a block comment.
   It can span as many lines as you like —
   handy for longer notes or temporarily switching off code.
*/

echo "Comments never appear in the output.\n"; // inline comment, also ignored
echo "Only the echoed text is sent to the browser.\n";
?>

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.)

<!DOCTYPE html>
<html>
<body>
  <h1>My Page</h1>

  <!-- The web server runs the PHP below and DROPS its output right here. -->
  <?php
    // This block runs on the server before the page is sent.
    $name = "Alice";              // a variable always starts with a dollar sign
    echo "<p>Hello, $name!</p>";  // inside "double quotes" PHP expands $name
  ?>

  <p>This plain HTML is sent as-is.</p>
</body>
</html>

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.

<?php
// 🎯 YOUR TURN — fill in each blank marked ___ , then run it.
// Remember: text goes in "double quotes" and every line ends with a semicolon.

// 1) echo your name
echo ___;                 // 👉 e.g.  "My name is Sam\n"

// 2) echo a second line that says what you're learning
echo ___;                 // 👉 e.g.  "I am learning PHP\n"

// ✅ Expected output (example):
//    My name is Sam
//    I am learning PHP
?>

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

<?php
// 🎯 YOUR TURN — only ONE of these lines should appear in the output.
// Turn the FIRST echo into a comment so PHP skips it.

___ echo "This line should be hidden.\n";   // 👉 put a  //  at the start to comment it out
echo "This line should be visible.\n";

// ✅ Expected output:
//    This line should be visible.
?>

Common Errors (and the fix)

Pro Tips

📋 Quick Reference — PHP Basics

SyntaxExampleWhat It Does
<?php ... ?><?php echo "Hi"; ?>Open / close a PHP block
echoecho "Hello";Output text to the page
printprint "Hello";Output one value (returns 1)
;echo "Hi";Ends every statement
// #// noteSingle-line comment
/* ... *//* note */Block (multi-line) comment

Mini-Challenge: About Me

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.

<?php
// 🎯 MINI-CHALLENGE: A tiny "about me" script.
// No code is filled in — work from the steps below, then run it.
//
// 1. echo a line:  "Name: <your name>"
// 2. echo a line:  "Town: <your town>"
// 3. echo a line:  "Learning: PHP"
// 4. Add a block comment ( /* ... */ ) above your code describing what it does.
// 5. Add one inline // comment on any line.
//
// Tip: end every echo with \n so each fact is on its own line.
//
// ✅ Expected output (example):
//    Name: Ada
//    Town: London
//    Learning: PHP

// your code here
?>

🎉 Lesson Complete!

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.

Continue this course

Frequently asked questions

What is PHP actually used for?

PHP is a server-side scripting language: it runs on the web server and builds the HTML that gets sent to the browser. It powers roughly three-quarters of all websites whose server language is known, including WordPress, Wikipedia, and applications built with frameworks like Laravel and Symfony. Anything that needs to talk to a database, handle a login, or process a form is a natural fit for PHP.

What is the difference between echo and print?

Both send text to the output. echo is slightly faster, can take several values separated by commas (echo "a", "b";), and returns nothing. print can only take one value and technically returns 1, so it can be used inside an expression. For everyday output most PHP code uses echo — print is rarely needed.

Do I need the <?php tag in every file?

You need it wherever you want PHP to run. A file can be pure HTML with no PHP at all, or it can mix HTML and PHP by opening a <?php block, doing some work, and closing it with ?>. Anything outside the tags is sent to the browser untouched; only the code inside the tags is executed.

How do I run a PHP file on my own computer?

Install PHP, then from a terminal run it one of two ways. For a quick script, php myfile.php runs it through the PHP command-line tool and prints the result. To serve a page like a real website, run php -S localhost:8000 in your project folder and open http://localhost:8000 in a browser — PHP's built-in server executes your .php files for you.