Solved

PHP Programming Problem

Posted on 2008-09-30
9
235 Views
Last Modified: 2013-12-12
I have a randomly assigned variable named $rand. I also have a set variable named $db. If $rand eqauls $db, I would like the script below to regenerate $rand up to seven times before displaying the "Stopped trying error." Why won't this code work? Attached is the code:
$rand = rand(100000, 999999);

$db = 5555555;

if ($rand != $db) { 

	echo $rand;

} else {

	for ($counter = 1; $counter < 7; $counter++) {

	$rand = rand(100000, 999999);

	if ($rand != $db) {

		echo $rand;

		} else { 

		echo "Random number could not be generated. Still trying... ";

		if ($counter == 7) {

		echo "Stopped trying."; }

	}

}

Open in new window

0
Comment
Question by:EMB01
  • 4
  • 3
  • 2
9 Comments
 
LVL 24

Accepted Solution

by:
glcummins earned 150 total points
Comment Utility
This is an excellent example of why the BSD/Allman style of coding should be used. Line up your brackets, and you can easily see that one was missing:
<?php

$rand = rand(100000, 999999);

$db = 5555555;

if ($rand != $db)

{ 

	echo $rand;

}

else

{

	for ($counter = 1; $counter < 7; $counter++)

	{

		$rand = rand(100000, 999999);

		if ($rand != $db)

		{

			echo $rand;

		}

		else

		{ 

			echo "Random number could not be generated. Still trying...\n ";

			if ($counter == 7)

			{

				echo "Stopped trying.\n";

			}

		}

	}

}

?>

Open in new window

0
 
LVL 14

Author Comment

by:EMB01
Comment Utility
Thanks, didn't see that - one problem remains though - when I take control of both variables to test the for function, it seems it doesn't echo the "Stopped trying" text. Why is this?
<?php

$rand = 555555;

$db = 5555555;

if ($rand != $db)

{ 

	echo $rand;

}

else

{

	for ($counter = 1; $counter < 7; $counter++)

	{

		$rand = 555555;

		if ($rand != $db)

		{

			echo $rand;

		}

		else

		{ 

			echo "Random number could not be generated. Still trying...\n ";

			if ($counter == 7)

			{

				echo "Stopped trying.\n";

			}

		}

	}

}

?>

Open in new window

0
 
LVL 24

Expert Comment

by:glcummins
Comment Utility
$counter will never equal 7 based on your for() statement. The loop only executes for $counter = 1 through $counter = 6. Less than seven will never equal seven.
0
 
LVL 14

Author Comment

by:EMB01
Comment Utility
Well, I thought that, too. Then I tried 6 and it didn't work either.
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 24

Expert Comment

by:glcummins
Comment Utility
$rand and $db also have to be equal. That block will never execute if they are unequal.
0
 
LVL 24

Assisted Solution

by:glcummins
glcummins earned 150 total points
Comment Utility
Look very closely at your assignments for $rand and $db. They are not the same.
0
 
LVL 2

Assisted Solution

by:idealws
idealws earned 100 total points
Comment Utility
There is a larger chance $rand and $db will never match, however this should get you pointed in the rigt direction.
$rand = rand(100000, 999999);

$db = 555555;

if ($rand == $db) { 

	echo $rand;

} else {

	for ($counter = 0; $counter <= 7; $counter++) {

		$rand = rand(100000, 999999);

		if ($rand == $db) {

			echo $rand."<br>";

		} else { 

			

			if ($counter >= 7) {

				echo "Stopped trying."; 

			}else{

				echo "Random number could not be generated. Still trying... <br>";

			}

		}

	}

}

Open in new window

0
 
LVL 2

Assisted Solution

by:idealws
idealws earned 100 total points
Comment Utility
You could also shorted your code up this way.
$rand = rand(100000, 999999);

$db = 555555;

for ($counter = 0; $counter <= 7; $counter++) {

	if ($rand == $db) {

		echo $rand."<br>";

		break;

	} else { 

		if ($counter >= 7) {

			echo "Stopped trying."; 

		}else{

			echo "Random number could not be generated. Still trying... <br>";

		}

	}

}

Open in new window

0
 
LVL 14

Author Closing Comment

by:EMB01
Comment Utility
Thanks for your help.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
I've been asked to discuss some of the UX activities that I'm using with my team. Here I will share some details about how we approach UX projects.
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now