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

  • Home
  • SEARCH
  • 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 6124703
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:09:47+00:00 2026-05-23T16:09:47+00:00

I have written a script using my local PHP 5.3 installation making use of

  • 0

I have written a script using my local PHP 5.3 installation making use of the goto statement. Uploading it to my webserver, I had to discover that my hoster still has PHP 5.2 installed and therefore doesn’t support goto. My question is therefore how to emulate goto‘s functionality in the following context:

foo();

iterator_start:

foreach ($array as $array_item) {
    switch ($array_item) {
        case A:
            foo();
            break;
        case B:
            // Substitute
            array_splice($array, offset($array_item), 1, array(A, A, B, C));
            // Restart iterator
            goto iterator_start;
            break;
    }
}

The idea is that an array must be modified according to a set of substitution rules. Once a rule has been executed (which may modify any number of array items in any position, replace them, even delete them) everything must start from zero because the entire array may have changed in an unpredictable fashion (thus recursive substitution rules are allowed as well). The solution I use, with goto simply jumping to the start of the loop after each substitution, seems very straightforward and even quite clean to me, but as I mentioned I cannot use it on my webserver.

Is there any substitute for goto here, or can the same task be accomplished in an entirely different manner (preferably without changing too much code)?

Any ideas are appreciated.

A polite request: Please spare me any lectures on the usefulness or dangers of goto here. I’ve read PHP and the goto statement to be added in PHP 5.3 and know about spaghetti code and all those en vogue “considered harmful” letters from the 1980s. Discussing the supposed evil of goto has nothing to do with my question, and treating any program construct as “bad style” per se is simply dogma, which has no place in my programming ideology.

  • 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-23T16:09:47+00:00Added an answer on May 23, 2026 at 4:09 pm

    You can use each() in a while loop. It uses the internal array cursor to retrieve the current element in the array and move the cursor to the next one. When slicing the array, reset to cursor to restart from the start of the array at the next while loop iteration. Calling reset() is probably not even required, it’s probably a side effect of array_splice() since it changes the array.

    foo();
    while (list($key, $array_item) = each($array)) {
      switch ($array_item) {
        case A:
          foo();
          break;
        case B:
          // Substitute
          array_splice($array, offset($array_item), 1, array(A, A, B, C));
          // Reset array cursor, this is probably not necessary 
          reset($array);
          break;
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a script for file download for IE. It works fine if
I am new to python and i have written a script that converts a
I have written a Perl CGI script which fetches an html page from a
I have been experimenting and trying to learn JQuery, using AJAX to consume a
I have written a C++ program that reads in a file and outputs the
I have written some extension methods that extend the html helper class for client
use warnings; $reff_filepath=/home/sharma/Documents/ref.txt; open (CONFIG,<,$reff_filepath)or die Unable to open $reff_filepath: $!; while(<CONFIG>) { chomp;
First, Let me apologize as I know NOTHING about ruby. I can read through
What's the best way to catch stack overflow in C? More specifically: A C

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.