How to make an IF read two category types at the same time?

Asked

Viewed 38 times

0

Good night, well, I wonder how I can put it: if ($this->GetCategoryName() == 'mmorpg' OR 'shots') on the same line of code, without having to do this:

if ($this->GetCategoryName() == 'mmorpg') {
    if ($this->GetCategoryName() == 'shots') {
        $url = $c_url;
    } else {
        $url = $c_url;
    }
} else {
    $url = U_LANG.'/'.$url_cat;
}
  • if ($this->GetCategoryName() == 'mmorpg' && $this->GetCategoryName() == 'shots') {

  • kkk Eae man :D, if you have Discord account? I am new in PHP, so if you could be my teacher rs, I have already set if, it was just by the OR thus: or there worked: $this->GetCategoryName() == 'mmorpg' or 'shots'

1 answer

4


Thus:

if ($this->GetCategoryName() == 'mmorpg' && $this->GetCategoryName() == 'shots') {

But there’s a logic error, if it’s one thing it’s another.

In PHP you can use

  • && for and

  • || for OR, is that not what it seeks?

But maybe you’re more interested in doing it this way:

$cat = $this->GetCategoryName();
$url = ($cat == 'mmorpg' || $cat == 'shots') ? 'url1' : 'url2';

That is "if the category is mmorpg OR category for shots, it is url1, if not url2"


Note that PHP has and and or also, but changes the precedence, see here:

What is the difference between "&&" and "||" and "and" and "or" in PHP? What to use?

  • Yeah, that second method got better: $url = ($this->GetCategoryName() == 'mmorpg' || $this->GetCategoryName() == 'shots') ? $c_url : U_LANG.'/'.$url_cat; thanks rs

  • Note que PHP tem and e or também, mas muda a precedência. Yeah, really.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.