Home » Wordpress » Fix PHP Notice: 'Undefined index' on WordPress

Fix PHP Notice: 'Undefined index' on WordPress

Posted by: admin February 26, 2018 Leave a comment

Questions:

I’m trying to fix the Undefined index PHP notice on a wordpress site.

At the moment this is what I got:

    <?php if($_SESSION['currency-select'] == "a") echo 'selected="selected"';?>

I tried to write this way but then the site goes down:

    <?php if(isset($_SESSION['currency-select'] == "a")) echo 'selected="selected"';?>

Also, I have a query which is also without isset and i’m trying to fix it:

                    if($_SESSION['currency-select'] == 'b') {

                if($_GET['pricing'] == '1') {

                    $args['meta_query'][] = array(

                        'key' => 'price',

                        'value' => array( '0', '250' ),

                        'compare' => 'BETWEEN',

                        'type' => 'numeric'

                    );

                }

I tried to write:

      if(isset($_GET['pricing'] == '1')) {
      if($_GET['pricing']) {

but it doesn’t work as well.

Thanks for your support!

Answers:

Try looking if the variable isset and then check the contents of it.

if( isset($_GET['pricing']) && $_GET['pricing'] == '1' ){
   //- do some magic
}

With your if-clause you check if the clause itself isset() instead of the variable.

ps: why do you check the number 1 as a string 1?

Questions:
Answers:

Simply convert your faulty isset() check.

if (isset($_GET['pricing'])) {
    $pricing = $_GET['pricing'];
}

The short hand version would look like this, if you want to assign a default value.

$pricing = (isset($_GET['pricing'])) ? $_GET['pricing'] : 0;

Questions:
Answers:

This should work if currency_select is set.

 <?php if(isset($_SESSION['currency_select'])) echo "selected='selected'";?>