doubledee Posted March 24, 2012 Share Posted March 24, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/259639-ternary-and-functions/ Share on other sites More sharing options...
requinix Posted March 24, 2012 Share Posted March 24, 2012 Yes, in that you can put any expression in the conditional part of a ternary operator. (For that particular code you'd want either isset() or !empty() but I don't think that's what you're asking about.) Quote Link to comment https://forums.phpfreaks.com/topic/259639-ternary-and-functions/#findComment-1330793 Share on other sites More sharing options...
doubledee Posted March 24, 2012 Author Share Posted March 24, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/259639-ternary-and-functions/#findComment-1330796 Share on other sites More sharing options...
AyKay47 Posted March 25, 2012 Share Posted March 25, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/259639-ternary-and-functions/#findComment-1330848 Share on other sites More sharing options...
doubledee Posted March 25, 2012 Author Share Posted March 25, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/259639-ternary-and-functions/#findComment-1330849 Share on other sites More sharing options...
AyKay47 Posted March 25, 2012 Share Posted March 25, 2012 $firstName = (isset($_SESSION['memberFirstName'])) ? $_SESSION['memberFirstName'] : ''; Quote Link to comment https://forums.phpfreaks.com/topic/259639-ternary-and-functions/#findComment-1330851 Share on other sites More sharing options...
doubledee Posted March 25, 2012 Author Share Posted March 25, 2012 $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 Quote Link to comment https://forums.phpfreaks.com/topic/259639-ternary-and-functions/#findComment-1330857 Share on other sites More sharing options...
requinix Posted March 25, 2012 Share Posted March 25, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/259639-ternary-and-functions/#findComment-1330861 Share on other sites More sharing options...
doubledee Posted March 25, 2012 Author Share Posted March 25, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/259639-ternary-and-functions/#findComment-1330864 Share on other sites More sharing options...
requinix Posted March 25, 2012 Share Posted March 25, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/259639-ternary-and-functions/#findComment-1330865 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.