0% found this document useful (0 votes)
2 views2 pages

Module 3 Ex1

The document provides PHP code examples for checking if a number is positive, negative, or zero, implementing a login check, printing numbers from 1 to 100, and generating a multiplication table for any number. Each section includes logic, code, and explanations of how the code works. The explanations clarify the conditions and operations used in the PHP code.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views2 pages

Module 3 Ex1

The document provides PHP code examples for checking if a number is positive, negative, or zero, implementing a login check, printing numbers from 1 to 100, and generating a multiplication table for any number. Each section includes logic, code, and explanations of how the code works. The explanations clarify the conditions and operations used in the PHP code.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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

You might also like