Link to home
Start Free TrialLog in
Avatar of vrmetro
vrmetroFlag for United States of America

asked on

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

Avatar of ddrudik
ddrudik
Flag of United States of America image

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

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

Avatar of vrmetro

ASKER

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

ASKER CERTIFIED SOLUTION
Avatar of ddrudik
ddrudik
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of vrmetro

ASKER

Beautiful, thank you!