tags:

views:

24

answers:

2

I have a page that includes/embeds a file that contains a number of functions.

One of the functions has a variable I want to pass back onto the page that the file is embedded on.

<?php
include('functions.php');

userInGroup();

if($user_in_group) {
 print 'user is in group';
} else {
 print 'user is not in group';
}
?>

function within functions.php

<?php
function userInGroup() {
 foreach($group_access as $i => $group) {
  if($group_session == $group) {
   $user_in_group = TRUE;
   break;
  } else {
   $user_in_group = FALSE; 
  }
 }
}?>

I am unsure as to how I can pass the value from the function userInGroup back to the page it runs the conditional if($user_in_group) on

Any help is appreciated. Update:

I am userInGroup(array("STAFF","STUDENTS","FACULTY"));

which then is

<?php
function userInGroup($group_access) {
    session_start();
    if(isset($_SESSION['user_session'])) {
        $username = $_SESSION['user_session'];
        $group_session = $_SESSION['group_session'];
        $user_full_name = $_SESSION['user_full_name'];

        foreach($group_access as $i => $group) {
          if($group_session == $group) {
                $user_in_group = TRUE;
                break;
            } else {
                $user_in_group = FALSE; 
            }
        } return $user_in_group;
    } else {
      print 'not logged in';
    }
?>
+2  A: 

Easiest way:

$user_in_group = userInGroup();

function userInGroup() {
 foreach($group_access as $i => $group) {
  if($group_session == $group) {
   $user_in_group = TRUE;
   break;
  } else {
   $user_in_group == FALSE; 
  }
 }

 return $user_in_group;
}

Use the return statement.

erenon
that isn't exactly how my code is set up, just used it as an example of what I was trying to accomplish. I am passing values thru the userInGroup(), for example: userInGroup(array("STAFF","STUDENTS","FACULTY"));
Brad
Your function is completly wrong. You should return the status, and don't pollute the namespace. 2; Do you check in every function wheter the user logged in or not? YOu shouldn't.
erenon
i read your update, you still need to have the function return something.
Galen
added return $user_in_group; after the for loop
Brad
andd......what happened?
Galen
nothing - is there a way to accomplish this w/o doing $user_in_group = userInGroup();
Brad
A: 

You can use your original function with one minor modification:

<?php 
function userInGroup() { 
 **global $user_in_group;**
 foreach($group_access as $i => $group) { 
  if($group_session == $group) { 
   $user_in_group = TRUE; 
   break; 
  } else { 
   $user_in_group = FALSE;  
  } 
 } 
}?> 
Zsolti
setting it to global did not resolve it.
Brad