What will the following PHP code output?$foo = 10;$bar = (boolean)$foo;echo $bar;PHP Parse errorPHP Parse error: syntax error01
Question
What will the following PHP code output?bar = (boolean)bar;PHP Parse errorPHP Parse error: syntax error01
Solution
The PHP code you provided will output "1". Here's the step by step explanation:
-
$foo = 10;This line is setting the variable$footo the integer10. -
$bar = (boolean)$foo;This line is casting the integer10to a boolean. In PHP, any integer that is not0is consideredtruewhen cast to a boolean. -
echo $bar;This line is outputting the value of$bar. Since$baris a booleantrue, andechooutputs booleans as1fortrueand0forfalse, this will output1.
Similar Questions
What will be the output of the following PHP code?< ?phpint $one = 1;echo "$one";?>01$oneError
What will be the output of the following PHP code?< ?phpecho $red ;?>0NothingTrueError
What will be the output of the following PHP code?< ?php$hello = "Hello World";$bye = "Bye";echo $hello;"$bye";?>
What will be the output of the following PHP code?<?php$x = 10;$y = 20;if ($x > $y && 1||1) print "PHP MCQ TEST!" ;else print "Welcome to Marwadi University!";?>Group of answer choicesWelcome to Marwadi University!errorPHP MCQ TEST!no output
What will this code display?<?php $a = 1; function Test() { echo "a = $a"; } Test();?>Question 10Select one:a.1b.3c.Warning or no valued.An error
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.