Jump to content

variable scope / session question


r0k3t

Recommended Posts

Hi there,

OK - I understand about scope for the most part, function foo can't access function bar's variables. But I thought I could stick a variable in the $_SESSION array and access it anywhere I tried but that doesn't seem to work. If I do a var_dump($_SESSION) in the function I see all the variables I have set. If I do it in after the function executes it is empty. These variables don't get set on the next page either.

Can someone point me in the right direction on this? In other programming languages If I set a global variable from inside a function then I have no problem.

Here is the function in question
function createSicList($studyNumberObj){

        $sicObjArrayCounter = $_SESSION['sicObjArrayCounter'];
        $masterSicArray = $_SESSION['masterSicArray'];

        $studyNumber = $studyNumberObj->getStudyNumber();
        if($studyNumberObj->getFirst_Previous_study()){
                $stdNum = $studyNumberObj->getFirst_Previous_study();
                //Set the value of $match if we find the word new...
                preg_match('/NEW/', $stdNum, $match);
                //if $match has a value skip over it
                if(!$match){
                        $First_Previous_Number = $studyNumberObj->getFirst_Previous_study();
                        $firstPreviousSICNumArray = getPreviousStudySIC($First_Previous_Number);
                        $masterSicArray[$sicObjArrayCounter] = $firstPreviousSICNumArray;
                        $sicObjArrayCounter++;
                }
        }
        $_SESSION['sicObjArrayCounter'] = $sicObjArrayCounter;
        $_SESSION['masterSicArray'] = $masterSicArray;
        echo $_SESSION['sicObjArrayCounter'];
        var_dump($_SESSION); //I can see everything from this var_dump
}
var_dump($_SESSION); //$_SESSION['sicObjArrayCounter'] and $_SESSION['masterSicArray'] have no value.
?>


Thanks... Ken
Link to comment
https://forums.phpfreaks.com/topic/32086-variable-scope-session-question/
Share on other sites

If you don't execute the function, the values aren't stored.

Try this simple test:
[code]<?php
session_start();
function test()
{
        $_SESSION['one'] = 'two';
        $_SESSION['two'] = 'three';
}
echo 'before<br>';
echo '<pre>' . print_r($_SESSION,true) . '</pre>';
test();
echo 'after<br>';
echo '<pre>' . print_r($_SESSION,true) . '</pre>';
?>[/code]

Ken

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.