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.
Key Features of PHP
How PHP Works
index.php).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 browserVariables in PHP
$ 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:
$Name ≠ $namePHP Data Types
"Hello" or 'Hello')10, -5)10.5)true or falsePHP 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>
$_POST['field_name'] retrieves input valuesisset() checks if form was submitted