variables somehow changing across functions...

Hi. I have some weird problem which i have not been able to understand for some hours now. I consider myself fairly knowledgeable in php, but this problem must be caused by information i missed somewhere.

I have an array with object values assigned to each of its elements:

#######################
# fields info
#######################

	//field
	$field_name = "name";
	$form_fields_info[$field_name] -> title = "naam";
	$form_fields_info[$field_name] -> input_type = "text";
	$form_fields_info[$field_name] -> required = true;
	$form_fields_info[$field_name] -> fieldset = "persoonsgegevens";
	$form_fields_info[$field_name] -> maxlength = 200;
	
	//field
	$field_name = "address";
	$form_fields_info[$field_name] -> title = "adres";
	$form_fields_info[$field_name] -> input_type = "text";
	$form_fields_info[$field_name] -> required = true;
	$form_fields_info[$field_name] -> fieldset = "persoonsgegevens";
	$form_fields_info[$field_name] -> maxlength = 200;

this array is initiated inside a function. after initiating the array i perform a check on submitted form values through a different function:

if($_POST['submitted'] == "yes")
{
	$submitted_form_fields_info = $form_fields_info;
	
	list($submitted_form_fields_info, $warnings_string) = verify_submitted_info($submitted_form_fields_info, $form_hidden_fields_info, $prefix);
	
	if(!empty($submitted_form_fields_info))
	{
		$form_fields_info = $submitted_form_fields_info;
	}
}

The problem is that when the verify_submitted_info function has been performed, somehow, even when the returning array $submitted_form_fields_info is empty, $form_fields_info will get the same (new) values which were added to $submitted_form_fields_info in the verify_submitted_info function. I always thought variables are only valid within the function they are used in (except for global variables of course). Yet adding values to elements of a DIFFERENT array in a another function is having its effect on a “lookalike” array in the calling function… Can anyone please help me understand what is causing this behaviour?

beneath you will find teh complete code. Ty in advance.

<?php

if (strpos($_SERVER["SCRIPT_NAME"],basename(__FILE__)) !== false)
{
	header("location: " . $_SERVER['host_name'] . "/index.php");
	exit;
}

function get_concerts_info()
{
	global $db;
	
	$sql =
	"
		SELECT *
		FROM `events`
		WHERE	`name` = 'concert'
		ORDER BY `date` ASC, `time` ASC, `city` ASC;
	";
	
	$concerts_info = $db -> getAssoc($sql);
	
	return $concerts_info;
}

