Question

Simple stuff,

This works without any problems:

$openMonday =  rtrim(chunk_split($result['opening_hours']['periods'][1]['open']['time'], 2, ':'), ':');
$business->openingTimes['monday']       = isset($openMonday) ? $result['opening_hours']['periods'][1]['open']['time'] : '';

But I don't want to write two lines for this because then I would have to do it also for all the other opening hours.

Why can't I just write

$business->openingTimes['monday']       = isset(rtrim(chunk_split($result['opening_hours']['periods'][1]['open']['time'], 2, ':'), ':')) ? $result['opening_hours']['periods'][1]['open']['time'] : '';

I'm always getting the error that it's expecting a variable. How can I use methods in the isset with ternary operator?

Was it helpful?

Solution

The problem here is that isset() is not a real function but a language construct, which requires its arguments to be variables, or it issues a syntax error.

See also the manual entry: http://php.net/manual/en/function.isset.php

Anyway, as also @deceze said, you probably do not want to use isset() here, since it is used to check if a variable exists.

In this case, you could use empty(), so instead of writing

$openMonday =  rtrim(chunk_split($result['opening_hours']['periods'][1]['open']['time'], 2, ':'), ':');
$business->openingTimes['monday'] = isset($openMonday) ? $result['opening_hours']['periods'][1]['open']['time'] : '';

you could do

$openMonday =  rtrim(chunk_split($result['opening_hours']['periods'][1]['open']['time'], 2, ':'), ':');
$business->openingTimes['monday'] = !empty($openMonday) ? $result['opening_hours']['periods'][1]['open']['time'] : '';

And you should not worry to make it a one-liner at any cost! Create a function instead, and then call it when needed... But if you really still want to do it inline, then you could do

$business->openingTimes['monday'] = !empty(rtrim(chunk_split($result['opening_hours']['periods'][1]['open']['time'], 2, ':'), ':')) ? $result['opening_hours']['periods'][1]['open']['time'] : '';

just remember that the above requires at least PHP 5.5!

And finally, the row above could just be written as

$business->openingTimes['monday'] = !rtrim(chunk_split($result['opening_hours']['periods'][1]['open']['time'], 2, ':'), ':') ? $result['opening_hours']['periods'][1]['open']['time'] : '';

without any need for isset() nor empty()

OTHER TIPS

You don't need isset here! isset is used to safely test whether... well... a variable exists. You know your variable exists, because you're declaring it on the line before. If you want to mush it into one line, you're not even using a variable, you're directly working with the value. You just want to compare the value to false, which you can do inline easily.

Just get rid of isset. And read The Definitive Guide To PHP's isset And empty.

SOLUTION

    $business->openingTimes['monday']       = isset($result['opening_hours']['periods'][1]['open']['time']) ? rtrim(chunk_split($result['opening_hours']['periods'][1]['open']['time'], 2, ':'), ':') : '';

To explain what I did. I asked if the variable is set, and if so it uses the method chunk_split otherwise the string will be empty.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top