here are i am providing some examples of echo
Example 1
echo 'The sum is ', 1 + 2; //works fine
Example 2
$x=true;
echo '$x is undefined value ' , ($x===true) ? 'NO' : 'Yes'; //Works fine
Example 3
function f($arg){
return $arg;
}
echo 'hello ' , f('buddy'); //Works fine
Example 4 -
echo 'hello ' , if($a) { return "wowo"; } else { return "fine"; }; // generate parse error
Here is the point that all example working fine but example 4 its generating parse error.
my questions is that -
1. language constructs
can be used for echo
arguments ?
2. If yes language constructs
can be used, how can we use language constructs
for echo
arguments ?
3. Only functions
that return strings can be passed to echo
arguments ?
Yes you can use language constructs to echo arguments or return values. If else is a conditional statement which can be use to evaluate a value. using this you will be able to break the the logic into several pieces and evaluate the condition. the echo will work separately for an each statement. But if you return a value , it will be returned from function and not from the statement. EX:
$value = "hello";
if($a) { $value.= "wowo"; } else { $value.= "fine"; }
echo $value;
in the other hand ternary operator is an operator that takes three arguments and performs an operation .