spelltwister Posted August 20, 2006 Share Posted August 20, 2006 Hey all,I was wondering... say that this is my function:function find_game_day(){ $game_day = ""; switch($current_day){ case "Mon": $game_day = 6; case "Tue": $game_day = 0; case "Wed": $game_day = 1; case "Thu": $game_day = 2; case "Fri": $game_day = 3; case "Sat": $game_day = 4; case "Sun": $game_day = 5; default: $game_day = ""; }}this is my code:<?php$current_day = date("D",time());get_game_day();echo $game_day;?>when I call the function, does it import all the code including variables to the main code or do I have to leave the variables outside for later referencing because it's out of scope in the function.Thanks,Mike Quote Link to comment Share on other sites More sharing options...
Barand Posted August 20, 2006 Share Posted August 20, 2006 Pass the $current_day var to the function. Have your function return the value of $game_day.Case statements need "break;" otherwise next case statement is executed also[code]<?phpfunction find_game_day($current_day){ $game_day = ""; switch($current_day){ case "Mon": $game_day = 6; break; case "Tue": $game_day = 0; break; case "Wed": $game_day = 1; break; case "Thu": $game_day = 2; break; case "Fri": $game_day = 3; break; case "Sat": $game_day = 4; break; case "Sun": $game_day = 5; break; default: $game_day = ""; break; } return $game_day;}$cd = date("D");$game_day = find_game_day($cd);echo $game_day ;?>[/code]A shorter alternative would be[code]function find_game_day($current_day){ $game_day = array('Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun', 'Mon'); return array_search($current_day, $game_day);}[/code] Quote Link to comment Share on other sites More sharing options...
spelltwister Posted August 20, 2006 Author Share Posted August 20, 2006 well, it will execute assuming it is met, which it's not possible. though they are in my script anyway ( just didn't list them ). Umm... So, can I return multiple things and if I return it does the variable have to be outside the function or is it then initialized and set outside. Good point about passing the $current_day, i forgot about that. everything inside the function needs to be passed or it's a local variable correct?Thanks,Mike Quote Link to comment Share on other sites More sharing options...
Barand Posted August 21, 2006 Share Posted August 21, 2006 You could declare a variable as "global" within a function but better to pass it as an argument.There are 2 ways to return multiple results.1 ) return an array[code]<?phpfunction dayOfBirth1 ($dob) { $result = array(); $tstamp = strtotime ($dob); $result['born'] = date('l', $tstamp); $birthday = mktime(0,0,0,date('m', $tstammp), date('d', $tstamp), date('Y')); $result['birthday'] = date('l', $birthday); return $result;}$res = dayOfBirth1('1949-01-22');echo "You were born on {$res['born']} and this year your birthday is on {$res['birthday']}";?>[/code]2 ) pass args by ref[code]<?phpfunction dayOfBirth2 ($dob, &$born, &$birthday) { $result = array(); $tstamp = strtotime ($dob); $born = date('l', $tstamp); $bday = mktime(0,0,0,date('m', $tstammp), date('d', $tstamp), date('Y')); $birthday = date('l', $bday);}$res = dayOfBirth2('1949-01-22', $day, $dayThisYear);echo "You were born on $day and this year your birthday is on $dayThisYear";?>[/code] Quote Link to comment 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.