2016-07-15 88 views
0

我有一些代码块嵌套的代码,我有麻烦嵌套:PHP和报价

return 'autoOpenPopup: '.!empty($options["autoOpenPopup"]) ? $this->int_to_bool($options["autoOpenPopup"]) : $this->int_to_bool(false) . PHP_EOL.''; 

这种打印的,而不是虚假的(autoOpenPopup VAR的结果):

autoOpenPopup: false 

它的工作原理,如果我做这个:

$t = !empty($options["autoOpenPopup"]) ? $this->int_to_bool($options["autoOpenPopup"]) : $this->int_to_bool(false) . PHP_EOL; 

return 'autoOpenPopup: '.$t.''; 

但是我想嵌套这是可能的。

+0

当你编写代码时要记住的一件事是让你的代码可以理解并具有可读性,有时候最好有两行而不是一行。 – Epodax

+2

你有没有尝试用'('和')'包装你的三元代码?像这样:'return'autoOpenPopup:'。 (!empty ..?..:.. PHP_EOL。'');' – FirstOne

+0

你能否快速解释'int_to_bool'的作用? (它是否将'1'转换为'true'(字符串)并将'0'转换为'false'(字符串)? – FirstOne

回答

1

尝试在括号'(...)'中包装三元组;

return 'autoOpenPopup: '.(!empty($options["autoOpenPopup"]) ? $this->int_to_bool($options["autoOpenPopup"]) : $this->int_to_bool(false)) . PHP_EOL.''; 
1

在圆括号中添加条件,并将类型转换为布尔型为String。

return 'autoOpenPopup: '.(string) (!empty($options["autoOpenPopup"]) ? $this->int_to_bool($options["autoOpenPopup"]) : $this->int_to_bool(false) . PHP_EOL); 
+2

为什么要op _try this_?你有什么改变?请解释你的答案;) – FirstOne

+0

当然,谢谢@FirstOne –