/    Sign up×
Community /Pin to ProfileBookmark

How to know php variable name

Do u know how to get a php variable name ?

Suppose, I have defined a variable as $myvar=”my variable”;

I want to get the variable name by a function as below :

is_set($var){
if(isset($var)){
return $var; // I want to return the variable name not the value
}else{
return NULL;
}
}

for the purpose like :
$action=is_set($_REQUEST[‘view’]);
$action=is_set($_
REQUEST[‘add’]);

switch($action){
case “view” : include “action.view.php”; break;
case “add” : include “action.new.php”; break;
default : include action.default.php”; break;
}

to post a comment
PHP

10 Comments(s)

Copy linkTweet thisAlerts:
@chazzyDec 26.2006 — i'm not sure what you mean.

are you trying to say if view is set on the request, give them action.view page, if add is set in the request give them action.new and if neither is set give them action.default?
Copy linkTweet thisAlerts:
@NightShift58Dec 27.2006 — There are ways to retrieve variable names as opposed to values - to that end, read: http://cr.php.net/manual/en/language.variables.php

However, I don't think that you would really need to go through the trouble, especially not by looking at the example you provided... In general, you'll be better off if you control the names of variables in your script rather than try to find out how they are called afterwards. Variable names don't just happen...

You could do something like this:[code=php]IF (isset($_REQUEST['view'])) :
$action = "view";
ELSEIF (isset($_REQUEST['add'])) :
$action = "new";
ELSE :
$action = "default";
ENDIF;

include"action.$action.php";
[/code]
Copy linkTweet thisAlerts:
@phpshift2Dec 27.2006 — why not just do this:
[code=php]
<?php
is_set($var){
if(isset($var)){
return "$var"; // I want to return the variable name not the value
}else{
return NULL;
}
}
?>
[/code]
Copy linkTweet thisAlerts:
@rubin_sthauthorDec 28.2006 — Thanks to NightShift58 (Pura vida) for the advice. But my need is exactly that. I know the alternative u gave me and I've already given the exam code above too (hope u got my idea). I need to know the variable name exactly becoz i'm not counting on what the value the variable holds but instead i want to include the acc. page defined by the variable. See the ex.

1. purpose.php?page=country&add

2. purpose.php?page=country&view

3. purpose.php?page=country&delete

4. purpose.php?page=location&add

5. purpose.php?page=location&view etc.

the only meaning is that i don't want to use something like this below :

purpose.php?page=location&action=view&add=add

becoz I have set a hidden field which is set when the add form is posted

I hope u got my idea... yea, and the link u gave me gives http 404 error, is there any other link ?
Copy linkTweet thisAlerts:
@pcthugDec 28.2006 — [code=php]
$actions = array('add', 'view', 'delete'); // put all the valid actions here

foreach ($actions as $key => $value)
{
if (isset($_GET[$key]))
{
$action = $key;
break;
}
}
[/code]
Copy linkTweet thisAlerts:
@NightShift58Dec 28.2006 — I think this link should work: http://www.php.net/manual/en/language.variables.php

In the preceding example (by PcThug), if that's what you're looking for, we may have a problem. The variable $action will resolve to 0,1,2,... as opposed to "add", "view", "delete",...

You may want to change the loop to:[code=php]foreach ($actions as $key) {
if (isset($_GET[$key])) {
$action = $key;
break;
}
}[/code]
Otherwise, I don't think I understand what you're really looking for.
Copy linkTweet thisAlerts:
@pcthugDec 28.2006 — Oops, I forgot the array elements were uninitiated when I was writing the foreach statement. :rolleyes: Use NightShift's code. You may also want to assign the [I]action[/I] variable a default value, [B]view[/B] per se.
Copy linkTweet thisAlerts:
@NightShift58Dec 28.2006 — After reading your question once more, I think - maybe - I understand...[code=php]<?php
$QUERYstring = $_SERVER['QUERY_STRING'];

$QUERYarray = explode("&", $QUERYstring);

FOREACH ($QUERYarray as $QUERYparameter) :

$tmpARRAY = explode("=", $QUERYparameter);
$QUERYvariable = $tmpARRAY[0];

print $QUERYvariable . "<br>";

ENDFOREACH;
?>[/code]
Are you looking for something like this? This code would give you the names of the parameters ("variables") used in the URL, regardless of whether they were assigned a value or not. That would be, though, the functional equivalent - more or less - of looping through the $_GET array, as below, which is the version I would prefer.[code=php]FOREACH ($_GET as $key => $value) :
print $key . "<br>";
ENDFOREACH;
[/code]
Is this what you're looking for? Am I even close?
Copy linkTweet thisAlerts:
@pcthugDec 28.2006 — Or,
[code=php]
print_r($_GET);
[/code]

Or you may want to have a look at the [url=http://www.php.net/manual/en/function.parse-str.php]parse_str[/url] function.
Copy linkTweet thisAlerts:
@NightShift58Dec 28.2006 — I used the individual print statements as placeholders for the instructions he would want to execute once he obtains the URL parameter names.

Instead of [B]print $key;[/B], we would likely end up with something like [B]$action = $key;[/B] - I think.... because I'm not sure I fully grasp the needs in the OP...
×

Success!

Help @rubin_sth spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...