1

I like to use form[fieldsdata] only if it is defined:

  $fieldsJson = $data["form[fieldsdata]"] ? $data["form[fieldsdata]"] : "";

But still the error message is:

Notice: Undefined index: form[fieldsdata]

3 Answers3

6

You can either use isset() to check if it's defined, or (if you're using PHP 7) use the null coalescing operator (??)

Using isset

$fieldsJson = isset($data["form[fieldsdata]"]) ? $data["form[fieldsdata]"] : "";

Using null-coalescing operator (PHP 7 only)

$fieldsJson = $data["form[fieldsdata]"] ?? "";

Be aware that using null coalescing will also apply the empty string value if the index exists, but has a null value.

Daniel
  • 10,641
  • 12
  • 47
  • 85
2

Use

 $fieldsJson = isset($data["form[fieldsdata]"]) ? $data["form[fieldsdata]"] : "";
Andrei Lupuleasa
  • 2,677
  • 3
  • 14
  • 32
0
// Declare an array 
$array = array(); 
// Use isset function 
echo isset($array['geeks']) ? 'array is set.' : 'array is not set.'; 

Output:

array is not set.
M.Hemant
  • 2,345
  • 1
  • 9
  • 14