Click here to Skip to main content
14,639,057 members
Rate this:
Please Sign up or sign in to vote.
See more:
I have written a program in Basic and now I need to reproduce it in php.

Within a (basic) while loop I use the “goto” facility to bypass code in the loop not required if a certain condition is met.

I found a goto code in php manual but it will not work in real life.

Here is the basic code that works

Basic Code
num = TextWindow.Read()
flip = 1
Start:
While (flip < num)
  flip = flip + 1 
   rn = Math.GetRandomNumber(2)  
 	 If rn = 1 Then
 	 goto start
 	EndIf
A = A +1

Here is the PHP code that I cannot get to work
PHP code
<?php 
$num =  100;
start: 
while($flip <= $num) {
	$flip++;
    $rn = rand(0,1);
       if ($rn=0){
       	goto start;
  		}
       $A++;
      } 
      echo " The number of FLIPS Are: $flip <br>";
      echo " The number of Number ONES Are: $A <br>";  
?>


Any help would be appreciated

What I have tried:

All the major php code tute sites
Posted
Updated 11-Aug-20 22:59pm
v3
Rate this:
Please Sign up or sign in to vote.

Solution 1

For starters, even in Basic, you shouldn't use Goto - and certainly not when rewriting code into a more modern language than SmallBasic!

Instead, reverse the If test:
Start:
while (condition)
   if (other condition)
      Goto start
   do something
Can easily become simpler, more obvious just by reversing the condition and losing the Goto:
while (condition)
   if (Not (other condition))
      do something
This is a processes called "despaghettification" and it results in much more maintainable code.

Try it: take your SmallBasic code, clean it up, and then try to write the same functionality - which isn't the same as translated code - in PHP: and pay attention to the details! For example, What is the initial value of flip in both?
   
Comments
OzWaz 3-Aug-20 4:04am
   
Thanks Richard for the response however in the particular application for which I use this program I am not sure that your solution would work PLUS I don’t know enough php to make a new system work in php.

This program takes a coin flips it 1,000,000,000 time calculates the number of recurring sequences for each category up to 35 iterations for a single sequence and then calculates the number of sequencing for each category and then the % differentiation between each category and then % of the total events
I can do that because I understands Basic and have been using it since 1982 and now to old now to learn a new way of plucking chickens, I was hoping to easily convert to php so I could embellish the program with HTML
Here is the program if you can tell me how to achieve that in php I would be grateful

program:
TextWindow.ForegroundColor = "Yellow"
TextWindow.Writeline("What amount?")
num = TextWindow.Read()
flip = 1
Start:
While flip < num
flip = flip + 1

rn = Math.GetRandomNumber(2)
If rn = 1 Then
goto start
EndIf
A = A +1

rn = Math.GetRandomNumber(2)
If rn = 1 Then
goto start
EndIf
B = B +1

rn = Math.GetRandomNumber(2)
If rn = 1 Then
goto start
EndIf
C = C +1

rn = Math.GetRandomNumber(2)
If rn = 1 Then
goto start
EndIf
D = D +1

rn = Math.GetRandomNumber(2)
If rn = 1 Then
goto start
EndIf
E = E +1

rn = Math.GetRandomNumber(2)
If rn = 1 Then
goto start
EndIf
F = F +1

rn = Math.GetRandomNumber(2)
If rn = 1 Then
goto start
EndIf
G = G +1

rn = Math.GetRandomNumber(2)
If rn = 1 Then
goto start
EndIf
H = H +1

rn = Math.GetRandomNumber(2)
If rn = 1 Then
goto start
EndIf
I = I +1

rn = Math.GetRandomNumber(2)
If rn = 1 Then
goto start
EndIf
J = J +1

rn = Math.GetRandomNumber(2)
If rn = 1 Then
goto start
EndIf
K = K +1

rn = Math.GetRandomNumber(2)
If rn = 1 Then
goto start
EndIf
L = L +1

rn = Math.GetRandomNumber(2)
If rn = 1 Then
goto start
EndIf
M = M +1

rn = Math.GetRandomNumber(2)
If rn = 1 Then
goto start
EndIf
N = N +1

rn = Math.GetRandomNumber(2)
If rn = 1 Then
goto start
EndIf
O = O +1

