简体   繁体   中英

get the value from variable in if else statement in php

I want to get the value from variable in if else statement here and store the value in $mydata variable . When I try to print it or use it in another place nothing appear, it look like nothing stored in there and I want to use it in another php block in the same file. I also tried to make it in function and use it direct in the block I want but the same.

first block that have the value I need to:

<?php
    $mydata; //here when I give it a value directly it work so good but I don't need it like that

         if($t1) { 
                        //statement 
                         if(//check statement ){
    
                             $mydata = 'user1';
        
                         }else
                             // echo message ;
                       }
                       else if($t2){ 
                            //statement 
        
                            if(//check statement ){
        
        
                              $mydata = 'user2';
        
                          }
                          } else
                            // echo message ;
                     }
?>

Second php block:

if(// check statement ) {
         echo ' <form  action="file2.php" method="post">
             // input fields 
          <input type="hidden" name="table" value='.$mydata.'>
         <input  name="login" type="submit" value="submit"> ';
)

The problem is with the way you have written your if...else statement, I have sanitized it for you so that your $mydata variable is now visible and accessible as expected.

$t1 to $t4 are the various test conditions you will like to check your decisions against.

<?php
$t1 = 1;
$t2 = 1;
$t3 = 1;
$t4 = 1;

if ($t1) {
    //statement
    if ($t3) {

        $mydata = 'user1';

    } else {
        // echo message ;
    }
} else if ($t2) {
    //statement

    if ($t4) {

        $mydata = 'user2';

    }
} else {
    // echo message ;
}

echo $mydata;

if ($t4) {
    echo ' <form  action="file2.php" method="post">
          <input type="hidden" name="table" value=' . $mydata . '>
         <input  name="login" type="submit" value="submit">
         </form>';
}
;
?>

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