Jump to content

Recommended Posts

Yes, in that you can put any expression in the conditional part of a ternary operator.

 

Okay, great.

 

 

(For that particular code you'd want either isset() or !empty() but I don't think that's what you're asking about.)

 

Right.

 

Thanks,

 

 

Debbie

 

 

In this statement...

 

	$firstName = (isset($_SESSION['memberFirstName']) ? $_SESSION['memberFirstName'] : '');

 

 

Can isset() be replaced with most other functions, e.g. empty(), is_numeric(), is_null(), etc??

 

Thanks,

 

 

Debbie

 

the one thing I see wrong is you have the entire ternary wrapped in parenthesis, whereas you only want the condition wrapped.

This could simply have been a typo, however wanted to point it out.

In this statement...

 

	$firstName = (isset($_SESSION['memberFirstName']) ? $_SESSION['memberFirstName'] : '');

 

 

Can isset() be replaced with most other functions, e.g. empty(), is_numeric(), is_null(), etc??

 

Thanks,

 

 

Debbie

 

the one thing I see wrong is you have the entire ternary wrapped in parenthesis, whereas you only want the condition wrapped.

This could simply have been a typo, however wanted to point it out.

 

I don't follow you.

 

Can you repost how it should look?

 

Thanks,

 

 

Debbie

 

$firstName = (isset($_SESSION['memberFirstName'])) ? $_SESSION['memberFirstName'] : '';

 

It looks like any of these would work...

 

$memberID = isset($_SESSION['memberID']) ? $_SESSION['memberID'] : '';

$memberID = (isset($_SESSION['memberID'])) ? $_SESSION['memberID'] : '';

$memberID = (isset($_SESSION['memberID']) ? $_SESSION['memberID'] : '');

$memberID = ((isset($_SESSION['memberID'])) ? $_SESSION['memberID'] : '');

 

Does one versus the other really matter?

 

 

Debbie

 

 

1/2 and 3/4 are identical pairs. The parentheses around the condition mean nothing, but parentheses around the whole ternary could make a difference depending on operator precedence.

$variable = $condition ? $a : $b;
// equivalent to $variable = ($condition ? $a : $b) because ?: has higher precedence than =

$variable == $condition ? $a : $b;
// equivalent to ($variable == $condition) ? $a : $b because == has higher precedence than ?:

Operator precedence table

1/2 and 3/4 are identical pairs. The parentheses around the condition mean nothing, but parentheses around the whole ternary could make a difference depending on operator precedence.

$variable = $condition ? $a : $b;
// equivalent to $variable = ($condition ? $a : $b) because ?: has higher precedence than =

$variable == $condition ? $a : $b;
// equivalent to ($variable == $condition) ? $a : $b because == has higher precedence than ?:

Operator precedence table

 

So which format would you recommend?

 

 

Debbie

 

I personally like having parentheses around the whole thing because of how it looks (literally) to me.

$firstName = (isset($_SESSION['memberFirstName']) ? $_SESSION['memberFirstName'] : '');

If you're not sure about the precedence thing then I recommend using the parentheses too, but ultimately it's up to your preferences and style.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.