Page 1 of 1

Retruning multiple values

Posted: Fri Sep 20, 2002 7:39 am
by kendall
I have the following code

------CODE------------
function CheckFields($requiredFields)
{
foreach($requiredFields as $FieldName => $Value)
{
if (!$Value)
{
include_once("register.php");
$Error = "Please ENTER YOUR ".$FieldName;
}
}
}
----end---------

How do i get to return multiple values?
how can i get it back into an array

Returning Values

Posted: Fri Sep 20, 2002 8:05 am
by AVATAr
for a function to return a value:

Code: Select all

function hello_world(){
return 'Hello world';
}
If you want to return more than one value, you can return an array with those variable in it. Yoy have to do like this.

Code: Select all

function CheckFields($requiredFields) 
  { 
     include_once("register.php"); 
     $i = 0;
     $error_array = array();
     foreach($requiredFields as $FieldName => $Value) 
       { 
        if (!$Value) 
           { 
           //$Error = "Please ENTER YOUR ".$FieldName; 
           $error_arrayїi] = $FieldName;  
           i++; //increment i
           } 
       } 
      return $error_array;
  }
hope it helps... (i dont try this code, please check it)

REturning multiple varibales from a function

Posted: Fri Sep 20, 2002 8:23 am
by kendall
i dont get it i did dat
-----CODE-----------

$inValidFields = CheckFields($requiredFields);
if ($inValidFields)
{
foreach($inValidFields as $FieldName => $Field)
{
echo $Field;
}
}
function CheckFields($requiredFields)
{
foreach($requiredFields as $FieldName => $Value)
{
if (!$Value)
{
return array($FieldName);
}
}
}

---------END------------

But i get only one value being passed?

do you mean array($FieldName, $FieldName); for every field name passed?

Wrong

Posted: Fri Sep 20, 2002 8:33 am
by AVATAr
Frist of all you are not using an array. Check the manual and read again my code.

First you have to initialize a variable like an array, then you have to put the values in the array (using some kind of index: i). t

Then you return the variable.

returning multiple variables

Posted: Fri Sep 20, 2002 9:34 am
by kendall
AAAAAAAAAARRRRRRRRRRRGH!

this is driving me nuts

ok so i did what you told me

i now have the following code that follows the previous one
----CODE-----------
$inValidFields = CheckFields($requiredFields);
if ($inValidFields)
{
include("register.php");
}
-----END--------

in the included file i have

---CODE------
function generateError($inValidFields)
{
$FieldName = in_array($Field,$inValidFields);
if($FieldName == "TRUE" )
{
$Error = "PLEASE ENTER YOUR ".$Field;
}
return $Error;
}
---------------end----------

which is called 5 times

like

---------CODE----------------
@ $Error = generateError("First_Name",$inValidFields);
echo $Error;
----------end----------
why is it that Error is called 3 times istead of all 5 times ???

:cry:

Posted: Wed Sep 25, 2002 8:51 am
by Bjai
check your:

function CheckFields($requiredFields)

firstly, $requiredFields should be an array of 'Fields'

Code: Select all

e.g. 
$array_fields = array ('1','2,'3');
then, you run CheckFields($array_fields);
however, you need to edit your CheckFields function so that it loops thru all the elements in the array

Code: Select all

e.g.
for ($i=0; $i< count($array_fields); $i++&#123;
 ... // Do your Check Fields routine here;
&#125;