Undefined Index

The following line of code is creating an ‘Undefined Index’ notice.

<?php if ($_POST['title'] && empty($success)) { echo $_POST['title']; } ?>

I have tried the following line, but its wrong somehow, but I think I’m on the right lines.

<?php if (isset($_POST['title'] && empty($success)) { echo $_POST['title']; }) ?>

The first line doesn’t work because, well, the index is not defined. You have to use isset here, but in the second line you forgot to close the brackets. Didn’t it actually throw a syntax error?

Using the isset the brackets are wrong.

<?php if ((isset($_POST['title'])) && (empty($success))) { echo $_POST['title'];} ?>

Ah I see yes, thanks guys

I am also getting the same issue with this line below, which I thought wouldn’t need the isset addition.

<?php echo $q['IdCntry_Hot']?>

PHP Notice: Undefined index: IdCntry_Hot

If there’s a chance that a certain element of an array is not present (yet), you have to check this with isset. Otherwise the index might not be defined and you get the above notice, which actually says it all.

1 Like

I see, ok I didn’t know that, thank you.

Its basic, but how would I use isset on this occasion.

<?php if(isset($q['IdCntry_Hot'])) { echo $q['IdCntry_Hot'] ; } ?>

Right I got you, thank you all

Your could use

echo isset($q['IdCntry_Hot'] ? $q['IdCntry_Hot'] : '';

At the start of the Php page try checking and setting the following for all possible $_POST values.


<?php 
error_reporting(-1);
ini_set('display_errors', '1');

$IdCntry = isset( $q['IdCntry_Hot'] ) ? $q['IdCntry'] : FALSE;

The original version would be almost correct for PHP 7 where the null coalesce operator can be used instead of isset.

<?php echo $q['idCntry_Hot']??""; ?>

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.