-1

I saw this piece of code into one project that I started to work and I find it a bit difficult to understand what really this statement does, the following is the code:

private function _idMatches() {
    return $this->_id ? $this->_id == $this->_organization->getId() : false;
  }

Well this is suppose to check if some id matches but I'm just maybe not used to write this shortcut code in php and would like to know if someone can break it down and explain this statement.

Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
Mizlul
  • 1
  • 7
  • 41
  • 100
  • 3
    This is called a _ternary operator_, search for that expression. – Arnaud Jul 12 '18 at 10:18
  • 2
    @B001ᛦ not really. `if ($this->_id) return $this->_id == $this->_organization->getId(); else return false;` – Federico klez Culloca Jul 12 '18 at 10:19
  • @B001ᛦ if u dont have an answer u can better be silent and not just write something, u cannot find such thing on google. and u can learn from Berger answer he already helped quite alot by answering it the terminology of this stm and I can actually google it by ternary operator in php – Mizlul Jul 12 '18 at 10:22
  • 2
    thnx @Berger this helped quite alot, i didnt know what is called this kind of stmm thnx alot :) – Mizlul Jul 12 '18 at 10:24
  • @Mizlul please don't. Let's keep it civil please. B001ᛦ was not really helpful but that's no way to answer. – Federico klez Culloca Jul 12 '18 at 10:24
  • well at least I can give him some warning that that's not the way he should answer on stackoverflow if he cant help on smth! – Mizlul Jul 12 '18 at 10:26
  • @tereško why do u think there is any criteria to fit to give warnings, it was obvious that his comment was rude, that's why he actually deleted it – Mizlul Jul 12 '18 at 10:29
  • 1
    @Mizlul stop being entitled – tereško Jul 12 '18 at 10:30
  • I don't even know what b001's comment even was but "u cannot find such thing on google" I don't quite believe. I actually just searched for "statement with a question mark in it php" and it took me directly too [the solution](https://stackoverflow.com/questions/1276909/php-syntax-question-what-does-the-question-mark-and-colon-mean) – IsThisJavascript Jul 12 '18 at 10:32
  • @IsThisJavascript probably waiting for Berger to write his answer so I can mark it as he was the first one answering otherwise I can mark it the current answer – Mizlul Jul 12 '18 at 10:32
  • @IsThisJavascript are you trying now to make it longer this conversation? – Mizlul Jul 12 '18 at 10:34
  • No @Mizlul I'm just pointing out how to use google to find a solution so you can avoid this controversy in the future. – IsThisJavascript Jul 12 '18 at 10:35
  • thnx alot, i appreciate your effort @IsThisJavascript :) – Mizlul Jul 12 '18 at 10:36

1 Answers1

1

This function return true if $this->_id == $this->_organization->getId() and $this->_id is not empty/null.

In any other situation it return false.

To better understand this operation please read this: https://davidwalsh.name/php-ternary-examples.

Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
patryno
  • 154
  • 14