-1

I am trying to convert an existing check for a file to use a ternary operator instead. My original check is this...

if (isset($_FILES["file"])) {
    echo '7';
};

This works correctly, my version using ternary operator looks like this...

isset($_FILES['file']) ? $_FILES['file'] : 7;

I get the error message...

Undefined index: file

Why is this happening, where am I going wrong?

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
fightstarr20
  • 11,682
  • 40
  • 154
  • 278
  • 1
    try `echo $result = (isset($_FILES['file'])) ? 7 : 'no file uploaded';` . Assign return value to variable and `echo` it, or `print_r` it (in case of `$_FILES`) – Alive to die - Anant Jun 12 '19 at 11:39
  • 1
    @AlivetoDie without the `$result` bit, no? (if it's only an echo) – treyBake Jun 12 '19 at 11:43
  • @treyBake correct, but i used variable that may be OP want to use it somewhere else in his code – Alive to die - Anant Jun 12 '19 at 11:45
  • Possible thread duplicated. Check [here](https://stackoverflow.com/questions/17981723/how-to-write-a-php-ternary-operator) – wasanga7 Jun 12 '19 at 11:46
  • Possible duplicate of [How to write a PHP ternary operator](https://stackoverflow.com/questions/17981723/how-to-write-a-php-ternary-operator) – wasanga7 Jun 12 '19 at 11:46
  • Well, we would need more code to trace the issue. This looks ok to me if you have something like `print_r(isset($_FILES['file']) ? $_FILES['file'] : 7)`. – nice_dev Jun 12 '19 at 12:10

1 Answers1

-2

This code does not appear abnormal

<?php
error_reporting(E_ALL);
echo isset($_FILES['file']) ? $_FILES['file'] : 7;

Click to run: https://3v4l.org/PtTmm#output

Please check the location that caused the error.

dingdayu
  • 1
  • 2