Box Posted August 6, 2009 Share Posted August 6, 2009 Sorry I don't know the name of thing function(?) but i do know how to use it i have a table with firname lastname and nickname in it not everyone has a nickname and I don't want it to show a double space and have it forced in so put in a check to see if it exists. At the moment it currently only shows the nickname which has got me very confused. Id rather not use actual ifs and elses as I know this should work. Im assuming ive got an extra character in there somehwree or a " in the wrong place maybe? the line is: $name = $row['firstname']." ".$row['nickname']?$row['nickname']." ":"".$row['lastname']; any help? Thanks Quote Link to comment Share on other sites More sharing options...
smerny Posted August 6, 2009 Share Posted August 6, 2009 $name = ($row['nickname'] == "" ? $row['firstname'] : $row['nickname']); $name .= " "; $name .= $row['lastname']; is this what your trying to do? edit: had things backward in the if else Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted August 6, 2009 Share Posted August 6, 2009 It's called the ternary operator. expr ? true_part : false_part If expr is true, then the code in true_part runs, else the code in false_part runs. Now let's look at your statements: expr: $row['firstname']." ".$row['nickname'] true_part: $row['nickname']." " false_part: "".$row['lastname'] I want you to think long and hard about why your expr will always be true, thus your false_part will never, ever execute. Quote Link to comment Share on other sites More sharing options...
smerny Posted August 6, 2009 Share Posted August 6, 2009 my previous post is if you want EITHER their first name or their nickname INSTEAD if it exists... but looking at your code i now believe you maybe want all 3 if they exist... if that is the case, do this instead: $name = $row['firstname']; $name .= ($row['nickname'] == "" ? " " : " ".$row['nickname']." "); $name .= $row['lastname']; Quote Link to comment Share on other sites More sharing options...
Box Posted August 6, 2009 Author Share Posted August 6, 2009 yeah i want all 3 Ie firstname: billy lastname kidd nickname the would show "billy the kid" I only want the expression to be: $row['nickname'] to basically check if it has anything in it. I guess I need to split it into lines to make it work then? $name = $row['firstname']." "; $name .= $row['nickname']!=""?"'".$row['nickname']."' ":""; $name .= $row['lastname']; is working fine. and now shows the nickname with '' around it. ie billy 'the' kid im still not sure as to why the other code didnt work. even if the exression was always true it should have just forced the nickname to be shown (and if there wasnt one just a double space in the name) Te fact it was either the nickname or nothing was the main confusing part thanx for the help though ill try remember the name of it ok upon reading again i now realise that the firstname was never shown as it was included as part of the expression, and the reason the surname was never shown is because it was art of the false statement. what i has wanted was: expr: $row['nickname'] true_part: $row['nickname']." " false_part: "" i guess all on one line was just too much O_o Quote Link to comment Share on other sites More sharing options...
smerny Posted August 6, 2009 Share Posted August 6, 2009 ill try remember the name of it I always called it shorthand if statement lol Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted August 6, 2009 Share Posted August 6, 2009 Te fact it was either the nickname or nothing was the main confusing part To clarify: it was always showing the nickname and just the nickname. So if they had a nickname, that's what it showed. If the nickname was empty, then it showed that (and if you're viewing it in a browser, browsers ignore extra whitespace, so you only saw one space). That's why it showed the nickname or nothing (nothing happened to be the nickname on some of your rows). In the future, remember that you can control the order in which PHP will evaluate expression with parentheses. <?php echo $firstname . (strlen( $nick ) > 0 ? " '{$nick}' " : ' ') . $lastname; // ^- close paren // ^- open paren // That excludes $firstname and $lastname from the ternary operator's test and result // (but it also makes your code slightly more difficult to read) ?> Quote Link to comment 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.