in bash there is something like
$_
which is a temporary variable that stores the last argument of previous command
is there a similar variable or construct in PHP or how can I access the last used variable in the script?
this would be very handy for simple debugging functions or if you need a function that you have to add in several places at your code.
Or you could add something like:
do_some_more($_);
in several places in your code
There is no such feature in PHP.
$_ is just like any other variable.
$_ = 'hello world';
echo $_; // hello world
Related
Can I do something like this?
$captcha_results = $_REQUEST[$_REQUEST['field_name']];
I am interested in it, because I try to find out where I've made a mistake.
Thank you in advance.
Yes, of course. The one you've just asked is one of variable variables. You can learn more the PHP variables here:
PHP Variable Basics
Variable in Variable
For example, you have $var containing value of foobar. You can use the value for other references (in my case, as an index of an array), as shown below.
$var = 'foobar';
$array['foobar'] = 'bar';
echo $array[$var];
This will output bar as a result.
I'm rewriting a PHP plugin, and there are functions called $expode_top[] & $expode_bottom[]. I understand what the normal explode function does, but what are these?
It seems to be impossible to find an answer on Google because it replaces the underscore with a space.
Those are array variables, not functions, they just happen to start with a keyword you are familiar with. Anything beginning with a $ is a variable in PHP.
Using [] will put the assigned variable into the "next" position of the array. For example:
$expode_top = array();
$expode_top[] = "testing";
if ( $expode_top[0] == "testing" ){
echo "it does equal testing";
}
As #gwillie rightly commented, it could also be a variable function - the name of the variable is replaced and then that function is executed. Second example:
$expode_top = "echo";
$expode_top("testing");
Is functionally the same as:
echo("testing");
Those are array variable names, not functions, as indicated by the $ and []. Also, neither exists as a function.
I have a very simple question. But is really making me crazy.
I have a statement say:
example and example with one php variable like $loggedin_user_name
First of all, I want to store the above sentence in MySQL database and then take it back whenever I want to print the above statement. It seems that their is no issue.
But when I tried to print data after extracting from database it is printing the same statement. But i guess, it has to print the logged in user name instead of $loggedin_user_name in the above statement.
So, is it possible to print the variable within the variable? If yes, please suggest a way.
use sprintf()
$str = "example and example with one php variable like %s";
Then load it from database and fill
$out = sprintf($str, $loggedin_user_name);
If it is always the same variable name, I would suggest using
echo str_replace($fromDb, '$variableToReplace', $variableToReplace);
You can use preg_match to find you variable name in string and then replace it with str_replace.
$name = "ABC";
$bla = "$name";
echo $bla; //ABC
Will always be "ABC", because PHP is evaluating your variable when asigning to $bla.
You can use single-quotes to avoid that behaviour (like $bla='$name'; //$name) or you quote the $-sign (like $bla="\$name"; //$name). Then you can store your string like you wanted into your database.
But you can not (only when using eval(), wich you MUST NOT DO in good PHP-Code) build this behaviour, that php has, when printing fulltext.
Like Mentioned in another answer, you should use printf or sprintf and replace the $loggedin_user_name with %s (for "string).
Best would be to concatinate a string:
$exampleWithUsername = 'example' . $loggedin_user_name;
echo $exampleWithUsername;
'example' is a hardcoded string, but you can give it a variable containing string $example, or directly concatinate $username into $example.
You can use eval function, it can be used like your example:
$loggedin_user_name = 'bilal';
$str = "example and example with one php variable like $loggedin_user_name";
eval("\$str = \"$str\";");
echo $str;
Cons:
If your str variable or string/code which you give to eval as a parameter is filled by users, this usage creates a vulnerability.
In case of a fatal error in the evaluated code, the whole script exits.
I have the following code in a website.
$magic_number_output = 'The magic number is: $magic_number';
function show_magic_number($magic_number) {
global $magic_number_output;
eval("\$magic_number_output = \"$magic_number_output\";");
echo '
' . $magic_number_output;
}
The variable $magic_number_output is being set in a separate PHP file, then referenced using global inside a function as there will be different versions of this variable for different languages. $magic_number is passed into the function and this is replaced in the variable by using eval(). However, when the function is called more than once, the $magic_number does not change from the first value sent to the function. For example, if called like this:
show_magic_number(5);
show_magic_number(2000);
...the output is like this:
The magic number is: 5
The magic number is: 5
...when I would like it to be this:
The magic number is: 5
The magic number is: 2000
Obviously, I've misunderstood how global and eval() works and have tried searching for answers and experimenting but am simply too much of a noob to figure out a solution. I thought there might have been some way of resetting the global variable each time so that it doesn't continue to reference the $magic_number (i.e. 5) sent to the function the first time.
Can anyone suggest a way around this?
Try this. It will at least get rid of the eval()
$magic_number_output = 'The magic number is: $magic_number';
function show_magic_number($magic_number) {
global $magic_number_output;
$output=$magic_number_output;
echo str_replace('$magic_number',$magic_number,$output);
}
Don't use eval, take a look at the sprintf function:
$magic_number_output = 'The magic number is: %d';
function show_magic_number($magic_number) {
global $magic_number_output;
echo sprintf($magic_number_output, $magic_number);
}
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What's an actual use of variable variables?
i saw the concept of variable variables in php .
that is a variable whose name is contained in another variable .
like this
$name = ’foo’;
$$name = ’bar’;
echo $foo;
// Displays ’bar’
one advantage that i see is , you can create variable names with numbers or lettes which you can not use normally . like this
$name = ’123’;
/* 123 is your variable name, this would normally be invalid. */
$$name = ’456’;
// Again, you assign a value
echo ${’123’};
// Finally, using curly braces you can output ’456’
and also you can call some functions like this
function myFunc() {
echo ’myFunc!’;
}
$f = ’myFunc’;
$f(); // will call myFunc();
and in a book i saw this
Variable variables are a very powerful tool, and should be used with
extreme care, not only because they can make your code difficult to
understand and document, but also because their improper use can lead
to some significant security issues.
The problem i have is if using variable variables in the code can be that dangerous . why are we using it . and is there any major advantages using variable variables in my code , if any what are those advantages .
you can use it in case of not-user-input. Look
function getVar($gid){
$name = "gid".$gid;
global $$name;
$var = $$name;
return $var;
}
It's useful when you have a lot of variables (same start with ending number, etc...) which is probably save
Look at your example:
function myFunc() {
echo ’myFunc!’;
}
$f = ’myFunc’;
$f(); // will call myFunc();
It is powerful: $f can have a value dynamically and the function called based on this value.
At the same time: If the user was given limitless access to $f this could be a security threat
In some MVC Frameworks they use it to run a function which will vary depending on the URL:
http://www.domain.com/thecontroller/theaction
in PHP side they will parse the url, get the second segment which is the name of the function then run it, but how? they will assign to a variable like what you have mentioned:
$toRun = 'theaction';
$toRun();
I've used double and even treble indirect pointers in C, but have never explicitly used variable variables in PHP (but I have used variable functions and classes).
I think it's somehow reassuring that there's more functionality in PHP than I use - and that I can make informed choices about which constructs I do use (I often use explicit references for example).
A nice usage I saw in some framework is using them to access, easier, values passedfrom another script as an array:
$array['test'];
$array['other'];
$array['third_index'];
// or simply $array = array('test','other','third_index');
foreach($array as $k=>$v)
{
$$k = $v;
}
So you could then have $test, $other and $third_index being usable as variables. Pretty handy when dealing with views, for example.