rn = Math.GetRandomNumber(2)
If rn = 1 Then
goto start
EndIf
P = P +1

rn = Math.GetRandomNumber(2)
If rn = 1 Then
goto start
EndIf
Q = Q +1

rn = Math.GetRandomNumber(2)
If rn = 1 Then
goto start
EndIf
R = R +1

rn = Math.GetRandomNumber(2)
If rn = 1 Then
goto start
EndIf
S = S +1

rn = Math.GetRandomNumber(2)
If rn = 1 Then
goto start
EndIf
T = T +1

rn = Math.GetRandomNumber(2)
If rn = 1 Then
goto start
EndIf
U = U +1

rn = Math.GetRandomNumber(2)
If rn = 1 Then
goto start
EndIf
V = V +1

rn = Math.GetRandomNumber(2)
If rn = 1 Then
goto start
EndIf
W = W +1

rn = Math.GetRandomNumber(2)
If rn = 1 Then
goto start
EndIf
X = X +1

rn = Math.GetRandomNumber(2)
If rn = 1 Then
goto start
EndIf
Y = Y +1

rn = Math.GetRandomNumber(2)
If rn = 1 Then
goto start
EndIf
Z = Z +1




EndWhile

TextWindow.Writeline("Number of flips undertaken was: " + flip)
TextWindow.Writeline("Number of 1 consecutive heads was:" + A + " @ " + A*100 /flip + " % of Flips")
TextWindow.Writeline("Number of 2 consecutive heads was:" + B + " @ " + B*100 /flip + " % of Flips " + "PLUS @ " + Math.Round(B*100/A) + " % of 1")
TextWindow.Writeline("Number of 3 consecutive heads was:" + C + " @ " + C*100 /flip + " % of Flips " + "PLUS @ " + Math.Round(C*100/B) + " % of 1")
TextWindow.Writeline("Number of 4 consecutive heads was:" + D + " @ " + D*100 /flip + " % of Flips " + "PLUS @ " + Math.Round(D*100/C) + " % of 1")
TextWindow.Writeline("Number of 5 consecut
Patrice T 3-Aug-20 4:18am
   
Use Improve question to update your question.
So that everyone can pay attention to this information.
Richard MacCutchan 3-Aug-20 4:41am
   
"I understands Basic and have been using it since 1982 and now to old now to learn a new way of plucking chickens"
I started programming in 1966, but only learned PHP in the last 6 to 12 months. You are never too old to learn.

With all those duplicate bits of code, and goto statements it is very difficult to turn it into something logical, without spending quite a lot of time.
Rate this:
Please Sign up or sign in to vote.

Solution 2

You have not declared either $flip or $A before trying to use them in an expression. Also, using goto is really bad practice, there are better ways of resetting your values.

I have modified your code to the following, although I am not sure what it is supposed to do. As shown, it runs (almost) for ever.
$num =  100;
$flip = 0;
$A = 0;
while($flip <= $num) {
    $flip++;
    $rn = rand(0,1);
    if ($rn == 0){
       $flip = 0;
       $A = 0;
    }
    $A++;
} 
    echo " The number of FLIPS Are: $flip <br>";
    echo " The number of Number ONES Are: $A <br>";  


[edit]
Here is a complete webpage with the implementation of your algorithm, without any goto statements:

<!DOCTYPE HTML>  
<html>
<head>
</head>
<body>  

<?php
    function flipper($count) {
        $num = $count;
        $sets = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
        for ($flip = 0; $flip < $num; $flip++) {
            for ($i = 0; $i < 26; $i++) {
                $r = rand(0,1);
                if ($r == 1) {
                    break;
                }
                $sets[$i]++;
            }
        }
        for ($i = 0; $i < 26; $i++) {
            echo '$sets[' . $i . "]: $sets[$i]" . '<br>';
        }
    }

    flipper(1000);
?> 


</body>
</html>


[/edit]
   
v3
Comments
OriginalGriff 3-Aug-20 4:30am
   
He posted a reply starting "Thanks Richard" to my answer - but it's pretty long so I'll leave it there instead of reposting it.
Richard MacCutchan 3-Aug-20 4:42am
   
Thanks, added my two penn'orth.
OzWaz 4-Aug-20 0:14am
   
Thanks, Richard, for trying to give me assistance with the php code

I don’t see how it can work the same as the Basis code does.

As you can see from the code above (Basic), the program acts like cascading wine glasses assessing consecutive event and aborting progress when an unsupported option is presented.

This together with the counter allows the recording of a large the number of consecutive events for 26 circumstances.

Here is a sample of the data generated

Events 10,000
Selected 5008
Consecutive
2 2474
3 1236
4 672
5 332
6 153
7 77
8 31
9 16
10 6
11 8
12 1
13 0
And so on …

The loop needs to be aborted/redirected each time an unsupported option occurs as a consequence of an event (Random Number generated)

Only approx. 3 times in 1,000,000,000 events does an entire Loop occur.

The reason why “it runs (almost) for ever.” is that as you can see from the Basic Program code above “While flip < num” php equivalent - is missing.

Coding is not a large part of my life and fondly enough I find it more time efficient to copy and paste that to spend time searching for a more efficient alternative.

It actually does the same job and the computer doesn't seem to mind how long you code is.

I did read some of the criticisms of using the “goto” facility but in certain situations, like mine, where my programs are not complex and for no one else’s use but mine they still have a legitimate (and BEST Option) application - if only they (php) would work.

Regards
Richard MacCutchan 4-Aug-20 3:37am
   
Well that is considerably different from the code in your original question, which was less than clear. And as I said in my earlier comment, it would take quite some time to analyse your original program and rewrite it into any other language. As to your last sentence, PHP will work, but only if you write the correct code.
Richard MacCutchan 4-Aug-20 4:13am
   
See my updated solution, actually took less time than I thought.
OzWaz 7-Aug-20 16:53pm
   
Firstly, Richard I would like you to know I appreciate your efforts, PLUS: People who volunteer to give of their time and abilities to help other people - for no reward, I find are amazing.

However when I posted a portion of code seeking help from the Forum to resolve a conundrum, the outcome I was looking for was to find out why the PHP “goto” facility in my code (as described in the PHP manual https://www.php.net/manual/en/control-structures.goto.php) was not performing as such, in my code.

In my search to resolve the problem before I sought the assistance of this Forum I had already been acquainted with the fact that in the practice of coding in this language, the use of the 'goto' facility in your code was, at worse, frowned upon and at best to be used with caution.

So I was already aware of that particular situation BUT I noted the comment on the above cited webpage “Basically, the fight against 'goto' is just a side effect of a misleading article many decades ago. Those monsters are gone now. Feel free to use it when you know what you're doing.”

Many people are happy to eat food without known how to grow it, unfortunately for me, my personality is not of that ilk.

My objective is to be able to learn enough and understand its function in PHP to be able for me to reproduce the basic program that works- in PHP.

Figuring out how something works so that I can use it with confidence and fix it if it breaks is how I like to do things.

I did out of curiosity also run your code on my XAMPP system but come up with this error:-"Parse error: syntax error, unexpected '<', expecting end of file in C:\xampp\htdocs\coin\fliper.php on line 28"

If you know the reason as to why the goto function in the initial code that I posted does not work, it would be great if you would share that, otherwise thank you for all your efforts but I must look elsewhere.

Regards Warren
Richard MacCutchan 8-Aug-20 3:48am
   
"this error:-"Parse error: syntax error, unexpected '<', expecting end of file in C:\xampp\htdocs\coin\fliper.php on line 28""
Well, unless you show us the actual code and identify line 28, there is nothing we can do to assist. Incidentally the code I offered above has only 17 lines.

"why the goto function in the initial code that I posted does not work"
I don't think I made that claim. I merely showed you how easy it is to write code that does not use goto. If you wish to use it then that is entirely your choice, we cannot stop you any more than we can stop you eating vegemite.
OzWaz 9-Aug-20 0:43am
   
Sorry I didn’t actually look at the code in detail, I just copied and pasted it into a file with some HTML header and ran it through my XAMPP system.

Not being that familiar with PHP and now taking a look at the code I see the code was not closed off “?>” - So once I closed it, the error message when away.

Now when I run the code nothing prints to the screen.

If the code was replicating the function of the Small Basic program it would have delivered a printout like this:

Number of flips : 1000
Heads - 1 :488
Heads - 2 :242
Heads - 3 :117
Heads - 4 :59
Heads - 5 :27
Heads - 6 :20
Heads - 7 :7
Heads - 8 :4
Heads - 9 :3
Heads - 10 :1
Heads - 11 :
Heads - 12 :
Heads - 13 :
Heads - 14 :
Heads - 15 :
Heads - 16 :
Heads - 17 :
Heads - 18 :
Heads - 19 :
Heads - 20 :
Heads - 21 :
Heads - 22 :
Heads - 23 :
Heads - 24 :
Heads - 25 :
Heads - 26 :

My Dreamweaver program is telling me that there is a problem with this line of code
“$sets = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];”

However, https://phpcodechecker.com/is telling me that the code is fine.

But none of this really matters because all I am trying to do is to find out why my use of the "goto" in my PHP scrip did not work. I wasn’t actually looking for someone to write the program for me.

In closing I don’t understand what you meant by this

["why the goto function in the initial code that I posted does not work"- I don't think I made that claim.]
and $Vegemite="Yuck";

Regards
Richard MacCutchan 9-Aug-20 3:54am
   
See my latest update which is a complete webpage that does what you want. I have no idea how or if you need to modify it for dreamweaver, but it works in Chrome. I get the following on the screen when I load the page:
$sets[0]: 476
$sets[1]: 224
$sets[2]: 106
$sets[3]: 53
$sets[4]: 29
$sets[5]: 17
$sets[6]: 7
$sets[7]: 6
$sets[8]: 3
$sets[9]: 2
$sets[10]: 2
$sets[11]: 2
$sets[12]: 1
$sets[13]: 1
$sets[14]: 0
$sets[15]: 0
$sets[16]: 0
$sets[17]: 0
$sets[18]: 0
$sets[19]: 0
$sets[20]: 0
$sets[21]: 0
$sets[22]: 0
$sets[23]: 0
$sets[24]: 0
$sets[25]: 0

which is as close as makes no difference to your results.

So while your code with goto statements may work, mine is much simpler. And crucially, when it goes wrong it is much easier to debug.
OzWaz 11-Aug-20 2:10am
   
Yes, that is correct it is now a PHP program that does exactly the same as small basic does - but with a few limitations.

Firstly, small basic also has an input facility so that you can enter the number of flips from the front end as would do with php and the HTML form tag into a database- But I can work on that one.

Secondly, it’s not correct to say “And crucially, when it goes wrong it is much easier to debug.”

It’s only easier to debug for those who have a good grasp of PHP but for me just starting to learn about PHP and without constructing the program myself and understanding how it all works - it would be a horror, whereas small basic is very easy for me to debug.

But it is a solid base and by analyzing it and playing with it, I am sure that I can deduce what all the functions are and eventually I will be able to use it in the format I want.

I couldn’t stay in small basic because I don’t know how to embed that into a HTML webpage.

I do appreciate that you’ve stayed with me on this project it’s been an interesting journey and again I thank you for all your efforts
Richard MacCutchan 11-Aug-20 2:53am
   
Adding an input feature is a simple matter of some HTML: an input type and a button, and there are plenty of samples around. I strongly suggest you go to W3Schools Online Web Tutorials[^] where you can learn both HTML and PHP in a simple interactive set of tutorials.
OzWaz 12-Aug-20 4:59am
   
The program is working fine but I am sorry to report Richard that I think there is a couple of things that php cannot do that my basic program could.
For example, here is print out of my Basic
Number of flips undertaken was: 5000000
Number of 1 consecutive heads was:2498815 @ 49.9763 % of Flips
Number of 2 consecutive heads was:1249607 @ 24.99214 % of Flips AND @ 50.22 % of 1
Number of 3 consecutive heads was:624458 @ 12.48916 % of Flips AND @ 49.85 % of 2
Number of 4 consecutive heads was:311990 @ 6.2398 % of Flips AND @ 51.26 % of 3

As you can see - as well as recording the consecutive data (A) it also calculates and prints the percentage of each particular consecutive relevant to the total (B) PLUS it then calculates and prints the percentage of each particular consecutive relevant to the one immediately prior (C).
I was able to deduce how to modify your program to achieve the achieve B but to achieve C maybe a bridge to far I think
Viz:
Consecutive Heads No. of times % of Total Tested
1 500173 50.0173
2 249894 24.9894
3 124893 12.4893
4 62705 6.2705
5 31249 3.1249

The other deficiency is that it cannot analyse 1,000,000,000 cycles it returns this
“Fatal error: Maximum execution time of 30 seconds exceeded in C:\xampp\htdocs\coin\fliper3.php on line 42”
About 75,000,000 cycles is all it will do I have not idea why
I have supplied the program in case you want to take it for run

Regards
Richard MacCutchan 12-Aug-20 5:07am
   
You can calculate anything in PHP, it is a rich functional language. What you cannot do is learn everything in a couple of days. As to the error about max time, see maximum execution time php - Google Search[^].
OzWaz 12-Aug-20 5:02am
   

Probabilities




Enter Number :






';
echo "Of which the following resulted:-";
?>







";

}
}

