← All articles
How to Use Claude to Write and Fix PHP Code (and What to Double-Check)
Training and AI

How to Use Claude to Write and Fix PHP Code (and What to Double-Check)

By John Simiyu · 30 November 2026 · 7 min read

About the author

John Simiyu, known as Simiyu the Software Developer, is an experienced full-time full-stack software developer with proven, robust skills in web development using PHP, Bootstrap, JavaScript, HTML, MySQL and CSS, Python for automation, and Kotlin and Java for native Android development. He has over 5 years of experience building websites, business systems and mobile apps for clients, and he teaches AI-assisted coding in his PHP and MySQL Developer Training.

Why this matters

I use AI tools like Claude in my daily work, and I teach my students to do the same. Used well, they save hours: drafting a form, explaining a confusing error, suggesting a cleaner way to write a query.

But the quality of what you get depends heavily on how you ask and on what you check afterwards. In this guide I share the approach I use for PHP, including example prompts you can copy and a checklist of what to verify before you trust any code.

Part 1: Writing code with Claude

Start with a clear description

Claude works best when it knows exactly what you want. A good request includes:

  • - What you are building, for example a registration page
  • Your environment: PHP version, MySQL, whether you use PDO or mysqli
  • What the code must do, step by step
  • Any rules, such as validation or security requirements
  • The format you want, such as one file or separate files

Here is an example prompt:

"I am building a member registration page in PHP 8 with MySQL using PDO. The table is called members with columns id, full_name, email and created_at. Write a page with a form that asks for full name and email, validates both, rejects duplicate emails and saves the record using a prepared statement. Show friendly error messages and escape any output. Keep it in one file and explain what each part does."

That prompt gives the tool enough to produce something useful, and it asks for explanations at the same time.

Ask for small pieces

Instead of "build my whole system", request one feature at a time: the database table first, then the connection file, then registration, then login. Test each piece before asking for the next. If something breaks, you know exactly where to look.

Tell it your level

You can say: "I am a beginner, so please use simple code and explain each line." Or the opposite: "I am comfortable with PHP, so show the concise version." The answer changes to match.

Ask for the reasoning

Add phrases such as:

  • - "Explain why you chose this approach"
  • "What are the alternatives?"
  • "What could go wrong with this code?"

This turns the tool into a tutor instead of a code vending machine.

Part 2: Fixing code with Claude

What to include when something breaks

For debugging, give it everything it needs:

  • - The full error message, copied exactly
  • The code around the line mentioned
  • What you expected to happen
  • What actually happened
  • What you already tried

A real-style example

Suppose you see this on your page:

Warning: Undefined array key "email" in register.php on line 8

And line 8 reads:

PHP
$email = trim($_POST['email']);

A useful prompt would be:

"I get this error in PHP 8: Warning: Undefined array key "email" in register.php on line 8. The line is $email = trim($_POST['email']); and it appears when I first open the page, before submitting the form. What causes it and how should I fix it?"

A good answer will explain that the code runs even when the form has not been submitted, so $_POST is empty. The fix is to run that code only when the request method is POST, or to check that the key exists first:

PHP
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $email = trim($_POST['email'] ?? '');
}

You should still understand why this works. Ask: "Why does the original code fail when the page first loads?" If you cannot explain the fix in your own words, you have not learned from it yet.

Ask for a review of working code

You can also paste code that works and ask:

"Review this PHP code for security problems, bugs and bad practices. List the issues in order of importance and show the fixed version."

This is one of the most valuable uses for beginners, because it shows you what you did not know to look for.

Part 3: What to double-check every time

AI code can look professional and still contain problems. Before you rely on anything, go through these checks.

1. Security

  • - Are database queries using prepared statements, or is user input pasted into the SQL?
  • Are passwords stored with password_hash() and checked with password_verify()?
  • Is output escaped with htmlspecialchars() when displayed?
  • Are forms protected with CSRF tokens where needed?
  • Are file uploads restricted by type and size?
  • Are sessions handled safely, for example by regenerating the ID after login?
  • Are detailed errors hidden from visitors on the live site?

2. Validation

  • - Does the code check for empty fields, wrong formats and overly long input?
  • Does it handle unexpected input without crashing?
  • Does validation happen on the server, not only in the browser?

3. Correctness

  • - Does it do exactly what you asked, or something similar?
  • Does it work with an empty database, one record and many records?
  • Do the table and column names match your real database?
  • Are you using functions that your PHP version supports? AI sometimes suggests outdated methods.

4. Secrets and configuration

  • - Have you replaced placeholder passwords with real ones only in your own files?
  • Are database passwords and API keys kept out of public pages and public repositories?
  • Did you avoid pasting real keys or customer data into your prompts?

5. Money and sensitive data

If the code handles payments, loans, personal records or health data, do not trust it alone. Test it carefully and have an experienced developer review it. Mistakes in these systems cost real money and can expose real people.

A quick testing routine

  • - Run the happy path: enter correct data and confirm it works
  • Run the bad path: empty fields, wrong email format, very long text
  • Try to break it: submit the form twice, refresh after submitting, use special characters such as quotes
  • Check phpMyAdmin to confirm the data in the database is what you expect
  • Check the page source or your browser to confirm nothing sensitive is displayed

Good prompts to keep handy

  • - "Explain this code line by line for a beginner."
  • "What security problems does this code have?"
  • "Rewrite this using PDO prepared statements."
  • "What happens if the user submits an empty form?"
  • "Suggest a simpler way to write this."
  • "Give me test cases I should try on this feature."

Habits that make you a better developer

  • - Keep a copy of every working version before you try changes
  • Write down what you learn from each fix
  • Try to solve the problem yourself for a few minutes before asking
  • Use the tool to explain, not only to produce
  • Review your old code occasionally and improve it with what you have learned

Common mistakes to avoid

  • - Giving vague descriptions and accepting the first answer
  • Pasting code without reading it
  • Skipping the security review
  • Not testing with bad input
  • Sharing real passwords or customer data in prompts
  • Using AI-generated code in a payment system without expert review

Bonus points before you go

  • - Bonus 1: If a fix does not work after two tries, stop and describe the problem from scratch. A fresh, clearer description often solves it.
  • Bonus 2: Ask the tool to point out its own possible mistakes. It often catches things it missed the first time.
  • Bonus 3: The more you understand PHP, the better your prompts become. Learning and prompting improve each other.
  • Bonus 4: Never judge code only by how neat it looks. Run it, test it and read it.
  • Bonus 5: A developer who can verify AI code is worth far more than one who can only generate it.

Final thoughts

Claude and similar tools can make learning and building in PHP much faster, but speed is only valuable when the code is correct and safe. Ask clearly, build in small steps, understand what you receive and always run the checks above before you trust it.

If you want to learn this properly, my PHP and MySQL Developer Training teaches students to use AI as a coding partner while building a real, live project. You can see the available slots and book at https://johnsimiyuweb.site/training. For questions or help with a project, message me on WhatsApp at 0739334082 or email consult@johnsimiyuweb.site.

Next, read: From Zero to a Live Website: What My 5-Day PHP and MySQL Training Looks Like

Want to learn PHP & MySQL live with me? Book the training →

Comments (0)

No comments yet. Be the first to share your thoughts.

Leave a comment

Comments are reviewed before they appear. Links are not allowed.

Keep reading