Jump to content

Uninitialized string offset


papasmurf

Recommended Posts

I’m new to this group so I’m not sure if I’m posting this in the right category but I’m having a problem with some error messages that appear in Chrome’s View Page Source. They are generated after running a script that displays correctly on a web page but not in View Page Source. They seem to be connected to an array that I’m using in one of the classes. Without getting into too much detail, gettype() calls the array ‘array’ on the page but in View Page Source it calls it ‘string’. Because of this it generates a number of ‘Notice: Uninitialized string offset’ messages in code that follows that statement. This occurs when xdebug is enabled or disabled. If I hard-code this array in a separate script, independent of the program I’ve written, and run a loop to display data I get no errors in View Page Source as it only occurs within the program.

If anyone has some good advice as to why this is happening I’d appreciate it. Thanks.

Link to comment
Share on other sites

As you've already figured it, the problem is you think a variable is an array but for some reason something made it be a string. So you have to find out where that's happening.

If you aren't sure where to start looking, posting your code would be a good first step.

 

Link to comment
Share on other sites

Its part of a method within a class that’s over 6000 lines but this is the part that causes the error…

// Retrieve the MySql array
$reportArr = $this->findArrValue(‘logArr’);
echo 'type: '.gettype($reportArr);

Its calling a method that looks up a value in another array. The value is the array I need. It finds it and returns it. Then like I said it displays it and everything’s fine on-screen. It’s just the HTML source code that shows it as a string and not an array.

Link to comment
Share on other sites

The problem is not that the page says the value is a string. The problem is that the value is a string. You have to find out why that is, even if that means dealing with 6000 lines of code.

findArrValue() is the first place to start. If you're not sure you'll be able to spot where the problem is just by reading the code then insert some debugging statements into the method, such as

echo "<!-- line ", __LINE__, " value is ", gettype($variable), " -->";

 

If the View Source shows something different from the actual page then your browser isn't truly showing you the source of the page. It means it's re-requesting the page and getting a different result. For some reason I don't know because I have no idea what your application is or how it works, but likely involving a form or the session.

Link to comment
Share on other sites

Well I've narrowed the problem down to the Chrome browser. I get no errors in IE or Firefox. It appears that when data is sent to another page via _POST (as in a form), the HTML/data that is displayed in the forwarded pages' View Page Source does not reflect the same data that appears on the current page so as I see it, any errors that are displayed should be ignored. As a result I intend to use Firefox instead of Chrome for development. I can't find any recent posts about any fixes Google may have implemented regarding this issue so I'm assuming it's still not fixed. Perhaps they don't consider it important? Thank you all for trying to help me out with this. I think I can rest assured that it's not a coding issue.

Link to comment
Share on other sites

I'm confused by this line:

Its calling a method that looks up a value in another array. The value is the array I need.

You refer to the expected result of the function as an array but are not getting that.  What is the 'thing' that this function is searching thru to find your result - is it an array of arrays?

Also - you say that the results 'displays' fine but you are unhappy with the html source code.  What DOES the display show you if it is not a simple string?  You haven't mentioned that. 

Link to comment
Share on other sites

browsers and other http clients can/do make extra requests to pages for different reasons and your code must deal with them.

it's likely that your post method form processing code is receiving a get request that it should ignore. is your code detecting that a post request was made before running any of the form processing code?

as to the error you are getting, for a likely extra get request, it sounds like the variable is being initialized to an empty string or perhaps being assigned an empty string rather than having an array element added to the variable. 

Link to comment
Share on other sites

findArrValue() searches an array that contains arrays and strings for a value. The method is passed a string that is a key in one of the subarrays. When it finds that it will return the value, be it a string or an array. In this case it’s an array. I know it returns an array because the array is subsequently processed and displayed as a table on the console. As I mentioned in a previous post I used gettype() to confirm that an array is returned and the console displays ‘type: array’ but in View Page Source it reads ‘type: string’.

I tried including:

 error_reporting(E_ALL);

 ini_set('display_errors', '1');

at the beginning of the script but the output is the same. Thanks for the suggestion though.

Link to comment
Share on other sites

I am assigning the value in the _POST request to a variable only if it is set:

if (isset($_POST["log_date"])) {    
            // Get the log date
            $logDate = $_POST["log_date"]; // e.g. 18-Feb-18    

}

This is being done after some supporting scripts have been included and a session is started:

session_start();

I’m not sure if that make a difference? All processing is done after this happens.

As to your mention of a possible empty string, it sounds plausible but again, why would it display the arrays contents as a table if it was simply an empty string. Also, this only occurs in Chrome and not IE or Firefox. It’s mysterious to say the least.

Link to comment
Share on other sites

Since you can't use PHP to echo out an array without breaking it down to individual elements I'm wondering if your view of the html source is missing the fact that there are probably multiple displays of the elements of the returned 'array' value. 

Link to comment
Share on other sites

Since I am essentially a PHP newbie, my application is heavy on methods. Efficiency was not necessarily considered a priority and if I were to make the code available via GitHub (or another similar site), I would welcome any advice that would enhance its performance but for now I’ll just stick to the problem at hand.

I need to explain that the value (which happens to be an array), is taken from a CSV file that Excel exported. This CSV is imported to a phpMyAdmin table, then converted to an array using a third-party script.

The main script for this page ‘taxi.php’ accepts _POST data from a previous page, then passes this and other data, including the MySql array, to a method setData(), which stores it all as a array class variable $data, which is used throughout the program. The script ‘taxi.php’ then calls a method createPage() which returns a long HTML string that displays  the entire page.

The HTML is derived from createPage() calling, in sequence, createHtml(), create(), setHtml() and finally getHtml(), which is returned to ‘taxi.php’ and echoed.

