php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #6414 foreach() doesn't accept string for an argument
Submitted: 2000-08-29 10:47 UTC Modified: 2003-03-17 02:12 UTC
From: cynic at mail dot cz Assigned:
Status: Closed Package: Feature/Change Request
PHP Version: 5.0 OS: ANY
Private report: No CVE-ID: None
 [2000-08-29 10:47 UTC] cynic at mail dot cz
$b = "abcdefgh" ;

foreach( $b as $y ) {
	echo $y ;
} ;

Warning: Invalid argument supplied for foreach() 

Patches

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2000-08-29 11:12 UTC] jmoore@php.net
You need to cast your string as an array..

<?php

$b = "abcdefgh" ;
$b = (array)$b;

foreach( $b as $y ) {
	echo $y ;
};

?>

Works.

James
 [2003-03-17 01:46 UTC] yuki_yew78 at yahoo dot com
me too
 [2003-03-17 02:12 UTC] helly@php.net
This is a thing to think about:
Since strings are NOT accepted as foreach input we could
add that by delivering eash single character.

>php -r '$a="abc";foreach($a as $i => $c) echo "$i:$c\n";'
0:a
1:b
2:c

 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Aug 15 15:01:27 2024 UTC