PHP is a programming language that runs on a web server. When someone visits a page built with PHP, the server runs the PHP code first, and only the finished result (usually HTML) travels to the visitor's browser. The visitor never sees your PHP code.
The name is a recursive acronym: PHP: Hypertext Preprocessor. "Preprocessor" is a good clue to what it does: it prepares the page before it is sent.
What can PHP do?
PHP powers a huge share of the web, from small school websites in Kisumu to large platforms such as WordPress. With PHP you can:
- Build pages whose content changes: today's date, the logged-in user's name, the latest products.
- Read and process data sent from HTML forms (sign-up forms, M-Pesa payment callbacks, contact forms).
- Store and fetch data in databases such as MySQL and MariaDB.
- Remember visitors between pages with sessions and cookies.
- Create, read and write files on the server, send email and talk to other web services (APIs).
- Control who can see what, for example an admin dashboard that only staff can open.
How a PHP page is served
Here is what happens when a learner opens https://example.co.ke/welcome.php:
- The browser sends a request to the server for
welcome.php. - The web server (for example Apache) sees the
.phpextension and hands the file to the PHP engine. - PHP runs the code in the file from top to bottom and builds the output.
- The server sends that output back to the browser as the response.
- The browser displays the HTML. It has no idea PHP was involved.
This is the big difference between PHP and JavaScript in the browser. JavaScript in a web page runs on the visitor's computer; PHP runs on your server. That is why PHP can safely hold things like database passwords: they never leave the server.
A first look
A PHP file usually mixes ordinary HTML with PHP blocks. Code inside <?php ... ?> is run; everything outside it is sent as-is.
<!DOCTYPE html>
<html>
<body>
<h1>Karibu!</h1>
<p><?php echo "This line was written by PHP."; ?></p>
</body>
</html><!DOCTYPE html> <html> <body> <h1>Karibu!</h1> <p>This line was written by PHP.</p> </body> </html>
Notice that the output contains no PHP at all. The <?php echo ...; ?> part was replaced by the text it produced. If the visitor chooses "View page source" in their browser, this output is exactly what they would see.
PHP can make decisions
The real power of PHP is that the output can be different each time. The next example calculates a value instead of just printing fixed text.
<?php
$county = "Kisumu";
$students = 120;
$perClass = 40;
echo "$county has " . ($students / $perClass) . " classes.";Kisumu has 3 classes.
Do not worry about the details yet. You will learn about variables (the names that start with $) and the . operator in the coming lessons.
Why learn PHP?
- It is everywhere. Almost every shared hosting plan in Kenya (and the world) supports PHP out of the box, often with cPanel.
- It is free and open source, and it runs on Windows, Linux and macOS.
- It is beginner friendly but grows with you: modern PHP 8 has types, classes, enums and good performance.
- It works well with MySQL/MariaDB, the most common databases on cheap hosting.
- Huge ecosystem: WordPress, Laravel, Drupal, Moodle and many more are built with PHP.
What you should know first
Before starting, you should be comfortable with basic HTML (and ideally a little CSS). PHP mostly produces HTML, so knowing what good HTML looks like helps a lot. Any programming experience (for example from our JavaScript or Python tutorials) is a bonus but not required.
What is a PHP file?
- A PHP file has the extension
.php. - It can contain text, HTML, CSS, JavaScript and PHP code.
- PHP code is run on the server, and the result is returned to the browser as plain HTML.
match) do not exist in PHP 7. Check your version with php -v, which you will do in the next lesson, Installing PHP.Complete the opening tag that starts a block of PHP code.
< echo "Habari"; ?>It starts with a question mark followed by the language name.
Which file extension tells the server to run a file through PHP?
welcome.
SKOPE