Functions

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

bob_the _builder
Forum Contributor
Posts: 131
Joined: Sat Aug 28, 2004 12:25 am

Functions

Post by bob_the _builder »

Hi,

I guess you cant call on a function() within a function ..

Whats the best way to store post variables to call on at anytime within functions?

Code: Select all

$fname = ValidateInput($_POST['fname']);
$lname = ValidateInput($_POST['lname']);

function add() {

// if ($_POST['edit'] == 'edit') {

query database for records here

}else{

// call for the stored $_POST data here

}

Thanks
Last edited by bob_the _builder on Tue Oct 10, 2006 3:42 pm, edited 1 time in total.
bob_the _builder
Forum Contributor
Posts: 131
Joined: Sat Aug 28, 2004 12:25 am

Re: Functions

Post by bob_the _builder »

..
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Re: Functions

Post by volka »

bob_the _builder wrote:I guess you cant call on a function() within a function ..
but you can

Code: Select all

<?php
function foo() {
	echo 'bar';
}

function xyz() {
	foo();
}

xyz();
?>
bob_the _builder wrote:Whats the best way to store post variables to call on at anytime within functions?
The array _POST is "superglobal", i.e. it can be accessed from anywhere.

Code: Select all

<?php
function foo() {
	if (isset($_POST['bar']) && 0<strlen($_POST['bar']) ) {
		echo '<pre>bar: ', $_POST['bar'], '</pre>';
	}
}

function xyz() {
	foo();
}
?>
<html>
	<head><title>post test</title></head>
	<body>
		<?php xyz(); ?>
		<form method="post">
			<div>
				<input type="text" name="bar" />
				<input type="submit" />
			</div>
		</form>
	</body>
</html>
bob_the _builder
Forum Contributor
Posts: 131
Joined: Sat Aug 28, 2004 12:25 am

Post by bob_the _builder »

Hi,

I had allready tried:

Code: Select all

function post() {
    $fname = $_POST['fname'];
    $lname = $_POST['lname'];
}

function form() {

if ($_POST['edit'] == edit ) {

// query database for record

}else{

post();

}

<div>
<input type="text" name="fname" value="<?php echo $fname; ?>" />
<input type="text" name="lname" value="<?php echo $lname; ?>" />
</div>
}

But form(); is called and $_POST data is presant it isnt been echoed back into the text fields. I think its the same concept as your posted code?


Thanks
Last edited by bob_the _builder on Tue Oct 10, 2006 4:56 pm, edited 1 time in total.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

That's a scope issue. Variables created inside the funcion can only be seen inside that function unless you specify otherwise. There are to approaches to what you're doing here:

1. Specify the variables as global

Code: Select all

function post() {
    global $fname, $lname;
    
    $fname = $_POST['fname'];
    $lname = $_POST['lname'];
}
2. Pass them by-reference

Code: Select all

function post(&$var1, &$var2) {
    $var1 = $_POST['fname'];
    $var2 = $_POST['lname'];
}

post($fname, $lname);
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Why are you assigning $fname = $_POST['fname'];?
$fname and $lname are not superglobal, when post() ends they end to exist.
What do you want to achieve/What's the purpose of the code?
bob_the _builder
Forum Contributor
Posts: 131
Joined: Sat Aug 28, 2004 12:25 am

Post by bob_the _builder »

Its so I can share the same form to edit records and create new records ...

$fname = $row['fname']

for an edit or

$fname = $_POST['fname']

the post data being used when the form is reloaded from mandetory fields, and wanting them held in a function so I can call on the post(); to place into the database when the form is successfully submited.


Which is the better approch, global or reference?


Thanks
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

reference; passing an array to the function.
bob_the _builder
Forum Contributor
Posts: 131
Joined: Sat Aug 28, 2004 12:25 am

Post by bob_the _builder »

I couldnt get global to work .. reference is working, altho seems you need to be repetitve in typing the variables .. ie

function post(&$fname, &$lname, &$company) {


then typing them all over when calling the function

post($fname, $lname, $company);


Thanks
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

The former is a function definition. It does not represent the variable names that are being passed to the function, but the variable names as they would be known inside the function itself.

The latter is the function call, since three (required) arguments were defined for the function, three must be passed in.
bob_the _builder
Forum Contributor
Posts: 131
Joined: Sat Aug 28, 2004 12:25 am

Post by bob_the _builder »

So the best option all round is:

Code: Select all

function post(&$fname, &$lname, &$company, &$dname, &$email, &$package, &$notes, &$day, &$month, &$year) {

 	$fname = ValidateInput($_POST['fname']);
	$lname = ValidateInput($_POST['lname']);
	$company = ValidateInput($_POST['company']);
	$dname = ValidateInput($_POST['dname']);
	$email = ValidateInput($_POST['email']);
	$package = ValidateInput($_POST['package']);
	$notes = ValidateInput($_POST['notes']);
	$day = ValidateInput($_POST['day']);
	$month = ValidateInput($_POST['month']);
	$year = ValidateInput($_POST['year']);
}

post($fname, $lname, $company, $dname, $email, $package, $notes, $day, $month, $year);

<div> 
<input type="text" name="fname" value="<?php echo $fname; ?>" /> 
<input type="text" name="lname" value="<?php echo $lname; ?>" /> 
</div> 

....

Thanks
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Return values from your functions...

Code: Select all

<?php
function assign_from_post($fieldname) {
    $post_var = '';

    if (isset($_POST[$fieldname]))
    {
        $post_var = $_POST[$fieldname];
    }

    return $post_var;
}
?>
Usage:

Code: Select all

<?php
$fname = assign_from_post('fname');
?>
User avatar
dibyendrah
Forum Contributor
Posts: 491
Joined: Wed Oct 19, 2005 5:14 am
Location: Nepal
Contact:

Post by dibyendrah »

Inside the function, unless you pass the variable as argument, other variable cannot be accessed unless you call global variable. So, you better call the global POST, GET variable by writing the statement :

Code: Select all

global $HTTP_POST_VARS;
will solve your problem.

Code: Select all

function validateInput(){
    global $HTTP_POST_VARS;
    if(empty($HTTP_POST_VARS['fname'])){
        return false;
    }else{
        return true;
    }
}
With Regards,
Dibyendra
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

$_POST, $_GET, $_COOKIE are already globals (superglobals actually) so you don't need to define them as globals in any functions. see Predefined variables
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

This function seems a bit pointless anyway to be honest, you can just create those variables in the global scope if that's what your objective is.
Post Reply