PHP
Next

Introduction


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

  1. Simple Syntax – Easy for beginners, similar to C, Java, or Perl.
  2. Dynamic Web Pages – Can generate content on the fly.
  3. Form Handling – Collects and processes user input.
  4. Database Support – Works with MySQL, PostgreSQL, SQLite, and more.
  5. Session Management – Maintains user login or preferences.
  6. Extensive Library – Functions for strings, arrays, files, dates, etc.
  7. Supports Object-Oriented Programming (OOP) – Classes, objects, inheritance.

How PHP Works

  1. User requests a web page (e.g., index.php).
  2. The web server (Apache, Nginx, etc.) passes the request to PHP.
  3. PHP processes the code, interacts with the database if needed, and generates HTML output.
  4. 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 code
  • echo → 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

  1. String – Text ("Hello" or 'Hello')
  2. Integer – Whole numbers (10, -5)
  3. Float/Double – Decimal numbers (10.5)
  4. Booleantrue or false
  5. Array – List of values
  6. Object – Instance of a class
  7. 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 values
  • isset() checks if form was submitted


PHP
Introduction PHP Configuration PHP CRUD (Create, Read, Update, Delete) PHP Error Handling PHP Form Submission
All Courses
Bootstrap Content Writing CSS Cyber Security Data Analysis Deep Learning Email Marketing Excel HTML Java Script Machine Learning MySQLi PHP Power Bi Python for Analysis SEO SMM SQL