1

I wonder if they disabled array_shift in > PHP 5.2.6

$realid = array_shift(explode("-", $id));

Because this code was working fine on my server PHP Version 5.2.6, while is not working in another server with higher PHP version.

If so, is there anyway I can do the following:

For a URL like this 87262-any-thing-here.html how can I get only the number, 87262, so that I will use it to call any entry from database:

$qryrec="select * from mytable where id='$realid'";
$resultrec=mysql_query($qryrec) or die($qryrec);
$linerec=mysql_fetch_array($resultrec);

Is there any way to do the same without array_shift?

gen_Eric
  • 223,194
  • 41
  • 299
  • 337
Reham Fahmy
  • 4,937
  • 15
  • 50
  • 71
  • It works fine on 5.3. Anyway you can do the same using regexp – Fenec Dec 28 '11 at 22:32
  • `array_shift` works fine in PHP 5.2.6 and beyond. http://ideone.com/NnGIL – gen_Eric Dec 28 '11 at 22:32
  • 3
    *sigh* "Not working" is **never** a good error description. What happens or doesn't happen? What errors do you get? – Pekka Dec 28 '11 at 22:32
  • 1
    You should rather use `current(explode(..))` anyway, or better yet `strtok($id, "-")` if you only want the first part. – mario Dec 28 '11 at 22:35
  • 1
    `php -r 'var_dump(array_shift(explode("-", "87262-any-thing-here.html")));'` Prints `string(5) "87262"`. Update your question and provide full inputs and expected outputs. – Mike B Dec 28 '11 at 22:40
  • Please specify which version the other server is running. Also, try running `$buf = explode ("-", $id); $realid = array_shift ($buf);`, it might be a bug in your specific PHP version. – zrvan Dec 28 '11 at 22:44
  • 1
    strtok($id, "-") works perfect ~ thanks everybody – Reham Fahmy Dec 28 '11 at 23:07

2 Answers2

3

Use

$realid = explode("-", $id);
$realid = $realid[0];
Eugen Rieck
  • 64,175
  • 10
  • 70
  • 92
  • 1.) I use the $realid = $realid[0]; construct allways if I don't need the rest of the array, as it frees the memory used by the rest. 2.) From my reading the construct to reading the first element of an int-based array is $array[0]. 3.) AFAIK array_shift() needs a reference parameter. The output of a function is not one. This might have easily become stricter or less strict in different PHP versions. 4.) Not trusting in PHP "working as I intend, not as I write" seems like a good idea. – Eugen Rieck Dec 28 '11 at 23:39
2

Edit: To obtain the decimal value at the beginning of a string, you can use sscanf:

$url = '87262-any-thing-here.html';
list($realid) = sscanf($url, '%d');

In case the URL has no decimal number at the beginning, $realid will be NULL. With explode you will get an undefined result depending on URL.


array_shift­Docs by it's function reference needs a variable:


enter image description here


(see as well: Passing by Reference)

But you give it a function:

 $realid = array_shift(explode("-", $id));

I would not expect it to always properly work because of that. Additionally this can trigger warnings and errors on some installations.

Instead use a variable:

 $ids = explode("-", $id);
 $realid = array_shift($ids);
 unset($ids);

Or in your case:

 list($realid) = explode("-", $id);

which will assing the first element of the array returned by explode to $realid. See list­Docs.

hakre
  • 193,403
  • 52
  • 435
  • 836
  • 1
    What are you talking about? `array_shift(explode("-", $id));` works fine (I think it throws a E_STRICT warning in PHP 5.3 though). – gen_Eric Dec 28 '11 at 22:34
  • php will simply evaluate the explode() before passing it as an argument. and explode returns an array so it is fine. – David Chan Dec 28 '11 at 22:39
  • @Rocket: Works does not mean is. PHP versions differ and so do error logging configurations, e.g. `Strict Standards: Only variables should be passed by reference` - I would stick to the documentation of the function at first. – hakre Dec 28 '11 at 22:40
  • @hakre: Where in the documentation does it say I can't do `array_shift(explode("-", $id));`? – gen_Eric Dec 28 '11 at 22:41
  • @hakre: It seems to throw a strict warning, but still works. http://codepad.viper-7.com/GRS3At – gen_Eric Dec 28 '11 at 22:44
  • @Rocket: I added a screenshot which highlights it. The error I pasted earlier is the same as in your codepad link (even from ;)). – hakre Dec 28 '11 at 22:49
  • @hakre: Ah, didn't notice the error in your earlier comment. Anyway, I didn't realize passing `explode()` as a reference was bad. Learn something new everyday :-) – gen_Eric Dec 28 '11 at 22:51
  • 1
    @Rocket: Now as you know the problem, here is a PHP hack that prevents the warning ;) (but I discourage using it for production code because this might not work in the future): http://codepad.viper-7.com/BOtWEU – hakre Dec 28 '11 at 22:52
  • @hakre: Heh, wrapping it in `()`. Neat. – gen_Eric Dec 28 '11 at 22:53
  • 1
    @David Chan: But the returned array is not in variable context. See my previous comment how you can get into that context somehow, if you dislike to use a variable. – hakre Dec 28 '11 at 22:54
  • 1
    @Rocket: See this question: [Parentheses altering semantics of function call result](http://stackoverflow.com/q/6726589/367456) – hakre Dec 28 '11 at 22:58