How to make a function where every time a variable is called is added plus 1? [closed]

3

For example,

In the middle of my code I do

echo $num;  //me retorna 1, mas se eu ecoar novamente a variável "$num" 
            //me retornará 2   e assim sucessivamente.

I have tried in many ways, but I have not been able to:

function add_um(){  
}

add_um(); //Nesse caso toda vez que eu chamasse a função ela
//me traria um número diferente do anterior, sempre acrescentando 1 unidade.

I tried with while , but fell into an infinite loop 1234567 ...

ADDED:

Can I use $num to name a session? type, $_SESSÃO[$num]; and then name another session hypothetically like this: $_SESSÃO[$num++]; ?

That's what I needed. Naming sessions automatically.

I thought of using for to create a $i that would increment ( i++ ) to each loop to name the sessions, but I'm already working inside a for and then the loops would keep repeating .

    
asked by anonymous 25.08.2014 / 20:41

3 answers

6

To do this in a variable:

$num = 0;
echo $num++ ;
echo $num++ ;
echo $num++ ;
echo $num++ ;

Using pass by reference:

function add_um( &$num ){  
   $num++;
}

$num = 0;
add_um( $num );
echo $num;
add_um( $num );
echo $num;
add_um( $num );
echo $num;

or even

function add_um( &$num ){  
   $num++;
   echo $num;
}

$num = 0;
add_um( $num );
add_um( $num );
add_um( $num );

Using global (I do not recommend):

function add_um(){  
   global $num;
   $num++;
}

$num = 0;
add_um();
  

The global was a generic example, there are a thousand ways to do this.

    
25.08.2014 / 20:51
11

Another possibility would be using magic methods.

example

$counter = new Counter();
echo $counter;
echo $counter;
echo $counter;

echo $counter, $counter, $counter;


output

123

class

class Counter
{
    public static $counter = 0;

    public function __toString()
    {
        static::$counter++;
        return (string) static::$counter;
    }
}

Whenever you give% of% to a variable containing the object, the ++ increaser of method echo will be executed.

PHP

  

The __toString () method allows a class to decide how to behave when it is converted to a string. For example, what echo $ obj; will print. This method needs to return a string, otherwise an E_RECOVERABLE_ERROR level error is generated.

    
25.08.2014 / 21:32
2

There are two simple ways to do this, and with slightly different results, see which one fits your need best:

$num = 1;

echo $num++ . '<br>'; // retorna o valor atual da variável (que é 1) e soma + 1 (fica 2)

echo $num++ . '<br>'; // retorna o valor atual da variável (agora é 2) e soma + 1 (fica 3)

echo $num++ . '<br>'; // retorna o valor atual da variável (agora é 3) e soma + 1 (fica 4)

// e assim sucessivamente...

// retorno:
1
2
3

// ou desta forma:
$num = 1;

echo ($num += 1) . '<br>'; // Soma + 1 e retorna o novo valor da variável (que fica 2)

echo ($num += 1) . '<br>'; // Soma + 1 e retorna o novo valor da variável (que fica 3) 

echo ($num += 1) . '<br>'; // Soma + 1 e retorna o novo valor da variável (que fica 4)

// retorno:
2
3
4

For each request of the page, the counter add 1, just do this:

session_start();

if (!isset($_SESSION['num'])) $_SESSION['num'] = 0;

$num =& $_SESSION['num'];

echo $num++;

Remembering that this is per session, that is, each user will have an independent accountant while his session is alive ...

    
25.08.2014 / 22:27