Find where variable is defined

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
coolbung
Forum Newbie
Posts: 8
Joined: Mon Feb 18, 2008 9:37 am

Find where variable is defined

Post by coolbung »

Hi all,

is it possible in PHP to find out where a variable is defined.
I.e. find out the filename and line number where a variable is set.

To be more specific i'm working on an installation of Joomla which has been worked upon by some developer and it seems he has reset the database name somewhere is his code. viz., the $mosConfig_db variable
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Find where variable is defined

Post by John Cartwright »

A little bored this morning :banghead:

Code: Select all

$needle = '$foobar =';
$content = file_get_contents('path/to/file');
preg_match_all('#.*?'. preg_quote($needle) .'#is', $content, $matches);
 
if (isset($matches[0][0])) {
    'Needle found on Line '. count(explode(PHP_EOL, $matches[0][0]));
} else {
    'Needle not found';
}
coolbung
Forum Newbie
Posts: 8
Joined: Mon Feb 18, 2008 9:37 am

Re: Find where variable is defined

Post by coolbung »

Thats my problem,

I dont know where the variable is being defined.
I want to know the filename as well as line number where the variable gets defined.
The filename atleast.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Find where variable is defined

Post by John Cartwright »

use glob() to search the directory, or search these boards for recursive directory iterator (if you need to search subdir's as well).

You are better off using grep though if you do not need do this programmatically
coolbung
Forum Newbie
Posts: 8
Joined: Mon Feb 18, 2008 9:37 am

Re: Find where variable is defined

Post by coolbung »

Thanks for the lead, but i could not find the syntax to make grep scan recursively

I'm using this syntax -
grep -o '$mosConfig_db' *
coolbung
Forum Newbie
Posts: 8
Joined: Mon Feb 18, 2008 9:37 am

Re: Find where variable is defined

Post by coolbung »

Thanks JCart. I was able to write the syntax.

Just for reference i used - grep -ro '$varname' *
coolbung
Forum Newbie
Posts: 8
Joined: Mon Feb 18, 2008 9:37 am

Re: Find where variable is defined

Post by coolbung »

Btw u can mark this closed from my side!
Post Reply