Paul
Paul

Reputation: 3368

Ternary operator only running first part of condition

I am having trouble getting a count condition to work. Currently, whether I upload one or more than one file, the first condition where "Multiple files were" is running. The second part is never running.

Is there something else I have to do to get the second part of the condition to run if only one file has been uploaded?

$fu = new fileUpload();
$filename = $fu->upload();
$out = (count($filename) ? 'Multiple files were' : 'A file was'). '  uploaded. You can download ' . (count($filename) ? 'them' : 'the file'). ' from:</ul>';

Upvotes: 0

Views: 97

Answers (1)

Mitya
Mitya

Reputation: 34556

Your logic is faulty.

count($filename) resolves to truthy if it's any value other than 0.

So whether it's 1 file or 64, that first part will always resolve to true. You need:

count($filename) > 1 ?

Upvotes: 4

Related Questions