Jump to content

[SOLVED] loop within a function problems


thefortrees

Recommended Posts

The foreach loop is throwing the error "invalid argument supplied for foreach()" even though I defined the array right before it. Any ideas?

 

$required = array ('email', 'password1', 'password2', 'fname', 'lname', 'phone', 'address1', 'city', 'state', 'zip', 'nameoncert');

function checkData(){
	foreach($required as $index){		
		$value = $_POST[$index];
		if (empty($value) ){
			$msg = 'Please fill out all required fields.';				
			return $msg;
		}
	}

	if ($_POST['password1'] != $_POST['password2']){
		$msg = 'Password does not match.';
		return $msg;
	}

	if ( !checkEmailAddress($_POST['email']) ){
		$msg = 'Invalid e-mail address.';
		return $msg;
	}	
}

Link to comment
https://forums.phpfreaks.com/topic/54458-solved-loop-within-a-function-problems/
Share on other sites

<?php

function checkData($required){
	foreach($required as $index){		
		$value = $_POST[$index];
		if (empty($value) ){
			$msg = 'Please fill out all required fields.';				
			return $msg;
		}
	}

	if ($_POST['password1'] != $_POST['password2']){
		$msg = 'Password does not match.';
		return $msg;
	}

	if ( !checkEmailAddress($_POST['email']) ){
		$msg = 'Invalid e-mail address.';
		return $msg;
	}	
}

$required = array ('email', 'password1', 'password2', 'fname', 'lname', 'phone', 'address1', 'city', 'state', 'zip', 'nameoncert');
$result = checkdata($required);

?>

 

Alternatively you can do this:

 

<?php

$required = array ('email', 'password1', 'password2', 'fname', 'lname', 'phone', 'address1', 'city', 'state', 'zip', 'nameoncert');

function checkData(){

	global $required;

	foreach($required as $index){		
		$value = $_POST[$index];
		if (empty($value) ){
			$msg = 'Please fill out all required fields.';				
			return $msg;
		}
	}

	if ($_POST['password1'] != $_POST['password2']){
		$msg = 'Password does not match.';
		return $msg;
	}

	if ( !checkEmailAddress($_POST['email']) ){
		$msg = 'Invalid e-mail address.';
		return $msg;
	}	
}

?>

You have scope problems.

 

Variables defined outside a function can not be seen inside a function.

 

You can do 1 of three things here:

  • Define the array inside the function

<?php
function checkData(){
               $required = array ('email', 'password1', 'password2', 'fname', 'lname', 'phone', 'address1', 'city', 'state', 'zip', 'nameoncert');
?>

[*] Define the array as global inside the function:

<?php
function checkData(){
               global $required;
?>

[*]Pass the array as an argument to the function:

<?php
$required = array ('email', 'password1', 'password2', 'fname', 'lname', 'phone', 'address1', 'city', 'state', 'zip', 'nameoncert');

function checkData($required){
?>


 

Ken

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.