Open: PHP array_shift (); VS reset (); disarmed (); array_splice ();

Resumed:

Since PHP is a server-side language, I had the assumption that it doesn’t matter which browser I used, but apparently not so. I just used Google Chrome.

I installed WAMP on my local computer to check it locally to make sure that it did the same thing as my hosting account. Both codes worked as they should (in Chrome). Then I looked at the same code on my shared hosting - one worked, the other did not.

I called my hosting support and they tried to duplicate this problem and said that they did not find it. So I tried it in Firefox with them on the line and in IE, here it is ... it worked just fine in WAMP.

That still doesn't make much sense to me. I am using PHP 5.3 on my shared hosting account with a well-known hosting company, but I am not getting anywhere because they cannot really fix the code. They can reproduce the problem, but they cannot answer the question. I am going to try to find out more in the coming weeks and post an update as I know more.

What I'm trying to do is:

  • Creating a range of numbers
  • Shuffle them in random order
  • Copy this array of randomly ordered numbers into an array of sessions
  • Get the first value of the array, delete this value and shift all values ​​down one

Here is my problem:

I tried using array_shift(); , and it worked great on the first run, but every time I ran the code, it deleted the first two elements.

To check what is happening, I tried to print the array first, do array_shift(); and then print the array again to find out what happened.

Expected results:

Launch number 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:

Launch number 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 problem (continued)

So, I tried using reset($array); , unset($array[0]); and array_splice($array,1,0); alternatively array_shift ($ array); and it worked! Then I tried to compare them side by side and cleared the code, and now they do the opposite of each other. Sometimes reset, unset and array_shift; will even skip to 7 cells in the array when called once. array_shift(); It works the way I want, but I want to know why. It drives me crazy! If someone could help me, I would be so grateful.

Dump code:

unset, reset, splicing

 <?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 />"; } ?> 
+7
source share
2 answers

In order to eventually find out what is happening, I suggest registering a checkmark function . The tick function can be executed every time a PHP statement is executed .

Here is a simple one that keeps track of the line / file of each statement (feel free to add more 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 ways to track the problem, this one does not require external infrastructure .

+1
source

Assuming you have inserted the actual code, the only way to do this is to make an unintentional interim request. It would be impossible for us to determine how and why this happens only with this information.

My suggestion is to use the developer tool of your choice (like Firebug) and monitor the network tab to make sure that only one request is sent to the script. If this does not affect your problem, add some simple debugging protocols, for example:

 $log = date('Ymd 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 the test to make sure that only one entry is added.

+1
source

All Articles