Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shorthand expression for an if ( $a == $b and $a == $c ) statement in PHP

Recently, I posted a question about if ( $a == $b or $a == $c ) expression. Now I want to know if there's also a shorthand expression for this code:

if ( $a == $b && $a == $c ) { /*statement*/ }

That code can be read like "if $a is equal to $b and $a is equal to $c".

Is there code that's more shorter that can be read like:

if ( $a is equal to ( $b and $c ) ) { /*statement*/ }

like image 970
5ervant - techintel.github.io Avatar asked Sep 27 '22 22:09

5ervant - techintel.github.io


1 Answers

Because you are testing if $a is equal to $b and $c this logically results in $b being equal to $c, I don't know how much shorter this would be but if you wanted to quickly test if all prepossed values are the same you could create an array of them and call array_unique. If the length of the resulting array is 1 then they are all equal to each other. I imagine this would only be easier if you had a large set of values, as the syntax might obscure your intent.

like image 133
Jason Sperske Avatar answered Oct 03 '22 01:10

Jason Sperske