Skip to content

PHP Behaviour Across Versions With Runnable Examples

These three scripts show a change you can spot when updating PHP. Each script runs on both sides of the change. Predict the output, then run the script with your installed PHP version.

Download all three source files (ZIP)

Open the extracted php-behaviour-examples folder in your terminal. Use php -v to see which runtime you are testing. The older version bands describe what to expect from historical runtimes. The PHP support schedule lists releases you can use for a current project.

<?php
var_dump(0 == 'pineapple');
var_dump(0 === 'pineapple');
RuntimeFirst lineSecond line
PHP 7.4bool(true)bool(false)
PHP 8.0 and laterbool(false)bool(false)

Run php loose-comparison.php or open the loose comparison source. In PHP 8, loose comparison of a number and a non-numeric string compares their string forms. Strict comparison checks the types and values. Try replacing 'pineapple' with '0'. The loose result is true in both versions because '0' is numeric. PHP 8.0 migration guide

<?php
echo number_format(1764.25, -2), PHP_EOL;
RuntimeOutput
PHP 7.4 to 8.21,764
PHP 8.3 and later1,800

Run php round-before-decimal.php or open the rounding source. From PHP 8.3, negative precision tells number_format how many places to round before the decimal point. Earlier versions treated it as zero. If you maintain code that runs on both, use number_format(round(1764.25, -2)) to make the rounding explicit and get 1,800 from either. number_format in the PHP manual

<?php
try {
new DateTimeImmutable('this is not a date');
} catch (Exception $error) {
echo get_class($error), PHP_EOL;
}
RuntimeOutput
PHP 7.4 to 8.2Exception
PHP 8.3 and laterDateMalformedStringException

Run php invalid-date.php or open the date source. A catch for Exception handles either. If your project requires PHP 8.3 or later, catch DateMalformedStringException specifically where you want to ask a reader for a different date. Try replacing the input with 2026-11-20 to run the success path. DateTimeImmutable constructor changelog

For more on updating an existing project, see the PHP 8.6 upgrade checklist.