How to fix Warning Illegal string offset in PHP

前端 未结 3 1350
伪装坚强ぢ
伪装坚强ぢ 2020-12-19 05:42

I have this chunk of PHP code which is giving me the error:

Warning: Illegal string offset \'iso_format_recent_works\' in C:\\xampp\\htd

相关标签:
3条回答
  • 2020-12-19 06:08

    Please check that your key exists in the array or not, instead of simply trying to access it.

    Replace:

    $myVar = $someArray['someKey']
    

    With something like:

    if (isset($someArray['someKey'])) {
        $myVar = $someArray['someKey']
    }
    

    or something like:

    if(is_array($someArray['someKey'])) {
        $theme_img = 'recent_works_iso_thumbnail';
    }else {
        $theme_img = 'recent_works_iso_thumbnail';
    }
    
    0 讨论(0)
  • 2020-12-19 06:22

    1.

     if(1 == @$manta_option['iso_format_recent_works']){
          $theme_img = 'recent_works_thumbnail';
     } else {
          $theme_img = 'recent_works_iso_thumbnail';
     }
    

    2.

    if(isset($manta_option['iso_format_recent_works']) && 1 == $manta_option['iso_format_recent_works']){
        $theme_img = 'recent_works_thumbnail';
    } else {
        $theme_img = 'recent_works_iso_thumbnail';
    }
    

    3.

    if (!empty($manta_option['iso_format_recent_works']) && $manta_option['iso_format_recent_works'] == 1){
    }
    else{
    }
    
    0 讨论(0)
  • 2020-12-19 06:29

    Magic word is: isset

    Validate the entry:

    if(isset($manta_option['iso_format_recent_works']) && $manta_option['iso_format_recent_works'] == 1){
        $theme_img = 'recent_works_thumbnail';
    } else {
        $theme_img = 'recent_works_iso_thumbnail';
    }
    
    0 讨论(0)
提交回复
热议问题