Fix PHP Notice: 'Undefined index' on Wordpress

17,330

Solution 1

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?

Solution 2

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;

Solution 3

This should work if currency_select is set.

 <?php if(isset($_SESSION['currency_select'])) echo "selected='selected'";?>
Share:
17,330
tlt2w
Author by

tlt2w

Updated on June 04, 2022

Comments

  • tlt2w
    tlt2w about 2 years

    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!

  • jdp
    jdp over 10 years
    php > $a = 1; php > var_dump($a == '1'); bool(true) Because php!
  • Rottingham
    Rottingham over 10 years
    Using == will equate '1' and 1 as the same. I think the OP was thinking IF ISSET RETURN VALUE = 1 THEN USE IT
  • tlt2w
    tlt2w over 10 years
    but how can I check if it is set just to a specific value? ['currency_select'] == 'something' ?
  • makallio85
    makallio85 over 10 years
    <?php if(isset($_SESSION['currency_select']) && $_SESSION['currency_select'] == 'something') echo "selected='selected'";?>
  • tlt2w
    tlt2w over 10 years
    thanks that was the right way to do it. I saw that @strub was first to do this kind of if statement so I marked his answer. Thanks!