Jump to content

[SOLVED] an iif thing (i dont know the php name, the "statment?true:false" thing)


Box

Recommended Posts

Sorry I don't know the name of thing function(?) but i do know how to use it :P

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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'];

Link to comment
Share on other sites

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  :facewall:

 

thanx for the help though :) ill try remember the name of it :P

 

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

Link to comment
Share on other sites

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)
?>

Link to comment
Share on other sites

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.