TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
 
 
LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
Old 02-28-2009, 09:43 PM   #1 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default array fundamentals

Hey guys, I read the array article and I'm trying to shore up my fundamentals of arrays in PHP. I think I am having some confusion.

First, I assume that in PHP4 and PHP5, that arrays are passed by reference, just like objects in PHP5...is that correct?

Secondly, I am using PHP version 5.2.8


Code:
<?php




$test = array("house" => "car", "dog" => "cat");


###First Array Print
foreach($test as $key => $value) {
	
	echo "$key => $value \n";
	

}

$array_count = count($test);
echo "count: $array_count \n";


###Second Array Print
while(list($key,$value) = each($test)){
	
	echo "$key => $value \n";
}




?>
will print:

house => car
dog => cat
count: 2


But notice that the second array print doesn't print anything.

If I comment out the FIRST array print, then the second one will work and print this:

count: 2
house => car
dog => cat

Code:
<?php




$test = array("house" => "car", "dog" => "cat");


###First Array Print
#foreach($test as $key => $value) {
	
	#echo "$key => $value \n";
	

#}

$array_count = count($test);
echo "count: $array_count \n";


###Second Array Print
while(list($key,$value) = each($test)){
	
	echo "$key => $value \n";
}




?>


So I have 2 questions.


1) Why is it that simply printing out the array the first time suddenly makes the array inaccessible in another loop?

2) The fact that the second loop wasn't able to print anything when the first loop already printed something, why does my $array_count variable work? Why does it echo the number "2"??

shouldn't it be inaccessible just the same way the second loop wasn't able to print anything??
allworknoplay is offline  
Reply With Quote
 



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
How Would I Apply htmlentities() To Every Array Item vujsa Advanced PHP Programming 11 01-10-2009 01:28 PM
Array mess Killswitch Absolute Beginners 4 12-14-2008 07:35 AM
Build multi-dimensional array out of a flat array drewbee Advanced PHP Programming 2 05-28-2008 11:38 PM
(array) or array()? Orc General 4 01-20-2008 07:52 PM
Part 1: Getting Started with Array Functions Wildhoney Absolute Beginners 6 10-01-2007 10:53 AM


All times are GMT. The time now is 05:59 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design