Trying to setup a function

Can you tell me what I'm doing wrong here?
//Set Function
function check_field($field) {
		if (isset($_SESSION[''.$field.''])) {
		$field = $_SESSION[''.$field.''];
		} else {
		$field='';
		}
 
//Call function	
check_field(M_buss_name);

Open in new window

vrmetroAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ddrudikCommented:
Do you want to return something from the function?  Nothing is returned as of yet.  Functions have non-shared variable scopes by default so you would need to use the global function to use a var outside of a function that you set to a value within the function.  Also, do you want to create a variable variable name such as ${$field} or did you just want a var named field?
//Set Function
$field='';
function check_field($field) {
  global $field;
		if (isset($_SESSION[$field])) {
		$field = $_SESSION[$field];
		} else {
		$field='';
		}
 
//Call function	
check_field('M_buss_name');

Open in new window

0
ddrudikCommented:
Still had some syntax issues, should read:
<?php
$field='';
$_SESSION['M_buss_name']="some value";
function check_field($fld) {
  global $field;
  $field = isset($_SESSION[$fld]) ? $_SESSION[$fld] : '' ;
}
check_field('M_buss_name');
echo $field;
?>

Open in new window

0
vrmetroAuthor Commented:
I wanted a function that would do what I'm doing below, later in the page I print the variable either blank or with whatever is in it.

Per the code below, it doesn't simplify me writing below over and over again because I'll need to define:
$_SESSION['M_buss_name']="some value";

Will I need that there?

if ( isset($_SESSION['MC_vatnr'])) {
	$MC_vatnr=$_SESSION['MC_vatnr'];
	} else {
	$MC_vatnr='';
	}

Open in new window

0
ddrudikCommented:
If I understand your goal:
<?php
$_SESSION['M_buss_name']="some set value";
function check_field($fld) {
  global ${$fld};
  ${$fld} = isset($_SESSION[$fld]) ? $_SESSION[$fld] : '' ;
}
check_field('M_buss_name');
echo $M_buss_name;
echo "<hr>";
check_field('M_var_not_set');
echo $M_var_not_set;
?>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
vrmetroAuthor Commented:
Beautiful, thank you!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.