Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What should be passed into if() to print 'Hello World'?

Tags:

php

logic

What should be passed into the if() to print the output as "Hello World"? [Note: It should execute the else block.]

if(?){

} else {
   echo "World";
}
like image 515
Mahavir Munot Avatar asked May 03 '12 10:05

Mahavir Munot


2 Answers

I needs to evaluate to false, and print "Hello" at the same time. printf returns the length of outputted string upon success which is evaluated to true when read in a Boolean context. So reversing that will evaluate to false, executing the else block.

if(!printf("Hello ")){

} else {
   echo "World";
}
like image 78
Madara's Ghost Avatar answered Sep 26 '22 00:09

Madara's Ghost


!printf("Hello ")

By default, printf in 'C' returns true.

like image 26
Anirudh Avatar answered Sep 25 '22 00:09

Anirudh