← Back to PHP Course | Chapter 16: Testing & Tools | Lesson 10 of 10

PHP Common Mistakes और Best Practices

यह lesson beginners अक्सर जो slips करते हैं और उन्हें avoid करने वाली अच्छी habits इकट्ठा करता है, किसी बड़े test से पहले tips की एक list जैसा। इन्हें follow करना आपके PHP code को safe और tidy रखता है।

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
<?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");
?>

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
<?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";
}
?>

SQL Injection Vulnerabilities

user input को सीधे एक SQL string में concatenate करना injection attacks का दरवाज़ा खोलता है जो आपका पूरा database पढ़ या destroy कर सकते हैं; prepared statements data को query structure से अलग रखकर risk खत्म कर देते हैं।

उदाहरण: SQL Injection Vulnerabilities

php
<?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";
?>

Variable Scope Leaks

global variables को overuse करना यह जानना मुश्किल बना देता है कि एक function actually किस पर depend करता है या क्या बदल सकता है, क्योंकि इसका behavior पूरी तरह इसके parameters से describe नहीं होता -- state को local और explicitly pass रखना उस hidden coupling से बचाता है।

उदाहरण: Variable Scope Leaks

php
<?php
$counter = 0;
function increment() {
    global $counter;
    $counter++;
}
increment();
echo $counter;
// Depending on a global hides this function's real dependency
?>

Best Practices Checklist

एक solid production checklist -- हर input validate करना, errors consistently handle करना, और error display बंद करना -- real users तक पहुँचने से पहले ज़्यादातर security और reliability issues पकड़ लेती है।

उदाहरण: Best Practices Checklist

php
<?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');
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. === की ज़रूरत होने पर == इस्तेमाल करना, ताकि "0" == false और similar loose comparisons bugs का कारण बनें।
  2. isset check किए बिना या ?? इस्तेमाल किए बिना एक array key पढ़ना, जो एक warning raise करता है।
  3. 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 से बचने में मदद करती हैं।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.