Theory
वह String जो Add हो गई
Event portal seats की number को एक form से पढ़ता है। Form data हमेशा एक STRING की तरह आता है, तो $seats "350" hold करता है, quotes के साथ। फिर भी यह काम करता है:
echo "350" + 10; // prints 360
Java में यह एक type error होती; PHP में यह बस... काम करता है, क्योंकि PHP ने quietly string को एक number में बदल दिया। यह helpful-but-dangerous behaviour type juggling कहलाता है, और इसे समझना (और == बनाम === distinction जो यह बनाता है) इस lesson का heart है।
At a glance
PHP Data Types
| Type | Hold करता है | Example |
|---|---|---|
| string | Text | $e = "Garba Night"; |
| int | Whole numbers | $seats = 350; |
| float | Decimals | $fee = 49.5; |
| bool | true / false | $open = true; |
| array | एक ordered/keyed collection | $list = ["Riya", "Aman"]; |
| object | एक class का instance | $user = new User(); |
Theory
Type Juggling और Casting
PHP dynamically typed है: एक variable का type जो भी value यह hold करता है उसे follow करता है, और बदल सकता है।
Type juggling PHP का एक operation के दौरान AUTOMATICALLY types convert करना है: "5" + 3 string "5" को number 5 में juggle करता है, 8 देते हुए। Convenient, पर यह आपको surprise कर सकता है।
Type casting इसे EXPLICITLY करना है: (int)"5abc" 5 देता है (यह leading digits पढ़ता है), (int)"abc" 0 देता है, (float)"3.5" 3.5 देता है।
चूँकि form input हमेशा strings होती है, juggling यही वजह है form data पर arithmetic अक्सर बस काम करता है, और casting वह तरीका है जिससे आप conversion को deliberate और safe बनाते हैं।
Practical
Operators और == बनाम === Trap (हर एक Predict कीजिए)
<?php
echo 10 % 3; // 1 (modulus: remainder)
echo 2 ** 3; // 8 (exponent)
echo "5" + 3; // 8 (juggling: string to number)
$msg = "Seats: ";
$msg .= 350; // .= appends -> "Seats: 350"
echo $msg;
var_dump("5" == 5); // bool(true) loose: juggles, then compares
var_dump("5" === 5); // bool(false) strict: different types
?>
Theory
== Loose है, === Strict है
यह PHP में single सबसे important operator idea है:
- `==` (loose equality) compare करने से PEHLE types juggle करता है, तो
"5" == 5true है और0 == ""जैसे even odd cases आपको surprise कर सकते हैं - `===` (identical) value AND type compare करता है कोई juggling के बिना, तो
"5" === 5false है (string बनाम int)
चूँकि juggling unexpected trues produce कर सकता है, professional habit यह है कि जब आपको एक reliable comparison चाहिए `===` prefer कीजिए। यही != (loose) बनाम !== (strict) पर भी apply होता है। जब एक form की string को एक expected value से compare करते हैं, === आपको PHP की helpfulness backfire होने से protect करता है।
Quiz
var_dump("5" == 5); और var_dump("5" === 5); क्रमशः क्या print करते हैं?
- true और true
- true और false: == string को एक number में juggle करता है (equal), === type भी check करता है (string बनाम int, identical नहीं)
- false और false
- false और true
Show the answer
true और false: == string को एक number में juggle करता है (equal), === type भी check करता है (string बनाम int, identical नहीं)
== LOOSE है: यह "5" को number 5 में juggle करता है, तो "5" == 5 true है। === STRICT है: यह value AND type compare करता है, और एक string एक int नहीं है, तो "5" === 5 false है। यही THE PHP comparison distinction है, और === prefer करना loose juggling से होने वाली surprises से बचाता है। Option A भूलता है === type check करता है। Option C भूलता है == juggle करता है। Option D दोनों reverse करता है। याद रखिए: == पूछता है 'juggling के बाद equal?', === पूछता है 'same value और same type?'।
Think first
Form Validation === क्यों Prefer करती है?
Portal check करता है कि एक submitted role string "admin" के equal है या नहीं। यहाँ == इस्तेमाल करना risky क्यों हो सकता है, और === safer? Juggling के बारे में reason कीजिए, फिर tap कीजिए।
Show the answer
== के साथ, PHP compare करने से पहले types juggle करता है, जो edge cases में unexpected trues produce कर सकता है (उदाहरण के लिए, 0 या empty values वाले comparisons loose rules के under surprisingly behave करते हैं)। 'क्या यह user एक admin है?' जैसे एक security-sensitive check के लिए, एक unexpected true एक disaster है। === कोई juggling के बिना value AND type compare करता है, तो यह सिर्फ़ तब true return करता है जब input EXACTLY string "admin" हो: कोई surprises नहीं। Rule, especially validation और authorization के लिए: === (और !==) इस्तेमाल कीजिए ताकि PHP की automatic type conversion एक wrong match में backfire न हो सके। Loose equality एक convenience है जिसे आप वहाँ switch off करते हैं जहाँ correctness matter करता है।
Watch out
Type और Operator Traps
== Surprises: loose equality types juggle करता है; reliable comparisons के लिए === prefer कीजिए।
.= बनाम +=: .= एक STRING में append करता है; += NUMBERS add करता है: इन्हें mix करना data corrupt करता है।
Non-Numeric Strings Casting करना: (int)"abc" 0 है, error नहीं: trust करने से पहले validate कीजिए।
Form Data को Numeric Assume करना: $_GET/$_POST values STRINGS हैं; math से पहले cast या validate कीजिए।
/ हमेशा एक Result देता है: 10 / 3 एक float 3.333... है; whole-number needs के लिए intdiv() या % इस्तेमाल कीजिए।
Theory
Types हाथ में, अब Logic
अब आप portal को चाहिए वाली हर kind के data को store और compute कर सकते हैं, और आप juggling समझते हैं जो PHP को convenient AND कभी-कभी tricky बनाती है। अगला portal को DECISIONS लेने और REPEAT करने (if/switch, loops) और, crucially, LISTS hold करने की ज़रूरत है: registration list एक ARRAY है। Control structures और arrays, PHP के powerful array functions के साथ, अगले हैं।
Summary
Key takeaways
- PHP types: string, int, float, bool, array, object (और null); dynamically typed (type value follow करता है)।
- Type juggling operations में types auto-convert करता है: "5" + 3 = 8; type casting ((int)"5abc" = 5) explicit है।
- Operators: arithmetic (+ - / % *), assignment (=, +=, .= strings append करता है), comparison, logical (&& || !)।
- == loose है (types juggle करता है, तो "5" == 5 true है); === strict है (value AND type, तो "5" === 5 false है)।
- Reliable comparisons के लिए, especially validation और authorization में, === (और !==) prefer कीजिए।
- Form data strings की तरह आता है: math से पहले cast या validate कीजिए।
- Memory hook: == juggle करता है, === नहीं करता।