flipper($tell);


?>

Consecutive Heads No. of times % of Total Tested $i $sets[$i] $per
OzWaz 12-Aug-20 5:11am
   
Richard

Maybe best to look here http://warrenbolton.com/site/fliper3.php
Richard MacCutchan 12-Aug-20 5:15am
   
Sorry I cannot see anything, apart from the fact that your calculations do not yield valid numbers. NAN in floating point means it is too big or too small to be represented as an actual value. And without seeing the code that creates those values ...
Rate this:
Please Sign up or sign in to vote.

Solution 3

<html><head></head><body><!DOCTYPE HTML>  


<link href="basic.css" rel="stylesheet" type="text/css">


<h1>Probabilities</h1>
<br>
<form method="POST">
<label>Enter Number : </label>
<input type="number" name="numb">
<input type="submit" value="Submit">
</form>
<br>


<!--?php
$tell=$_POST["numb"];
echo " The Number of flips were: " ;
echo $tell . '<br--><br>';
echo  "Of which the following resulted:-";
?>

<br><br>




      
                
        		
                
  


			         
			         
			 	  
			      ";
			
        }
    }

    flipper($tell);
	
	
?> 

<table width="50%" margin-left:auto;="" margin-right:auto;="" border="1" cellspacing="10" cellpadding="10"><tbody><tr><th width="50%" align="center">Consecutive Heads </th><th width="25%" align="center">No. of times </th><th width="25%" align="center"> % of Total  Tested</th></tr><!--?php
$tell=$_POST["numb"];

    function flipper($count) {
        $num = $count;
        $sets = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
        for ($flip = 0; $flip < $num; $flip++) {
            for ($i = 1; $i < 26; $i++) {
                $r = rand(1,2);
			
                if ($r == 2) { 
                    break;
                }
                $sets[$i]++;
            }
        }
        for ($i = 1; $i < 26; $i++) 
		{
			$per =  $sets[$i]*100/$num;
			echo "<tr--><tr><td style="text-align: center">$i</td><td style="text-align: center">$sets[$i] </td><td style="text-align: center">$per</td></tr><tr></tr></tbody></table>


</body></html>
   
v2
Comments
Richard MacCutchan 12-Aug-20 5:34am
   
$per =  $sets[$i]*100/$num;

That is integer arithmetic so the answer will always be NAN. You need to convert the value to floating point by multiplying by a floating point value thus:
$per =  $sets[$i] * 100.0 / $num;
OzWaz 12-Aug-20 19:07pm
   
Thanks
Interesting experience for me.

I effected the changes as advised and uploaded it to my web server.

Strangely enough entering one value (Say 10000) will returns a "Percentage" result to two decimal places, yet entering a subsequent value (Say 11000) will returns a result to 12 decimal places.

It also gives the same result without the change.

You might like to tested it yourself but it is beyond my understanding-any further suggestion?

Regards
Richard MacCutchan 13-Aug-20 5:31am
   
That is correct, the default when printing a floating point number is to print all digits if possible. If you want only the first two the you need to use a formatting function: see PHP: printf - Manual[^]. It is also an idea to understand how floating point numbers can give what you might think are strange results: see What Every Computer Scientist Should Know About Floating-Point Arithmetic[^].

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100