Jump to content


Photo

functions


  • Please log in to reply
3 replies to this topic

#1 spelltwister

spelltwister
  • Members
  • PipPipPip
  • Advanced Member
  • 41 posts

Posted 20 August 2006 - 10:54 PM

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

#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 20 August 2006 - 11:11 PM

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

<?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 ;
?>

A shorter alternative would be
function find_game_day($current_day){
  $game_day = array('Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun', 'Mon');
  return array_search($current_day, $game_day);
}

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#3 spelltwister

spelltwister
  • Members
  • PipPipPip
  • Advanced Member
  • 41 posts

Posted 20 August 2006 - 11:25 PM

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

#4 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 21 August 2006 - 12:08 AM

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

<?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']}";
?>



2 ) pass args by ref

<?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";
?>

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users