8 / 100 SEO Score

📘 Chapter 1: Getting Started with PHP

PHP (Hypertext Pre-Processor) is one of the most popular programming languages for creating dynamic websites and web applications. It’s powerful, yet simple enough for beginners to learn. With PHP, you can make websites more interactive, connect with databases, and even build full web apps.

In this chapter, we’ll cover: –
✅ PHP syntax (how code is written)
✅ Variables & Data types
✅ Operators (math, comparison, logic)
✅ Conditionals (if/else statements)
✅ A real project: Salary Calculator

By the end, you’ll be able to write simple PHP programs that take inputs, process them, and display results.


🛠 PHP Syntax: Your First Program

Every PHP program starts and ends with special tags:-

<?php
   // your code here
?>

👉 Save your file with a .php extension (example: syntax.php).

Let’s print our first line:

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

Run it in your terminal or browser, and you’ll see:

Hello World!

Congrats 🎉 you just wrote your first PHP program


💾 Variables & Data Types

Variables are like containers to store data. In PHP, they always start with a $ sign.

Example:

<?php
$name = "John Doe";     // String
$age = 25;              // Integer
$hourlyRate = 10.50;    // Float
$isActive = true;       // Boolean
?>

Common data types in PHP:

  • String → “Hello World”
  • Integer → 123
  • Float → 3.14
  • Boolean → TRUE or FALSE

➕ Operators in PHP

Operators let you perform calculations or comparisons.

Math Operators

$a + $b  // Addition
$a - $b  // Subtraction
$a * $b  // Multiplication
$a / $b  // Division
$a % $b  // Remainder

Comparison Operators

$a == $b   // Equal
$a === $b  // Equal + same type
$a != $b   // Not Equal
$a < $b    // Less than
$a > $b    // Greater than

Logical Operators

!$a        // NOT
$a && $b   // AND
$a || $b   // OR

🔀 Conditionals (if/else)

Conditionals control the program’s flow.

Example:-

<?php
$animal = "lion";

if($animal == "cat"){
   echo "Meow";
} elseif($animal == "dog"){
   echo "Woof";
} elseif($animal == "lion"){
   echo "Roar";
} else {
   echo "What does the fox say?";
}
?>

Output:-

Roar

🏗️ Activity: Employee Salary Calculator

Let’s combine everything into a real-world project.

<?php
$hourlyRate = 10.00;
$hoursWorked = 12;
$rateMultiplier = 1.5;   // overtime pay
$commissionRate = 0.10;
$grossSales = 1200;      // sales made
$bonus = 0;

// Salary calculations
$holidayRate = $hourlyRate * $rateMultiplier;
$holidayPay = $holidayRate * $hoursWorked;
$commission = $commissionRate * $grossSales;
$salary = $holidayPay + $commission;

// Bonus condition
if($grossSales >= 1000){
   $bonus = 1000;
}

// Final results
echo "Salary: $" . $salary . "\n";
echo "Bonus: $" . $bonus . "\n";
echo "Total: $" . ($salary + $bonus) . "\n";
?>

👉 Change $grossSales or $hoursWorked and see different results. This is how real apps calculate pay


📌 Summary

In this chapter, you learned: –
✔ PHP syntax and “Hello World”
✔ Variables & data types
✔ Math, comparison, and logical operators
✔ Using conditionals with if/else
✔ Built a real-world salary calculator

You now understand the building blocks of PHP 🚀

Leave a Comment