php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #1585 Arrays & scalar variables of same name cant coexist
Submitted: 1999-06-22 14:06 UTC Modified: 2000-02-12 22:58 UTC
From: al52x at nih dot gov Assigned:
Status: Closed Package: Documentation problem
PHP Version: 3.0.9 OS: FreeBSD 3.1R, Solaris 2.4
Private report: No CVE-ID: None
 [1999-06-22 14:06 UTC] al52x at nih dot gov
The document on migration from PHP2 to PHP3 says that $var is no longer equivalent to $var[0], but I just spent some hours finding out that the reason for "Illegal string index" errors in the following code fragments seems to be that once a scalar variable is set, it's an error to try to re-use it as an array variable.  I imagine that unset($var) between the various lives of a variable would be a workaround, but loose typing of variables in PHP as opposed to C ($a="string"; $a=3.4; etc) tends to make this a surprise.  If you could just mention this in the docs where you're talking about variable types and/or arrays and/or the PHP2-->3 doc, maybe this could save someone else some head scratching.  You might not be surprised to hear that this behavior is the same in PHP 3.0.9, 3.0.7, and in fact 3.0RC4.  In the code that follows, qry() is just a wrapper for mysql_query():

$q="select * from skedb where uid = ".$id;
$res=qry($q,"Getting values for current schedule");

/* imtg1 is an integer here: */
$imtg1=mysql_Result($res,0,"mtg1");

$qq="select md from leave where uid = ";
if($imtg1>0) { 
  $q=$qq.strval($imtg1);    /* still an integer, but */
  /* finished using it. Now want an array of similar name.*/
  $res=qry($q,"Getting MDs on leave on Monday");
  $im1=mysql_NumRows($res);
  $i=0;
  while($i<$im1) {
    /* following generates an illegal string index error */
    /* but interestingly, not until $imtg1[3] access */
    /* attempted */
    $imtg1[$i]=mysql_Result($res,$i,0);
    $i++;
    }
  }

old_function qry $a,$b (
  global $db,$conn;
  $r=mysql_query($a,$conn);
  if(!$r) {
    echo "Problem with database. Check server status.";
    echo "<br>Was in section <i>$b</i><br>";
    echo "<p>Query was: $a<p>";
    exit;
    }
  return($r);
  );

By the way, thanks for some truly wonderful glue between Apache & MySQL!!

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2000-02-12 22:58 UTC] torben at cvs dot php dot net
There is now a note in the docs about this, but the code below isn't doing
quite what you think and so you're hitting this problem.

Background: the behaviour of implicit conversion between array and string
types using [] is undefined. For instance:

<?php
$a = "This is a test";
$a[0] = 'X';
echo $a;  // should this work?
?>

In this example, should 'echo $a' produce 'Xhis is a test', or 'Array'? The
assignment (knowing that $a is a string) could mean either.

This applies to your example because when you run this:

/* imtg1 is an integer here: */
$imtg1=mysql_Result($res,0,"mtg1");

...$imtg1 is *not* an integer; it's a string holding a numeric value. 
Apparently, it's only 3 chars long, so when you attempt to assign to
$imtg1[3], you've gone off the end of the string. 

The easiest workaround is simply to use another variable there. 


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Jun 15 12:01:29 2024 UTC