I have a variable $newExtract[$x][3]. When I try to explode it as:
explode("/", $newExtract[$x][3])
It gives me error message:
"Notice: Undefined offset: 3 in C:\xampp\htdocs\torrent\classes\sm9.class.php on line 63".
But, when I echo it using echo $newExtract[$x][3]; die();, it gives me the result as 13/08/2012 20:58.
Can anyone help me what is happening? Why, I am not able to explode it?
Thanks
I've seen several places in PHP where implicit string conversion (or string dereferencing, in this case) causes issues and the simplest path to sanity is using an intermediate variable:
$date = $newExtract[$x][3];
explode("/", $date);
It seems to show up more often with certain internal functions, and I usually run into it more with objects and __toString(), but this wouldn't be the first time for this situation, either.
Related
I came across this line:
list($diff, $current, $concurrent) = $diff;
Documentation states that this should result in undefined behaviour. What are the possible variants of this behaviour? Variable $diff is array, containing 3 elements with variable content.
This line is part of application that contains a bug and author of this line is unavailable. Though I am almost sure that it is not what I am looking for, it would be nice to be 100% sure.
I am using PHP 5.6.25 as FPM/FastCGI.
Thanks in advance.
As the documentation for list() also states:
In PHP 5, list() assigns the values starting with the right-most parameter. In PHP 7, list() starts with the left-most parameter.
In other words: This line might work as intended in PHP 5, because the variable $diff that appears on both sides is the last variable to get assigned. However, in PHP 7 the $diff variable gets assigned first, so $diff has already changed by the time the assignments for $current and $concurrent are done.
In general I think the hint about undefined behaviour relates to the fact that you cannot rely on certain assignments to yield the expected results, if a variable appears on both sides of the = sign. A workaround for the issue could look like this:
list($temp, $current, $concurrent) = $diff;
$diff = $temp;
unset($temp);
This way you avoid the undefined behaviour.
I was trying to use the $$ syntax in PHP for accessing arrays where we can put name of a variable inside another variable and access that variable.
I have used this syntax many times before in different ways, but to my surprise this didn't work for me and lost lot of time over it.
Here is sample code to replicate my problem:
$test=array(
'a'=>array(array(1,2,3),array(4,5,6),array(7,8,9))
);
$var = 'test';
var_dump($$var);
var_dump($$var['a']);
The line var_dump($$var) works as expected, but I'm getting a Warning: Illegal string offset 'a' at line var_dump($$var['a']); and the var_dump prints just null
Why doesn't this work? What am I doing wrong here?
Is there any work around if the syntax is not supported for arrays?
Your $$var['a'] is equivalent to ${$var['a']}. Not ${$var}['a']. The latter being the workaround syntax you are looking for.
Quoting the PHP Manual on Variable Variables:
In order to use variable variables with arrays, you have to resolve an ambiguity problem. That is, if you write $$a[1] then the parser needs to know if you meant to use $a[1] as a variable, or if you wanted $$a as the variable and then the [1] index from that variable. The syntax for resolving this ambiguity is: ${$a[1]} for the first case and ${$a}[1] for the second.
See http://codepad.org/lR7QJygX
I was working on my site yesterday and all the content was appearing fine. Then today, I refreshed and something has broken.
I'm getting this error on the homepage:
Notice: Use of undefined constant Page - assumed 'Page' in includes\survey_inside.php on line 19
Notice: Use of undefined constant Listing - assumed 'Listing' in includes\survey_inside.php on line 20
When I look on line 19 the code is:
$strPage = $_REQUEST[Page];
if($_REQUEST[mode]=="Listing"){
Like I said, everything worked fine yesterday, and I didn't change these lines. I've been looking around for several hours and am stuck. Any thoughts on how to proceed with figuring out what happened?
Thanks!
When you want to access an array like $_REQUEST, you have to use some key (string value). In this case you are calling $_REQUEST[Page] and $_REQUEST[mode].
Since Page and mode are not constant values, you have to use it as a string, so:
$strPage = $_REQUEST['Page'];
if ($_REQUEST['mode'] == "Listing") { }
Don't know much about php, but I can figure somethings out and copy and paste.
My webhost seems to have updated php and code that worked for years now throws up errors. I had someone develop my php connection to a filemaker database and he is no longer available to help me with this problem:
The problem occurs in two places:
> Undefined offset: 0 in
this is the code:
$resultData = $Result['data'][$DataKeys[0]];
// and here:
$aRecID = preg_split('[.]',$DataKeys[0]);
It means $DataKeys[0] does not exist.
Try a var_dump on $DataKeys and simply take a look what is inside.
Use this small helper function, it generates a better output:
function pr($var) {
echo "<pre>";
var_dump($var);
echo "</pre>";
}
So currently I'm trying to fix up some old code for a stats server for a game (that is definitely outdated, and has been replaced recently with a new version). MUCH of the code is deprecated, so it's kind of guesswork on fixing it right, but I found a section thats completely broken that I have no idea how to fix, and it's causing a fatal error.
The code is as follows
for ($i=0; $i<$armyCount; $i++)
{
$summary['total']['time'] += $armies[0]['time'.$i];
$summary['total']['win'] += $armies[0]['win'.$i];
$summary['total']['loss'] += $armies[0]['loss'.$i];
$summary['total']['score'] += $armies[0]['score'.$i];
$summary['total']['best'] += $armies[0]['best'.$i];
$summary['total']['worst'] += $armies[0]['worst'.$i];
$summary['total']['brnd'] += $armies[0]['brnd'.$i];
}
The errors I get are as follows
Notice: Uninitialized string offset: 0 in C:\xampp\htdocs\dontneedthis\playerstats.inc.php on line 136
Fatal error: Cannot use string offset as an array in C:\xampp\htdocs\dontneedthis\playerstats.inc.php on line 136
I've seen similar questions asked, and seen how they were resolved, but I don't fully understand how it was done, so don't quite know how to go about fixing this one. Any assistance would be awesome, and I fully intend to release the bugfixed and fully working code (whenever I get that done) to the community that remains.
It looks like your $armies[0]['time'.$i] is initialized as an empty string and not as an array (likely $armies= "").
Notice: Uninitialized string offset: 0
That means that php tries to access your string-variable $armies as an array. If the string is non-empty then this would result in getting single letters from that string. But it seems that $armies is an empty string and so getting the letter with index 0 is not possible.
Fatal error: Cannot use string offset as an array
means that the result of the operation above (which showed the notice) cannot be accessed as an array. Remember that $armies is a astring and the first letter of the string was accessed and the result of this attempt will be accessed as an array.
Try resolving how the $armies variable is filled and why its filled the wrong way.
$armies is an empty string, and you are treating it as any array, in an unrecoverable way.
Show us what $armies should look like, what you get when you var_dump($armies);, and the code from where you assigned it, and we'll help you work out what went wrong.