Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 213
  • Last Modified:

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

0
vrmetro
Asked:
vrmetro
  • 3
  • 2
1 Solution
 
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
 
vrmetroAuthor Commented:
Beautiful, thank you!
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now