function book($_max_width)
{
	$concerts_info = get_concerts_info();
	
	$prefix = "bk_";
	
	$_measures[1] = $_max_width; //volle breedte hoofddiv
	$_measures[2] = 25; //required / inrequired div
	$_measures[3] = 20; //line height
	$_measures[4] = 165; //title_div width
	$_measures[8] = 20; //input spacing
	$_measures[7] = $_measures[1] - 20; //row div width
	$_measures[5] = $_measures[7] - $_measures[2] - $_measures[4] - 2; //input_div width (-2 for ie)
	$_measures[6] = $_measures[5] - $_measures[8]; //input_div width
	
	#######################
	# fields info
	#######################
	
	//field
	$field_name = "name";
	$form_fields_info[$field_name] -> title = "naam";
	$form_fields_info[$field_name] -> input_type = "text";
	$form_fields_info[$field_name] -> required = true;
	$form_fields_info[$field_name] -> fieldset = "persoonsgegevens";
	$form_fields_info[$field_name] -> maxlength = 200;
	
	//field
	$field_name = "address";
	$form_fields_info[$field_name] -> title = "adres";
	$form_fields_info[$field_name] -> input_type = "text";
	$form_fields_info[$field_name] -> required = true;
	$form_fields_info[$field_name] -> fieldset = "persoonsgegevens";
	$form_fields_info[$field_name] -> maxlength = 200;
	
	//field
	$field_name = "postalcode_city";
	$form_fields_info[$field_name] -> title = "postcode & woonplaats";
	$form_fields_info[$field_name] -> input_type = "text";
	$form_fields_info[$field_name] -> required = true;
	$form_fields_info[$field_name] -> fieldset = "persoonsgegevens";
	$form_fields_info[$field_name] -> maxlength = 200;
	
	//field
	$field_name = "country";
	$form_fields_info[$field_name] -> title = "land";
	$form_fields_info[$field_name] -> input_type = "text";
	$form_fields_info[$field_name] -> required = true;
	$form_fields_info[$field_name] -> fieldset = "persoonsgegevens";
	$form_fields_info[$field_name] -> maxlength = 200;
	$form_fields_info[$field_name] -> value = "Nederland";
	
	//field
	$field_name = "email";
	$form_fields_info[$field_name] -> title = "email";
	$form_fields_info[$field_name] -> input_type = "text";
	$form_fields_info[$field_name] -> required = true;
	$form_fields_info[$field_name] -> fieldset = "contactgegevens";
	$form_fields_info[$field_name] -> maxlength = 200;
	
	//field
	$field_name = "phone_nr";
	$form_fields_info[$field_name] -> title = "telefoonnummer";
	$form_fields_info[$field_name] -> input_type = "text";
	$form_fields_info[$field_name] -> required = true;
	$form_fields_info[$field_name] -> fieldset = "contactgegevens";
	$form_fields_info[$field_name] -> maxlength = 200;
	
	
	//field
	$field_name = "nr_bookings";
	$form_fields_info[$field_name] -> title = "aantal reserveringen";
	$form_fields_info[$field_name] -> input_type = "text";
	$form_fields_info[$field_name] -> required = true;
	$form_fields_info[$field_name] -> fieldset = "bestelgegevens";
	$form_fields_info[$field_name] -> maxlength = 3;
	$form_fields_info[$field_name] -> value = 1;
	
	//field
	$field_name = "discount";
	$form_fields_info[$field_name] -> title = "korting";
	$form_fields_info[$field_name] -> discribtion = "korting geldt alleen voor studenten & genodigden";
	$form_fields_info[$field_name] -> input_type = "radio";
	$form_fields_info[$field_name] -> required = true;
	$form_fields_info[$field_name] -> fieldset = "bestelgegevens";
	$form_fields_info[$field_name] -> value[1] = "ja";
	$form_fields_info[$field_name] -> value[2] = "nee";
	$form_fields_info[$field_name] -> checked = 2;
	
	//field
	$field_name = "event";
	$form_fields_info[$field_name] -> title = "concert";
	$form_fields_info[$field_name] -> input_type = "checkbox";
	$form_fields_info[$field_name] -> required = true;
	$form_fields_info[$field_name] -> fieldset = "bestelgegevens";
	
	foreach($concerts_info as $concertID => $concert_info)
	{
		if(!empty($concert_info -> day_name))
		{
			$concert_title = $concert_info -> day_name;
		}
		
		if(!empty($concert_info -> date))
		{
			$concert_date = sqlDate2screenDate_2($concert_info -> date);
			
			if(!empty($concert_title))
			{
				$concert_title .= " ";
			}
			
			$concert_title .= $concert_date;
		}
		
		if(!empty($concert_info -> city))
		{
			if(!empty($concert_title))
			{
				$concert_title .= ", ";
			}
			
			$concert_title .= $concert_info -> city;
		}
		
		if(!empty($concert_info -> facility_name))
		{
			if(!empty($concert_title))
			{
				$concert_title .= ", ";
			}
			
			if(!empty($concert_info -> facility_link))
			{
				$concert_title .= "<a href="{$concert_info -> facility_link}" target="_blank">" . $concert_info -> facility_name . "</a>";
			}
			else
			{
				$concert_title .= $concert_info -> facility_name;
			}
		}
		
		if(!empty($concert_info -> time))
		{
			$concert_time = substr($concert_info -> time, 0, 5);
			
			if(!empty($concert_title))
			{
				if(empty($concert_info -> facility_name))
				{
					$concert_title .= ", ";
				}
				else
				{
					$concert_title .= " ";
				}
			}
			
			$concert_title .= $concert_time;
		}
		
		if(!empty($concert_title))
		{
			$form_fields_info[$field_name] -> value[$concertID] = $concert_title;
		}
	}
	$form_fields_info[$field_name] -> checked = array();
	
	//field
	$field_name = "submit";
	$form_fields_info[$field_name] -> value = "verzend";
	$form_fields_info[$field_name] -> input_type = "submit";
	$form_fields_info[$field_name] -> required = false;
	$form_fields_info[$field_name] -> fieldset = "plaats reservering";
	
	//hidden fields
	
	#######################
	
	if($_POST['submitted'] == "yes")
	{
		$submitted_form_fields_info = $form_fields_info;
		
		list($submitted_form_fields_info, $warnings_string) = verify_submitted_info($submitted_form_fields_info, $form_hidden_fields_info, $prefix);
		
		if(!empty($submitted_form_fields_info))
		{
			$form_fields_info = $submitted_form_fields_info;
		}
	}
	
	$fields = create_fields($form_fields_info, $form_hidden_fields_info, $prefix, $_measures);
	
	$parent_div = <<<HERE
	<div class="floatLeft" style="width: {$_measures[1]}px; clear: both; margin-top: 5px;">
		<div class="floatLeft warning" style="width: {$_measures[1]}px; clear: both;">
				$warnings_string
		</div>
		<div class="floatLeft" style="width: {$_measures[1]}px; clear: both;">
			<form name="book_form" method="POST">
				$fields
			</form>	
		</div>
	</div>
HERE;
	
	return $parent_div;
}

