PHP (Hypertext Preprocessor) is a server-side scripting language designed for web development. It allows you to create dynamic web pages that interact with databases, process forms, and handle server-side logic.
- Server-side: PHP code runs on the server, not in the user’s browser.
- Dynamic content: Generates HTML dynamically based on user input or database data.
- Free & Open Source: PHP is widely supported and free to use.
- Works on all major operating systems and integrates with most web servers.
Key Features of PHP
- Simple Syntax – Easy for beginners, similar to C, Java, or Perl.
- Dynamic Web Pages – Can generate content on the fly.
- Form Handling – Collects and processes user input.
- Database Support – Works with MySQL, PostgreSQL, SQLite, and more.
- Session Management – Maintains user login or preferences.
- Extensive Library – Functions for strings, arrays, files, dates, etc.
- Supports Object-Oriented Programming (OOP) – Classes, objects, inheritance.
How PHP Works
- User requests a web page (e.g.,
index.php). - The web server (Apache, Nginx, etc.) passes the request to PHP.
- PHP processes the code, interacts with the database if needed, and generates HTML output.
- Browser receives the generated HTML (not the PHP code itself).
Flow diagram:
Browser → Server → PHP Processes → HTML Output → Browser
Basic PHP Syntax
PHP code is embedded in HTML using <?php ... ?> tags.
<!DOCTYPE html>
<html>
<head>
<title>PHP Example</title>
</head>
<body>
<h1>Welcome to PHP</h1>
<?php
// This is a single-line comment
echo "Hello, World!"; // Output text to the browser
?>
</body>
</html>
<?php→ Starts PHP code?>→ Ends PHP codeecho→ Outputs data to the browser
Variables in PHP
- Variables start with
$and do not require type declaration.
<?php $name = "Alice"; // String $age = 25; // Integer $price = 19.99; // Float $isActive = true; // Boolean echo "Name: $name, Age: $age, Price: $price"; ?>
Notes:
- Case-sensitive:
$Name≠$name - Variable names must start with a letter or underscore
PHP Data Types
- String – Text (
"Hello"or'Hello') - Integer – Whole numbers (
10,-5) - Float/Double – Decimal numbers (
10.5) - Boolean –
trueorfalse - Array – List of values
- Object – Instance of a class
- NULL – No value
PHP Operators
PHP Example: Variables & Basic Output
<?php $firstName = "John"; $lastName = "Doe"; $age = 30; echo "<h2>User Info:</h2>"; echo "Full Name: " . $firstName . " " . $lastName . "<br>"; echo "Age: $age"; ?>
Output:
User Info: Full Name: John Doe Age: 30
PHP Example: Simple HTML Form Handling
<!DOCTYPE html>
<html>
<head>
<title>PHP Form Example</title>
</head>
<body>
<h2>Contact Form</h2>
<form method="post" action="">
Name: <input type="text" name="name"><br><br>
Email: <input type="email" name="email"><br><br>
<input type="submit" name="submit" value="Submit">
</form>
<?php
if(isset($_POST['submit'])){
$name = $_POST['name'];
$email = $_POST['email'];
echo "<h3>Your Details:</h3>";
echo "Name: $name <br>";
echo "Email: $email";
}
?>
</body>
</html>
- Form data is sent to the same page
$_POST['field_name']retrieves input valuesisset()checks if form was submitted