ZeroSuf3r
ZeroSuf3r

Reputation: 2001

_SESSION increment function

My function:

function SessionSet($s, $v = '') {
    if (is_array($s)) {
        foreach ($s as $k => $v) $_SESSION[$k] = $v;
    } else {
        $_SESSION[$s] = $v;
    }
}

It's possible make it work like this:

SessionSet('user_visits')++;

Just a simple increment, but don't have any ideas how...

Upvotes: 0

Views: 76

Answers (4)

ComFreek
ComFreek

Reputation: 29434

It isn't possible to directly change the return value of a function.

It will throw a fatal error:

Fatal error: Can't use function return value in write context

But you could simply access $_SESSION['user_visits'] after the function call.

Upvotes: 0

Stefano Mtangoo
Stefano Mtangoo

Reputation: 6534

Why not something like internally in your function? Then when you call function it will do the increment internally. Or have I missed your real question?

$_SESSION[$s] = $v; //somewhere in your code you init the variable
//and in your function just do
$_SESSION[$s] = $_SESSION[$s]++;

//Assuming variable in seeion is incrementable!

Upvotes: 1

lanzz
lanzz

Reputation: 43188

No, it's not possible. You can return a reference from a function, but still PHP doesn't allow mutator operations on function return values.

Upvotes: 1

Marc B
Marc B

Reputation: 360782

How about

$_SESSION['user_visits']++;

instead?

You cannot increment the return value of a function - there's nowhere for the incremented value to be stored.

Upvotes: 5

Related Questions