hyster Posted May 25, 2011 Share Posted May 25, 2011 to learn a little about creating functions i decided to do a geetings message depending on the time of day which works fine. function greet() { $date=date("H"); if ($date >= '00' && $date <'12'){ echo "Good Morning"; }elseif ($date >= '12' && $date <'18') { echo "Good Afternoon" ; }elseif ($date >= '18' && $date <'24') { echo "Good Evening" ; }else{ echo "All Messed Up" ; } } echo "hello "; greet() so what i want to do next and the part im stuck on is using a switch to change language. the code below dosent work but i hope u get an idea of what i want function greet() { $date=date("H"); $uk = if ($date >= '00' && $date <'12'){ echo "Good Morning"; }elseif ($date >= '12' && $date <'18') { echo "Good Afternoon" ; }elseif ($date >= '18' && $date <'24') { echo "Good Evening" ; }else{ echo "All Messed Up" ; } $pl = if ($date >= '00' && $date <'12'){ echo "Dzien dobry"; }elseif ($date >= '12' && $date <'18') { echo "Dzien dobry" ; }elseif ($date >= '18' && $date <'24') { echo "Dobry wieczor" ; }else{ echo "Zjebales" ; } } echo "hello"; greet(pl) Quote Link to comment https://forums.phpfreaks.com/topic/237410-creating-functions/ Share on other sites More sharing options...
QuickOldCar Posted May 25, 2011 Share Posted May 25, 2011 I changed it a bit. <?php function greet($language) { $date=date("H"); if($language == "uk") { if ($date >= '00' && $date < '12'){ return "Good Morning"; }elseif ($date >= '12' && $date < '18') { return "Good Afternoon"; }elseif ($date >= '18' && $date < '24') { return "Good Evening"; }else{ return "All Messed Up"; } } elseif ($language == "pl") { if ($date >= '00' && $date < '12'){ return "Dzien dobry"; }elseif ($date >= '12' && $date < '18') { return "Dzien dobry" ; }elseif ($date >= '18' && $date < '24') { return "Dobry wieczor" ; }else{ return "Zjebales" ; } } else { return "No Language Selected"; } } echo greet(pl); echo "<br />"; echo greet(uk); ?> Quote Link to comment https://forums.phpfreaks.com/topic/237410-creating-functions/#findComment-1219902 Share on other sites More sharing options...
hyster Posted May 25, 2011 Author Share Posted May 25, 2011 thanks m8. i understand that so far. now if i wanted to add another 'section' for the day so i can do greet("pl/d") how would i add that ??? function greet($language,$date1) { // the other code from above// $date1 = date('D'); if ($date1 == 'Mon'){ return "Nom";} elseif ($date1 == 'Tue'){ return "Eut"; }elseif ($date1 == 'Wed'){ return "Dew"; }elseif ($date1 == 'Thu'){ return "Uht"; }elseif ($date1 == 'Fri'){ return "Irf"; } echo greet('pl/d'); echo "<br />"; echo greet("uk/d"); Quote Link to comment https://forums.phpfreaks.com/topic/237410-creating-functions/#findComment-1219922 Share on other sites More sharing options...
QuickOldCar Posted May 25, 2011 Share Posted May 25, 2011 Well the standard way would be to have them as variables //variable values coming from somewhere $language = "pl"; $month = date('M'); $day = date('D'); $year = date('Y'); $hour = date('h'); $minute = date('i'); $second = date('s'); greet($language,$month,$day,$year,$hour,$minute,$second); in function follows same order And in function you wouldn't need to change the value because it comes from before it function greet($language,$date1) { //$date1 = date('D'); //not needed $date1 value already there (unless you want to change the value) You can also do arrays or a string and can explode them separated by any delimiter that makes sense to you. Checking if value exists or is not empty as well. I don't have time to explain fully, I need to go to work. Hopefully I explained well enough or others can explain and give more examples of arrays and even exploding arrays from strings of text using delimiters Quote Link to comment https://forums.phpfreaks.com/topic/237410-creating-functions/#findComment-1220033 Share on other sites More sharing options...
hyster Posted May 25, 2011 Author Share Posted May 25, 2011 i understood the top piece of code i think but its not what im after as such. the content im using is not relevent to what i want to achive. its just filler content that shows something diffrent. im trying to figure out how to get 2 (or more) blocks of code to use arguments inside of a function. the ie: function test($var1,$var2,$var$){ $var1 block of code{ qq } $var2 block of code{ ee } $var3 block of code{ rr } } text (qq/ee/rr) hope that makes more sense to what im trying to learn Quote Link to comment https://forums.phpfreaks.com/topic/237410-creating-functions/#findComment-1220121 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.