Asked  7 Months ago    Answers:  5   Viewed   42 times

I have an array that I want on multiple pages, so I made it a SESSION array. I want to add a series of names and then on another page, I want to be able to use a foreach loop to echo out all the names in that array.

This is the session:


I want to add a series of names to that array using array_push like this:


I am getting this error:

array_push() [function.array-push]: First argument should be an array

Can I use array_push to put multiple values into that array? Or perhaps there is a better, more efficient way of doing what I am trying to achieve?



Yes, you can. But First argument should be an array.

So, you must do it this way

$_SESSION['names'] = array();

Personally I never use array_push as I see no sense in this function. And I just use

$_SESSION['names'][] = $name;
Wednesday, March 31, 2021
answered 7 Months ago

the php-memcached extension supports session locking

the memcache and memcached extensions look syntactically similar so it may not be too much of a headache to give it a try. (memcached has a stable version 2.1.0 released 2012-08-07).

if you are set on using memcache 2.2.7 you will most likely have to implement the lock yourself by setting some "session_is_locked" variable in your session and then releasing/unsetting it when the script is done writing to the session. Then you'd always need to check if that variable is set before continuing with any scripts which write to the session.

Wednesday, March 31, 2021
answered 7 Months ago

2021 UPDATE: As of PHP 8, the two characters are not the same. The sequence #[ is used for Attributes.(Thanks to i336 for the comment)

Original Answer:

The answer to the question Is there any difference between using "#" and "//" for single-line comments in PHP? is no.

There is no difference. By looking at the parsing part of PHP source code, both "#" and "//" are handled by the same code and therefore have the exact same behavior.

Sunday, August 8, 2021
answered 3 Months ago

You check if it's there, using in_array, before pushing.

foreach($something as $value){
    if(!in_array($value, $liste, true)){
        array_push($liste, $value);

The ,true enables "strict checking". This compares elements using === instead of ==.

Monday, August 9, 2021
answered 2 Months ago

Both your valid and invalid declarations are internally equivalent, i.e., the compiler converts the latter to the former.

What your function sees is the pointer to the first element of the array.

PS. The alternative would be to push the whole array on the stack, which would be grossly inefficient from both time and space viewpoints.

Tuesday, August 31, 2021
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :