How would I call a php function from a variable.
For example:
$variable = functionname;
so when calling $variable this will return functionname();
I have tried the following:
$variablereturn = $variable."(sometext)";
print $variablereturn;
and
print $variable."(". print $variabletext. ");";
neither seem to work.
Just use the following:
$variable = 'function_name';
$variable('your_argument');
Documentation
Related
I created a function to check for special characters in a string, but I can't seem to get it to echo the response message
Here is my code
<?php
function chk_string($string){
if (preg_match('/[\^£$%&*()}{##~?><>|=_+¬-]/', $string))
{
$chk_str="false";
} else {
$chk_str="true";
}
return $chk_str;
}
$string="this is just a test" ;
chk_string($string) ;
echo $chk_str;
?>
The "echo $chk_str" is not echoing anything.
If you did
$chk_str = chk_string($string);
then you could echo $chk_str;.
The $chk_str you are trying to echo is only visible in your function.
More description:
Your function (chk_string) is in a different scope than your echo.
Your function returns a variable, but that variable is "lost", since you don't assign it to another variable.
Your code currently does something like this line by line:
Your function declaration
$string means "this is just a test"
your functions result as a string, just floating in the code
write out a variable that doesn't exist.
I hope this helped in someway.
You need to store returned value in a particular variable if you want to echo that variable like this,
$chk_str = chk_string($string) ;
echo $chk_str;
Another way you can just directly echo returned value like this,
echo chk_string($string) ;
Your question is about variable scope and it is answered already, but I would recommend you to take a look at variable scope here https://www.php.net/manual/en/language.variables.scope.php.
Basically, every variable has its scope and we can not access a variable outside its scope. In your case, scope of variable $chk_str is inside function chk_string so you can not access it outside of the function. Because you return value of $chk_str in function chk_string so you still can access its value through response of function chk_string, for example:
echo chk_string('a string');
OR
$result = chk_string('a string');
echo $result;
i have following code.
$hello = "World";
$test = "hello";
echo $$test;
When I execute this I get as result: World
So far as good
But when I use a reserved variable, for example $_GET it doesn't work.
$test = "_GET";
var_dump($$test);
Here the result is NULL. Is there a way to get values of a reserved variable throught a variable variable?
Superglobals can only be dereferenced by variable variables in the global scope. The fact you can't get it to work seems to indicate that your code is in a function/method. In this case, you can use the $GLOBALS superglobal:
function foo() {
$str = '_GET';
var_dump($GLOBALS[$str]);
}
foo();
this is probably a basic question about php, but I can't figure out how to solve it.
Well, I have a file called globals.php
<?php
$DATA = array(
'first' => 'LOL',
'second' => 'Whatever'
);
?>
And I also have another file (omg.php) with the following function:
<?php
require_once('globals.php');
function print_text_omg($selector = 0){
global $DATA; //added this line of code. NOW IT WORKS
$var = '';
if($selector == 0){
$var = '';
}else{
$var = 'Hi. ';
}
//$DATA is a variable from globals.php that is supposed to be declared in require_once('globals.php');
//$var is a variable inside the function print_text_omg
//I am trying to concatenate string $var with the string $DATA['first']
$finaltext = $var.$DATA['first'];
echo $finaltext;
}
?>
Then, in main.php I have this:
<?php
include('omg.php');
print_text_omg();
print_text_omg(1);
?>
This should print something like:
//LOL
//Hi. LOL
Instead, I have this warning:
Notice: Undefined variable: DATA in ...
Which is the part of $finaltext = $var.$DATA['first'];
UPDATE
Thanks to user Casimir et Hippolyte' suggestion, I've edited my function and it works now. Added the line that worked for me.
It doesn't work because $DATA isn't in the scope of your function.
To make $DATA available inside your function, you must pass it as a parameter to the function or define $DATA as a global variable.
The problem isn't related to require_once.
http://php.net/manual/en/language.variables.scope.php
The error is correct, first I can't find where did you declare $var (maybe it should be an object of some class?) and also $var doesn't contain the $DATA array, and for last, . operator in php is for concatenate strings, for object navigation is -> operator
You haven't declared the variable $var, plus . is php's concatenation operator. Try changing your code to this
$finaltext = $DATA['first'];
I was playing around with anonymous functions in PHP and realized that they don't seem to reach variables outside of them.
Is there any way to get around this problem?
Example:
$variable = "nothing";
functionName($someArgument, function() {
$variable = "something";
});
echo $variable; //output: "nothing"
This will output "nothing". Is there any way that the anonymous function can access the $variable?
Yes, use a closure:
functionName($someArgument, function() use(&$variable) {
$variable = "something";
});
Note that in order for you to be able to modify $variable and retrieve the modified value outside of the scope of the anonymous function, it must be referenced in the closure using &.
If your function is short and one-linear, you can use arrow functions, as of PHP 7.4:
$variable = "nothing";
functionName($someArgument, fn() => $variable = "something");
I'm 'dissecting' PunBB, and one of its functions checks the structure of BBCode tags and fix simple mistakes where possible:
function preparse_tags($text, &$errors, $is_signature = false)
What does the & in front of the $error variable mean?
It means pass the variable by reference, rather than passing the value of the variable. This means any changes to that parameter in the preparse_tags function remain when the program flow returns to the calling code.
function passByReference(&$test) {
$test = "Changed!";
}
function passByValue($test) {
$test = "a change here will not affect the original variable";
}
$test = 'Unchanged';
echo $test . PHP_EOL;
passByValue($test);
echo $test . PHP_EOL;
passByReference($test);
echo $test . PHP_EOL;
Output:
Unchanged
Unchanged
Changed!
It does pass by reference rather than pass by value.
This allows for the function to change variables outside of its own scope, in the scope of the calling function.
For instance:
function addOne( &$val ) {
$val++;
}
$a = 1;
addOne($a);
echo $a; // Will echo '2'.
In the case of the preparse_tags function, it allows the function to return the parsed tags, but allow the calling parent to get any errors without having to check the format/type of the returned value.
It accepts a reference to a variable as the parameter.
This means that any changes that the function makes to the parameter (eg, $errors = "Error!") will affect the variable passed by the calling function.
It means that the variable passed in the errors position will be modified by the called function. See this for a detailed look.