function verify_submitted_info($submitted_form_fields_info, $form_hidden_fields_info, $prefix)
{
	$noSucces = false;
	
	foreach($submitted_form_fields_info as $field_name => $field_attributes)
	{
		unset($checked_values);
		
		switch($field_attributes -> input_type)
		{
			case 'text':
				if(empty($_POST[$prefix . $field_name]))
				{
					if($field_attributes -> required)
					{
						$noSucces = true;
						$warnings_string .= "het volgende veld is niet ingevuld: " . $field_attributes -> title . "<br />";
						$submitted_form_fields_info[$field_name] -> classes = "warning";
					}
					
					$submitted_form_fields_info[$field_name] -> value = "";
				}
				else
				{
					$submitted_form_fields_info[$field_name] -> value = $_POST[$prefix . $field_name];
				}
			break;
			
			case 'radio':
				if(empty($_POST[$prefix . $field_name]))
				{
					if($field_attributes -> required)
					{
						$noSucces = true;
						$warnings_string .= "het volgende veld is niet ingevuld: " . $field_attributes -> title . "<br />";
						$submitted_form_fields_info[$field_name] -> classes = "warning";
					}
					
					$submitted_form_fields_info[$field_name] -> checked = "";
				}
				else
				{
					$submitted_form_fields_info[$field_name] -> checked = $_POST[$prefix . $field_name];
				}
			break;
			
			case 'checkbox':
				foreach($field_attributes -> value as $value => $option_title)
				{
					if($_POST[$prefix . $field_name . "_" . $value] == "on")
					{
						 $checked_values[] = $value;
					}
				}
				
				if(empty($checked_values))
				{
					if($field_attributes -> required)
					{
						$noSucces = true;
						$warnings_string .= "het volgende veld is niet ingevuld: " . $field_attributes -> title . "<br />";
						$submitted_form_fields_info[$field_name] -> classes = "warning";
					}
					
					$submitted_form_fields_info[$field_name] -> checked = array();
				}
				else
				{
					$submitted_form_fields_info[$field_name] -> checked = $checked_values;
				}
			break;
		}
	}
	
	if($noSucces)
	{
		//return array($submitted_form_fields_info, $warnings_string);
	}
	else
	{
		return NULL;
	}
	
}

