𝗧𝗼𝗽 𝟭𝟬 𝗣𝗛𝗣 𝗕𝘂𝗴𝘀 𝗘𝘃𝗲𝗿𝘆 𝗕𝗲𝗴𝗶𝗻𝗻𝗲𝗿 𝗠𝗮𝗸𝗲𝘀
You write code. You hit refresh. You see a blank white screen.
This happens because PHP fails silently by default. You must force it to show errors during development.
Add these lines to the top of your files:
ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL);
Do not use this in production. Use file logs instead.
Here are 10 common mistakes and how to fix them:
Missing Semicolons PHP stops working if you forget a semicolon. It often causes a silent crash. Fix: Ensure every statement ends with a semicolon.
Loose Comparison (== vs ===) The double equals sign performs type coercion. This leads to logic errors. Fix: Use triple equals (===). It checks both value and type.
Undefined Variables Using a variable before you assign it breaks your logic. Fix: Use the null coalescing operator. Example: $name = $_GET['name'] ?? 'Guest';
Global Variable Scope Functions cannot see variables outside of them. Fix: Pass variables into functions as parameters.
SQL Injection Building queries with user input allows hackers to steal data. Fix: Use prepared statements with placeholders (?).
Cross-Site Scripting (XSS) Printing user input directly allows malicious scripts to run in browsers. Fix: Wrap output in htmlspecialchars().
Headers Already Sent You cannot redirect a user if you already sent text to the browser. Even a single space causes this error. Fix: Put header() calls at the very top. Always use exit() after a redirect.
Undefined Array Keys Accessing a form key that does not exist causes warnings. Fix: Use the null coalescing operator for all $_POST and $_GET data.
Include vs Require Include only shows a warning if a file is missing. The script keeps running and crashes later. Fix: Use require_once for critical files like database connections.
Unchecked Return Values Many PHP functions return false on failure. If you use that result immediately, you get more errors. Fix: Check if the result is false before using it.
Summary for success:
- Turn on error reporting in development.
- Use === for all comparisons.
- Sanitize all user input.
- Check return values.
Source: https://dev.to/bikkisingh/top-10-php-bugs-every-beginner-makes-and-how-to-fix-them-1anh