在PHP中传递给echo的参数

时间:2017-11-13 05:44:21

标签: php

这里是我提供一些echo

的例子

示例1

echo 'The sum is ', 1 + 2; //works fine

示例2

$x=true;
echo '$x is undefined value ' , ($x===true) ? 'NO' : 'Yes'; //Works fine

示例3

function f($arg){
  return $arg;
}

echo 'hello ' , f('buddy'); //Works fine

示例4 -

echo 'hello ' , if($a) { return "wowo"; } else { return "fine"; }; // generate parse error

以下是所有示例正常工作,但示例4生成解析错误。

我的问题是 -
1. language constructs可以用于echo个参数吗? 2.如果可以使用 language constructs,我们如何将language constructs用于echo个参数?
3.只有返回字符串的functions可以传递给echo个参数吗?

1 个答案:

答案 0 :(得分:-1)

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 .