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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:43:11+00:00 2026-06-14T14:43:11+00:00

I wrote a function for my coursework that will receive a string, a starting

  • 0

I wrote a function for my coursework that will receive a string, a starting number and a length. The function will take the string and output the string that’s created by taking all the letters including and after string{n}

for example:

string = kingsbridge, n = 4, l = 4.
string created = gsbr.  

The first two validation parts of the function work fine, but when it actually comes to creating the substring to be output the following occurs:

Notice: Uninitialized string offset: 11 in G:\xampp\htdocs\say\ws21C.php on line 58
Notice: Uninitialized string offset: 12 in G:\xampp\htdocs\say\ws21C.php on line 58
Notice: Uninitialized string offset: 13 in G:\xampp\htdocs\say\ws21C.php on line 58    
Notice: Uninitialized string offset: 14 in G:\xampp\htdocs\say\ws21C.php on line 58

…and so on and so on (infinite loop)

here’s my code:

function substring($s, $n, $l){
    $substring = "";

    if($n > length($s)){     //If asking for substring that starts with an index of the string that's greater than the actual length
        echo "Out of range";
    }
    else{
        if($l > (length($s) - ($n - 1))) { //If start is in index of string but length would mean substring would trail off string
            $newLength = ((length($s)) - ($n - 1));
            substring($s, $n, $newLength);
        }
        else{ //PROBLEM CODE
            for($letter = ($n - 1); $l; $letter++){
                $substring .= $s{$letter};  //Line 58
            }
            echo "$substring";
        }
    }
}

Any help as to why this is happening (or why it’s created an infinite loop) would be appreciated!

Thank you 🙂

edit: I know that in the example i gave string{4} would be s, but the user inputs the number, so I will minus 1 to get the letter they need

  • 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-14T14:43:13+00:00Added an answer on June 14, 2026 at 2:43 pm

    Your for loop is a mess, pure and simple. Take a good look at it (first clearing the infinite loop issue):

            for($letter = ($n - 1); $l; $letter++){
                $substring .= $s{$letter};  //Line 58
            }
    

    So, you’re setting $letter to be $n-1, and I assume that you want to loop over all the letters. Then, your loop condition is “if $l”. You’re not changing the value of $l at all in the for loop, so how could you expect the program to break out of it?

    If you’re trying to loop over all the letters, as it stands, you’re better off doing $letter = $n-1; $letter >= 0; $letter--.

    Finally, I need the content of $s to be able to debug the other issue. Edit your question!

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

Sidebar

Related Questions

I wrote a function in haskell that takes a few parameters like Word32, String
I wrote a function that that shortens a string (sentence of words) at requested
I wrote a function that selects all the users from my database and all
I wrote a function that gives me the length of a dynamic array by
I wrote following function void validateUser(void) { string uName; string uPassword; char c; map
I have the following function that I wrote function range_gen($num1,$num2){ $exp = explode('.',$num1); $accuracy
I wrote a function append_line() in ftplib (based on storelines()): def append_line(self, cmd, string):
I wrote this function that uses a binary search to look for a specific
I wrote this function which will attempt to store the map but its not
I created a textbox and wrote onfocus=getFocus() Now I wrote function in javascript function

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.