drq61040 2018-01-14 16:02
浏览 58
已采纳

PHP:创建一个“STATIC GLOBAL”变量,该变量在调用时保留其值

I want to code a web page that will work like this:

  • Input a number from the user,
  • Use the number to display some information,
  • Provide the user with the option to use that number and information for some purpose,
  • If so asked, use the number and information provided as directed.

I am having trouble with the last step - after the user selects the option to use the number, the number is gone - it is no longer set. I tried making the number static, which is how I would do this in languages other than PHP. I also tried something called the $GLOBALS array, but that did not help.

Below is source code illustrating the problem.

There must be a standard or prototypical way of dealing with this interaction in PHP, but I am not savvey to it. Any advice or correction would be appreciated!

<h2>Give Me a Number</h2>

<form method="post">
    <label for="MyNumber">Number Please</label>
    <input type="text" id="MyNumber" name="MyNumber">
    <input type="submit" name="view" value="View Number">
</form>

<?php

static $MyNumber;

if (isset($_POST['view'])) 
{

    try 
    {

        // Get the Number from the post variable for binding
        $MyNumber = $_POST['MyNumber'];

        echo "Your Number is ", $MyNumber, " on Line 24";
        echo "<br>", "Your GLOBAL Number is ", $GLOBALS['MyNumber'], " on Line 25";

    }

    catch(PDOException $error) 
    {
        echo $sql . "<br>" . $error->getMessage();
    }
}

if (isset($_POST['view'])) 
{
    if ($MyNumber > 0 ) 
    { ?>
        <h2>Again, Your Number is <?php echo $MyNumber, " on Line 40"; ?> </h2> 
        <h2>Again, Your Number is <?php echo $GLOBALS['MyNumber'], " on Line 41"; ?> </h2> 


        <table>
            <thead>
                <tr>
                    <th>My Number</th>
                    <th>Global Number</th>
                </tr>
            </thead>
            <tbody>
    <?php 
        for ($i = 1; $i <= $MyNumber; $i++)
        { ?>
            <tr>
                <td><?php echo $MyNumber; ?></td>
                <td><?php echo $GLOBALS['MyNumber']; ?></td>
            </tr>
        <?php 
        } ?>
        </tbody>
    </table>

    <?php 
    } 
    else 
    { ?>
        <blockquote>No NUMBER !!!! on Line 68</blockquote>
    <?php
    } ?> 

    <form method="post">
        <input type="submit" name="use" value="Use My Number">
    </form>

<?php }?> 

<?php
//////////////// USE the NUMBER //////////////////
if (isset($_POST['use'])) 
{
    if (isset($MyNumber) )
    {
        echo "I can USE (", $MyNumber, ") My Number !!!"; 
        echo "<br>", "and(", $GLOBALS['MyNumber'], ") My GLOBAL Number!"; 
    }
    else
    {

        echo "Hey, what happened to (", $MyNumber, ")? I wanted to use it!"; 
        echo "<br>", "Hey, GLOBAL (", $GLOBALS['MyNumber'], ") is gone also!"; 

    }
}

?>
  • 写回答

1条回答 默认 最新

  • duande9301 2018-01-14 20:54
    关注

    You should not use global variables in PHP. That's considered bad practice.

    What you should do is create a hidden input in your form with 'Use My Number'. This way you can pass your number to the next post.

    <form method="post">
        <input type="hidden" name="MyNumber" value="$_POST['MyNumber']">
        <input type="submit" name="use" value="Use My Number">
    </form>
    

    Another way to store data over multiple requests is using sessions.

    At the top of your pass set session_start(); to activate sessions. Then you can store your number into a $_SESSION['MyNumber']. This variable is then persisted over multiple page requests. More info can be found here.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料