PHP code notice

PHP code of whmbackup.solutuions gives a php warning (any support channel is apparently dead for now):

PHP Notice: Undefined offset: 1 in /home/myuser/whmbackupsolutions/whmbackup.php on line 49

And:

PHP Notice: Trying to access array offset on value of type null in /home/myuser/whmbackupsolutions/whmbackup.php on line 202

The line 49 says this:

list($arg_x, $arg_y) = explode('=', $arg);

Line 202 says:

if ($save_status["error"] == "1")

The entire code (copy/pasted):
https://wtools.io/paste-code/b6S5

I understand that PHP is getting less and less "liberal" and that the warning might turn into an error only with newer version updates.
The script itself works fine for now.
But if anyone has an easy fix for the problem, I'd appreciate it (I'm not a programmer).

BikeGremlin I/O
Mostly WordPress ™

Comments

  • First is because one of the arguments provided is blank, e.g. param=. The solution is do not provide empty parameters.

    Second is because update_status function is returning null (or just returning) rather than returning an array, but that function is in an external file.

    Thanked by (3)bikegremlin ialexpw Falzo
  • @bikegremlin said:
    PHP code of whmbackup.solutuions gives a php warning (any support channel is apparently dead for now):

    PHP Notice: Undefined offset: 1 in /home/myuser/whmbackupsolutions/whmbackup.php on line 49

    And:

    PHP Notice: Trying to access array offset on value of type null in /home/myuser/whmbackupsolutions/whmbackup.php on line 202

    The line 49 says this:

    list($arg_x, $arg_y) = explode('=', $arg);

    Line 202 says:

    if ($save_status["error"] == "1")

    The entire code (copy/pasted):
    https://wtools.io/paste-code/b6S5

    I understand that PHP is getting less and less "liberal" and that the warning might turn into an error only with newer version updates.
    The script itself works fine for now.
    But if anyone has an easy fix for the problem, I'd appreciate it (I'm not a programmer).

    The first is the same as https://stackoverflow.com/questions/16728268/undefined-offset-1

    Basically that some of the strings/inputs may not contain a "=" so you should check if the string contains a "=" before doing an explode with it e.g. https://gist.github.com/ialexpw/8b26023071a0e4b8aad35a448ee483ee

  • edited September 2021

    @tetech said:
    First is because one of the arguments provided is blank, e.g. param=. The solution is do not provide empty parameters.

    Second is because update_status function is returning null (or just returning) rather than returning an array, but that function is in an external file.

    most likely comes down to a comparable issue like with the warning seen in wordpress ;-)

    coders tend to be lazy and do not properly (pre-)check for cases out of scope, don't initialize vars or simply rely on PHP being forgiving internally and deal with logical errors gracefully. worked for decades...

    @bikegremlin said: I understand that PHP is getting less and less "liberal"

    yes.

    Thanked by (2)ialexpw bikegremlin
  • Line 202 can be fixed by changing it to the following:

    if (is_array($save_status) && $save_status["error"] == "1")

    Thanked by (1)bikegremlin
  • @Mr_Tom said:
    Line 202 can be fixed by changing it to the following:

    if (is_array($save_status) && $save_status["error"] == "1")

    might still throw a warning, if there is no "error" key in that array ;-)

    Thanked by (1)bikegremlin
  • @Falzo said: might still throw a warning, if there is no "error" key in that array ;-)

    Good point. Ignore my "fix" lol

  • @Mr_Tom said:
    Line 202 can be fixed by changing it to the following:

    if (is_array($save_status) && $save_status["error"] == "1")

    I did not bother to check if $save_status is not already always an array, but this is sorta bulletproof:
    if (is_array($save_status) && array_key_exists("error", $save_status) && $save_status["error"] == "1")

  • edited September 2021

    @Groentjuh said:

    @Mr_Tom said:
    Line 202 can be fixed by changing it to the following:

    if (is_array($save_status) && $save_status["error"] == "1")

    I did not bother to check if $save_status is not already always an array, but this is sorta bulletproof:
    if (is_array($save_status) && array_key_exists("error", $save_status) && $save_status["error"] == "1")

    rather not bulletproof, but more likely to still produce the exact same php notice as can be seen initially. if you look at it closely it is complaining about a value of null , so $save_status in this case probably contains something like 'error' => null ...which would still have array_key_exists being true!

    to fix it properly make sure there is a correct return value for 'error' in any case within the update_status function, so that you ideally can roll with a plain and simple if($save_status["error"]) in the end.

  • mikhomikho AdministratorHosting ProviderOG

    so add before $save_status["error"] == "1" a check if the value is null and if it is, change it from null to 0.

    0 usually means no error.

    Thanked by (1)AlwaysSkint
  • @Falzo said:

    @Groentjuh said:
    I did not bother to check if $save_status is not already always an array, but this is sorta bulletproof:
    if (is_array($save_status) && array_key_exists("error", $save_status) && $save_status["error"] == "1")

    rather not bulletproof, but more likely to still produce the exact same php notice as can be seen initially. if you look at it closely it is complaining about a value of null , so $save_status in this case probably contains something like 'error' => null ...which would still have array_key_exists being true!

    to fix it properly make sure there is a correct return value for 'error' in any case within the update_status function, so that you ideally can roll with a plain and simple if($save_status["error"]) in the end.

    I believe error is saying that $save_status is null. Accessing array offset "error" is not possible if $save_status is of type null (, which does not implement ArrayAccess). The check I mentioned will first check if the type is an array, then check if the array offset exists (otherwise you will get another notice: "Notice: Undefined offset: error"). The value of $save_status["error"] does not matter. The comparison "==" allows type juggling, so type will not matter for this comparison. It will not complain about the actual value of $save_status["error"] being null.

    Thanked by (1)Falzo
  • @Groentjuh said:

    @Falzo said:

    @Groentjuh said:
    I did not bother to check if $save_status is not already always an array, but this is sorta bulletproof:
    if (is_array($save_status) && array_key_exists("error", $save_status) && $save_status["error"] == "1")

    rather not bulletproof, but more likely to still produce the exact same php notice as can be seen initially. if you look at it closely it is complaining about a value of null , so $save_status in this case probably contains something like 'error' => null ...which would still have array_key_exists being true!

    to fix it properly make sure there is a correct return value for 'error' in any case within the update_status function, so that you ideally can roll with a plain and simple if($save_status["error"]) in the end.

    I believe error is saying that $save_status is null. Accessing array offset "error" is not possible if $save_status is of type null (, which does not implement ArrayAccess). The check I mentioned will first check if the type is an array, then check if the array offset exists (otherwise you will get another notice: "Notice: Undefined offset: error"). The value of $save_status["error"] does not matter. The comparison "==" allows type juggling, so type will not matter for this comparison. It will not complain about the actual value of $save_status["error"] being null.

    You're correct - it does prevent the warning. But I think the point is that the function itself should be checked to see why it is returning null and what might have been intended, rather than just come up with a way to suppress the error - should the function just return array('error' => 1); instead? And the function itself is in another (not disclosed) file so we can't really reach a conclusion.

  • bikegremlinbikegremlin ModeratorOG
    edited September 2021

    @tetech said:

    @Groentjuh said:

    @Falzo said:

    @Groentjuh said:
    I did not bother to check if $save_status is not already always an array, but this is sorta bulletproof:
    if (is_array($save_status) && array_key_exists("error", $save_status) && $save_status["error"] == "1")

    rather not bulletproof, but more likely to still produce the exact same php notice as can be seen initially. if you look at it closely it is complaining about a value of null , so $save_status in this case probably contains something like 'error' => null ...which would still have array_key_exists being true!

    to fix it properly make sure there is a correct return value for 'error' in any case within the update_status function, so that you ideally can roll with a plain and simple if($save_status["error"]) in the end.

    I believe error is saying that $save_status is null. Accessing array offset "error" is not possible if $save_status is of type null (, which does not implement ArrayAccess). The check I mentioned will first check if the type is an array, then check if the array offset exists (otherwise you will get another notice: "Notice: Undefined offset: error"). The value of $save_status["error"] does not matter. The comparison "==" allows type juggling, so type will not matter for this comparison. It will not complain about the actual value of $save_status["error"] being null.

    You're correct - it does prevent the warning. But I think the point is that the function itself should be checked to see why it is returning null and what might have been intended, rather than just come up with a way to suppress the error - should the function just return array('error' => 1); instead? And the function itself is in another (not disclosed) file so we can't really reach a conclusion.

    I had included the entire code in the original post, if that's of any help (I thought it would be important):
    https://wtools.io/paste-code/b6S5

    This is the entire "package" download link:
    https://whmbackup.solutions/download-latest-version/

    And this is the, (for) now apparently abandoned, project's home page:
    https://whmbackup.solutions/

    BikeGremlin I/O
    Mostly WordPress ™

  • function update_status($account_list, $log_file, $config_name = null)
    {
        global $directory;
        $store = json_encode(array("account_list" => $account_list, "log_file" => $log_file));
    
        $config_file = "config";
        if ((isset($config_name)) && (!empty($config_name)))
            $config_file = preg_replace('/[^a-zA-Z0-9]/', '', $config_name);
    
        $file_name = $directory . "temp" . DIRECTORY_SEPARATOR . "status-" . $config_file . ".php";
        $fp = fopen($file_name, 'w+');
        if ($fp == false)
            return array("error" => "1", "response" => "Unable To Open Status File (" . $file_name . ").");
    
        // Write to Status File.
        $fw = fwrite($fp, $store);
        if ($fw == false)
            return array("error" => "1", "response" => "Unable To Write to Status File (" . $file_name . ").");
    
        // Close Status File.
        $fc = fclose($fp);
        if ($fc == false)
            return array("error" => "1", "response" => "Unable To Close to Status File (" . $file_name . ").");
    }
    

    Yea, that function does not return anything. I guess this code could use some "Exception"-love actually. Possibly adding return array("error" => "0") at the end of this function might also solve all issues.

  • @Groentjuh said:

    @Falzo said:

    @Groentjuh said:
    I did not bother to check if $save_status is not already always an array, but this is sorta bulletproof:
    if (is_array($save_status) && array_key_exists("error", $save_status) && $save_status["error"] == "1")

    rather not bulletproof, but more likely to still produce the exact same php notice as can be seen initially. if you look at it closely it is complaining about a value of null , so $save_status in this case probably contains something like 'error' => null ...which would still have array_key_exists being true!

    to fix it properly make sure there is a correct return value for 'error' in any case within the update_status function, so that you ideally can roll with a plain and simple if($save_status["error"]) in the end.

    I believe error is saying that $save_status is null. Accessing array offset "error" is not possible if $save_status is of type null (, which does not implement ArrayAccess). The check I mentioned will first check if the type is an array, then check if the array offset exists (otherwise you will get another notice: "Notice: Undefined offset: error"). The value of $save_status["error"] does not matter. The comparison "==" allows type juggling, so type will not matter for this comparison. It will not complain about the actual value of $save_status["error"] being null.

    I see and stand corrected. I only remembered about some difference with array_key_exists and isset for key values of null and therefore probably misinterpreted the error message. looking at the update_status function - your are right, $save_status is of type null

  • @bikegremlin said:
    I understand that PHP is getting less and less "liberal" and that the warning might turn into an error

    One of the reasons people hate PHP. Not only it allows you to write bad code but pretty much encouraged it for way too long.

    Thanked by (1)bikegremlin
Sign In or Register to comment.