How do I change the value of a variable?

4

The fact is that I want to capture 2 variables per POST and that a group of if elseif compare them. Example

<?php
$var1 = $_POST['var1'];
$var2 = $_POST['var2'];
$var3 = 0;

if ($var1 == 1 && $var2 == 2){
            $var3++;
}
?>

The problem is that I want the value of the variable to change permanently in case the if throws a TRUE boolean, since thus reloading the page the $ var3 returns to 0.

    
asked by David 02.05.2018 в 19:38
source

2 answers

2

What I would recommend is that you send this variable by GET so that the value never changes and always remains as you say permanentemente .

<?php
$var1 = $_POST['var1'];
$var2 = $_POST['var2'];
$var3 = $_GET[valor];

if ($var1 == 1 && $var2 == 2){
            $var3++;
}
?>

your url should go more or less like this: www.localhost.com/?valor=0 . If your page is updated:

$pageRefreshed = isset($_SERVER['HTTP_CACHE_CONTROL']) &&($_SERVER['HTTP_CACHE_CONTROL'] === 'max-age=0' ||  $_SERVER['HTTP_CACHE_CONTROL'] == 'no-cache'); 
if($pageRefreshed == 1){
    header('Location: localhost.com/?valor='+$var3);
}
    
answered by 02.05.2018 в 19:48
1

I understand that what you want is to persist the value of $var3 between requests.

You can save the variable in session, for example:

<?php
session_start(); # inicia la sesión

$var1 = $_POST['var1'];
$var2 = $_POST['var2'];
// set de var3 antes del condicional, 
// bien 0 cuando accede por primera vez
// o el valor de sesión cuando se repite la llamada
$var3 = (isset($_SESSION['var3']))? $_SESSION['var3'] : 0;

if ($var1 == 1 && $var2 == 2){
    // si es positivo suma uno
    $_SESSION['var3'] = $var3 + 1;
}

print_r([$var1, $var2, $var3]);
    
answered by 02.05.2018 в 20:29