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
Retruning multiple values
Moderator: General Moderators
- AVATAr
- Forum Regular
- Posts: 524
- Joined: Tue Jul 16, 2002 4:19 pm
- Location: Uruguay -- Montevideo
- Contact:
Returning Values
for a function to return a value:
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.
hope it helps... (i dont try this code, please check it)
Code: Select all
function hello_world(){
return 'Hello world';
}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;
}- kendall
- Forum Regular
- Posts: 852
- Joined: Tue Jul 30, 2002 10:21 am
- Location: Trinidad, West Indies
- Contact:
REturning multiple varibales from a function
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?
-----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?
- AVATAr
- Forum Regular
- Posts: 524
- Joined: Tue Jul 16, 2002 4:19 pm
- Location: Uruguay -- Montevideo
- Contact:
Wrong
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.
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.
- kendall
- Forum Regular
- Posts: 852
- Joined: Tue Jul 30, 2002 10:21 am
- Location: Trinidad, West Indies
- Contact:
returning multiple variables
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 ???

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 ???
check your:
function CheckFields($requiredFields)
firstly, $requiredFields should be an array of 'Fields'
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
function CheckFields($requiredFields)
firstly, $requiredFields should be an array of 'Fields'
Code: Select all
e.g.
$array_fields = array ('1','2,'3');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++{
... // Do your Check Fields routine here;
}