PHP Fundamentals

Loading concept...

πŸš€ PHP Fundamentals: Your First Steps into Web Magic


🎭 The Story Begins: Meet PHP, Your Kitchen Chef

Imagine you’re building a restaurant. Your HTML is the menu that customers see. Your CSS is the beautiful tablecloth and decorations. But who actually cooks the food? That’s PHP!

PHP is like a chef in the kitchen. Customers never see the chef, but the chef prepares everything and sends out the finished dishes. The waiter (your web browser) only delivers what the chef made.


πŸ“– What is PHP?

PHP = Hypertext Preprocessor

PHP is a server-side programming language. This means it runs on the server (the restaurant kitchen), not on your computer (the dining table).

Simple Example:

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

This code tells PHP to send the words β€œHello, World!” to the browser.

Real Life Uses:

  • πŸ›’ Online stores like WooCommerce
  • πŸ“ Blogs like WordPress
  • πŸ“± Social media features
  • πŸ“§ Contact forms on websites

Why PHP?

Feature What It Means
Free Costs nothing to use
Easy Simple to learn
Powerful Runs 78% of websites
Friendly Works with HTML easily

βš™οΈ PHP Execution Model: How the Magic Happens

Think of ordering pizza online:

graph TD A[πŸ§‘ You click Order] --> B[πŸ“‘ Request goes to server] B --> C[πŸ‘¨β€πŸ³ PHP processes your order] C --> D[πŸ“„ PHP creates HTML response] D --> E[🌐 Browser shows Thank You page]

The 5-Step Dance:

  1. You click a button on a website
  2. Request flies to the server
  3. PHP runs on the server
  4. PHP creates HTML (like a chef plating food)
  5. Browser receives and shows the HTML

Key Point:

πŸ’‘ Your browser never sees PHP code. It only sees the finished HTML that PHP created!

Example:

You write this PHP:

<?php
$name = "Alex";
echo "Hello, $name!";
?>

Browser receives this HTML:

Hello, Alex!

The PHP code stays secret on the server. Only the result travels to you!


🏷️ PHP Version Differences

PHP has grown over time, like a child becoming an adult. Here are the major versions:

Version Timeline:

Version Year Big Changes
PHP 5 2004 Object-oriented programming
PHP 7 2015 2x faster! Better errors
PHP 8 2020 Named arguments, JIT compiler
PHP 8.3 2023 Newest features

What Changed From 7 to 8?

PHP 7 (Old way):

<?php
function greet($name = "Guest") {
    echo "Hello, $name!";
}
greet("Sam");
?>

PHP 8 (New way - Named Arguments):

<?php
function greet($name = "Guest") {
    echo "Hello, $name!";
}
greet(name: "Sam");
?>

Which Version Should You Use?

🎯 Use PHP 8.1 or higher for new projects. It’s faster and safer!


πŸ”§ PHP Setup and Configuration

Before cooking, you need a kitchen! Here’s how to set up PHP:

Option 1: All-in-One Package (Easiest! 🌟)

graph TD A[Download XAMPP or MAMP] --> B[Install it] B --> C[Start Apache & MySQL] C --> D[βœ… Ready to code!]
Tool Best For
XAMPP Windows, Linux, Mac
MAMP Mac (but works on Windows)
Laragon Windows (super fast)

Option 2: Check if PHP is Installed

Open your terminal and type:

php -v

You should see something like:

PHP 8.2.12 (cli)

The Config File: php.ini

This file controls PHP’s behavior. Important settings:

; Show errors (for learning)
display_errors = On

; Time limit for scripts
max_execution_time = 30

; Memory limit
memory_limit = 128M

πŸ’‘ Tip: In production (real websites), set display_errors = Off for security!


🏷️ PHP Tags and Embedding in HTML

PHP lives inside HTML! You tell the server where PHP starts and ends using special tags.

The Standard Tag (Use This! βœ…)

<?php
// Your PHP code here
?>

Mixing PHP with HTML

<!DOCTYPE html>
<html>
<head>
    <title>My Page</title>
</head>
<body>
    <h1>Welcome!</h1>

    <?php
    $hour = date("H");
    if ($hour < 12) {
        echo "<p>Good Morning! β˜€οΈ</p>";
    } else {
        echo "<p>Good Afternoon! 🌀️</p>";
    }
    ?>

    <p>Regular HTML here</p>
</body>
</html>

Short Echo Tag (For Quick Output)

<!-- Instead of this: -->
<?php echo $name; ?>

<!-- Use this: -->
<?= $name ?>

Important Rules:

Do This βœ… Not This ❌
<?php ... ?> <? ... ?> (old, unreliable)
Close tags in HTML files Leave open in pure PHP files

πŸ“ PHP Syntax Basics

PHP syntax is like English sentences with rules.

Rule 1: End Lines with Semicolon

<?php
echo "Hello";    // βœ… Has semicolon
echo "World";    // βœ… Has semicolon
?>

