[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

PHP Programming Problem

Posted on 2008-09-30
9
Medium Priority
?
275 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
9 Comments
 
LVL 24

Accepted Solution

by:
glcummins earned 600 total points
ID: 22609739
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
ID: 22609833
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
ID: 22609946
$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
Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

 
LVL 14

Author Comment

by:EMB01
ID: 22609956
Well, I thought that, too. Then I tried 6 and it didn't work either.
0
 
LVL 24

Expert Comment

by:glcummins
ID: 22610385
$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 600 total points
ID: 22610394
Look very closely at your assignments for $rand and $db. They are not the same.
0
 
LVL 2

Assisted Solution

by:idealws
idealws earned 400 total points
ID: 22611518
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 400 total points
ID: 22611542
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
ID: 31501746
Thanks for your help.
0

Featured Post

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Originally, this post was published on Monitis Blog, you can check it here . It goes without saying that technology has transformed society and the very nature of how we live, work, and communicate in ways that would’ve been incomprehensible 5 ye…
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…
Suggested Courses

656 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