Php 如何修复$\u GET上未定义的索引?

Php 如何修复$\u GET上未定义的索引?,php,get,http-headers,isset,Php,Get,Http Headers,Isset,我无法理解如何修复此通知。它在$\u GET['']上给出了一个未定义的索引,我无法设置isset(),因为它会阻止查询和代码 class.php private $perPage = 8; private $startPage = 0; public function latestArticles() { if($_GET['page'] <= 1) NOTICE ERROR <-- is here on the $_GET['page']; $this

我无法理解如何修复此通知。它在
$\u GET['']
上给出了一个未定义的索引,我无法设置
isset()
,因为它会阻止查询和代码

class.php

private $perPage = 8;
private $startPage = 0; 

public function latestArticles()
{
    if($_GET['page'] <= 1) NOTICE ERROR <-- is here on the $_GET['page'];
        $this->startPage = 0;
    else
        $this->startPage = $_GET['page'] * $this->perPage - $this->perPage;

    $this->db->setAttribute(PDO::ATTR_EMULATE_PREPARES, FALSE);
    $sth = $this->db->prepare("SELECT * FROM articles ORDER BY id DESC LIMIT ?, ?");
    $sth->execute(array($this->startPage, $this->perPage));

    $data = $sth->fetchAll();
    return $data;
}
<?php
    if(!isset($_GET['page']))
        $page = 1;
    else 
        $page = $_GET['page'];

    foreach($latestArticles as $article)
    {
        $title = $article['title'];

        echo ''.$title.'';
    }

    $prev = $page-1;
    $next = $page+1;

    echo "
    <a class='nxt_prevButton' href='?page=$prev'>previous page</a>
    <a class='nxt_prevButton' href='?page=$next'>next page</a>
    ";
?>
失败的尝试 我已经设置了
isset()
empty()
,但它不允许代码工作,这是一个用于获得更多结果的下一个和上一个按钮,每页8个结果。(通知消失,但代码(查询)停止工作)


解决这个问题的方法是什么,修复代码?未使用错误报告(0)

您肯定需要使用
isset
,这只是计算逻辑的问题

我认为,如果我理解正确,你可以这样做:

if(!isset($_GET['page']) || $_GET['page'] <= 1)
    $this->startPage = 0;
else
    $this->startPage = $_GET['page'] * $this->perPage - $this->perPage;
if(!isset($_GET['page'])|$_GET['page']起始页=0;
其他的
$this->startPage=$\u GET['page']*$this->perPage-$this->perPage;

因此,如果未设置
$\u GET['page']
,则
起始页将为0。

我明白了,这是有道理的,谢谢。您是否应始终使用isset或何时有规范?@iBrazilian2如果您想避免收到“未定义索引”通知,您必须在尝试访问它之前检查数组索引,是的。