Ikram Hasib Ikram Hasib - 1 year ago 71
PHP Question

How to get the value from the condition when it was first time true?

I try to get time count in a site. but it cannot work my code.
Here is my code:

$page_id = filter_input(INPUT_GET, 'page_id', FILTER_SANITIZE_STRING);
$time_start;
if ($page_id === 'question1') {
$time_start = microtime(true);
}

if ($page_id === 'finish') {
$time_end = microtime(true);
echo $time_start . " i m start time";
$duration = $time_end - $time_start;
if ($duration) {
$duration = round($duration, 4);
$sql = "INSERT INTO elapsed_time(time) VALUES('$duration')";
mysqli_query($conn, $sql);
}
}


$time_start shows null. how can i get this?

Answer Source

You have to use a session variable to keep a variable between different runs of the script.

Call session_start(); at the beginning of the script, then use $_SESSION['time_start'] instead of $time_start.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download