Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Macros? isset?

Status
Not open for further replies.

cchipman

IS-IT--Management
Joined
Sep 16, 2002
Messages
125
Location
US
I've got a form that being used to update a database. I'm having to do a lot of if "if (isset($varname)) $Array[] = $varname;" type of statements.

I tried adding the folllowing function
Code:
function AddToArrayIfSet($tgtArray, $tgtVar)
{
	if( isset($tgtVar))
	{
		$tgtArray[] = $tgtVar;
	}
}

but when called I got a lot of
Code:
Notice: Undefined variable: AoA_Service in c:\inetpub\[URL unfurl="true"]wwwroot\Questionaire2.php[/URL] on line 74

type warning. Is there any way to do macros? Or am I doomed to have to check every input?
 
Since you're passing in a value into $tdtVar, the isset() function will always return true.

Also, functions have their own variable scope and you are passing the array in as a pass-by-value. This means that the function is adding your values to the end of a copy of the array you're passing in. Once the function ends, the value will not be at the end of the array.

Also, since functions have their own variable scope, you can't check on variables outside the function without examining the $_GLOBALS superglobal array or through use of the global operator.

For example, the following code:
Code:
<?php
function add_to_array1($tgtArray, $tgtVar)
{
        $tgtArray[] = $tgtVar;
        
        print &quot;In function add_to_array1:\n&quot;;
        print_r ($tgtArray);
}

function add_to_array2(&$tgtArray, $tgtVar)
{
        $tgtArray[] = $tgtVar;

        print &quot;In function add_to_array2:\n&quot;;
        print_r ($tgtArray);
}

$thearray = array();

add_to_array1($thearray, 'foo');

print &quot;In main:\n&quot;;
print_r ($thearray);

add_to_array2($thearray, 'bar');

print &quot;In main:\n&quot;;
print_r ($thearray);
?>

Outputs:
Code:
In function add_to_array1:
Array
(
    [0] => foo
)
In main:
Array
(
)
In function add_to_array2:
Array
(
    [0] => bar
)
In main:
Array
(
    [0] => bar
)

(Notice the ampersand in the definition of add_to_array2().)

What, exactly, is this function supposed to do?

BTW, PHP does not have any kind of macro function.

Want the best answers? Ask the best questions: TANSTAAFL!!
 
I'm not sure if this is your intent or not, but if you're looking to make sure all the form fields are filled out (ie.. no null values into the db), you might be better served with a javascript function to parse the form for empties before submitting.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top