function create_fields($form_fields_info, $form_hidden_fields_info, $prefix, $_measures)
{
	
	$required_div = <<<HERE
	<div class="floatLeft required" style="width: {$_measures[2]}px; height: {$_measures[3]}px; padding: 0px;" title="verplicht"><!-- --></div>
HERE;
	
	$inrequired_div = <<<HERE
	<div class="floatLeft" style="width: {$_measures[2]}px; height: {$_measures[3]}px; padding: 0px;"><!-- --></div>
HERE;
	
	foreach($form_fields_info as $field_name => $field_attributes)
	{
		$fieldset_string = "";
		
		if(empty($last_fieldset))
		{
			$last_fieldset = $field_attributes -> fieldset;
			$fieldset_string = "<fieldset><legend>{$field_attributes -> fieldset}</legend>";
		}
		else
		{
			if(!($last_fieldset == $field_attributes -> fieldset))
			{
				$fieldset_string = "</fieldset><fieldset><legend>{$field_attributes -> fieldset}</legend>";
			}
			
			$last_fieldset = $field_attributes -> fieldset;
		}
		
		if($field_attributes -> required)
		{
			$requirency = $required_div;
		}
		else
		{
			$requirency = $inrequired_div;
		}
		
		unset($input_field);
		unset($ckecked_options);
		
		switch($field_attributes -> input_type)
		{
			case 'text':
				$input_field = <<<HERE
				<input id="input_id_{$field_name}" type="text" name="{$prefix}{$field_name}" value="{$field_attributes -> value}" maxlength="{$field_attributes -> maxlength}" class="floatRight" style="width: {$_measures[6]}px;">
HERE;
			break;
			
			case 'radio':
				
				$ckecked_options[$field_attributes -> checked] = "CHECKED";
				
				foreach($field_attributes -> value as $value => $option_title)
				{
					$input_field .= <<<HERE
					<label><input type="radio" name="{$prefix}{$field_name}" value="$value" $ckecked_options[$value]>
					$option_title</label><br />
HERE;
				}
				
				$input_field = <<<HERE
				<div class="floatLeft" style="margin-left: {$_measures[8]}px;">$input_field </div>
HERE;
			break;
			
			case 'checkbox':
				foreach($field_attributes -> checked as $checked_value)
				{
					$ckecked_options[$checked_value] = "CHECKED";
				}
				
				foreach($field_attributes -> value as $value => $option_title)
				{
					$input_field .= <<<HERE
					<label><input type="checkbox" name="{$prefix}{$field_name}_{$value}" $ckecked_options[$value]>
					$option_title</label><br />
HERE;
				}
				
				$input_field = <<<HERE
				<div class="floatLeft" style="margin-left: {$_measures[8]}px;">$input_field </div>
HERE;
			break;
			
			case 'submit':
				$input_field = <<<HERE
				<input class="floatRight" type="submit" value="{$field_attributes -> value}">
HERE;
			break;
		}
		
		$fields .= <<<HERE
		$fieldset_string
		<div class="floatLeft" style="width: {$_measures[7]}px; clear: both;">
			$requirency
			<div class="floatLeft {$form_fields_info[$field_name] -> classes}" style="width: {$_measures[4]}px; line-height: {$_measures[3]}px;">
				<label for="input_id_{$field_name}">
					{$field_attributes -> title}<br /><span class="attention_form">{$field_attributes -> discribtion}</span>
				</label>
			</div>
			<div class="floatLeft" style="width: {$_measures[5]}px; line-height: {$_measures[3]}px;">
				$input_field
			</div>
		</div>
HERE;
	}
	
	if(!empty($form_hidden_fields_info))
	{
		foreach($form_hidden_fields_info as $field_name => $value)
		{
			$fields .= <<<HERE
			<input type="hidden" name="$field_name" value="$value">
HERE;
		}
	}
	
	$fields .= <<<HERE
			<input type="hidden" name="submitted" value="yes">
HERE;
	
	$fields .= "</fieldset>";
	
	return $fields;
}

?>

Basically what we’re looking at is a functions page to be included in the actual website pages, correct?
I can’t really find any issues with this code, other than what appears to me as strange:

$form_fields_info[$field_name] -> title = "naam";

It looks like you’re actually calling upon a member variable of an object (in this case, an object in an array of objects). Why are you using this syntax, and are you sure it works the way you think it does (not that I doubt your expertise, it’s just that I’ve never seen such a construction before)?

Also, you seem to use this type of construction a lot, and to me it looks like you’re overwriting the value of the variable quite a few times throughout the function. Use echo statements to verify that the variables contain what you think they contain (also see the link in my signature for debugging tips). Echo statements will make your debugging life a LOT easier ;)

Sponsor our Newsletter | Privacy Policy | Terms of Service