Jump to content

Passing variables from function


phppup
 Share

Go to solution Solved by ginerjm,

Recommended Posts

While working with some code I wrote this and it works

<?

if(isset($_POST['submit'])) {
$greeting = "Good morning";
$late = "You are late";

if($_POST['time'] > 0) {
echo $greeting;
        }

if($_POST['time'] > 5) {
echo $late;
        }
  }

?>

Then I decided to modify it into a function (which would be preferable for expanding the code's usefulness)

That's when my problems began.

What do I need to do so that the function works as well as the straight code?


<?

$greeting = "Good morning";
$late = "You are late";

function greet(){
if($_POST['time'] > 0) {
echo $greeting;
        }

if($_POST['time'] > 5) {
echo $late;
        }
    }

if(isset($_POST['submit'])) {
greet();
}

?>

I thought I'd ask the experts.

Thanks.

Edited by phppup
Link to comment
Share on other sites

I think I'd prefer to pass them as arguments.

Can you save me the trouble of sorting through bad information from a search engine?

(I've seen some info about using an ampersand, but it wasn't very clear)

Are you saying that if I include the variables within the { } of the function that it will work?  I tried that earlier but it failed.

If these variables will be used as reference to other functions, Is there a Best Practices path to proceed on?  Will wrapping the variables in a function of their own to be called inside of this function provide the desired result?

Edited by phppup
Forgot item
Link to comment
Share on other sites

Create the greetings array in your function.  Something that links the time values (0,5) to the greeting label you want to see.  Then pass a single argument to the function - the post time value.  Let the function use that value to look up in your new array and return the text you want to output.

I'll let you figure this out - good practice.  

Link to comment
Share on other sites

Just did more reading and I'll have to review my implementation.

It seems that my attept to reduce (already short) code repetition will result in an increase of duplicated variable lists.

But the effort was a learning experience.

Thanks for the responses.

Edited by phppup
Link to comment
Share on other sites

@ginerjmYes, and I appreciate your input.

But the solution means that I have to include the array in every redundancy of similar functions that will use those variables.

Without using functions at all,I can list the variables once and write straight code.

Either way, something will be repeated, so I have to re-evaluate.

At least NOW, I can make an informed decision.

Edited by phppup
Typos
Link to comment
Share on other sites

  • Solution

So?  You write the array and save it as its own file.  Then you include it in every script that needs to use it.  The really smart thing would be to include the function there too and use an arg to feed it and accept a result when you call the function.

function GetGreeting($time)
{
	$greetings = array(
		'0'=>'Good morning',
		'5'=>'You are late'
		'10'=>'you are real late'
		);
	$str = (string) $time;
	return $greetings[$str];
}

In your other scripts you do an "include 'getgreetings.php;'" and then call it with:

$greeting = GetGreeting($time);

and then output $greeting.

Edited by ginerjm
Link to comment
Share on other sites

You were showing it as coming from the input from a form.  Anywhere you are getting it you can use.  I don't know exactly what type of value you are using since you were simply showing something like 0 or 5 in your first post.  Since I wrote the array of greetings using a string value as the key, I made sure to convert whatever value you passed into the function to a string too.  I dont know how you were getting a time.  I'm guessing it was a difference between actual and expected times.

Link to comment
Share on other sites

Well, I'm not sure (because the whole point was to minimize code lines... LOL).

And this was just a personal effort to see where it might lead me.

I wouldn't have thought of using INCLUDES and I don't do well when I lose track of my (visible) code.

The funny thing about this little project is that the"variables" are probably the most "constant" piece of the puzzle.

That's why I thought FUNCTIONS would be a good step.

In essence, some might be considered late after 5 minutes, others are 10. Some after 30, others 120.  Hence, a different (short) function for each scenario. 

Essentially, the messages are the same: Your late, your very late, see your supervisor, go home fool, etc. regardless of "group."

I'm still considering my options, and the other factors that might guide me.

No hurry for this one.

Edited by phppup
Typos
Link to comment
Share on other sites

So you might need to have several arrays to handle different classes of people's habits?  Or a mutli-dimensional array that combines them all.  Either way, you would   add a second arg to the function that adds this 'class' value.

Link to comment
Share on other sites

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.

 Share

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