p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

beginning_php thread: Problem with array_pop


Message #1 by "David Smith" <dave@c...> on Sat, 1 Feb 2003 16:54:50
I've got apache v1.3.27 running with PHP4 installed(win98se) , and for 
some reason when I execute the pop_array example using the code on page 
180, I end up with a blank screen (No data)

I've checked by uploading the same program to my host server and it 
executes correctly.

I'm assuming a fault with either the PHP installation or Apache..

Any thought please?

P.S. I'm a complete newbie, so please keep it simple.

Thanks in advance.

Dave
Message #2 by "Nikolai Devereaux" <yomama@u...> on Sat, 1 Feb 2003 10:14:29 -0800
> I've got apache v1.3.27 running with PHP4 installed(win98se) , and for
> some reason when I execute the pop_array example using the code on page
> 180, I end up with a blank screen (No data)
>
> I've checked by uploading the same program to my host server and it
> executes correctly.
>
> I'm assuming a fault with either the PHP installation or Apache..
>
> Any thought please?


I don't know what the code on page 180 is.  My guess is that you're not
passing a valid array to array_pop.  If you pass a non-existent variable to a
function that accepts an array, PHP will create an empty array for you, and
array_pop() will return nothing.


Take care,

Nik

Message #3 by "David Smith" <dave@c...> on Sat, 1 Feb 2003 18:35:30
I don't know what the code on page 180 is.  My guess is that you're not
passing a valid array to array_pop.  If you pass a non-existent variable 
to a
function that accepts an array, PHP will create an empty array for you, and
array_pop() will return nothing.


Take care,

Nik

Hi Nik, 

Thanks for the prompt response.

Here is the code I am using.

<html>
<head></head>
<body>
<?php
$director[4]="Orson Wells";
$director[1]="Carol Reed";
$director[93]="Fritz Lang";
$director[24]="Jaques Tourneur";

array_pop($director);

while (list($eiv, $ec) = each($director))
{
	echo "<br>$eiv - $ec";
}

?>
</body>
</html>

Now this works ok, when I upload it to a web server, but not when I use 
localhost...any thoughts..?

Dave
Message #4 by "Nikolai Devereaux" <yomama@u...> on Mon, 3 Feb 2003 10:45:33 -0800
Okay, that's weird then.  What version of PHP are you running?

Try this:


<html>
<head></head>
<body>
<?php

$director[4]="Orson Wells";
$director[1]="Carol Reed";
$director[93]="Fritz Lang";
$director[24]="Jaques Tourneur";

echo "<pre>\n";
echo "Before array_pop: ";
print_r($director);

array_pop($director);

echo "\nAfter array_pop: "

print_r($director);

echo "</pre>\n";

?>
</body>
</html>



What's the ouptut on your local machine?  I honestly don't understand why your
script wouldn't work at home.


Take care,

Nik

Message #5 by "David Smith" <dave@c...> on Mon, 3 Feb 2003 21:07:02
> 
Okay, that's weird then.  What version of PHP are you running?

Try this:


<html>
<head></head>
<body>
<?php

$director[4]="Orson Wells";
$director[1]="Carol Reed";
$director[93]="Fritz Lang";
$director[24]="Jaques Tourneur";

echo "<pre>\n";
echo "Before array_pop: ";
print_r($director);

array_pop($director);

echo "\nAfter array_pop: "

print_r($director);

echo "</pre>\n";

?>
</body>
</html>



What's the ouptut on your local machine?  I honestly don't understand why 
your
script wouldn't work at home.


Take care,

Nik

Thanks nik, initially a parse error line 19, no ; at the end of line 
echo "\nAfter array_pop: "

then prints out the array correctly, so the function is working correctly, 
but when you replace print_r(...) with print(...) there is no output.

At least I know the function is working...but strange no output...

Output:

Before array_pop: Array
(
    [4] => Orson Wells
    [1] => Carol Reed
    [93] => Fritz Lang
    [24] => Jaques Tourneur
)

After array_pop: Array
(
    [4] => Orson Wells
    [1] => Carol Reed
    [93] => Fritz Lang
)

Message #6 by "Nikolai Devereaux" <yomama@u...> on Mon, 3 Feb 2003 13:19:11 -0800
> then prints out the array correctly, so the function is working correctly,
> but when you replace print_r(...) with print(...) there is no output.

print() should only print "Array" since it doesn't know how to recursively
iterate through the array contents to print each index/value pair.

Now then, you know that array_pop is working, let's go back to your original
loop.

while (list($eiv, $ec) = each($director))
{
	echo "<br>$eiv - $ec";
}



This uses list() and each().  Perhaps the array_pop on the PHP you're running
at home tweaks the array pointers.  You should call reset($director) to move
the internal array pointer back to the beginning of the array after the call
to array_pop().


lemme know if that changes things!


nik

Message #7 by "David Smith" <dave@c...> on Tue, 4 Feb 2003 12:20:20
This uses list() and each().  Perhaps the array_pop on the PHP you're 
running
at home tweaks the array pointers.  You should call reset($director) to 
move
the internal array pointer back to the beginning of the array after the 
call
to array_pop().


lemme know if that changes things!


nik

Yes, using the reset($director) function made all the difference. I'll 
have to watch out for that if other array functions do not behave as I 
would expect.

Thanks again for you help and perservance with this little problem.

I'm using V4.3.0 php with Apache 1.3.27.

Cheers again

Dave

  Return to Index