php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #38974 String is not properly overwritten in
Submitted: 2006-09-27 14:47 UTC Modified: 2006-09-27 15:48 UTC
From: hendlerman at yahoo dot com Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 5.1.6 OS: windows XP
Private report: No CVE-ID: None
 [2006-09-27 14:47 UTC] hendlerman at yahoo dot com
Description:
------------
The error is caused by a user error, but I believe PHP should be throwing an error. PHP is not strongly typed, but I thought you get a warning if you try to convert a string to an array.

Sorry I am not in 5.1.6


Reproduce code:
---------------
/**
 * Output for me
 * PHP version 5.1.4 
 * Array ( [name] => 2alue )
 * 
 * */
function array_bug()
{
  echo "PHP version ". phpversion().'<br />';
  $test_array = array();
  
  $test_array['name'] = 'value';
  $test_array['name']['count'] = 2;
  
  print_r($test_array);
}

array_bug(); exit;

Expected result:
----------------
I would expect an error or warning

Actual result:
--------------
Array ( [name] => 2alue )

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-09-27 14:53 UTC] derick@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

.
 [2006-09-27 14:54 UTC] tony2001@php.net
$string[0] = 'c'; is perfectly valid code and that's what you get with $string['otherstring'].
 [2006-09-27 15:14 UTC] hendlerman at yahoo dot com
I understand that it's valid in PHP to overwrite the string with an array. The problem is that the string is not actually overwritten by the array. I thought that was clear from the sample code!

Doesn't seem like a feature if the array isn't overwritten. Perhaps I should have titled = "String is not properly overwritten "
 [2006-09-27 15:32 UTC] hendlerman at yahoo dot com
Changing title and re-opening so it gets reviewed again.
 [2006-09-27 15:34 UTC] hendlerman at yahoo dot com
edit title
 [2006-09-27 15:34 UTC] tony2001@php.net
# php -r '$s = "string"; $s[0] = 3; var_dump($s);'
string(6) "3tring"

This is expected behaviour.

 [2006-09-27 15:37 UTC] hendlerman at yahoo dot com
the code you've outlined is not the same as the code I have.

what happens to the key in the array called "count"?
 [2006-09-27 15:45 UTC] tony2001@php.net
It's autoconverted to integer, i.e. 0.
 [2006-09-27 15:48 UTC] hendlerman at yahoo dot com
OK.

I can see how this is not a "bug" and expected.
But I think the behaviour should be different. Maybe just:

1. don't loose keys

 I can't post to php-dev ATM, but is that the proper place to have this discussion?
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Mar 30 01:01:28 2024 UTC