PHP Common Mistakes और Best Practices
In this page:
Loose बनाम Strict Equality
== compare करने से पहले चुपचाप operand types convert कर देता है, जो पुराने PHP versions में 0 == abc के true evaluate होने जैसे surprising results produce करता है; === value और type दोनों compare करता है, bugs की उस पूरी class से बचते हुए।
उदाहरण: Loose vs Strict Equality
<?php
// Print a detailed dump (with types) of `"0" == "abc"`
var_dump("0" == "abc");
// Print a detailed dump (with types) of `"0" === "abc"`
var_dump("0" === "abc");
?>
Login to try C/C++/Java/PHP code in the editor
Uninitialized Array Keys
एक ऐसी array key पढ़ना जो कभी set ही नहीं हुई एक warning trigger करती है (या strict contexts में एक error); पहले isset() या array_key_exists() check करना warning और किसी भी undefined value पर भरोसा करने वाली downstream logic दोनों से बचाता है।
उदाहरण: Uninitialized Array Keys
<?php
// Declare `$user` as an array: `["name" => "Alice"]`
$user = ["name" => "Alice"];
// Check whether `isset($user['email'])`
if (isset($user['email'])) {
// Print `$user['email']` to the output
echo $user['email'];
// Otherwise, run this branch
} else {
// Print "Email key not set" to the output
echo "Email key not set";
}
?>
Login to try C/C++/Java/PHP code in the editor
SQL Injection Vulnerabilities
user input को सीधे एक SQL string में concatenate करना injection attacks का दरवाज़ा खोलता है जो आपका पूरा database पढ़ या destroy कर सकते हैं; prepared statements data को query structure से अलग रखकर risk खत्म कर देते हैं।
उदाहरण: SQL Injection Vulnerabilities
<?php
// Create a new `PDO` instance with 'sqlite::memory:', stored in `$pdo`
$pdo = new PDO('sqlite::memory:');
$pdo->exec("CREATE TABLE users (id INTEGER, name TEXT)");
$name = "Robert'); DROP TABLE users;";
// Declare `$stmt`, set to `$pdo->prepare("INSERT INTO users (name) VALUES (?)")`
$stmt = $pdo->prepare("INSERT INTO users (name) VALUES (?)");
$stmt->execute([$name]);
// Print "Safe -- prepared statement kept structure and data separate" to the output
echo "Safe -- prepared statement kept structure and data separate";
?>
Login to try C/C++/Java/PHP code in the editor
Variable Scope Leaks
global variables को overuse करना यह जानना मुश्किल बना देता है कि एक function actually किस पर depend करता है या क्या बदल सकता है, क्योंकि इसका behavior पूरी तरह इसके parameters से describe नहीं होता -- state को local और explicitly pass रखना उस hidden coupling से बचाता है।
उदाहरण: Variable Scope Leaks
<?php
$counter = 0;
function increment() {
global $counter;
$counter++;
}
increment();
echo $counter;
// Depending on a global hides this function's real dependency
?>
Login to try C/C++/Java/PHP code in the editor
Best Practices Checklist
एक solid production checklist -- हर input validate करना, errors consistently handle करना, और error display बंद करना -- real users तक पहुँचने से पहले ज़्यादातर security और reliability issues पकड़ लेती है।
उदाहरण: Best Practices Checklist
<?php
// Declare `$email`, set to `$_POST['email'] ?? ''`
$email = $_POST['email'] ?? '';
// Check whether `!filter_var($email, FILTER_VALIDATE_EMAIL)`
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
// Print "Invalid email" to the output
echo "Invalid email";
}
// Call `ini_set('display_errors', '0')`
ini_set('display_errors', '0');
?>
Login to try C/C++/Java/PHP code in the editor
===की ज़रूरत होने पर==इस्तेमाल करना, ताकि"0" == falseऔर similar loose comparisons bugs का कारण बनें।issetcheck किए बिना या??इस्तेमाल किए बिना एक array key पढ़ना, जो एक warning raise करता है।- prepared statements इस्तेमाल करने के बजाय user input concatenate करके SQL बनाना।
- PHPUnit, Xdebug, और PSR standards जैसे tools code test, debug, और style करने में मदद करते हैं।
- Git integration, deployment और hosting, और command line development workflows support करते हैं।
- WebSockets, email sending, और image processing यह बढ़ाते हैं कि PHP क्या कर सकता है, और best practices common mistakes से बचने में मदद करती हैं।
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: