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
Find where variable is defined
Moderator: General Moderators
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Re: Find where variable is defined
A little bored this morning
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';
}Re: Find where variable is defined
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.
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.
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Re: Find where variable is defined
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
You are better off using grep though if you do not need do this programmatically
Re: Find where variable is defined
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' *
I'm using this syntax -
grep -o '$mosConfig_db' *
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Re: Find where variable is defined
Thanks JCart. I was able to write the syntax.
Just for reference i used - grep -ro '$varname' *
Just for reference i used - grep -ro '$varname' *
Re: Find where variable is defined
Btw u can mark this closed from my side!