循环PHP的动态$ _SESSION变量会话名称

时间:2017-09-15 21:55:18

标签: php session for-loop

我正在使用简单的for循环创建动态if($dateweekday ==='Tuesday'){ for ($v=1;$v<=4;$v++){ $ttt = 'tuesday_'.$v; } echo $_SESSION[$ttt] = $newdateformat ; echo "<br>"; } 变量来打印日期。

2017-9-19 
2017-9-26 
2017-10-03 
2017-10-10 

结果

$_SESSION[$ttt]

看起来不错。但后来我关心的是$_SESSION['tuesday_1'] = 2017-9-19; $_SESSION['tuesday_2'] = 2017-9-26; $_SESSION['tuesday_3'] = 2017-10-03; $_SESSION['tuesday_4'] = 2017-10-10; 的价值。结果应该是

var_dump($_SESSION[$ttt])

当我string(10) string(9) string(9) string(9) 这就是我得到的

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

public class Main {

    public static void main(String[] args) throws Exception {

        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        PreparedStatement pstmt = null;

        try {
            // SETUP
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://...");

            stmt = conn.createStatement();
            stmt.execute("DROP TABLE IF EXISTS ttt");

            stmt.execute("CREATE TABLE ttt (value FLOAT)");

            // INPUT VALUE
            float input = 1.223473E6f;
            print(" Input", input);
            pstmt = conn.prepareStatement("INSERT INTO ttt VALUES (?)");
            pstmt.setFloat(1, input);
            pstmt.execute();

            // OUTPUT VALUES
            rs = stmt.executeQuery("SELECT value, "
                    + "AVG(value) as avg, MIN(value) as min, "
                    + "MAX(value) as max, value + 0 as plus, "
                    + "value * 1 as mult, "
                    + "cast(value as decimal) as valcast "
                    + "FROM ttt;");
            rs.next();
            System.out.println();
            print("Output", rs.getFloat("value"));
            print("   AVG", rs.getFloat("avg"));
            print("   MIN", rs.getFloat("min"));
            print("   MAX", rs.getFloat("max"));
            print("    +0", rs.getFloat("plus"));
            print("    *1", rs.getFloat("mult"));
            print("  CAST", rs.getFloat("valcast"));

        } finally {
            if (stmt != null)
                stmt.close();
            if (pstmt != null)
                pstmt.close();
            if (conn != null)
                conn.close();
        }

    }

    private static void print(String name, float value) {
        System.out.println(name + ": " + value + " (0x" 
            + Integer.toHexString(Float.floatToRawIntBits(value)) + ")");
    }

我该如何做到这一点?

1 个答案:

答案 0 :(得分:0)

我注意到两件事:

<强> 1 假设我们将$t设置为'value_before'

$t = 'value_before';

现在:

echo $t = 'value_after';

以上代码等于:

$t = 'value_after';
echo $t;

所以结果是回显'value_after',你永远不会回显旧值('value_before')。

2。我注意到的另一件事是你在一个循环中多次准备变量,然后你只回显一次,代码的这一部分:

for ($v=1;$v<=4;$v++){
    $ttt = 'tuesday_'.$v;
}

echo $_SESSION[$ttt] = $newdateformat;

因此,您回复的唯一内容是'$_SESSION['tuesday_4'] = $newdateformat',并且tuesday_1tuesday_2tuesday_3的ano不会被回显/修改。