0

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.

2
  • 4
    the || and other comparative operators in php will return a BOOLEAN value, it will not act like it does in javascript. Commented Aug 30, 2013 at 11:23
  • @jeroen:hope you understand.accept the answer if you think its worthy.. Commented Aug 30, 2013 at 11:59

4 Answers 4

3

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
Sign up to request clarification or add additional context in comments.

Comments

1

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'];

Comments

1

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

1 Comment

the edit you suggested is wrong..what i have done is correct for some valid reasons..what is filename1 and filename2.no $..no paranthesis..as i said,my answer is ideal for what the OP wants..
1

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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.