Toniq
Toniq

Reputation: 5006

Php nesting code with quotes

I have some chunks of code I have trouble nesting:

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

this prints false (result of autoOpenPopup var) instead of:

autoOpenPopup: false

It works if I do this:

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

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

But I wanted to nest this is possible.

Upvotes: 0

Views: 43

Answers (2)

Parag Chaure
Parag Chaure

Reputation: 3005

Add your conditions in a parenthesis and type cast Boolean to String.

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

Upvotes: 1

Jimish Fotariya
Jimish Fotariya

Reputation: 1097

Try to wrap the ternary in parenthesis '(...)';

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

Upvotes: 1

Related Questions