Check Positive or Negative Number
Logic
• If number > 0 → Positive
• If number < 0 → Negative
• If number = 0 → Zero
PHP Code
<?php
$number = -5;
if ($number > 0) {
echo "Number is Positive";
} elseif ($number < 0) {
echo "Number is Negative";
} else {
echo "Number is Zero";
}
?>
Explanation
• PHP checks conditions top to bottom
• Only one condition executes
Create Login Check using if–else
Logic
• Compare entered username & password
• If match → Login successful
• Else → Invalid credentials
PHP Code
<?php
$username = "admin";
$password = "12345";
if ($username == "admin" && $password == "12345") {
echo "Login Successful";
} else {
echo "Invalid Username or Password";
}
?>
Explanation
• == compares values
• && means both conditions must be true
Print Numbers from 1 to 100
Logic
• Start from 1
• Stop at 100
• Increase by 1
PHP Code (using for loop)
<?php
for ($i = 1; $i <= 100; $i++) {
echo $i . "<br>";
}
?>
Explanation
• $i = 1 → starting point
• $i <= 100 → condition
• $i++ → increment
Generate Table for Any Number
Logic
• Multiply number from 1 to 10
PHP Code
<?php
$num = 7;
for ($i = 1; $i <= 10; $i++) {
echo $num . " x " . $i . " = " . ($num * $i) . "<br>";
}
?>
Explanation
• Loop runs 10 times
• ($num * $i) performs multiplication
• . joins text and result