A Beginner-Friendly Guide by Cyber Gita
PHP is one of the most popular programming languages used to build dynamic and interactive websites. The full form of PHP is PHP: Hypertext Preprocessor. It is a server-side scripting language, which means the PHP code runs on the server before the web page is shown to the user.
Over the years, PHP has gained massive popularity because it is easy to learn, powerful, and widely supported. Today, PHP powers millions of websites, including popular platforms like WordPress, Shopify, and many custom business applications.
Because PHP works on multiple operating systems (Windows, Linux, macOS) and supports many web servers, it is an ideal choice for anyone starting a career in web development.
What You Will Learn in This Guide
This beginner guide will help you understand the core building blocks of PHP, including:
- Basic PHP syntax
- Variables and data types
- Operators
- Conditional statements (if–else)
- Writing and running simple PHP programs
By the end of this guide, you will be able to write simple PHP programs and understand how PHP logic works.
The Basics of PHP
Let’s start our journey by understanding PHP syntax and how PHP code is executed.
PHP Syntax
In PHP, syntax is very important. PHP code must always be written inside PHP opening and closing tags:
<?php
?>
These tags tell the server where PHP code starts and ends.
You can write PHP code inside an HTML file, but only the code inside PHP tags will be executed.
👉 Important:
Always save PHP files with the .php extension.
Your First PHP Program: Hello World
Let’s write our first PHP program.
Steps:
- Open your code editor
- Create a new file named
syntax.php - Add the PHP tags:
<?php
?>
- Now add this code inside the tags:
<?php
echo "Hello World";
?>
- Open your terminal and run:
php syntax.php
🎉 Output:
Hello World
If you see this output, your PHP setup is working correctly.
Variables and Data Types in PHP
Variables are used to store data temporarily in a program. Almost every PHP application uses variables.
Variable Syntax in PHP
$variableName = "value";
- Variables always start with a $ symbol
- Variable names cannot start with numbers
- PHP automatically detects the data type
Common Data Types in PHP
| Data Type | Example |
|---|---|
| String | "Hello World" |
| Integer | 123 |
| Float | 10.50 |
| Boolean | true or false |
Working with Variables (Real Example)
Let’s create a real-world example using variables.
File: variables.php
<?php
$name = "John Doe";
$age = 25;
$hourlyRate = 10.50;
$hours = 40;
echo $name . " is " . $age . " years old.\n";
echo $name . " makes $" . $hourlyRate . " an hour.\n";
echo $name . " worked " . $hours . " hours this week.\n";
?>
PHP also allows embedding variables inside strings:
echo "My name is $name";
Operators in PHP
Operators allow us to perform calculations, compare values, and control logic.
Comparison Operators
Used to compare two values:
| Operator | Meaning |
|---|---|
== | Equal |
=== | Identical (value + type) |
!= | Not equal |
< | Less than |
> | Greater than |
<= | Less than or equal |
>= | Greater than or equal |
Logical Operators
Used to check multiple conditions:
| Operator | Meaning |
|---|---|
! | NOT |
&& | AND |
| ` |
Mathematical Operators
Used for calculations:
| Operator | Function |
|---|---|
+ | Addition |
- | Subtraction |
* | Multiplication |
/ | Division |
% | Modulus |
Combining Variables and Operators
Let’s calculate a yearly salary using operators.
File: operators.php
<?php
$name = "John Doe";
$hourlyRate = 10.50;
$hours = 40;
$weeks = 52;
$weeklyPay = $hourlyRate * $hours;
$salary = $weeklyPay * $weeks;
echo $name . " will make $" . $salary . " this year.\n";
?>
Run the file using:
php operators.php
Conditionals in PHP (if Statements)
Conditionals control the flow of execution in a program.
Basic if Statement
if (condition) {
// code runs if condition is true
}
if–else Statement
if (condition) {
// true case
} else {
// false case
}
Working with Conditionals (Example)
File: conditionals.php
<?php
$animal = "lion";
if ($animal == "cat") {
echo "meow\n";
} else if ($animal == "dog") {
echo "woof\n";
} else if ($animal == "lion") {
echo "roar\n";
} else {
echo "Unknown animal\n";
}
?>
Activity: Employee Salary Calculator
This activity helps you practice variables, operators, and conditionals.
Scenario
Employees working on Black Friday:
- Get 1.5x hourly pay
- Get 10% commission
- Bonus of $1000 if sales ≥ $1000
File: index.php
<?php
$hourlyRate = 10;
$hoursWorked = 12;
$rateMultiplier = 1.5;
$commissionRate = 0.10;
$grossSales = 1200;
$bonus = 0;
$holidayRate = $hourlyRate * $rateMultiplier;
$holidayPay = $holidayRate * $hoursWorked;
$commission = $commissionRate * $grossSales;
$salary = $holidayPay + $commission;
if ($grossSales >= 1000) {
$bonus = 1000;
}
echo "Salary: $" . $salary . "\n";
echo "Bonus: $" . $bonus . "\n";
echo "Total Pay: $" . ($salary + $bonus) . "\n";
?>
Summary
In this guide, we learned:
- PHP syntax and structure
- Variables and data types
- Operators and calculations
- Conditional logic using if–else
- Writing real-world PHP programs
You now have a strong foundation in PHP basics.
In the next chapter, you’ll learn about arrays and loops, which will help you build more powerful applications.
🚀 Learn More with Cyber Gita
Visit: https://cybergita.in
Subscribe to Cyber Gita on YouTube for free live PHP classes and practical projects.
ddbet https://www.ddbetws.org
panalo99 https://www.uspanalo99.net