lundi 16 décembre 2019

Shorthand way of writing "if value is equal to this or this"

I searched but couldn't find what I was looking for. I have the below piece of code:

if ( $_SERVER[ "REQUEST_URI" ] === "/post" || $_SERVER[ "REQUEST_URI" ] === "/post/" ) {

    // redirect to "/"

} else {

    // logic to display the post (uri to db query)

}

Works fine, as expected. I tried to shorten the comparison to (truncated):

...... $_SERVER[ "REQUEST_URI" ] === "/post" || "/post/" .......

I didn't expect it to work, actually expected it to throw an error to be honest. No errors, but now any URI for this page; /post, /post/, /post/my-first-post all execute the if part of the if/else statement.

The second part will never be executed. Why is this and what is the proper way to do this?

Aucun commentaire:

Enregistrer un commentaire