简体   繁体   English

PHP条件语法帮助如果/其他?

[英]PHP Conditional Syntax Help If/Else?

I need the proper php syntax to handle a situation with my website's breadcrum links for product pages. 我需要适当的php语法来处理网站上产品页面的面包屑链接的情况。 All of my items are derived from a MySQL database. 我的所有项目均来自MySQL数据库。

The inventory items are organized either by itemCategory or itemAuthor. 库存物料由itemCategory或itemAuthor组织。 Not all items have an itemCategory associated with them so in the database they are NULL. 并非所有项目都有与之关联的itemCategory,因此在数据库中它们为NULL。 All items however, have an itemAuthor. 但是,所有项目都有一个itemAuthor。

What I basically want to say is: 我基本上想说的是:

If the item has a value for itemCategory, echo the itemCategory. 如果项目具有itemCategory的值,则回显itemCategory。 If itemCategory is NULL, echo itemAuthor instead. 如果itemCategory为NULL,则回显itemAuthor。

Thanks for any help. 谢谢你的帮助。

if(empty($itemCategory)) {
  echo $itemAuthor;
} else {
  echo $itemCategory;
}

The mysql solution: mysql解决方案:

SELECT ...
    IFNULL(`itemCategory`, `itemAuthor`) AS `itemCategory`
FROM ...

In PHP You just echo itemCategory. 在PHP中,您只需要回显itemCategory。 No additional conditions. 没有其他条件。 If it was NULL id database, it will be itemAuthor in PHP. 如果它是NULL id数据库,则它将是PHP中的itemAuthor。

The php solution: php解决方案:

if ($r['itemCategory']):
    echo $r['itemCategory'];
else:
    echo $r['itemAuthor'];
endif;

The NULL is returned as empty string. NULL作为空字符串返回。 It is converted to php false in if expressions. 在if表达式中将其转换为php false。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM