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
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
- 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
Install and set up
- Install PHP 8.3+ (Windows: windows.php.net or XAMPP; Mac:
brew install php; Ubuntu:sudo apt install php-cli). - Install Composer from getcomposer.org.
- In your project folder:
composer require --dev phpunit/phpunit.
Step 1 — the 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$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
Write PHPUnit tests for format_inr: 12 lakh, a small number, one crore, and a negative amount.
<?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
- Start every file with
declare(strict_types=1);so wrong types fail fast. - Rules file: “PHP 8.3+, strict_types, PDO prepared statements, PHPUnit, PSR-12 style”.
- In
php.inifor development keepdisplay_errors=Onanderror_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
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.