-2

How it will print hello - If hello is true, then the function must print hello, but if the function doesn’t receive hello or hello is false the function must print bye.

<?php
function showMessage($hello=false){
  echo ($hello)?'hello':'bye';
}
?>
  • 1
    And what is the problem with this function? – u_mulder Jul 09 '17 at 11:43
  • 1
    It is a shorthand if/else, see here: https://stackoverflow.com/questions/1506527/how-do-i-use-shorthand-if-else – chrki Jul 09 '17 at 11:44
  • Problem is whenever i am calling this function like showMessage("abc") , then also it is printing "hello". Also i am searching for how can i print "hello" if $hello is true and "bye" if $hello is false without using any if else condition and ternary operator. – Raunak Jain Jul 09 '17 at 11:54
  • This is impossible. – u_mulder Jul 09 '17 at 12:00
  • @RaunakJain check my answer below. is that what you want? – B. Desai Jul 09 '17 at 12:19
  • There is no such thing as a *"shorthand if/else"*. [`if`](http://php.net/manual/en/control-structures.if.php) is a [control structure](http://php.net/manual/en/language.control-structures.php), [`?:`](http://php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary) is an [operator](http://php.net/manual/en/language.operators.php). Statements and operators are different things that serve to different purposes. A control structures decides what statement to run next. An operator joins sub-expressions (usually two but `?:` joins three) into a larger expression. – axiac Jul 09 '17 at 12:33
  • [`"hello"` is `TRUE`](http://php.net/manual/en/types.comparisons.php#types.comparisions-loose). – axiac Jul 09 '17 at 12:35

4 Answers4

1

So if you don't want any condition you can add default value bye to para,eter. And simply echo it

<?php

function showMessage($hello="bye"){
  echo $hello;
}
?>
B. Desai
  • 16,414
  • 5
  • 26
  • 47
0

Basically ($hello)?'hello':'bye'; is the shorthand for:

if ($hello == true) { 
  echo 'hello';
} else {
  echo 'bye';
}

Reference: http://php.net/manual/en/control-structures.if.php

Elitmiar
  • 35,072
  • 73
  • 180
  • 229
  • I am agree with this, but i am searching for how can i print "hello" if $hello is true and "bye" if $hello is false without using any if else condition and ternary operator. – Raunak Jain Jul 09 '17 at 11:52
0

You are using ternary operator inside function, which will check the type of variable true or false. By default $hello variable type will be false.

So below code will be check if variable type is true then prine 'hello' else ternary operator will be print 'bye'.

It is same as like below

if($hello==true){
    echo 'hello';
}else{
   echo 'bye';
}
Ayaz Ali Shah
  • 3,453
  • 9
  • 36
  • 68
0

The reason why showMessage('abc') now prints 'hello' is because the ($hello) will evaluate to true as a non-empty string.

I guess what you are looking for is the type comparison operator ===. It will check whether the argument passed is actually a boolean value.

function showMessage($hello=false) {
   echo ($hello === true)?'hello':'bye';
} 
bbb
  • 702
  • 3
  • 6