Discover what PHP is, start the built-in server with php -S, write your first script with echo, and mix PHP and HTML.
Open this lesson in KodokonPHP is a programming language: a set of writing rules that lets you give instructions to a computer. What makes it special is that PHP code runs on a server, meaning a program that receives requests from a browser (Chrome, Firefox…) and sends back web pages. When you visit a PHP site, the server runs the code, builds the page, then sends the result to the browser. Visitors never see the PHP code, only the page it produces. Before you start, check that PHP is installed on your machine by opening a terminal (the app that lets you type commands) and entering:
php -vGood news: you don't need to install any heavy software to get to work. PHP ships with a built-in development server, perfect for learning. Create an empty folder (for example my-site), move into it with the terminal, then run the command below. localhost refers to your own computer, and 8000 is the port, a kind of door number the server listens on.
php -S localhost:8000Now create a file named index.php in this folder. A .php file is a script: a sequence of instructions run from top to bottom. PHP code always begins with the opening tag <?php. The echo statement sends text to the page that will be displayed. Every statement ends with a semicolon ;. Then open your browser at http://localhost:8000: your message appears!
<?php
echo "Hello, world!";PHP's real strength: it slips right in the middle of HTML. You write your HTML page as usual, and whenever you need a computed piece of content, you open a PHP block with <?php and close it with ?>. Here, the date() function returns today's date: the displayed page therefore changes every day, without you touching the file.
<!DOCTYPE html>
<html lang="en">
<body>
<h1>My first PHP page</h1>
<p>Today is <?php echo date("d/m/Y"); ?>.</p>
</body>
</html>echo statement do?php -S localhost:8000 command for?