Transfer a variable to earier point without goto

110 views Asked by At

How to write this without goto:

ob_start();
$a = 0;
echo "START of LEFT<br />";
begin:
   if($a > 0) {
      echo "CONTENT LEFT: $a<BR />";
      <VERY DIFFICULT ALGORHITM>
       goto end;
   }
<... ALL THE REST CODE OF LEFT ...>
echo "END of LEFT<br /><br />";
$output1 = ob_get_contents();
ob_end_clean();

ob_start();    
echo "START of CENTER<br />";
$a = 5; goto begin;
end:
   <... ALL THE REST CODE OF CENTER ...>
   echo "END of CENTER<br />";
   $output2 = ob_get_contents();
   ob_end_clean();
   // print it
   echo $output1.$output2;

To get this echo:

START of LEFT
CONTENT LEFT: 5
END of LEFT

START of CENTER
END of CENTER

Requirements:

1. I'm not allowed to change the order(CORE( echo $a ), and PLUGIN( $a=5 )):

ob_start();
$a = 0;
<ANY CODE>
echo $a;
$output1 = ob_get_contents();
ob_end_clean();

ob_start();
<ANY CODE>
$a = rand(0,10);
$output2 = ob_get_contents();
ob_end_clean();

2. Output must be generated via ob_get_contents();

But I'm allowed to write ANY CODE in places.

// Solvings ob_get_contents(); Helps only if want to replace few lines in output HTML CODE, but can't change a value of variable, to change the ALGORHYTM(depends of var value), which generates the random HTML code.

Also, As I checked my code, I understand, that my code, even with GOTO labels statement , DOES NOT going to change the $output1 content ?. How to do that? Is the only way is to recache the $output1 from his beggining. Or maybe I'm able to do this in other ways?

3

There are 3 answers

0
moritz On

You are familiar with the concept of methods/functions? If not ( and it seems that chances are.. ) you should really learn something about those first. It's then a piece of cake to split functionality out of a monolithic block of code to small, maintainable pieces of code.

0
Josh Ribakoff On
0
Dr.Optix On

If i understood well what you want to do I would use recursive functions instead of goto. for example look at this math expression parser i've witten in C some time ago:

    #include <cstdio>

const long MAX = 100010;
char S[MAX], *p=S;

long eval();
long termen();
long factor();


int main() {
    FILE *fin=fopen("evaluare.in", "r");
    FILE *fout=fopen("evaluare.out", "w");

    fgets(S, MAX, fin);
    fprintf(fout, "%ld\n", eval());
    return 0;
}

long eval() {
    long r = termen();
    while ( *p=='+' || *p=='-' ) {
        switch ( *p ) {
            case '+':
                ++p;                        // go over "+"
                r += termen();
                break;
            case '-':
                ++p;                        // go over "-"
                r -= termen();
                break;
        }
    }
    return r;
}

long termen() {
    long r = factor();
    while ( *p=='*' || *p=='/' ) {
        switch ( *p ) {
            case '*' :
                ++p;
                r *= factor();
                break;
            case '/':
                ++p;
                r /= factor();
                break;
        }
    }
    return r;
}

long factor() {
    long r=0;
    if ( *p == '(' ) {                      // subexpression
        ++p;                                // go over '('
        r = eval();
        ++p;                                // go over ')'
    } else {
        while ( *p>='0' && *p<='9' )  {     // number
            r = r*10 + *p - '0';
            ++p;
        }
    }
    return r;
}

The main idea is to split the code in functions and where you have let's say:

goto apocalypse

you'll have:

Apocalypse();

That's the long story said short.