【问题标题】:How to fix: Warning: Illegal offset type in isset or empty in F:\xampp\htdocs\digikalamvc\core\model.php on line 140如何修复:警告:第 140 行的 isset 中存在非法偏移类型或 C:\xampp\htdocs\digikala mvc\core\model.php 中为空
【发布时间】:2019-03-25 08:25:50
【问题描述】:

如何解决此错误:

警告:isset 中的偏移类型非法或 F:\xampp\htdocs\digikalamvc\core\model.php 第 140 行为空

致命错误:未捕获错误:F:\xampp\htdocs\digikalamvc\models\model_showcart4.php:90 中不支持的操作数类型:90 堆栈跟踪:#0 F:\xampp\htdocs\digikalamvc\controllers\showcart4.php(31 ): model_showcart4->saveOrder(Array) #1 F:\xampp\htdocs\digikalamvc\core\app.php(34): Showcart4->saveorder() #2 F:\xampp\htdocs\digikalamvc\index.php( 7): App->__construct() #3 {main} 在第 90 行的 F:\xampp\htdocs\digikalamvc\models\model_showcart4.php 中抛出

model.php 第 140 行的代码:(if (isset($_SESSION[$name])) {)

public static function sessionGet($name) {
    if (isset($_SESSION[$name])) {
        return $_SESSION[$name];
    } else {
        return false;
    }
}

model_showcart4.php 第 90 行中的代码:

$priceTotal =$basketPrice-$basketDiscount+$postprice;

代码model_showcart4:

$basket = $basketInfo[0];
    $basket = serialize($basket);
    $basketPrice = $basketInfo[1];
    $basketDiscount = $basketInfo[2];

    $postprice = $this->calculatePostPrice($addressInfo['city']);
    $postType = self::sessionGet(['postType']);
    if ($postType == 1) {
        $postprice = $postprice['pishtaz'];
    } elseif ($postType == 2) {
        $postprice = $postprice['sefareshi'];
    }

    $priceTotal =$basketPrice-$basketDiscount+$postprice;

代码 showcart4.php 在第 31 行:

function saveorder() {
    $this->model->saveOrder($_POST);
}

【问题讨论】:

  • 您将数组作为键传递给 isset 中的数组。 $postType = self::sessionGet(['postType']); 参数是数组。但是数组不能是其他数组中的键

标签: php session xampp offset


【解决方案1】:

$postType = self::sessionGet(['postType']); 如您所见,参数是一个数组。 所以在这里 isset($_SESSION[$name]) 代码无效,因为数组键应该是整数或字符串,而不是数组。

$postType = self::sessionGet('postType'); - 我猜这应该可行

【讨论】:

    【解决方案2】:

    您的警告表明为您的函数提供的键在 $_SESSION 数组中不存在。在第 83 行的 model_showcart4.php 中,您向函数传递了一个数组,这可能是导致此错误的原因。

    我建议在你的 if 语句中添加一个额外的检查。使用array_key_exists 函数检查键是否确实存在于给定数组中,因此如果由于某种原因给出的键根本不存在,您的代码不会中断。

    F:\xampp\htdocs\digikalamvc\core\model.php

    public static function sessionGet($name) {
        if (array_key_exists($name, $_SESSION) && isset($_SESSION[$name])) {
            return $_SESSION[$name];
        } else {
            return false;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-28
      • 1970-01-01
      相关资源
      最近更新 更多