PHP if Statement
In this page:
if (condition) {
// runs only when condition is true
}
Basic if Statement
एक if statement अपना block सिर्फ तभी चलाता है जब parentheses के अंदर की condition true evaluate हो; अगर यह false है, PHP पूरे block को skip कर देता है और सीधे इसके बाद जो भी है वहाँ चला जाता है — कोई error नहीं, कोई output नहीं, बस चुपचाप execution continue।
उदाहरण: Basic if Statement
<?php
// Declare `$age`, set to `20`
$age = 20;
// Check whether `$age >= 18`
if ($age >= 18) {
// Print "You are an adult." to the output
echo "You are an adult.";
}
?>
Login to try C/C++/Java/PHP code in the editor
Conditions के अंदर Comparison
=== बिना किसी automatic conversion के value और type दोनों compare करता है, जो इसे किसी if condition के अंदर safer default बनाता है — if ($status === active) गलती से 1 या "1" से match नहीं करेगा जैसे looser == operator कभी-कभी कर सकता है।
उदाहरण: Comparison inside Conditions
<?php
// Declare `$status`, set to "active"
$status = "active";
// Check whether `$status === 'active'`
if ($status === 'active') {
// Print "Status is active." to the output
echo "Status is active.";
}
?>
Login to try C/C++/Java/PHP code in the editor
Conditions में Logical AND (&&)
किसी condition के अंदर && को block चलने से पहले हर जुड़े हुए expression का true होना चाहिए — if ($age >= 18 && $hasConsent) सिर्फ तभी execute होता है जब दोनों checks pass हों, जो एक साथ कई independent requirements enforce करने के लिए exactly सही pattern है।
उदाहरण: Logical AND (&&) in Conditions
<?php
// Declare `$age`, set to `20`
$age = 20;
// Declare `$hasConsent`, set to `true`
$hasConsent = true;
// Check whether `$age >= 18 && $hasConsent`
if ($age >= 18 && $hasConsent) {
// Print "Access granted." to the output
echo "Access granted.";
}
?>
Login to try C/C++/Java/PHP code in the editor
Conditions में Logical OR (||)
किसी condition के अंदर || block को चलने देता है अगर जुड़े हुए expressions में से *कोई एक* भी true हो, जो same outcome तक पहुँचने के कई acceptable paths वाली situations में suit करता है — जैसे एक username या email address दोनों से login accept करना।
उदाहरण: Logical OR (||) in Conditions
<?php
// Declare `$username`, set to ""
$username = "";
// Declare `$email`, set to "[email protected]"
$email = "[email protected]";
// Check whether `$username || $email`
if ($username || $email) {
// Print "Login accepted." to the output
echo "Login accepted.";
}
?>
Login to try C/C++/Java/PHP code in the editor
Nested if Statements
एक if को दूसरे के अंदर रखना आपको एक दूसरी condition सिर्फ पहले वाली pass होने के बाद check करने देता है — यह तब उपयोगी है जब दूसरा check genuinely सिर्फ उस context में मायने रखता हो (मान लीजिए, किसी file के contents check करना सिर्फ यह confirm करने के बाद कि file exist ही करती है)।
उदाहरण: Nested if Statements
<?php
// Declare `$fileExists`, set to `true`
$fileExists = true;
// Check whether `$fileExists`
if ($fileExists) {
// Declare `$content`, set to "sample data"
$content = "sample data";
// Check whether `!empty($content)`
if (!empty($content)) {
// Print "File exists and has content." to the output
echo "File exists and has content.";
}
}
?>
Login to try C/C++/Java/PHP code in the editor
if ($x == 5)के बजायif ($x = 5)(assignment) लिखना, जो हमेशा block चलाता है।- condition के बाद एक semicolon लगाना,
if ($x > 5);, ताकि following block हमेशा execute हो। - यह उम्मीद करना कि
if ($str)"0"के लिए true होगा, जबकि PHP में"0"falsy है।
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first: