PHP 5.4 $ _GET注意:未定义的索引:

时间:2016-03-27 23:45:26

标签: php get undefined notice

此代码适用于PHP 5.3,但不适用于PHP 5.4>错误提示:未定义的索引:

//station
$station = $_GET['station'];

if (!$station)
    $station = 'main';



//base path

2 个答案:

答案 0 :(得分:1)

这取决于你的error_reporting的设置,但是你应该总是检查'station'是否已经通过http设置了 www.example.com?station=piccadily

if (isset($_GET['station']) && $station = $_GET['station'])
else $station = 'main;

或将其简化为:

$station = isset($_GET['station']) ? $_GET['station'] : 'main';

因为PHP7你可以使用coalesce operator编写:

$station = $_GET['station'] ?? 'main';

答案 1 :(得分:0)

<?php 
    $station = $_GET['station'];
    error_reporting(0);
    if ( isset ( $station ) ) :
        $station = 'main';
    else :
        $action = 'null';
    endif;
 ?>
相关问题