Mr Chris Posted October 21, 2010 Share Posted October 21, 2010 Hello All, I'm trying to put a lot of what I do into function calls now to make my code a bit tidier and consise. Take this simple if statement: if ($_GET['thedate'] =='') { $var = date('l, jS F, Y',time()); } else { $var = date('l, jS F, Y', strtotime($_GET['thedate'])); } How could I put this into a function and in some kind of switch statement? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/216493-turn-this-code-into-a-function/ Share on other sites More sharing options...
Pikachu2000 Posted October 21, 2010 Share Posted October 21, 2010 <?php function REGURGIDATE() { if ($_GET['thedate'] =='') { return date('l, jS F, Y',time()); } else { return date('l, jS F, Y', strtotime($_GET['thedate'])); } } echo REGURGIDATE(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/216493-turn-this-code-into-a-function/#findComment-1124897 Share on other sites More sharing options...
Mr Chris Posted October 21, 2010 Author Share Posted October 21, 2010 Thanks, but I was thinking maybe in some kind of switch statement. Is that possible within a function? Quote Link to comment https://forums.phpfreaks.com/topic/216493-turn-this-code-into-a-function/#findComment-1124900 Share on other sites More sharing options...
AbraCadaver Posted October 21, 2010 Share Posted October 21, 2010 Thanks, but I was thinking maybe in some kind of switch statement. Is that possible within a function? Yes, but why? It doesn't make any sense to do that. Quote Link to comment https://forums.phpfreaks.com/topic/216493-turn-this-code-into-a-function/#findComment-1124908 Share on other sites More sharing options...
ignace Posted October 21, 2010 Share Posted October 21, 2010 function getDate($format = 'l, jS F, Y', $offset = null) { return date($format, $offset == null ? time() : $offset); } echo getDate(), "<br>\r\n", getDate('l, jS F, Y', $_GET['thetime']); Quote Link to comment https://forums.phpfreaks.com/topic/216493-turn-this-code-into-a-function/#findComment-1124935 Share on other sites More sharing options...
Pikachu2000 Posted October 21, 2010 Share Posted October 21, 2010 Ignace's function may be what you meant, using the ternary operator. But I still think REGURGIDATE() is a better name for it Quote Link to comment https://forums.phpfreaks.com/topic/216493-turn-this-code-into-a-function/#findComment-1124937 Share on other sites More sharing options...
Colton.Wagner Posted October 21, 2010 Share Posted October 21, 2010 Agreed I would combine the two codes so that the function getDate() became REGURGIDATE(). I would use ignace's code it is shorter and more precise. However Pikachu2000's code will work aswell. Thanks, Colton Wagner Quote Link to comment https://forums.phpfreaks.com/topic/216493-turn-this-code-into-a-function/#findComment-1124940 Share on other sites More sharing options...
ignace Posted October 21, 2010 Share Posted October 21, 2010 I think no one would understand what REGURGIDATE() would do... until they pass it through Google: Did you mean: Regurgitation (vomiting) Sorry to spill your little fun there Quote Link to comment https://forums.phpfreaks.com/topic/216493-turn-this-code-into-a-function/#findComment-1124965 Share on other sites More sharing options...
Pikachu2000 Posted October 21, 2010 Share Posted October 21, 2010 And here I was thinking most English-speaking people would get my little play on words. D'oh! Quote Link to comment https://forums.phpfreaks.com/topic/216493-turn-this-code-into-a-function/#findComment-1124997 Share on other sites More sharing options...
Colton.Wagner Posted October 21, 2010 Share Posted October 21, 2010 I enjoyed it Pickachu2000 In all relevance though you wouldn't want to name it that for obvious reasons. Thanks, Colton Wagner Quote Link to comment https://forums.phpfreaks.com/topic/216493-turn-this-code-into-a-function/#findComment-1125002 Share on other sites More sharing options...
Pikachu2000 Posted October 21, 2010 Share Posted October 21, 2010 Well, forgive me for trying to inject some levity into an otherwise dry topic . . . Y'all are meanies; Ima take my football and go home. :'( Quote Link to comment https://forums.phpfreaks.com/topic/216493-turn-this-code-into-a-function/#findComment-1125005 Share on other sites More sharing options...
marcus Posted October 21, 2010 Share Posted October 21, 2010 I thought it was funny. And only the person using the code should have to know what it means. Quote Link to comment https://forums.phpfreaks.com/topic/216493-turn-this-code-into-a-function/#findComment-1125033 Share on other sites More sharing options...
ignace Posted October 22, 2010 Share Posted October 22, 2010 And here I was thinking most English-speaking people would get my little play on words. D'oh! I can write English but my vocabulary isn't so extensive that it contains words like regurgitate Quote Link to comment https://forums.phpfreaks.com/topic/216493-turn-this-code-into-a-function/#findComment-1125127 Share on other sites More sharing options...
Pikachu2000 Posted October 22, 2010 Share Posted October 22, 2010 I never would have thought that. You write English so well I always thought it was your native language. Quote Link to comment https://forums.phpfreaks.com/topic/216493-turn-this-code-into-a-function/#findComment-1125129 Share on other sites More sharing options...
ignace Posted October 22, 2010 Share Posted October 22, 2010 I never would have thought that. You write English so well I always thought it was your native language. LOL, no my native language is Dutch and you would laugh if you would ever hear me speak English Quote Link to comment https://forums.phpfreaks.com/topic/216493-turn-this-code-into-a-function/#findComment-1125231 Share on other sites More sharing options...
mentalist Posted October 25, 2010 Share Posted October 25, 2010 @Pikachu2K, You made me laugh @ignace, yes I thought English was your native language, but now in my mind when I read your posts, your voice sounds like Stevie Hawkins @Mr Chris, Did you even try to use a switch statement in a function before posting? Of course you can use switch in a function. Q. Are you the same Mr Chris from sheffieldforum.co.uk? Quote Link to comment https://forums.phpfreaks.com/topic/216493-turn-this-code-into-a-function/#findComment-1126145 Share on other sites More sharing options...
ignace Posted October 25, 2010 Share Posted October 25, 2010 @ignace, yes I thought English was your native language, but now in my mind when I read your posts, your voice sounds like Stevie Hawkins At least my English teacher can be rest assured that she did a good job on the writing part Not sure how Stevie Hawkins sounds but I'm not that old Quote Link to comment https://forums.phpfreaks.com/topic/216493-turn-this-code-into-a-function/#findComment-1126158 Share on other sites More sharing options...
mentalist Posted October 25, 2010 Share Posted October 25, 2010 Sorry Steven Hawkings... http://www.youtube.com/watch?v=HZkyRl5IreM Quote Link to comment https://forums.phpfreaks.com/topic/216493-turn-this-code-into-a-function/#findComment-1126163 Share on other sites More sharing options...
ignace Posted October 25, 2010 Share Posted October 25, 2010 Sorry Steven Hawkings... http://www.youtube.com/watch?v=HZkyRl5IreM @ignace, yes I thought English was your native language, but now in my mind when I read your posts, your voice sounds like Stevie Hawkins Isn't it annoying for you to always have to speak through my voice? Quote Link to comment https://forums.phpfreaks.com/topic/216493-turn-this-code-into-a-function/#findComment-1126275 Share on other sites More sharing options...
mentalist Posted October 25, 2010 Share Posted October 25, 2010 Thing is I hear Dutch a lot of the time because i'm a keen Blender user and the majority of tutorials are by Dutch peeps (since it's primary developer base and institute is there). I always imagine / copy voices of people, it the animator in me. ... On another note, Steven Hawkings voice could be updated to be a lot more natural, but he prefers it because it is now a part of him... Quote Link to comment https://forums.phpfreaks.com/topic/216493-turn-this-code-into-a-function/#findComment-1126284 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.