Jump to content

!not operator doesn't seem to be doing what i expect


c_shelswell

Recommended Posts

Hi i have a very simple for each statment spitting out some stuff on my page. One of the array id's is "username" and i'm after it missing that one out but doing this rest

I thought this would work
[code]
foreach ($details as $key => $value)
{
if (!(($i + 1)% 2))
{
if (!$key == 'username')
{
$head = explode("_", $key);
echo "<span class='textFieldReg'>".ucfirst($head[0]." ".$head[1]).":</span> \n";
echo "<input type='text' name='$key' value='$value' class='myInputReg'/><br />\n";
}
}
$i++;
}
[/code]
it's the "!$key == 'username'" bit i mean if i take away the "!" is just gives me the username field but if i put in the "!" it doesn't give me anything.

Can't quite understand.
Link to comment
Share on other sites

[quote author=HuggieBear link=topic=121777.msg501324#msg501324 date=1168439272]
It should be...

[code=php:0]if ($key != 'username'){[/code]

Not ...

[code=php:0]if (!$key == 'username'){[/code]

Regards
Huggie
[/quote]
Or... we could really throw a monkey wrench in it and check it this way:
[code]
<?php
if (!($key == 'username'))
?>
[/code]
Link to comment
Share on other sites

[quote author=HuggieBear link=topic=121777.msg501334#msg501334 date=1168439862]
I love a good Monkey Wrench every now and again :)

Huggie
[/quote]

LOL... yes, same here. I was basically just trying to point out that all the '!' operator does is negates the associated value. So, without the parenthetical, it's trying to negate $key alone. If you simply add the parenthesis around your comparison operation, it's actually negating the entire comparison process, therefore returning the value you're after.
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.