Grabbing variable from 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
MajorJerk
Forum Newbie
Posts: 2
Joined: Mon Aug 29, 2005 1:04 pm

Grabbing variable from a function

Post by MajorJerk »

Hello, I'm using this function

Code: Select all

function current_dir()
{
$path = dirname($_SERVER[PHP_SELF]);
$position = strrpos($path,'/') + 1;
print substr($path,$position);
}
substr($path,$position);
To grab the part of the current url that I need. The print line prints out the part I want just fine.

My question is, how can I use that extracted part as a variable to use in a sql queiry?
I tried $nm = substr($path,$position); but $nm is empty. :?:

Thanks. :D
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

neither $path nor $position are sent outside the function. You need to return the substring result.

Code: Select all

function current_dir()
{
  $path = dirname($_SERVER['PHP_SELF']);
  $position = strrpos($path,'/') + 1;
  return substr($path,$position);
}

$dir = current_dir();

If you want to get the current directory on the server's paths: getcwd() may help.
MajorJerk
Forum Newbie
Posts: 2
Joined: Mon Aug 29, 2005 1:04 pm

Post by MajorJerk »

Perfect! tyvm. :D
Post Reply