PHP Introduction

PHP is a server-side scripting language: your code runs on the server, and only the resulting HTML reaches the visitor's browser. Nobody can "view source" and read your PHP.

That single fact is what makes PHP useful — it can talk to a database, read files, check passwords, and send email, none of which browser JavaScript can safely do on its own.

A PHP file mixes HTML with PHP code inside <?php ... ?> tags:

PHP
<!DOCTYPE html>
<html>
<body>

<h1>My first PHP page</h1>

<?php
  echo "Hello, world!";
?>

</body>
</html>

The visitor receives:

HTML
<!DOCTYPE html>
<html>
<body>

<h1>My first PHP page</h1>

Hello, world!

</body>
</html>

What you need to run PHP

  • A server with PHP installed (XAMPP, Laragon, or php -S localhost:8000 for a quick local server).
  • Files saved with a .php extension.
  • Opening the file through the server (http://localhost/...), not by double-clicking it — a browser opening a .php file directly will just show you the source.

Where PHP is used

WordPress, Laravel, and a large share of Nepal's local business websites and hosting panels run on PHP. It remains one of the easiest languages to deploy: upload a file to shared hosting and it works.

Next: the exact syntax rules every PHP statement follows.

Knowledge Check

4 questions — test your understanding

1

What is the difference between `$a = 'hello'` and `$a = "hello $name"?

2

What does `===` do in PHP?

3

What is the output of `var_dump(0 == 'hello')` in PHP?

4

What does `$arr[] = $value` do?