$y = 20;
echo $x + $y;
?> | PHP | MYTAT"> $y = 20;
echo $x + $y;
?> | PHP | MYTAT">
Q
What does the following PHP code output?

<?php
$x = '10';
$y = 20;
echo $x + $y;
?>

Answer & Solution

Answer: Option A
Solution:
In PHP, if a string is used in a numeric operation, it will be automatically converted to a number if possible. Therefore, the code will output 30, as $x will be converted to the integer 10, and then added to $y, resulting in 30.
Related Questions on Average

Which of the following PHP functions is used to find the length of an array?

A). count()

B). length()

C). size()

D). sizeof()

What is the correct way to declare a variable in PHP?

A). $name = 'John';

B). name = 'John';

C). var name = 'John';

D). String name = 'John';

What will be the value of the variable $result after executing the following PHP code?

<?php
$x = 5;
$result = ($x > 10) ? 'Yes' : 'No';
?>

A). Yes

B). No

C). True

D). False

Which of the following PHP operators is used for concatenation of strings?

A). .

B). +

C). &&

D). *

Which of the following PHP functions is used to convert a string to uppercase?

A). strtoupper()

B). ucwords()

C). strtolower()

D). ucfirst()

What will be the output of the following PHP code?

<?php
$x = 5;
echo $x--;
echo $x;
?>

A). 5 4

B). 5 5

C). 4 4

D). 4 5

Which of the following statements is true about PHP comments?

A). Comments are ignored by the PHP interpreter

B). Comments can only be single-line

C). Comments must start with /* and end with */

D). Comments are displayed on the webpage

What will be the output of the following PHP code?

<?php
$x = 10;
$y = 5;
echo $x + $y;
?>

A). 15

B). 5

C). 10

D). Error

What is the value of $x after the following PHP code is executed?

<?php
$x = 5;
$x += 3;
echo $x;
?>

A). 5

B). 8

C). 15

D). 3

Which of the following is a correct way to declare an array in PHP?

A). $colors = array('Red', 'Green', 'Blue');

B). $colors = 'Red, Green, Blue';

C). $colors = {'Red', 'Green', 'Blue'};

D). $colors = [Red, Green, Blue];