Home » Php » php – Whats the difference between ? : and ||

php – Whats the difference between ? : and ||

Posted by: admin July 12, 2020 Leave a comment

Questions:

What difference is there between using the ?: conditional operator and the || Logical OR.

I am finding that my code works with:

$screenpixelratio = !empty($_COOKIE['screenpixelratio']) || $_COOKIE['screenpixelratio'] || $fallback_pixelratio;

But not:

$screenpixelratio = !empty($_COOKIE['screenpixelratio']) ? $_COOKIE['screenpixelratio'] : $fallback_pixelratio;

Could someone please explain why it would work with one, but not the other.

How to&Answers:

|| Binary operators are operators that deal with two arguments

as its says it will check first if its true than not gonna check further else check further

?: ternary operator is an operator that takes three arguments. The arguments and result can be of different types.

Expression1 ? Expression2 : Expression3;

enter image description here

Answer:

The first (conditional or) is saying…

this or this or this

The other (ternary operation) is saying

if this then this otherwise that