Click here to Skip to main content
15,884,986 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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

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?
 
Share this answer
 
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.
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.
PHP
$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:

HTML
<!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]
 
Share this answer
 
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.
HTML
<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>
 
Share this answer
 
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, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900