Why Does PHP Not Complain When I Treat a Null Value as an Array Like This

Why does PHP not complain when I treat a null value as an array like this?

There is an active bug report started at 2006.

And in documentation it is a notice about this in String section.
Sample Image

Why is my variable being treated like it's null when it's being passed a value?

You are unsetting "$formname" here? $formname = ""; So doesn't matter if you pass it in the method call, it will always be empty

Why php does not complain when referencing a non existing variable?

If memory of the PHP interpreter reference var allocation serves me right, PHP will create a null element in the hash table with a key like the one you sent and reference it. This is visible by running the following test:

<?php
$i = 0;
$arr = [];
$arrS = null;
$v = memory_get_peak_usage();
for ($i = 0; $i < 150; $i++) {
$arrS = &$arr[rand()];
}
$v = memory_get_peak_usage() - $v;
echo $v;

Until the default heap size, PHP will return exactly an extra 0 memory used - as it is still allocating already "prepared" array items (PHP keeps a few extra hash table elements empty but allocated for performance purposes). You can see this by setting it from 0 to 16 (which is the heap size!).

When you get over 16, PHP will have to allocate extra items, and will do so on i=17, i=18 etc..., creating null items in order to reference them.

P.S: contrary to what people said, this does NOT throw an error, warning or notice. Recalling an empty item without reference would - referencing an inexistent item does not. Big big BIG difference.

$foo[bar] = 1; Can I ask PHP to complain if $foo doesn't exist?

Unfortunately, you are setting up an array with the command. Why would php throw an exception if you are setting up this?

It's like assigning a value to a variable and then asking why did PHP assign the value to the variable?

$foo["bar"] = 1;

print_r($foo);
// This prints the following:
// Array ( [bar] => 1 )

The correct way of checking would be:

if(isset($foo))
{
$foo['bar'] = 1;
}
else
{
// do something if $foo does not exist or is null
}

Hope this helps! In short the answer to your question is no: there isn't a way to make PHP throw an exception or print a warning in your example.

How to fix Trying to access array offset on value of type null error

When you receive this error after fetching data from the database then it means that the database didn't found any matching rows. Most database fetching functions return either null or an empty array when there are no matching records or when the result set has been exhausted.

To solve the problem you need to check for the truthiness of the value or for the existence of the key that you want to access.

$monday_lectures = "SELECT * from lectures where lecture_time = '11am to 1pm' and lecture_day = 'firday'";
$result_11to1 = mysqli_query($con, $monday_lectures);
$m11to1 = mysqli_fetch_array($result_11to1);
if ($m11to1 && $m11to1["lecture_day"] == !'') {
echo "<td>".$m11to1["lecture_name"]."</td>";
} else {
echo "<td> no class</td>";
}

If what you are after is a single value from the result array then you can specify a default in case the result is not present.

$monday_lectures = "SELECT * from lectures where lecture_time = '11am to 1pm' and lecture_day = 'firday'";
$result_11to1 = mysqli_query($con, $monday_lectures);
$m11to1 = mysqli_fetch_array($result_11to1);
$lecture = $m11to1["lecture_day"] ?? null;

The same applies to PDO.

$monday_lectures = $pdo->prepare("SELECT * from lectures where lecture_time = '11am to 1pm' and lecture_day = 'firday'");
$monday_lectures->execute();
$m11to1 = $monday_lectures->fetch();
$lecture = $m11to1["lecture_day"] ?? null;

Using short circuiting to get first non-null variable

It would be (PHP 5.3+):

echo $post['story'] ?: $post['message'] ?: $post['name'];

And for PHP 7:

echo $post['story'] ?? $post['message'] ?? $post['name'];


Related Topics



Leave a reply



Submit