Introduction to PHP
PHP is a server-side scripting language built specifically for the web. It runs on the server before a page reaches the browser, making it the engine behind huge portions of the web, including WordPress and countless custom applications.
Server-Side vs Client-Side
JavaScript typically runs in the visitor's browser (client-side). PHP runs on the web server, generating the HTML that gets sent to the browser in the first place. This means PHP can safely access databases, files, and secrets that should never be exposed to a visitor's browser.
PHP files are usually saved with a .php extension and processed by a web server (like Apache or Nginx with PHP installed) before any output reaches the visitor.
Embedding PHP in HTML
PHP code lives inside special <?php ... ?> tags, and can be mixed directly with regular HTML — the server processes the PHP and outputs plain HTML in its place.
<!DOCTYPE html>
<html>
<body>
<?php
echo "Hello, World!";
?>
</body>
</html>
Common Mistakes
- Forgetting the opening
<?phptag, causing PHP code to be output as literal text instead of being executed. - Opening a PHP file directly in a browser via a file:// path instead of through a running web server — PHP requires server processing.
- Confusing PHP (runs on the server) with JavaScript (runs in the browser) when deciding where a piece of logic belongs.
- Forgetting a semicolon at the end of a PHP statement.
Professional Tip
The visitor's browser never sees your PHP source code — only the final HTML output. This is exactly why PHP is the right place for anything sensitive: database credentials, business logic, and private calculations.
Your Turn
Set up a local PHP environment (covered in the next lesson) and create a page that uses echo to print your name and a short welcome message.
Mini Quiz
Where does PHP code execute?