简体   繁体   中英

PHP: using variable in another switch case

Yes i have this line in my first "upload" case of the switch:

$file_name = $USER . "-".rand(1, 888).".jpg";

Then in my switch case "crop" (that takes care of the image you crop), I wish to replace the uploaded one with the new one, simply by uploading with same name. Now i cant use $file_name in the case "crop" so how can I do this?

$file_name = "";

switch ($step_name)
{
    case "upload":
        $file_name = $USER . "-".rand(1, 888).".jpg";
        break;
    case "crop":
        //now you can use $file_name because the scope is outside of the switch statement
        break;
    default:
        break;
}

You would need to place $file_name in a different scope.

A switch is just a lot os IFs. Did you try with IFs?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM