phppup Posted September 28, 2021 Share Posted September 28, 2021 (edited) 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 September 28, 2021 by phppup Quote Link to comment https://forums.phpfreaks.com/topic/313831-passing-variables-from-function/ Share on other sites More sharing options...
Barand Posted September 28, 2021 Share Posted September 28, 2021 The variables $greeting and $late do not exist within the function. You will need to pass them as arguments or define them within the function. Quote Link to comment https://forums.phpfreaks.com/topic/313831-passing-variables-from-function/#findComment-1590473 Share on other sites More sharing options...
phppup Posted September 28, 2021 Author Share Posted September 28, 2021 (edited) 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 September 28, 2021 by phppup Forgot item Quote Link to comment https://forums.phpfreaks.com/topic/313831-passing-variables-from-function/#findComment-1590475 Share on other sites More sharing options...
gw1500se Posted September 28, 2021 Share Posted September 28, 2021 Include them between the () of the function definition. Example: function myfunction($arg1,$arg2) { . . . } Quote Link to comment https://forums.phpfreaks.com/topic/313831-passing-variables-from-function/#findComment-1590477 Share on other sites More sharing options...
phppup Posted September 28, 2021 Author Share Posted September 28, 2021 I guess I can try that. Any other effective alternatives? Quote Link to comment https://forums.phpfreaks.com/topic/313831-passing-variables-from-function/#findComment-1590478 Share on other sites More sharing options...
phppup Posted September 28, 2021 Author Share Posted September 28, 2021 If my argument list grows, is their a way to incorporate them all into an array to minimize replications of coding? Quote Link to comment https://forums.phpfreaks.com/topic/313831-passing-variables-from-function/#findComment-1590480 Share on other sites More sharing options...
ginerjm Posted September 28, 2021 Share Posted September 28, 2021 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. Quote Link to comment https://forums.phpfreaks.com/topic/313831-passing-variables-from-function/#findComment-1590481 Share on other sites More sharing options...
phppup Posted September 28, 2021 Author Share Posted September 28, 2021 (edited) 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 September 28, 2021 by phppup Quote Link to comment https://forums.phpfreaks.com/topic/313831-passing-variables-from-function/#findComment-1590483 Share on other sites More sharing options...
ginerjm Posted September 28, 2021 Share Posted September 28, 2021 I told you how to do it. Quote Link to comment https://forums.phpfreaks.com/topic/313831-passing-variables-from-function/#findComment-1590486 Share on other sites More sharing options...
phppup Posted September 28, 2021 Author Share Posted September 28, 2021 (edited) @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 September 28, 2021 by phppup Typos Quote Link to comment https://forums.phpfreaks.com/topic/313831-passing-variables-from-function/#findComment-1590488 Share on other sites More sharing options...
Solution ginerjm Posted September 28, 2021 Solution Share Posted September 28, 2021 (edited) 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 September 28, 2021 by ginerjm Quote Link to comment https://forums.phpfreaks.com/topic/313831-passing-variables-from-function/#findComment-1590490 Share on other sites More sharing options...
phppup Posted September 28, 2021 Author Share Posted September 28, 2021 Quote $str = (string) $time; So, $time is calculated elsewhere. What is (string) ? Quote Link to comment https://forums.phpfreaks.com/topic/313831-passing-variables-from-function/#findComment-1590492 Share on other sites More sharing options...
ginerjm Posted September 28, 2021 Share Posted September 28, 2021 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. Quote Link to comment https://forums.phpfreaks.com/topic/313831-passing-variables-from-function/#findComment-1590493 Share on other sites More sharing options...
phppup Posted September 28, 2021 Author Share Posted September 28, 2021 Ok. This is an interesting approach that I wouldn't have thought of on my own. I'm going to play with it and see if I can get more comfortable with it. Thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/313831-passing-variables-from-function/#findComment-1590494 Share on other sites More sharing options...
ginerjm Posted September 28, 2021 Share Posted September 28, 2021 You mean add more code lines? Quote Link to comment https://forums.phpfreaks.com/topic/313831-passing-variables-from-function/#findComment-1590497 Share on other sites More sharing options...
phppup Posted September 29, 2021 Author Share Posted September 29, 2021 (edited) 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 September 29, 2021 by phppup Typos Quote Link to comment https://forums.phpfreaks.com/topic/313831-passing-variables-from-function/#findComment-1590498 Share on other sites More sharing options...
ginerjm Posted September 29, 2021 Share Posted September 29, 2021 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. Quote Link to comment https://forums.phpfreaks.com/topic/313831-passing-variables-from-function/#findComment-1590499 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.