duanpanyang1962 2012-11-21 15:37
浏览 20

我的if语句无法正确读取'||'

I am trying to create a system where depending on what user is a)logged in or b)if the username is found in the array.

If you are logged in and in the friend_array of the person, you should see the 'Unfriend' button. If you're on the page which is your own profile, everything is hidden If your not on your page and not in the array, 'Add Friend'.

The statement for the 'Add Friend' must be reading my statement incorrectly, as I only want it to appear when your not in the array and when the _Session username is not equal to the username selected by getting the id.

    $cool = $_GET['id'];

    $evan = mysql_query("SELECT * FROM users WHERE username='{$_SESSION['user_login']}'");
    $joe1 = mysql_fetch_assoc($evan);

$cookie = mysql_query("SELECT * FROM users WHERE id='{$_GET['id']}'");
$joe = mysql_fetch_assoc($cookie);

$i = 0;
$berbs = $joe['username'];
error_reporting(E_ERROR | E_PARSE);

if (in_array($_SESSION['user_login'], $friendArray)) {
 $addAsFriend = '<input type="submit" name="removefriend" value="UnFriend">';
echo $addAsFriend;

}
error_reporting(E_ERROR | E_PARSE);

 if ($_GET['id'] == $joe1['id']) {
     $addAsFriend = '<input style="display:none;" type="submit" name="addfriend" value="Add Frimm1end">';
    echo $addAsFriend;

}
error_reporting(E_ERROR | E_PARSE);

if (!in_array($joe1['username'], $friendArray) || $_GET['id'] != $joe1['id']) {
     $addAsFriend = '<input type="submit" name="addfriend" value="Add Friend">';
     echo $addAsFriend; 
}
  • 写回答

2条回答 默认 最新

  • dongzhenqi2015 2012-11-21 15:39
    关注

    The problem is that you have inverted the boolean logic incorrectly.

    You started with an expression like this: (a() || b == c) and you want to invert it. You have assumed that !(a() || b == c) is the same as (!a() || b != c), but this is not true. According to De Morgan's laws you need to change || to && giving (!a() && b != c).

    The corrected code is as follows:

    if (!in_array($joe1['username'], $friendArray) && $_GET['id'] != $joe1['id']) 
    

    Alternatively, you could avoid trying to do the boolean logic in your head:

    if (!(in_array($joe1['username'], $friendArray) || $_GET['id'] == $joe1['id']))
    

    You can also notice your mistake from the way you have worded your question:

    I only want it to appear when your not in the array and when the _Session username is not equal to the username selected by getting the id.

    Notice that you have used the word and when describing your problem. This corresponds directly to the operator &&.

    评论

报告相同问题?

悬赏问题

  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多
  • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。
  • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000
  • ¥30 ppOCRLabel导出识别结果失败
  • ¥15 Centos7 / PETGEM