Ravindra BagaleCourses & study guides Track your progress
AI CODING ASSISTANTS · CHAPTER 3 · LANGUAGES

How to Code in PHP Using AI

Use an AI assistant to write a PHP function, run it from the command line, and prove it with PHPUnit tests. Works in VS Code, PhpStorm or any editor.

In short: say the PHP version and strict_types, give exact examples, ask for PHPUnit tests, run vendor/bin/phpunit.

Why PHP with AI

WHY

PHP powers WordPress, Laravel apps and many Indian business websites and portals. A lot of old PHP code is online, so AI often writes old-style PHP. A clear prompt keeps it modern and safe.

What you need

WHAT
  • PHP 8.3 or newer: check with php -v.
  • Composer: PHP's package manager, from getcomposer.org.
  • PHPUnit: the standard PHP test tool: composer require --dev phpunit/phpunit.
  • IDE + assistant: VS Code with Copilot or Claude Code, or PhpStorm with Copilot.

How it flows

1 · Describe the function PHP 8, strict types, examples 2 · AI writes code read every line 3 · php file.php check output 4 · Ask for PHPUnit tests edge cases 5 · vendor/bin/phpunit OK = keep

Install and set up

HOW
  1. Install PHP 8.3+ (Windows: windows.php.net or XAMPP; Mac: brew install php; Ubuntu: sudo apt install php-cli).
  2. Install Composer from getcomposer.org.
  3. In your project folder: composer require --dev phpunit/phpunit.

Step 1 — the prompt

COPY THIS PROMPT

Write a PHP 8 function format_inr(float $amount): string with strict_types that formats money the Indian way (lakh and crore commas), 2 decimals, with the ₹ sign, and a minus sign before ₹ for negatives. Example: 1234567.5 → "₹12,34,567.50". When the file is run from the command line, print that example. Only return the code.

Step 2 — the code (tested here)

inr.php
<?php
// Format a number the Indian way (lakh, crore). Built with an AI assistant, then tested.
declare(strict_types=1);

function format_inr(float $amount): string
{
    $sign = $amount < 0 ? '-' : '';
    $parts = explode('.', number_format(abs($amount), 2, '.', ''));
    $int = $parts[0];
    $last3 = substr($int, -3);
    $rest = substr($int, 0, -3);
    if ($rest !== '' && $rest !== false) {
        $rest = preg_replace('/\B(?=(\d{2})+(?!\d))/', ',', $rest);
        $last3 = $rest . ',' . $last3;
    }
    return $sign . '₹' . $last3 . '.' . $parts[1];
}

if (PHP_SAPI === 'cli' && realpath($_SERVER['argv'][0] ?? '') === __FILE__) {
    echo format_inr(1234567.5), PHP_EOL;   // ₹12,34,567.50
}
php inr.php
# -> ₹12,34,567.50
Real catch while writing this: the first AI version used $argv[0] to check if the file was run directly. When PHPUnit loaded the file, $argv was not defined there, and PHP printed warnings. The fix: $_SERVER['argv'][0] ?? ''. The tests found it, not our eyes.

Step 3 — ask for PHPUnit tests, then run them

FOLLOW-UP PROMPT

Write PHPUnit tests for format_inr: 12 lakh, a small number, one crore, and a negative amount.

InrTest.php
<?php
declare(strict_types=1);
use PHPUnit\Framework\TestCase;
require_once __DIR__ . '/inr.php';

final class InrTest extends TestCase
{
    public function testLakh(): void  { $this->assertSame('₹12,34,567.50', format_inr(1234567.5)); }
    public function testSmall(): void { $this->assertSame('₹999.00', format_inr(999)); }
    public function testCrore(): void { $this->assertSame('₹1,00,00,000.00', format_inr(10000000)); }
    public function testNegative(): void { $this->assertSame('-₹1,500.00', format_inr(-1500)); }
}
vendor/bin/phpunit InrTest.php
# -> OK (4 tests, 4 assertions)

Settings and config

  1. Start every file with declare(strict_types=1); so wrong types fail fast.
  2. Rules file: “PHP 8.3+, strict_types, PDO prepared statements, PHPUnit, PSR-12 style”.
  3. In php.ini for development keep display_errors=On and error_reporting=E_ALL, so warnings like the one above show up.

Watch out (common AI mistakes in PHP)

  • SQL injection — never build SQL by joining strings; ask for PDO prepared statements.
  • Old mysql_* functions — removed in PHP 7; use PDO or mysqli.
  • No output escaping — use htmlspecialchars() when printing user input.
  • Made-up Composer packages — check on packagist.org.

Quick quiz

1. Which tool runs PHP unit tests?

PHPUnit (vendor/bin/phpunit).

2. What does format_inr(1234567.5) return?

₹12,34,567.50.

3. How do you stop SQL injection in PHP?

Use PDO prepared statements, never string-joined SQL.

4. What bug did the tests catch in this chapter?

Using $argv when the file was loaded by PHPUnit caused warnings.

Save tokens and money

SMART AI USE

Paste one function, not the vendor/ folder. Turn on full error reporting locally so PHP tells you the problem for free, then send the AI only that warning line.

Save these cards

INTERVIEW CARDAI Coding

How do you use AI safely for PHP?

PHP 8 + strict_types + examples in the prompt.
PDO prepared statements; prove with PHPUnit.
Ravindra Bagale · ravindrabagale.com/ai-coding
COMPARISON CARDAI Coding

Old PHP vs modern PHP

Old: mysql_*, no types, string SQL.
Modern: PDO, strict_types, typed functions, PHPUnit.
Ravindra Bagale · ravindrabagale.com/ai-coding
REVISION CARDAI Coding

PHP with AI in 3

strict_types on.
Indian format: lakh/crore commas.
phpunit OK = proof.
Ravindra Bagale · ravindrabagale.com/ai-coding