𝗧𝗼𝗽 𝟭𝟬 𝗣𝗛𝗣 𝗕𝘂𝗴𝘀 𝗘𝘃𝗲𝗿𝘆 𝗕𝗲𝗴𝗶𝗻𝗻𝗲𝗿 𝗠𝗮𝗸𝗲𝘀

You write code. You refresh the page. You see a blank white screen.

This happens because PHP fails silently by default. If you do not see errors, you cannot fix them.

Start your development by adding these lines to the top of your file:

ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL);

This forces PHP to show you what is wrong. Do not use this in production.

Here are 10 common bugs and how to fix them.

  1. Missing Semicolons PHP needs a semicolon to end a statement. If you miss one, the script stops. Fix: Always end your lines with a semicolon.

  2. Loose Comparison The == operator changes types before comparing. This leads to wrong results. Fix: Use ===. This checks both the value and the type.

  3. Undefined Variables Using a variable you did not create causes logic errors. Fix: Use the null coalescing operator. $name = $_GET['name'] ?? 'Guest';

  4. Global Variable Scope Functions cannot see variables outside of them. Fix: Pass variables into functions as parameters.

  5. SQL Injection Putting user input directly into a query is dangerous. Attackers can steal your data. Fix: Use prepared statements with placeholders.

  6. Cross-Site Scripting (XSS) Printing raw user input to the screen allows malicious scripts to run in browsers. Fix: Use htmlspecialchars() on all output.

  7. Header Errors You cannot send a header after you send any text or spaces to the browser. Fix: Put header() calls at the very top. Always use exit() after a redirect.

  8. Missing Form Keys Accessing a form key that does not exist causes errors. Fix: Use the null coalescing operator to provide a default value.

  9. Include vs Require The include command only gives a warning if a file is missing. The script keeps running and crashes later. Fix: Use require_once for critical files like database connections.

  10. Unchecked Return Values Many PHP functions return false if they fail. Using that false value in the next step causes a crash. Fix: Check if the function returned false before you use the result.

Summary for better code: • Turn on error reporting in development. • Use === instead of ==. • Check return values. • Sanitize all user input.

Source: https://dev.to/bikkisingh/top-10-php-bugs-every-beginner-makes-and-how-to-fix-them-1anh