Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reopened: PHP array_shift(); VS reset(); unset(); array_splice();

Tags:

php

unset

reset

Reopened:

Since PHP is a server-side language I had the assumption that it didn't matter what browser I used, but apparently not so. I had only been using Google Chrome.

I installed WAMP on my local machine so I can test it locally to see if it did the same as my shared hosting account. Both codes worked the way they should (in Chrome mind you). Then I looked at the exact same code on my shared hosting -- one worked, the other didn't.

I called my shared hosting support and they tried to duplicate the issue and said they weren't finding it. So I tried it in Firefox with them on the line and in IE, lo and behold... it worked perfectly in both just like on WAMP.

This still isn't making much sense to me. I'm using PHP 5.3 on my shared hosting account with a well known hosting company but not getting anywhere because they can't really troubleshoot code. They can replicate the issue but can't answer why. I'm going to try to find out more in the next weeks and post an update as I know more.

What I'm trying to do is this:

  • Generate a range of numbers
  • Shuffle them into a random order
  • Copy that array of randomly ordered numbers into a session array
  • Get the first value of the array, delete that value, and shift all the values down by one

Here's my issue:

I tried using array_shift(); and it worked fine the first run but each time I ran the code after it would remove the first two elements.

To test what was going on I tried to print the array first, do array_shift();, and then print the array again to see what happened.

Expected Results:

Run #1:

[0]=>5 [1]=>2 [2]=>1 [3]=>4 [4]=>3  //print_r($array);
                                    //execute array_shift($array);
[0]=>2 [1]=>1 [2]=>4 [3]=>3         //print_r($array);

Run#2:

[0]=>2 [1]=>1 [2]=>4 [3]=>3         //print_r($array);
                                    //execute array_shift($array);
[0]=>1 [1]=>4 [2]=>3                //print_r($array);

Actual Results:

Run #1:

[0]=>5 [1]=>2 [2]=>1 [3]=>4 [4]=>3  //print_r($array);
                                    //execute array_shift($array);
[0]=>2 [1]=>1 [2]=>4 [3]=>3         //print_r($array);

Run#2:

[0]=>1 [1]=>4 [2]=>3                //print_r($array);
                                    //execute array_shift($array);
[0]=>4 [1]=>3                       //print_r($array);

My issue (cont.)

So then I tried using reset($array);, unset($array[0]);, and array_splice($array,1,0); as an alternative to array_shift($array); and it worked! Then I tried to compare them side by side and cleaned up the code and now they are doing the opposite of each other. Sometimes reset, unset, and array_shift; will even jump through up to 7 cells in the array when called once. array_shift(); is working the way I want it to now but I want to know why. It's driving me nuts! If someone could please help me I'd be so appreciative.

Code Dump:

unset, reset, splice

<?php
session_start();
$min = A; 
$max = S;

if((!isset($_SESSION['image'])) || ($_SESSION['image'] == null))
{
$numbers = range($min, $max);  //set a range for all images 
shuffle($numbers); //shuffle the order for randomness

$_SESSION['image'] = $numbers;

echo "<br />Current value: " . $_SESSION['image'][0] . "<br />";
print_r($_SESSION['image']);

reset($_SESSION['image']);
unset($_SESSION['image'][0]);
array_splice($_SESSION['image'],1,0);
echo "<br />New value: " . $_SESSION['image'][0] . "<br />";
echo "<br />1st exec<br />";

}
else
{
echo "<br />Current value: " . $_SESSION['image'][0] . "<br />";
print_r($_SESSION['image']);



reset($_SESSION['image']);
unset($_SESSION['image'][0]);
array_splice($_SESSION['image'],1,0);
echo "<br />New value: " . $_SESSION['image'][0] . "<br />";
echo "<br />2nd exec<br />";

}
?>

shift

<?php
session_start();
$min = A; 
$max = S;

if((!isset($_SESSION['id2'])) || ($_SESSION['id2'] == null))
{
$numbers = range($min, $max);  //set a range for all images 
shuffle($numbers); //shuffle the order for randomness

$_SESSION['id2'] = $numbers;

echo "<br />Current value: " . $_SESSION['id2'][0] . "<br />";
print_r($_SESSION['id2']);

array_shift($_SESSION['id2']);
echo "<br />New value: " . $_SESSION['id2'][0] . "<br />";
echo "<br />1st execution<br />";

}
else
{
echo "<br />Current value: " . $_SESSION['id2'][0] . "<br />";

print_r($_SESSION['id2']);

array_shift($_SESSION['id2']);
echo "<br />New value: " . $_SESSION['id2'][0] . "<br />";
echo "<br />2nd execution<br />";

}
?>
like image 832
Jeff Reeves Avatar asked Sep 19 '12 23:09

Jeff Reeves


2 Answers

To ultimately learn what's going on, I propose to register a tick-function. The tick-function can execute at each PHP statement execution.

Here's a simple one, which traces line/file of each statement (feel free to add further details):

// Execute at each single statement
declare(ticks=1);

// Function to get called at each executing statement    
function logEachStatement() {

  $traceInfo = debug_backtrace();
  $lastActivation = $traceInfo[ count( $traceInfo ) - 1 ];

  $info = "\n" 
        . $lastActivation[ 'line' ]  . '@' . $lastActivation[ 'file' ] 
        ;

  $targetFile = dirname( __FILE__ ) . '/stmnt.log' );

  file_put_contents( $targetFile, $info, FILE_APPEND );

}

// using a function as the callback
register_tick_function( 'logEachStatement', true );

While there are other options to track down the problem, this one does not require external infrastructure.

like image 105
SteAp Avatar answered Nov 03 '22 00:11

SteAp


Assuming that you pasted the actual code, the only way for this to happen is if you made an unintentional intermediate request. It would be impossible for us to determine how or why this is the case with only the given information, though.

My suggestion is to use your browser dev tool of choice (e.g. Firebug) and monitor the network tab to make sure that only one request is being sent to the script. If this doesn't turn up your problem, add some simple debug logging, such as this:

$log = date('Y-m-d H:i:s')
     . ' :: Request received from '
     . $_SERVER['REMOTE_ADDR']
     . "\n";
file_put_contents('/path/to/your/log/file', $log, FILE_APPEND);

Then check your log after your test to make sure only one entry was added.

like image 29
FtDRbwLXw6 Avatar answered Nov 03 '22 01:11

FtDRbwLXw6