Jump to content

Question about Function and Parameters


doubledee

Recommended Posts

This thread is about what you can and cannot call Variables and Parameters in a Function...

 

I have this query which returns 1 or 2 for $gender...

	// Check # of Records Returned.
	if (mysqli_stmt_num_rows($stmt1)==1){
		// Member was Found.

		// Bind result-set to variables.
		mysqli_stmt_bind_result($stmt1, $firstName, $username, $photoName, $photoLabel,
								$gender, $birthYear, $location,
								$occupation, $interests, $aboutMe);

 

 

And here is my Function...

function displayGender($gender){
	if ($gender == 1){
		$gender = 'Male';
	}elseif ($gender == 2){
		$gender = 'Female';
	}else{
		$gender = '';
	}

	return $gender;
}//End of displayGender

 

 

Questions:

1.) Is it a problem having $gender as both my Parameter and Argument?

 

2.) Is it a problem renaming $gender inside the Function like I do?

 

3.) Is it a problem returning $gender like I do?

 

 

Thanks,

 

 

Debbie

 

 

Link to comment
Share on other sites

1.) Is it a problem having $gender as both my Parameter and Argument?

 

What do you mean? Those terms are synonymous.

 

2.) Is it a problem renaming $gender inside the Function like I do?

 

You don't rename $gender you just give it a new value. If that's what you meant, then no.

 

3.) Is it a problem returning $gender like I do?

 

No, in most cases it is preferable to return things from functions rather than echo them since echo'ing limits how you can use the function.

 

 

Really though you should be storing male/female in the database, not numbers. Numbers don't really make any sense, and you can't work with that kind of data very easily. Furthermore, using numbers over male/female provides no benefit at all.

Link to comment
Share on other sites

1.) Is it a problem having $gender as both my Parameter and Argument?

 

What do you mean? Those terms are synonymous.

 

No, "Parameter" and "Argument" do NOT mean the same thing.

 

A "Parameter" is the variable that holds the value you pass to it.

 

An "Argument" is the value you pass to a "Parameter".

 

 

In my main script, I run a query and bind the results to variables like this...

// Check # of Records Returned.
if (mysqli_stmt_num_rows($stmt1)==1){
	// Member was Found.

	// Bind result-set to variables.
	mysqli_stmt_bind_result($stmt1, $firstName, $username, $photoName, $photoLabel,
							$gender, $birthYear, $location,
							$occupation, $interests, $aboutMe);

 

So at this point $gender comes to life in my script and might hold a value like "2" (for female).

 

But in my Function, I am also using a variable (i.e. "Parameter") called $gender...

function getGenderName($gender){
	if ($gender == 1){
		$genderName = 'Male';
	}elseif ($gender == 2){
		$genderName = 'Female';
	}else{
		$genderName = '';
	}

	return $genderName;
}//End of displayGender

 

I didn't know if that would cause any conflicts.

 

Also, if my Function were different and I somehow manipulated the $gender variable INSIDE my function would that then affect the original $gender variable in my main script?

 

Follow me now?

 

 

 

3.) Is it a problem returning $gender like I do?

 

No, in most cases it is preferable to return things from functions rather than echo them since echo'ing limits how you can use the function.

 

In my Function above, what is happening with $genderName that is being returned?

 

Is that really a variable outside of the Function?

 

Could I call $genderName and use it like a regular variable?

 

 

Really though you should be storing male/female in the database, not numbers. Numbers don't really make any sense, and you can't work with that kind of data very easily. Furthermore, using numbers over male/female provides no benefit at all.

 

Most database people I know would disagree.  In fact, most people I talked to recommended using either a Boolean or follow the ISO standards for Gender.

 

Now, I could return "Male/Female" using a lookup table, but I hate to muck up my clean queries with an extra join.

 

I also figured this was a good way to practice using my first-ever Function!  :)

 

Thanks,

 

 

Debbie

 

Link to comment
Share on other sites

1.) Is it a problem having $gender as both my Parameter and Argument?

No, "Parameter" and "Argument" do NOT mean the same thing.

 

A "Parameter" is the variable that holds the value you pass to it.

 

An "Argument" is the value you pass to a "Parameter".

 

Soooo.... you answered your own question then, no?

 

Also, if my Function were different and I somehow manipulated the $gender variable INSIDE my function would that then affect the original $gender variable in my main script?

Take a look into scopes. In this case, no.

 

 

 

In my Function above, what is happening with $genderName that is being returned?

 

Is that really a variable outside of the Function?

