1

I have a piece of code as below:

ini_set("display_errors", "1");
error_reporting(E_ALL);

$i = 0;
do {
$filename = "links".DIRECTORY_SEPARATOR . $i++ . "-WriteURLs.txt";
} while (file_exists($filename));

$write_URLs = fopen($filename, "w");

foreach ($list_URLs as $htmlIndividual) {
      GetURLs ($htmlIndividual);
      foreach ($URLs as $URLK) {
      fwrite($write_URLs, $URLK ."\n");
    }
echo "Hello World (Inside Foreach Loop) <br>";    
}    
echo "Hello World (First Foreach Loop) <br>";
fclose($write_URLs);
$trimmed = explode("\n",file_get_contents($filename));
foreach ($trimmed as $URLSa) {
    echo "Hello World (Second Foreach Loop) <br>";
}

The code writes the URLs to 0-WriteURLs.txt, 1-WriteURLs.txt, 2--WriteURLs.txt and ... and echos the below on every execution of the code:

Hello World (Inside Foreach Loop) 
Hello World (Inside Foreach Loop) 

But, as you can see, the code stops executing the codes after the first foreach loop and it can not echo the Hello World (First Foreach Loop) and Hello World (Second Foreach Loop) accordingly. Could you please let me know how I can solve this problem? thanks

Note: I added error_reporting = E_ALL to php.ini file and increased memory limit to memory_limit = 256M, but still there is no change.

9
  • turn errors on and tell us what you get Commented Jul 8, 2014 at 19:49
  • 1
    With proper (better) indentation, you wouldn't have to search for matching curly braces. Commented Jul 8, 2014 at 19:54
  • @MrJack, I added error_reporting = E_ALL to php.ini file, but still no change, Commented Jul 8, 2014 at 20:01
  • @Apiah I think you need some refactoring, 4 loops for such simple task is too much, and Hello World (First Foreach Loop) is not even inside of a loop Commented Jul 8, 2014 at 20:06
  • How long does all this take to execute, are you timing out, maybe? (no errors is strange) What does GetURLs do and why is it not $URLS = GetURLS ? Commented Jul 8, 2014 at 20:08

1 Answer 1

1

Your code is executing just fine for me printing both Hello World (First Foreach Loop) and Hello World (Second Foreach Loop) as well as creating the txt file(s).

It's not currently printing "Hello World (Inside Foreach Loop)" as $list_URLs is not defined.

Also try explicitly defining the error reporting error_reporting(E_ALL); ini_set('display_errors', 1);

in your php file to see all errors and warnings inline. This way it's not permanent across your system, just for the one file.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.