Page 1 of 1

Breaking out of this while statement...

Posted: Wed Feb 04, 2004 8:33 pm
by neophyte
Okay I need to figure out how to break out of this while statement. I'm searching the file $comparefile for a duplicate link ($newlink). The script executes and then stops when $newlink != $word. But I need to be able to break out to send an error message when a match is found. Thanks.

Code: Select all

$fp = fopen("templates/links.txt", 'r');
		$comparefile = fgets($fp);
		$delims = """";
		$word = strtok($comparefile, $delims);
		while (is_string($word) &&  $newlink != $word ){
			if ($word){
			print "$word <br>";
			//C an't figure out where to put this. print "Link has already been used.";
			&#125;
			$word = strtok($delims);
			&#125;

Posted: Wed Feb 04, 2004 8:47 pm
by DuFF
Break out of it then :D

Dude man

Posted: Wed Feb 04, 2004 8:51 pm
by neophyte
Like if I knew how to break; out I would do it.... :lol:

Hopless lost newbie....

Give a dog a bone?

Posted: Wed Feb 04, 2004 9:00 pm
by DuFF
I guess you could just do this?

Code: Select all

<?php
$fp = fopen("templates/links.txt", 'r');
      $comparefile = fgets($fp);
      $delims = """";
      $word = strtok($comparefile, $delims);
      while (is_string($word))  {
         if($newlink == $word)
         {
              echo "Link has already been used."
              break 2;  //break out of the if statement and the while statement
         }
         if (!empty($word))  {
              print "$word <br>";
         }
         $word = strtok($delims);
      }
?>
There are many other ways to avoid this, you rarely see "break" used.

Code: Select all

<?php
      if($newlink == $word)  {
          echo "Link has already been used."
      }
      else  {
		  while (is_string($word) &&  $newlink != $word ){
			if ($word){
			print "$word <br>";
			}
			$word = strtok($delims);
		  }
      }
?>

THANKYOU FOR THE BONE!

Posted: Wed Feb 04, 2004 9:24 pm
by neophyte
Thanks Duff the code worked great! But I'm sure that's no suprise. I was so close I could smell it. MMMMMMMMM SILICONE! I knew the elements I needed but didn't quite know how to make it come together.....

You da man,

Thanks! :D