3v4l.org

run code in 300+ PHP versions simultaneously
<?php function build_external_url(string $host, string $path = null, array $query = [], string $schema = null, int $port = null): string { $url = $host; if (null !== $port) { $url .= ':' . $port; } if (null !== $path) { $url .= '/' . ltrim($path, '/'); } if (! empty($query)) { $url .= '?' . http_build_query($query); } return (null === $schema ? $url : ($schema . '://' . $url)); } $tests = [ [ 'foo.co' ], [ 'foo.co', '/bar' ], [ 'foo.co', 'bar', [ 'x' => 'baz' ]], [ 'foo.co', 'bar', [], 'ftp' ], [ 'foo.co', 'bar', ['email' => 'me@foo.co'], 'ftp', '24225'], ]; foreach ($tests as $args) { echo build_external_url(...$args) . PHP_EOL; }
Output for 8.4.1 - 8.4.12
Deprecated: build_external_url(): Implicitly marking parameter $path as nullable is deprecated, the explicit nullable type must be used instead in /in/IT2fo on line 3 Deprecated: build_external_url(): Implicitly marking parameter $schema as nullable is deprecated, the explicit nullable type must be used instead in /in/IT2fo on line 3 Deprecated: build_external_url(): Implicitly marking parameter $port as nullable is deprecated, the explicit nullable type must be used instead in /in/IT2fo on line 3 foo.co foo.co/bar foo.co/bar?x=baz ftp://foo.co/bar ftp://foo.co:24225/bar?email=me%40foo.co
Output for 7.1.0 - 7.1.33, 7.2.0 - 7.2.34, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.25
foo.co foo.co/bar foo.co/bar?x=baz ftp://foo.co/bar ftp://foo.co:24225/bar?email=me%40foo.co

preferences:
100.69 ms | 409 KiB | 5 Q