Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8012043
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T19:12:35+00:00 2026-06-04T19:12:35+00:00

Say we have this array of strings: $arrString = [1, 2, 3]; One traditional

  • 0

Say we have this array of strings:

$arrString = ["1", "2", "3"];

One traditional way of converting the values to integers are like so:

foreach ($arrString as $key => $value)
    $arrString[$key] = (int) $arrString[$key];

echo var_dump($arrString);

This outputs:

array(3) { [0]=> int(1) [1]=> int(2) [2]=> int(3) }

Much expected. However, I believe using a reference is a much quicker way of getting the same work done:

foreach ($arrString as &$strValue)
    $strValue = (int) $strValue;

 echo var_dump($arrString);

Well guess what it outputs?

array(3) { [0]=> int(1) [1]=> int(2) [2]=> &int(3) }

Which is to say it assigned the last value as a reference to an int. This always happens to the last element when using a reference in the loop (even when there’s just one element), and it also happens irrespectively if I use the (int) cast or PHP’s settype- and intval functions.

It beats me; Why is this happening? And should I really care?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T19:12:36+00:00Added an answer on June 4, 2026 at 7:12 pm

    You should care, and it’s been explained many times before here on SO (and there’s an explicit warning in the PHP documentation)

    Do

    unset($strValue);
    

    after the loop

    See here for an explanation

    EDIT

    Reference

    with quote:

    Warning

    Reference of a $value and the last array element remain even after the foreach loop. It is recommended to destroy it by unset().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have an array of Strings declared like this: String[] strings = new
Lets say I have an array with a structure like this: $arr= Array( array(
Let's say I have an array like this: $array = array (this,is,me,and,I,am,an,array,hello,beautiful,world); How can
Say I have an array like this: [auburn, http://auburn.craigslist.org/web/, http://auburn.craigslist.org/cpg/, http://auburn.craigslist.org/eng/, http://auburn.craigslist.org/sof/, http://auburn.craigslist.org/sad/] What
Say I have an array like this: [white, red, blue, red, white, green, red,
I have a 2d array, let's say like this : 2 0 8 9
Lets say I have string like this: String q = foo (one) bla (two)
Let's say we have an array of PRINTER_INFO_2 like this: PRINTER_INFO_2* printers = (PRINTER_INFO_2*)malloc(sizeof(PRINTER_INFO_2)
I have a mutable array of strings. For this example, let's say there are
So let's say I have this array: $requiredFruit= @(apple,pear,nectarine,grape) And I'm given a second

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.