Jump to content

functions


spelltwister

Recommended Posts

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
Link to comment
Share on other sites

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]<?php
function 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]
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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]<?php
function 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]<?php
function 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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.

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