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 6381767
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:26:15+00:00 2026-05-25T02:26:15+00:00

In PHP, How do I do this: $test = array(1,2,3,4); $i=0; While (!empty($test)) {

  • 0

In PHP, How do I do this:

$test = array(1,2,3,4);

$i=0;
While (!empty($test)) {

  //do something with $test[i];

  //remove  $test[$i]; 
  unset($test[$i]);

}

Thanks

  • 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-05-25T02:26:15+00:00Added an answer on May 25, 2026 at 2:26 am

    There are a few ways to modify an array while iterating through it:

    You can use array_pop or array_shift depending on the order that you want the elements in

    while( $val = array_pop( $arr ) )
    -or-
    while( $val = array_shift( $arr ) )
    

    This does have a catch in this form, it will end for any falsy value listed in the type comparison table, array(), false, "", 0, "0", and null values will all end the while loop

    Typically this is mitigated with

    while( ( $val = array_pop( $arr ) ) !== null )
    

    The strict type comparison should be used as null == false and the other falsey values. Again the issue will be that it will stop the iteration if a value in the array is actually null

    A safer way of iterating through an array is to test that it’s not empty:

    while( $arr )
    //while( count( $arr ) )
    //while( !empty( $arr ) )
    {
      $val = array_pop( $arr );
    
      //for forward iteration
      //$val = array_shift( $arr );
    }
    

    It’s really that simple empty() is the opposite of an if() check. There is a hidden advantage to !empty() in the while loop, which is that it suppresses errors if the variable is undefined at the time of the check.

    In well-written code™ this isn’t an issue, however well-written code™ doesn’t occur naturally in the wild, so you should be extra careful just-in-case.

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

Sidebar

Related Questions

is it possible to do something like this in PHP 5.2.1? abstract class Test
I have a page that is accessed via a URL like this: http://power-coder.net/Test/something.php?id=3#Page1 I
<?php $language = $_SERVER['HTTP_ACCEPT_LANGUAGE']; echo $language; ?> When I use Firefox to test this
This is driving me crazy. I have this one php file on a test
This example is from php.net: <?php function Test() { static $a = 0; echo
Let's say I have this url: www.example.com/test.php?page=4 How do i retrieve the value of
http://steph.net23.net/work.php here is my test link. This page has a jquery script in it
i have tried this: <?php $fileip = fopen(test.txt,r); ?> this should have opened the
im using a simple test script from http://www.tuxradar.com/practicalphp/8/11/0 like this <?php $fp = fopen(foo.txt,
Stupid question, this code: <?php $variable = system(cat /home/maxor/test.txt); echo $variable; ?> with file

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.