Rule 2: Variables Start with $

<?php
$name = "Luna";
$age = 10;
$is_happy = true;
?>

Rule 3: PHP is Case-Sensitive (Sometimes!)

<?php
$Name = "Alice";
$name = "Bob";
// These are DIFFERENT variables!

echo $Name;  // Alice
echo $name;  // Bob

// But functions are NOT case-sensitive:
ECHO "Hi";   // Works
echo "Hi";   // Also works
?>

Rule 4: Strings Use Quotes

<?php
$single = 'Hello';        // Single quotes
$double = "Hello $name";  // Double quotes (variables work!)
?>

Quick Reference:

graph TD A[PHP Statement] --> B[Keyword/Variable] B --> C[Operator] C --> D[Value] D --> E[Semicolon ;]

πŸ’¬ Comments: Notes for Humans

Comments are notes that PHP ignores. They help YOU remember what your code does!

Single-Line Comments

<?php
// This is a comment
echo "Hello";  // Comment at end of line

# This also works (less common)
?>

Multi-Line Comments

<?php
/*
  This comment
  spans multiple
  lines!
*/
echo "World";
?>

DocBlock Comments (Professional Style)

<?php
/**
 * Calculate the area of a rectangle
 * @param int $width  The width
 * @param int $height The height
 * @return int The area
 */
function area($width, $height) {
    return $width * $height;
}
?>

When to Use Comments:

Use Comments For Example
Explaining WHY // Skip weekends for business logic
TODO reminders // TODO: Add validation
Tricky code // Using bit shift for speed

πŸ’‘ Pro Tip: Good code is like a good jokeβ€”if you have to explain it, it’s not that good! Write clear code first, then add comments only where needed.


πŸ“’ Echo and Print: Talking to the World

These commands send text to the browser. They’re how PHP β€œspeaks”!

Echo (Most Common ⭐)

<?php
echo "Hello, World!";
echo "Multiple", " ", "strings";  // Can take many values
?>

Print (Slightly Different)

<?php
print "Hello, World!";
// print "One", "Two";  // ❌ Error! Only ONE value
?>

Echo vs Print:

Feature Echo Print
Speed Tiny bit faster Tiny bit slower
Values Multiple Only one
Return Nothing Returns 1

Outputting Variables

<?php
$name = "Star";
$age = 5;

// Method 1: Concatenation (dots)
echo "Name: " . $name . ", Age: " . $age;

// Method 2: Double quotes (cleaner!)
echo "Name: $name, Age: $age";

// Method 3: Curly braces (complex variables)
echo "Name: {$name}, Age: {$age}";
?>

Outputting HTML

<?php
echo "<h1>Welcome!</h1>";
echo "<p style='color:blue;'>Blue text</p>";
echo "<ul>
        <li>Item 1</li>
        <li>Item 2</li>
      </ul>";
?>

The Short Echo Tag

<!-- In HTML files, use this shortcut: -->
<p>Hello, <?= $name ?></p>

<!-- Instead of: -->
<p>Hello, <?php echo $name; ?></p>

πŸŽ‰ Congratulations!

You’ve learned the foundations of PHP! Here’s what you can now do:

graph TD A[βœ… Understand what PHP is] --> B[βœ… Know how PHP runs] B --> C[βœ… Set up PHP locally] C --> D[βœ… Mix PHP with HTML] D --> E[βœ… Write basic syntax] E --> F[βœ… Add comments] F --> G[βœ… Output with echo/print]

Your First Mini-Project:

Try this code in a file called greeting.php:

<!DOCTYPE html>
<html>
<head>
    <title>My First PHP Page</title>
</head>
<body>
    <?php
    // Get current hour
    $hour = date("H");
    $name = "Future Developer";

    // Choose greeting based on time
    if ($hour < 12) {
        $greeting = "Good Morning";
        $emoji = "β˜€οΈ";
    } elseif ($hour < 18) {
        $greeting = "Good Afternoon";
        $emoji = "🌀️";
    } else {
        $greeting = "Good Evening";
        $emoji = "πŸŒ™";
    }
    ?>

    <h1><?= $emoji ?> <?= $greeting ?>, <?= $name ?>!</h1>
    <p>The time is <?= date("h:i A") ?></p>

    <!-- PHP version info -->
    <footer>
        <small>Powered by PHP <?= phpversion() ?></small>
    </footer>
</body>
</html>

πŸš€ Next Step: Practice! The best way to learn PHP is by writing PHP. Start small, make mistakes, and keep building!

Loading story...

No Story Available

This concept doesn't have a story yet.

Story Preview

Story - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

Interactive Preview

Interactive - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Interactive Content

This concept doesn't have interactive content yet.

Cheatsheet Preview

Cheatsheet - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Cheatsheet Available

This concept doesn't have a cheatsheet yet.

Quiz Preview

Quiz - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Quiz Available

This concept doesn't have a quiz yet.