1

I'm currently working on a website for my project, but for some reason when everything is as it has to be ( session is started, session variable is defined ), then only array_push() function doesn't add the value to session variable, when I move array_push() out of if, foreach and if (if contains foreach and foreach contains another if), it works fine, I already tried to put it to first if, then to foreach but it didn't help. So it works only when it is out of that code (When it is not in that if, which contains foreach, which contains another if). Please try to understand, my English is not well.

What do I have to do to fix it?

if(isset($_GET["setLang"])) {
    foreach ($languages as $item) {
        if($item === $_GET["setLang"]) {
            $_SESSION["selLang"] = $item;
            array_push($_SESSION["sessionMessage"], "Language changed successfully!");
            header("location: index.php?path=/");
            exit();
        }
    }
}
2
  • Is $_SESSION["sessionMessage"] really an array? Commented May 12, 2019 at 16:14
  • Yes, it is for sure - session_start(); $_SESSION["sessionMessage"] = array(); / This is actually at start of the file (<?php is there too :D) Commented May 12, 2019 at 16:16

1 Answer 1

2

Since the code work correctly out of the if statement , it's cleary evident that the condition cause the problem

$item === $_GET["setLang"]

Using triple = will check value and type . So may be the type of each ones is different than other .

Replace it with :

$item == $_GET["setLang"] // only use two =
Sign up to request clarification or add additional context in comments.

2 Comments

Code works, but string is not pushed even while using that code.
Well I tried also $_SESSION["sessionMessage"][] ... but it didnt work too

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.