Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

Problems with variables after upgrading to PHP 5.6?

I have a script which is working perfectly fine under PHP 5.4.3. However, after upgrading to 5.6.26, I am getting an undefined variable error without changes to the script.

The following script is supposed to check for a variable passed from the $_GET array. If the variable doesn't exist, then it is declared. The $cur_time variable is sometimes passed via the $_GET array so that I can manually check the information on the page at a future time. The below script normalizes the time sits it if it doesn't exist.

extract($_GET);

if (!$cur_time){

$cur_time = date('Y-m-d');

} else {

$cur_time = strtotime($cur_time);

$cur_time = date("Y-m-d');

}

This code is returning the error "Undefined variable: cur_time in...". However, the correct time is showing up later in the script inside a query where the $cur_time variable is used.

2 Answers

Relevance
  • 5 years ago

    This is the first time I ever saw anyone use extract($_GET); I think one reason is that it becomes unclear where those variables come from. $_GET['cur_time'], makes it clear that cur_time is from $_GET, and if it doesn't exist, trace the error to source of problem. Overwriting existing variable problem, if you already have a $cur_time variable in that scope, it will be populated with what's in GET, making the problem difficult harder to find.

    Your problem is here, sorry caps :)

    else {

    $cur_time = strtotime($cur_time); // THIS BECOMES REDUNDANT

    $cur_time = date("Y-m-d'); // BECAUSE THIS OVERWRITES THAT

    }

    ----

    // See Example#2 here on how to check time --> http://php.net/manual/en/function.strtotime.php

    // This is quite dense, but it should replace what you have there.

    // $current_date would be what you reference later to use

    if (empty($_GET['cur_time']) || ($current_date = strtotime($_GET['cur_time'])) === false) {

    $current_date = date('Y-m-d');

    }

    ----

    Walk through:

    The || or condition first checks left hand side if it is true, if it is, we are good to go, right side won't be processed, code will jump into the if block.

    If $_GET['cur_time'] is empty, we proceed to populate $current_date with today's date.

    If $_GET['cur_time'] is not empty, script proceeds to right hand side of the || conditional check. Here, we check if $_GET['cur_time'] is valid time by using strtotime() and assigning the result to a variable $current_date, if $current_date is false, it means strtotime cannot parse that string, and we proceed to populate $current_date with today's date.

    So if $current_date is not false, it will hold a correctly formatted date that can be used later in your script.

  • Chris
    Lv 7
    5 years ago

    Replace

    if (!$cur_time) {

    with

    if (!isset($cur_time)) {

    There's also a mismatched quote in the second date() String.

    Also, in the else clause the first line is redundant.

Still have questions? Get your answers by asking now.