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
Run # 2:
[0]=>2 [1]=>1 [2]=>4 [3]=>3
Actual Results:
Launch number 1:
[0]=>5 [1]=>2 [2]=>1 [3]=>4 [4]=>3
Run # 2:
[0]=>1 [1]=>4 [2]=>3
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 />"; } ?>