Link to home
Start Free TrialLog in
Avatar of Jorge Batres
Jorge BatresFlag for United States of America

asked on

PHP 7.4.2 Notice: Trying to access array offset on value of type bool

Hi, I just upgraded one of my websites to PHP 7.4.2 and now I'm getting this message:

 
Notice: Trying to access array offset on value of type bool

The line of code is:
$last_name = $end_element['name'];

Open in new window


Could you please help me solve this? I'm attaching the script and the error is in line 26.

Thanks,

Jorge Batres
PageConfig.php
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Hi,

If you look at this code:

$end_element = end($this->actions);
$last_name = $end_element['name'];

You're calling end() on $this->actions. This method "returns the value of the last element or FALSE for empty array". The fact that your error states that you're trying to get an index from a boolean tells me that your end() call returned FALSE, indicating that $this->actions is not returning a populated array. At the start of your code you have var $actions = array(), but there's nothing I can see that actually populates it.
It is possible that the array is being populated (or supposed to be) in the parent.

What does the following file contain

require_once ROOT . 'modules/XForm/XmlParser.php';

Open in new window

Avatar of Jorge Batres

ASKER

I modified the line with this and the message went away but now the script is showing 2 other lines in another script called Page.php on lines 136 and 151. I'm trying attaching the file here but I don't know how to do it.


$last_name = isset($end_element['name']); 

Open in new window

this are the contents of XMLParser.php


<?php
/**
 * SAX Parser for xml file. Handlers implement in child classes.
 *
 * @copyright MitriDAT (c) 2005
 * @author Yuriy Tumakha <tumakha@gmail.com>
 * @since June 06, 2005 10:11
 * @version 1.0
 */

class XmlParsers
{

	var $_parser;
	
	function XmlParser($encoding = '') //  "ISO-8859-1"
	{
		if ($encoding)
			$this->_parser = xml_parser_create($encoding);
		else
			$this->_parser = xml_parser_create();
		xml_set_object($this->_parser, $this);
		xml_set_element_handler($this->_parser, "startElement", "endElement");
		xml_set_character_data_handler($this->_parser, "characterData");
	}
	
	function __destruct()
	{
		if (!empty($this->_parser)) xml_parser_free($this->_parser);
		unset($this->_parser);
	}
	
	function startElement($parser, $name, $attrs)
	{
		//Implement this function in child class.
	}
	
	function endElement($parser, $name)
	{
		//Implement this function in child class.
	}
	
	function characterData($parser, $data)
	{
		//Implement this function in child class.
	}
	
	function parse($file)
	{
		if (!($fp = fopen($file, "r")))
		{
			die("Could not open XML file '$file'");
		}
		
		while ($content = fread($fp, 4096))
		{
			if (!xml_parse($this->_parser, $content, feof($fp)))
			{
				die(sprintf("XML error: %s at line %d",
					xml_error_string(xml_get_error_code($this->_parser)),
					xml_get_current_line_number($this->_parser)));
			}
		}
	}

}//class XmlParser
?>

Open in new window

and these are the two new errors:

Page.php

Notice: Undefined index: name in /httpdocs/calendar/modules/Core/Page.php on line 136

Notice: Undefined index: name in /httpdocs/calendar/modules/Core/Page.php on line 151

All you're doing with this:

$last_name = isset($end_element['name']);

Is setting $last_name to either true or false, which is not what your code expects. For some reason, actions property is not being filled with an array. Take a look at your XML config file and make sure that's in the correct format

Ok, so I'll revert this line to its original form

$last_name = isset($end_element['name']);

Open in new window

but as far as the other file, I'm lost 

ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial

Thank you Julian Hansen, I applied the line of code you provided and it worked! All the messages went away.


Thank you so much.

That could be good news - but the fact that there are no more errors is not a guarantee the code is right. Assuming what you showed us is the complete code base the code does not really make sense - and all that might have happened with my last suggestion is that the CPU is doing unnecessary work.

I would still be interested to know what the intended purpose of the PageConfig is so that we can properly assess whether the code in the class is doing what it is supposed to.

I agree with you, Julian. The fact is that I bought this script back in 2005 from a guy that worked for a company that used to sell PHP forms.


The script wasn't written for me but was actually modified to suit my needs.  


I use the same script for two different applications that are somewhat connected, The calendar script and my reservations script.


I would actually like to have the scripts re-written to something lighter and more efficient, as for the last few years, I have been updating all the deprecated features.