English 中文(简体)
Transfer a variable to earier point without goto
原标题:

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?

问题回答

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.

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
", 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.





相关问题
Write-though caching of large data sets in WCF?

We ve got a smart client that talks to a SQL Server database via WCF, displaying the entities in the database, and allowing the user to edit those entities. Some of the WCF calls return a large data ...

Clearing RSL in Cache

I have built a flex application which has a "main" project and it is assosciated with a few RSL s which are loaded and cached once i run my "main" application. The problem i am facing is that the ...

how to tell clicking "back" to load cache?

I would like for my site when someone clicks "Back" or "Forward" for the server to tell the browser to load the cache instead of reloading the entire page. I ve tested some headers and done research, ...

java plugin cache and dynamic IP host

I m trying to use Amazon S3 and Amazon Cloudfront CDN to deliver the jar files of my applet application. I m seeing several cache misses of my jars by the java plugin. This is a show-stopper for me, ...

Frequently Used metadata Hashmap

Are there any implementations of a static size hashtable that limits the entries to either the most recently or most frequently used metadata? I would prefer not to keep track of this information ...

PHP - Memcache - HTML Caching

I would like to create a caching system that will bypass some mechanisms in order to improve the performance. I have some examples: 1-) I have a dynamic PHP page that is updated every hour. The page ...

Performance of Sql subqueriesfunctions

I am currently working on a particularly complex use-case. Simplifying below :) First, a client record has a many-to-one relationship with a collection of services, that is, a single client may have ...

热门标签