The errors I am receiving begin in a method addDetail(), which is one of the methods used for processing this array. As you can see it doesn’t appear as though the errors are caused by code that displays the contents of the array but rather code that processes it prior to display. I would gladly provide you with code but my dilemma is which code do you want to see. The methods I have mentioned so far reside in six classes and thousands of lines of code.

Link to comment
Share on other sites

You can describe what you have all you want. If we cant see the code for this case this is just going to be an endless thread like I said. We are now SIXTEEN posts in and you are no closer to an answer than the first one. I told you what you need to do. If you refuse to provide what is asked of you then you are just going to have to figure out the problem yourself. Despite our many years of expertise, we do not have magic crystal balls and we cannot see your screen. Since you do not know what code to provide you need to provide all of it. GitHub is the simplest way for us to review a lot of code easily. The problem at hand, is you do not know the problem at hand and neither do we.

Link to comment
Share on other sites

Papa,

I have no idea why you posted all of that stuff when all I (we) want to see is the code that is displaying the output that you say is correct but the browser's 'view source' tool isn't showing you.  (BTW - if the browser is displaying what you want, who cares what the 'view source' is displaying?)

Link to comment
Share on other sites

The code that displays the output is:

echo $page->createPage();

‘All that stuff’ that I posted prior explains the procedure. I hope this satisfies your hunger to see my code.

As for your kind anecdote – that’s my point exactly, who cares what View Page Source reports? But mac_gyver mentioned 'browsers and other http clients can/do make extra requests to pages for different reasons and your code must deal with them.’. This is what prompted me to continue with this investigation.  No-one has yet answered MY question as to whether saving my project to a GitHub repository is a mandatory requirement to communicate on this forum. If I am to pay a minimum of $84/annum to do that, I’d at least appreciate some justification that it’s money well spent. Thank you for your generous cooperation.

Link to comment
Share on other sites

46 minutes ago, ginerjm said:

Papa,

I have no idea why you posted all of that stuff when all I (we) want to see is the code that is displaying the output that you say is correct but the browser's 'view source' tool isn't showing you.  (BTW - if the browser is displaying what you want, who cares what the 'view source' is displaying?)

because it's an indication that the code isn't correctly handling input and it's taking up unnecessary resources detecting and handling execution errors, which a hacker can then learn information from since they are being output to the client. 

the OP should actually be logging all php errors when on a live/public server.

Link to comment
Share on other sites

No - you don't need to post to anywhere else.  Go thru your code, isolate where you think the problem is and post that snippet here, wrapped in a set of "php" tags which can be genereated using the <> icon in the tool bar in the posting window. Avoid posting huge blocks of irrevelant code since that is the surest way of turning off visitors.

Link to comment
Share on other sites

you can zip up your files and attach them to a post in this forum. because this is an execution/data problem, it will take having all the code that is needed to reproduce the problem.

the issue is the forum software probably won't let you post 6000+ lines of code in a reply.

 

Link to comment
Share on other sites

Posting to GitHub is not a requirement. It is also FREE for public repository's. You can also use Bitbucket if you want free private repositories. As @mac_gyver said, you can also attach a zip of the project files.

We are now at TWENTY-TWO posts and counting and still no further than the first post. Are you starting to see what I said about an endless thread?

 

Link to comment
Share on other sites

Master Coder,

From what I read on GitHub, there’s nothing FREE about it unless you’re a student (which I’m not). The only thing that’s free is if you sign up for the $21/mth package, you get a FREE TRIAL so I don’t know where you get your information. The only thing I’ve got to say to you is that if you reply to this post it’ll be TWENTY-THREE posts – now who’s counting? Maybe you're the one that wants to make it an endless thread?

Link to comment
Share on other sites

I have some suggestions for you that may help you up your game a bit in terms of development debugging, but I'll start with the problem at hand:

Quote

Notice: Uninitialized string offset

So what is a Notice exactly?  Well it's not an error if you don't want it to be.  Most production sites run without Notices turned on:  

error_reporting(E_ALL & ~E_NOTICE);

I am not telling you to ignore this problem,  but Notices don't necessarily mean that the code won't work.

This particular notice, if you google it, is telling you that your code is attempting to utilize a string as if it was an array in this manner:

$bar = '';
$foo = $bar[0];  // This will generate a notice.

It doesn't have to be an assignment, it could be echo or any other code that attempts to use a numeric index on an array that is actually a string being treated like an array.   Clearly this an unexpected condition in your code.  There are several ways to protect against this type of problem.  In particular, you could test the variable with is_array 

 

<?php


if (is_array($foo)) {
  // Good
} else {
  // Problem with $foo
}

Another way to handle this is, if the variable is being passed as a parameter, use PHP type hinting

 

Now some recommendations:

Make a Bitbucket account, learn Git, start using it.  This has nothing to do with the posting of your code on Github.  It just has to do with the fact that you should be using some form of version control for your project, and Git won the version control system wars.  Install git, git init your project, commit and push it to a private bitbucket repo you made for your project. 

Get really familiar with the Chrome Dev tools.  You should be looking at your site in the developer tool network tab if you have any question as to what requests and responses are happening with your code.

I'm guessing that you may have visited StackOverflow when googling about problems you have.  They expect someone posting a question to provide a MCVE.  Essentially people would like you to try and do the same.  

A few times now you posted echo $page->createPage() and previously$reportArr = $this->findArrValue(‘logArr’).   

You apparently know that they error occurs inside these methods.  It should not be hard to isolate portions of the code and determine where the problem is happening, but the obvious thing is that we can't help you without the code and example data.  From what information we have so far, it seems clear that findArrValue makes an assumption about the data it works upon, and this assumption is incorrect in some circumstances.  

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.