I am trying to understand if it's correct for PHP to throw an undefined offset error when using isset() function.
The data comes from an eloquent collection.
$interest->vehicles
The problem happens when the collection is empty.
Below code tested on 3 different setups (Mac+Mamp PRO, Windows+Xampp, Mac+Mamp), throws an undefined offset in first two setups but not in mine which is the third one (Mac+Mamp)
All tested scenarios are done with PHP 7 and display errors On.
isset($interest->vehicles[0]['make'])
I understand why the error may be happening to the other setups as the offset i try to isset doesn't exist, but the thing i don't understand is why i don't see the error on my setup when using isset.
I also tried to reinstall Mamp on my maching, upgraded to Sierra OS, restarted my laptop, changed php.ini settings to always display all errors .
ErrorException in Collection.php line 1043:
Undefined offset: 0 (View: /Users/efood-leo/Sites/cardealer/resources/views/panel/interest/form.blade.php) (View: /Users/efood-leo/Sites/cardealer/resources/views/panel/interest/form.blade.php)
This is the error i am talking about, and happens only if i try to retrieve
$interest->vehicles[0]['make']
When the vehicles[0] does not exist.
if i do :
isset($interest->vehicles[0]['make'])
Then there is no error in my setup, and 2 other devs report the error still happens with isset.
Try this code.
Isset() function is returning true somehow so without isset it works
$interest = (object)['vehicles' => []];
echo $interest->vehicles[0]['make'];
Read more on isset():
http://php.net/manual/en/function.isset.php
Related
this is how it displays the error
I tried changing the PHP version to 7.3 still not working
This the line of code that's giving that error
As the error is saying the problem comes from the fact that you try to use a null value as an array, from what I see this code is problematic:
$appitem->stage->sresource['stage_id']
Could be a typo maybe that you have sresource instead of resource and that is the reason why $appitem->stage->sresource is null?
I came across a fat-free framework where indexes are accessed without checking if there's data present or not.
few e.g.
e.g.1
$sObj = new Site($db); // this returns null?
$site = $sObj->getById($sessionSiteId)[0];
e.g.2
$ppObject = new Page($db);
$pheader = $ppObject->getByCodeandsiteid($siteid,'Header');
F3::set('headercode',$pheader[0]['description']);
so these are just few cases. Most of the practice done is to just access the index.
This throws error in my env. php 8.1 and fat-free 3.8.1.
But running this same code on my colleague's env. : php 5.6.40 and fat-free 3.5.1` this doesn't throw error but simply gives null value.
I also checked with some random unassigned variable on his env. and to my surprise, this also seem to simply give null instead of error.
Is this expected behavior on previous versions?
Also checked in phpinfo() that error_reporting value for both is 22527.
we moved our website to a new server that came with a new IP address. What puzzles me; the website login sessions do not work on the new server but when I change the database IP to the old server they are working.
MySQL Version
Old server = 5.1.58- Community
New server = 5.1.68 - Community
At first I thought it was a PHP error but I now believe it's not and suspect its MySQL related. Anyone who knows what might have caused this conflict?
Debugging Error
Notice: A session had already been started - ignoring session_start() in C:\inetpub\wwwroot\gtest\libs\products.php on line 2
Notice: Undefined index: uUserTypeID in C:\inetpub\wwwroot\gtest\admin\index.php on line 50
Notice: Undefined offset: 0 in C:\inetpub\wwwroot\gtest\admin\index.php on line 52
Notice: Undefined offset: 0 in C:\inetpub\wwwroot\gtest\admin\index.php on line 52
Line 50
GetUserType($_SESSION['uUserTypeID'], $UserTypeID, $UserTypeDescr, $Active_Tag);
Line 52
if (($UserTypeDescr[0] == 'Admin') || ($UserTypeDescr[0] == 'Report'))
Let's go through the notices in order:
session_start() was already called before. No need to call it again.
There's no such variable as $_SESSION["uUserTypeID"]. It's not set.
The array at $UserTypeDescr doesn't have a 0 index. It's probably empty. If you got it from a database query, it either failed or returned an empty resultset.
Same as 3.
Notice: A session had already been started - ignoring session_start() in C:\inetpub\wwwroot\gtest\libs\products.php on line 2
At a guess session.auto_start is enabled hence the session is being enabled before the session handler is overridden (or the new session handler is failing).
The most safest method of preventing such error (i.e. Session already started) is by always checking if session has already been started before starting a session.
You can do this by simply including
if (!isset($_SESSION))session_start();
I hope this solve the problem? upon trying it, you can simply add your comment or observations to further improve the answer and above all, solve the probelm. I hope this helps?
I am seeing an undefined property warning PHP notice on my registration form in Joomla 2.5 , wondering whats causing this issue. Its happening on the lines which try to echo with $this
<?php if($this->title == 'Me'){echo "selected=\"selected\"";}?>>Me</option>
Update: $this in this case is referring to the correct class. Its the $title thats the issue which is prompting the undefined property issue. Whats the remedy to use variables in a php file which have not been defined yet?
Thanks.
As the message indicated that it is an undefined variable, after running xdebug I noticed that it was not being set and that was the issue. Thus checking if the variable is set and only then using the variable would help.
Using adLDAP.php class
receiving following error: Notice: Undefined index: memberof in /web/ee_web/include/adLDAP.php on line 762
line 762: if (is_array($groups[0]["memberof"])) {
Also receiving error: Notice: Undefined index: count in /web/ee_web/include/adLDAP.php on line 982
line 982: $entries[0]["memberof"]["count"]++;
Unsure of what I need to do to resolve these error messages, it seems that the script is working fine, but I'd like get rid of these errors.
Using: http://adldap.sourceforge.net/wiki/doku.php?id=api
You could edit the code to something along the lines of:
if(isset($groups[0]["memberof"]))
{
if (is_array($groups[0]["memberof"])){ ... }
}
And
if(isset($entries[0]["memberof"]["count"]))
{
$entries[0]["memberof"]["count"]++;
}
It will keep you from getting the errors, though it won't necessarily handle some pretty lax sanity checking by the original author of the code.
It seems like you have your error_level set to show every possible error/warning/notice, that's why you're getting it.
If the script is working fine, then it's not an error, simply a missing check the coder forgot to put in the library.
To get rid of thos messages, you have 2 options:
a) Use # before the calls you do to that library, such as
$var = #the_function(param1);
This will avoid those messages for just that line.
b) Set the error level to something like this with error_reporting():
error_reporting(E_ALL ^ E_NOTICE);
This will affect the whole script you're running.
It's up to you what to use depending on the project.