BUG in PHP v5.0.5? Notice: Uninitialized string offset

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
djot
Forum Contributor
Posts: 313
Joined: Wed Jan 14, 2004 10:21 am
Location: planet earth
Contact:

BUG in PHP v5.0.5? Notice: Uninitialized string offset

Post by djot »

-
I have trouble with PHP version 5.0.5. It throws notices

Code: Select all

Notice: Uninitialized string offset: 0
Variables initialized to e.g $foo=''; are NULL instead of typ STRING with string length of 0.

Also checking those variables with empty() does not work.

Code: Select all

if (!emtpy ($foo)) {
  code here is executed, while it should not.
}

Anyone else got these notices with PHP 5.0.5?


I do not have these notices with PHP 5.0.4 or below or PHP 4.xx.


Bad solution: check for strlen() >=1 instead of !empty()
(notices will come anyway)



Sample code not working (last line throws notice):

Code: Select all

function ReplacePlaceholders($text, $gb) {
$gb_indexes = ARRAY ('count', 'date', 'datetime', 'email', 'id', 'ip', 'message', 'name', 'sent_verify', 'time');

foreach ($gb_indexes AS $item) {
    if (!isset ($gb[$item])) {
        $gb[$item] = '';
    }
}

$template = $text;
$template = str_replace("{COUNT}", $gb['count'], $template);
...

djot
-
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

It thinks $gb is a string.

var_dump($gb) to see it.

(We had this already today I am certain.. hint of sarcasm)
djot
Forum Contributor
Posts: 313
Joined: Wed Jan 14, 2004 10:21 am
Location: planet earth
Contact:

Post by djot »

-
We had this already today I am certain.. hint of sarcasm
Yes I read it and tried before on an other part of the code. There it didn't help. Then I decided to post this code.



adding

Code: Select all

if (!is_array($gb)) { $gb=ARRAY(); }
in front of foreach solves the problem.

thx.



Anyway, why is PHP 5.0.5 behaving that different in throwing notices than versions 5.0.4 and below?


djot
-
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

unknown, but you should be defining/declaring your variables before they are to have values assigned to them anyway. It's good practice to do so.

Sounds like the behavior of variables has been changed in PHP and so when calling for an indice on an undefined var it now assumes string offset and not array index.
djot
Forum Contributor
Posts: 313
Joined: Wed Jan 14, 2004 10:21 am
Location: planet earth
Contact:

Post by djot »

-
And what is the error here? !empty get's passed on variable set to $foo=''; and require fails for that. (The globals are set as you can see in the warning.)

Code: Select all

if (!isset($para['alias'])) {
$para['alias'] = '';
}

if (!empty($para['alias'])) {
    require($GLOBALS['abdj_path_voting'].$GLOBALS['abdj_config_voting']['datafile_prefix'].$para['alias'].".php");
}

This is the error message (line 711 is the require part)

Code: Select all

Notice: Uninitialized string offset: 0 in C:\apachefriends1.5.0pl1\xampp\htdocs\addonsbydjot\addonsbydjot\addonsbydjot.php on line 711

Warning: Addonsbydjot::Voting(./addonsbydjot/voting/voting..php) [function.Voting]: failed to open stream: No such file or directory in C:\apachefriends1.5.0pl1\xampp\htdocs\addonsbydjot\addonsbydjot\addonsbydjot.php on line 711

Fatal error: Addonsbydjot::Voting() [function.require]: Failed opening required './addonsbydjot/voting/voting..php' (include_path='.;c:\apachefriends1.5.0pl1\xampp\php\pear\') in C:\apachefriends1.5.0pl1\xampp\htdocs\addonsbydjot\addonsbydjot\addonsbydjot.php on line 711

djot
-
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

voting..php (note the extra .)

Code: Select all

<?php

echo phpversion()."\n";

$foo='';

if(!empty($foo)) {
    echo "foo is not empty\n";
}

?>
outputs:

Code: Select all

>E:\PHP\php.exe -q test.php
5.0.5

>Exit code: 0    Time: 0.204
on mine..

And

Code: Select all

<?php

$a[1] = 'woo!';

var_dump($a);

?>
outputs:

Code: Select all

>E:\PHP\php.exe -q test.php
array(1) {
  [1]=>
  string(4) "woo!"
}

>Exit code: 0    Time: 0.203
so at a guess.. your installation is not quite right..
djot
Forum Contributor
Posts: 313
Joined: Wed Jan 14, 2004 10:21 am
Location: planet earth
Contact:

Post by djot »

-
Well the extra . would not be there if $para['alias'] was set to eg. "test". Then the file would be named voting.test.php what is correct.
Anyway, $para['alias']=''; passes the IF statement and makes require failing with fatal error.

I did do your test from above several times today, it works. My code shown above does not.

djot
-
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

have you tried var_dump($paras['alias']) ?
djot
Forum Contributor
Posts: 313
Joined: Wed Jan 14, 2004 10:21 am
Location: planet earth
Contact:

Post by djot »

-
Yes. (I did search the forum before posting and tried myself for sure).

It's output is

Code: Select all

string(0) ""
djot
-
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

try changing the if statement to:

Code: Select all

if (!empty($paras['alias']) && $paras['alias'] != '') {
just to test.

I just tried:

Code: Select all

<?php

$a['a'] = '';

if (!empty($a['a'])) {
    echo "\$a['a'] is not empty\n";
}

?>
which didn't output anything (as expected)

If that still fails, I would suggest reinstalling PHP, or even downloading it again as there is something not quite right.
djot
Forum Contributor
Posts: 313
Joined: Wed Jan 14, 2004 10:21 am
Location: planet earth
Contact:

Post by djot »

-
This works.

Code: Select all

if (!empty($para['alias']) && $para['alias'] != '') {
My solution was to check string length of $para['alias'] and change the if to

Code: Select all

if (!empty($para['alias']) AND strlen($para['alias'])>=1) {

Anyway, $para['alias'=''; should be detected as empty. See here .


djot
-
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Yes, I am aware that empty() should pick up an empty string as empty, hence my suggestion that somehow your installation of PHP is 'bugged'.
djot
Forum Contributor
Posts: 313
Joined: Wed Jan 14, 2004 10:21 am
Location: planet earth
Contact:

Post by djot »

-
It's a package from apachefriends.org. But they use the original files of apache and php without changes. I have several version of XAMPP running. All that have PHP 5.0.5 included fail. The others with PHP 5.0.4 or below work quite well.


I did not look for it, but the code presented here is part of a script that has around 50 files that all use the same way of setting variables.

First I check with isset(), then with empty() . But only one of these 50files has this strange errors.


Thx for your help and efforts.


djot
-
djot
Forum Contributor
Posts: 313
Joined: Wed Jan 14, 2004 10:21 am
Location: planet earth
Contact:

Post by djot »

-
LOL, I got status 'Professional' today ...

... and can't even set variables properly

HAha


djot
-
Post Reply