简体   繁体   English

从 php 的 if else 语句中的变量中获取值

[英]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 .我想在此处从 if else 语句中的变量中获取值,并将该值存储在$mydata 变量中。 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.当我尝试打印它或在另一个地方使用它时,什么都没有出现,它看起来没有存储在那里,我想在同一个文件的另一个 php 块中使用它。 I also tried to make it in function and use it direct in the block I want but the same.我还尝试在 function 中制作它并直接在我想要的块中使用它,但相同。

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:第二个 php 块:

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.问题在于您编写 if...else 语句的方式,我已经为您清理了它,以便您的$mydata变量现在可见并可按预期访问。

$t1 to $t4 are the various test conditions you will like to check your decisions against. $t1 到 $t4 是您想要检查您的决定的各种测试条件。

<?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>';
}
;
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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