jeroen
jeroen

Reputation: 502

PHP smart || (OR) operator just like in Javascript

Is it safe to use constructs like these in PHP:

$filename = $file['filename1'] || $file['filename2'];

I have a background in JS and this is safe to use since we can trust truthy/falsey values. However my colleague is used to constructs utilizing isset:

$filename = 'filename'. (isset($file['filename2']) ? '2' : '1');

Which to me seem a little verbose.

I found an article on phabricator.com which outlines the different truthy/falsey values in PHP and offers this table:

VALUE             if()        empty()     isset()

null              false       true        false
0                 false       true        true
0.0               false       true        true
"0"               false       true        true
""                false       true        true
false             false       true        true
array()           false       true        true
EVERYTHING ELSE   true        false       true

I would appreciate anyone giving me insights in this matter.

Upvotes: 0

Views: 100

Answers (4)

HIRA THAKUR
HIRA THAKUR

Reputation: 17757

First thing:

$filename = $file['filename1'] || $file['filename2'];  //incorrect syntax

should be

$filename = $file['filename1'] || $filename = $file['filename2'];//depending on the condition

Second thing:

isset($file['filename2']) ? '2' : '1';

this is a shorthand which replaces verbose code.

same code can be written as:

if(isset($file['filename2']){
   $filename='filename2';
}else{
   $filename='filename1';
}

so,4 lines of code can be written in one line.Thats not verbose.Thats smart coding.


Third thing:

This is the ideal way of doing it.

return isset($file['filename2'])?$file['filename2']:$file['filename1'];

sample for understanding:

echo (age>18)?"where is my beer":"you are Underage!!!"; //codition:true:false

Upvotes: 3

Mihaela
Mihaela

Reputation: 109

If you want a random value from an array you can use array_rand(), but if you want one value based on a condition you should use the ternary operator:

$filename = (condition) ? what_happens_if_true : what_happens_if_false;

Upvotes: 1

Deepanshu Goyal
Deepanshu Goyal

Reputation: 2813

the line

$filename = 'filename'. (isset($file['filename2']) ? '2' : '1');

is same as

if(isset($file['filename2']){
   $filename='filename2';
}else{
   $filename='filename1';
}

|| is used if you have multiple conditions in your if and if even one pf them is true, it'll be true.

The ?: is a ternary operator

Upvotes: 1

victorantunes
victorantunes

Reputation: 1169

By using

$filename = $file['filename1'] || $file['filename2'];

if $file['filename1'] isn't set, you'll receive an error such as Undefined index: filename1' since the (OR) operator will first check the first statement.

However, if $file['filename1'] is set, but $file['filename2'] isn't, the value 1 would be returned and no errors would be thrown, but your code would remain incorrect.

So, you'll want to go the isset way, or use (as pointed by VAGABOND):

$filename = $file['filename1'] || $filename =$file['filename2'];

Upvotes: 1

Related Questions