variables and function within a function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bkk_mike
Forum Newbie
Posts: 1
Joined: Tue Jun 22, 2004 6:10 am

variables and function within a function

Post by bkk_mike »

I have a piece of code like this:
function a()
{
$a="some value"
$b="some other value"
function b($someArg)
{.....}
function c($someArg)
}
in function b and c, I would like to access the values of $a and $b.
how can I do that?
Thanks a bunch in advance :-)
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

Code: Select all

<?php
global $a, $b;
?>
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

or

Code: Select all

function a(){
   ...
   ...
   return array($a,$b);
}

function b(){
  list(a,b)=a();
}
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Return values from functions rather than declaring globals. If you get into the habit of declaring globals, it's harder to trace back where a variable gets it's value set.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply