The if statement is used to execute a block of code only if a specified condition is true.
The if...else statement executes one block of code if a condition is true, and another if it is false.
The if...elseif...else statement allows you to test multiple conditions, executing different blocks of code depending on which condition is true.
You can nest if statements within each other to create more complex decision-making structures.
<?php
$age = 20;
if ($age >= 18) {
echo "You are eligible to vote.";
} else {
echo "You are not eligible to vote.";
}
?>
In this example, the code checks if the $age
variable is greater than or equal to 18. If true, it outputs that the person is eligible to vote; otherwise, it states they are not.
Console Output:
You are eligible to vote.
The elseif keyword allows you to check multiple conditions in sequence, executing different blocks of code based on which condition is true.
<?php
$score = 85;
if ($score >= 90) {
echo "Grade: A";
} elseif ($score >= 80) {
echo "Grade: B";
} elseif ($score >= 70) {
echo "Grade: C";
} else {
echo "Grade: D";
}
?>
This script evaluates a student's score and assigns a grade based on the value. It uses elseif
to handle multiple grading criteria efficiently.
Console Output:
Grade: B
Nested if statements allow you to place an if statement inside another if statement to test multiple conditions.
<?php
$day = "Monday";
$time = 10;
if ($day == "Monday") {
if ($time < 12) {
echo "Good morning!";
} else {
echo "Good afternoon!";
}
} else {
echo "Have a nice day!";
}
?>
In this example, the code first checks if the day is Monday. If true, it further checks the time to greet accordingly. Otherwise, it wishes a nice day.
Console Output:
Good morning!
Logical operators like AND (&&
) and OR (||
) can be used to combine multiple conditions in an if statement.
<?php
$age = 25;
$hasLicense = true;
if ($age >= 18 && $hasLicense) {
echo "You can drive.";
} else {
echo "You cannot drive.";
}
?>
This example checks if a person is old enough and has a license to drive using the &&
logical operator, ensuring both conditions are met.
Console Output:
You can drive.
For multiple conditions that check the same variable, a switch statement can sometimes be more readable than multiple if...elseif statements.
<?php
$fruit = "apple";
switch ($fruit) {
case "apple":
echo "Apples are red.";
break;
case "banana":
echo "Bananas are yellow.";
break;
default:
echo "Unknown fruit.";
}
?>
This switch statement checks the value of $fruit
and outputs a message based on its value, demonstrating a cleaner alternative to multiple if statements.
Console Output:
Apples are red.
The ternary operator (?:
) is a shorthand for simple if...else statements, useful for concise conditions.
<?php
$isMember = true;
$message = $isMember ? "Welcome back!" : "Please sign up.";
echo $message;
?>
This example uses the ternary operator to decide the welcome message based on membership status, offering a succinct alternative to traditional if...else.
Console Output:
Welcome back!
Complex conditions involving multiple variables and logical operators can be managed with nested and combined if statements.
<?php
$userRole = "admin";
$isActive = true;
if ($userRole == "admin" && $isActive) {
echo "Admin access granted.";
} elseif ($userRole == "editor" && $isActive) {
echo "Editor access granted.";
} else {
echo "Access denied.";
}
?>
This example checks the user's role and activity status to determine access permissions, demonstrating the use of logical operators in complex conditions.
Console Output:
Admin access granted.
When using if...else...elseif, ensure conditions are mutually exclusive and ordered logically to avoid unnecessary checks and improve readability.
<?php
$temperature = 30;
if ($temperature >= 30) {
echo "It's hot!";
} elseif ($temperature >= 20) {
echo "It's warm.";
} elseif ($temperature >= 10) {
echo "It's cool.";
} else {
echo "It's cold.";
}
?>
This example demonstrates ordering conditions logically to provide clear and efficient temperature feedback, avoiding unnecessary checks.
Console Output:
It's hot!
Newsletter
Subscribe to our newsletter for weekly updates and promotions.
Wiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterCompany
About usCareersPressCompany
About usCareersPressCompany
About usCareersPressLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesCompany
About usCareersPressCompany
About usCareersPressCompany
About usCareersPressLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesAds Policies