Page 1 of 1

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

Posted: Wed Nov 16, 2005 6:24 pm
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
-

Posted: Wed Nov 16, 2005 6:40 pm
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)

Posted: Wed Nov 16, 2005 7:09 pm
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
-

Posted: Wed Nov 16, 2005 7:12 pm
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.

Posted: Wed Nov 16, 2005 7:16 pm
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
-

Posted: Wed Nov 16, 2005 7:22 pm
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..

Posted: Wed Nov 16, 2005 7:26 pm
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
-

Posted: Wed Nov 16, 2005 7:33 pm
by Jenk
have you tried var_dump($paras['alias']) ?

Posted: Wed Nov 16, 2005 7:36 pm
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
-

Posted: Wed Nov 16, 2005 7:44 pm
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.

Posted: Wed Nov 16, 2005 7:52 pm
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
-

Posted: Wed Nov 16, 2005 7:54 pm
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'.

Posted: Wed Nov 16, 2005 8:02 pm
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
-

Posted: Wed Nov 16, 2005 8:18 pm
by djot
-
LOL, I got status 'Professional' today ...

... and can't even set variables properly

HAha


djot
-