Jump to content


Photo

php function help?


  • Please log in to reply
3 replies to this topic

#1 nochlux

nochlux

    Newbie

  • Members
  • Pip
  • 5 posts

Posted 14 May 2013 - 09:17 PM

Suppose I have this code

$num = $_SESSION['count'];
$_SESSION['a'][$num] = $a_letter;
$_SESSION['b'][$num] = $b_letter;
$_SESSION['c'][$num] = $c_letter;
$_SESSION['d'][$num] = $d_letter;
$_SESSION['e'][$num] = $e_letter;
$_SESSION['f'][$num] = $f_letter;



if(!exists) {

$num = $_SESSION['count'];
$_SESSION['a'][$num] = $a_letter;
$_SESSION['b'][$num] = $b_letter;
$_SESSION['c'][$num] = $c_letter;
$_SESSION['d'][$num] = $d_letter;
$_SESSION['e'][$num] = $e_letter;
$_SESSION['f'][$num] = $f_letter;
}


else {
$num = $_SESSION['count'];
$_SESSION['a'][$num] = $a_letter;
$_SESSION['b'][$num] = $b_letter;
$_SESSION['c'][$num] = $c_letter;
$_SESSION['d'][$num] = $d_letter;
$_SESSION['e'][$num] = $e_letter;
$_SESSION['f'][$num] = $f_letter;

}

Now I don't want those codes to copy paste repetitively, but instead I want to come up with a function - So I probably have to create a function in php then??


function list ()

{
$num = $_SESSION['count'];
$_SESSION['a'][$num] = $a_letter;
$_SESSION['b'][$num] = $b_letter;
$_SESSION['c'][$num] = $c_letter;
$_SESSION['d'][$num] = $d_letter;
$_SESSION['e'][$num] = $e_letter;
$_SESSION['f'][$num] = $f_letter;

}

$value = function list();

if (!exists)

{
$value

}


else {

$value

}

How does this work please?

Edited by nochlux, 14 May 2013 - 09:18 PM.


#2 davidannis

davidannis

    Freak Dr.

  • Members
  • PipPipPip
  • 457 posts
  • LocationOkemos, MI USA

Posted 14 May 2013 - 09:27 PM

I can't follow what you are doing. Looks to me like you do the same thing in your if{} and your else{} Then you create a function that returns nothing. Sorry I can not help more.



#3 requinix

requinix

    Playful Member

  • Moderators
  • 5,743 posts
  • LocationWA

Posted 14 May 2013 - 09:29 PM

...You're doing the exact same thing in not just three places but in both branches of an if. It's not a problem of code reuse but of code redundancy.

#4 Q695

Q695

    Advanced Member

  • Members
  • PipPipPip
  • 656 posts
  • LocationMN

Posted 14 May 2013 - 10:28 PM

Do it on your frame layout, so it is on all pages by default.


if ($problem==mark_solved){

solution ($problem);}

 

if ($skill_level==learning && $my_knowledge==the_required_level_of_knowledge){

     echo "I will try to help, because you're still $skill_level , and my skill level is at $my_knowledge";

} else {

     echo "I can't help you, go away.";

}





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com