Consider this PHP code snippet. What will it output?
<?php $a = '5'; $b = 3; echo $a + $b; ?>
PHP automatically converts strings to numbers when using arithmetic operators.
PHP converts the string '5' to the integer 5 when adding to 3, so the output is 8.
What happens when you run this PHP code?
<?php $a = 'hello'; $b = 10; echo $a + $b; ?>
PHP 8+ is stricter about adding strings that cannot convert to numbers.
Adding a non-numeric string to an integer results in 0 + 10 = 10 in PHP 8+, but no TypeError is thrown.
Look at this PHP function and its output. Why does it return 'Result: 15' instead of 'Result: 510'?
<?php function add($x, $y) { return $x + $y; } echo 'Result: ' . add('5', '10'); ?>
Think about how PHP treats '+' with string operands.
The '+' operator in PHP performs numeric addition, so string numbers are converted to integers before adding.
Which option correctly enables strict typing and declares a function that only accepts integers?
Strict typing must be declared at the top of the file before any code.
Option C correctly places declare(strict_types=1); at the top before any code, enabling strict typing.
Given this PHP code, what will it output?
<?php declare(strict_types=1); function formatValue(int|string $value): string { return match(gettype($value)) { 'integer' => "Number: $value", 'string' => "Text: $value", default => 'Unknown', }; } echo formatValue(42) . ', ' . formatValue('hello'); ?>
Look at how match uses gettype to decide the output string.
The function returns 'Number: 42' for integer input and 'Text: hello' for string input, joined by a comma.