Again, take a look at scopes.

 

Could I call $genderName and use it like a regular variable?

It is a regular variable... within the function's scope.

 

 

PS - Trying and fiddling with things like this is a great way to learn it. Do you have a local sandbox to test stuff like this in?

Link to comment
Share on other sites

1.) Is it a problem having $gender as both my Parameter and Argument?

No, "Parameter" and "Argument" do NOT mean the same thing.

 

A "Parameter" is the variable that holds the value you pass to it.

 

An "Argument" is the value you pass to a "Parameter".

 

Soooo.... you answered your own question then, no?

 

I guess so.

 

So I will leave $gender in my query result set and then leave the Function Parameter also called $gender for simplicity of following things...

 

 

 

In my Function above, what is happening with $genderName that is being returned?

 

Is that really a variable outside of the Function?

 

Again, take a look at scopes.

 

I did read the link, but didn;t see anything to answer this question.

 

If we are "returning" something, *presumably* it would get passed OUTSIDE of the Function, but I am not sure?!

 

 

Could I call $genderName and use it like a regular variable?

 

It is a regular variable... within the function's scope.

 

And what is that scope?

 

 

PS - Trying and fiddling with things like this is a great way to learn it. Do you have a local sandbox to test stuff like this in?

 

I have been fiddling with things, but scope isn't as easy to see or test, so I figured I would ask the experts here before I hand myself!!

 

 

Debbie

 

 

Link to comment
Share on other sites

Debbie, with the exception of objects (and that's only in PHP 5+), arguments are passed into functions by value.  That means that if you have an external variable named $gender, and you pass it into a function, $gender's value is COPIED into the function.  What happens to that copied value within the function has no effect on the $gender variable outside the function.  Example;

 

$test1 = "Forrest Gump";
$test2 = "Bubba";

function prestoChangeo($x) {
   $x = "Lt. Dan";
   return $x;
}

prestoChangeo($test1);
$test2 = prestoChangeo($test2);

echo "$test1<br />$test2";

 

Regarding parameters vs. arguments: parameters are simply named placeholders.

Link to comment
Share on other sites

Debbie, with the exception of objects (and that's only in PHP 5+), arguments are passed into functions by value.  That means that if you have an external variable named $gender, and you pass it into a function, $gender's value is COPIED into the function.  What happens to that copied value within the function has no effect on the $gender variable outside the function.  Example;

 

$test1 = "Forrest Gump";
$test2 = "Bubba";

function prestoChangeo($x) {
   $x = "Lt. Dan";
   return $x;
}

prestoChangeo($test1);
$test2 = prestoChangeo($test2);

echo "$test1<br />$test2";

 

Regarding parameters vs. arguments: parameters are simply named placeholders.

 

I ran your code and am not following what is going on?!

 

The output totally is not what I would expect?!

 

Can you help me figure out what is going?

 

Thanks,

 

 

Debbie

 

Link to comment
Share on other sites

This outputs:

 

Forrest Gump<br />Lt. Dan

 

This is because when $test1 is passed to the function, it is not linked to $x. It's value is simply copied to $x and $x is part of the function scope where-as $test1 is part of the global scope. Using a reference you can bind $x in the function scope to $test1 by changing the declaration of $x to &$x. When you then run the above code you will get:

 

Lt. Dan<br />Lt. Dan

 

I did not alter the code and then run it to tell you this. Most people on these forums can tell you this without executing the code because they understand how PHP/functions works, and you should too.

Link to comment
Share on other sites

This outputs:

 

Forrest Gump<br />Lt. Dan

 

This is because when $test1 is passed to the function, it is not linked to $x. It's value is simply copied to $x and $x is part of the function scope where-as $test1 is part of the global scope. Using a reference you can bind $x in the function scope to $test1 by changing the declaration of $x to &$x. When you then run the above code you will get:

 

Lt. Dan<br />Lt. Dan

 

I did not alter the code and then run it to tell you this. Most people on these forums can tell you this without executing the code because they understand how PHP/functions works, and you should too.

 

I misread the code, and was thinking this...

<?php

$test1 = "Forrest Gump";
$test2 = "Bubba";

function prestoChangeo($x) {
   $x = "Lt. Dan";
   return $x;
}

prestoChangeo($test1);
$test2 = prestoChangeo($test2);

echo prestoChangeo($test1) . "<br />";
echo "$test1<br />";
echo $test2;


// Lt. Dan
// Forrest Gump
// Lt. Dan

?>

 

 

Debbie

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.