What does this line mean?
if ( ${fun("str1")} (fun("str2"), ${fun("str3")}) )
Evaluate function returning_value_for_str1_of_fun()_name with the parameters return value for str2 and variable with name return_value_for_str3 ?
What does this line mean?
if ( ${fun("str1")} (fun("str2"), ${fun("str3")}) )
Evaluate function returning_value_for_str1_of_fun()_name with the parameters return value for str2 and variable with name return_value_for_str3 ?
Wow. That's convoluted code. Let's examine it bit by bit:
Let's start with this:
fun("str1")
In fact, this is simply a function call to a function named fun()
, passing in a string value as a parameter.
This function call is repeated three times in your code, with different strings as the arguments. The function fun()
itself is not supplied in your example code, so I can't tell what it does, but given the context I assume it returns a string.
Which leads us onto the next bit we can examine:
${fun("str1")}
The ${...}
syntax in PHP takes the contents of the braces and references a variable of that name.
So, for example, ${"myvar"}
is the same as saying $myvar
. This is called a dynamic variable name. While it does have its uses, it is a very easy way to write bad code, that is difficult to read, understand or maintain. Your example definitely falls into this category.
However, now that we understand the syntax, it's easy to see that it is taking the string output of the fun()
function call, and turning it into a variable name.
Expanding further, we can rewrite the code as follows to make it clearer:
$var1 = fun("str1");
$var2 = fun("str2");
$var3 = fun("str3");
if ( $$var1 ($var2, $$var3) )
Here, $$var1
is being used as a function name, called with $var2
and $$var3
as parameters.
So in $var1
, we have a function call returning a string that is being referenced as a variable name, which is being called as a function.
We still don't know what fun()
function returns, or whether the variable names that are generated by its return are valid, but we can make some assumptions, as $var1
and $var2
would need to be populated with valid function names in order for your line of code to work at all.
We now have an understanding of the whole line of code, but still not a clear view of what it's trying to acheive (beyond being excessively 'clever' and obtuse).
This is very very poorly written code. It is deliberately obscure, and inefficient (ie it will run slowly).
This tests the return value of the function, whose name is the value in the variable named fun("str1")
, and given the arguments fun("str2")
and the value of the variable named fun("str3")
.
Example:
If fun("str1")
equals "x"
, fun("str2")
equals 34
, and fun("str3")
equals "y"
, then the statement would look like:
if ( $x (34, $y) )
fun("str1")
returns string that should be name of variable and the value of this variable is anonymous function (that probably is not void and returns boolean) that gets two arguments first is return value fun("str2")
and the second is the value of the variable with the name that matches string returned by fun("str3")
.
Some work around:
$func = 'fun';
$str3 = 'str3';
echo ${fun("str1")} (fun("str2"), ${fun("str3")}); // will output 'str2'
function fun($param1, $param2 = ''){
if($param1 == 'str2' || $param1 == 'str3')
return $param1;
elseif($param1 == 'str1')
return 'func';
else
echo ' you are done';
}
Evaluates as follows:
fun("str1") -> 'func'
${fun("str1")} -> $func -> fun
fun("str2") -> 'str2'
fun("str3") -> 'str3'
${fun("str3")} -> $str3
${fun("str1")} (fun("str2"), ${fun("str3")})
=> $func ("str2", $str3)
=> fun("str2", "str3")
=> "str2"
Wow. That's convoluted code. Let's examine it bit by bit:
Let's start with this:
fun("str1")
In fact, this is simply a function call to a function named fun()
, passing in a string value as a parameter.
This function call is repeated three times in your code, with different strings as the arguments. The function fun()
itself is not supplied in your example code, so I can't tell what it does, but given the context I assume it returns a string.
Which leads us onto the next bit we can examine:
${fun("str1")}
The ${...}
syntax in PHP takes the contents of the braces and references a variable of that name.
So, for example, ${"myvar"}
is the same as saying $myvar
. This is called a dynamic variable name. While it does have its uses, it is a very easy way to write bad code, that is difficult to read, understand or maintain. Your example definitely falls into this category.
However, now that we understand the syntax, it's easy to see that it is taking the string output of the fun()
function call, and turning it into a variable name.
Expanding further, we can rewrite the code as follows to make it clearer:
$var1 = fun("str1");
$var2 = fun("str2");
$var3 = fun("str3");
if ( $$var1 ($var2, $$var3) )
Here, $$var1
is being used as a function name, called with $var2
and $$var3
as parameters.
So in $var1
, we have a function call returning a string that is being referenced as a variable name, which is being called as a function.
We still don't know what fun()
function returns, or whether the variable names that are generated by its return are valid, but we can make some assumptions, as $var1
and $var2
would need to be populated with valid function names in order for your line of code to work at all.
We now have an understanding of the whole line of code, but still not a clear view of what it's trying to acheive (beyond being excessively 'clever' and obtuse).
This is very very poorly written code. It is deliberately obscure, and inefficient (ie it will run slowly).
© 2022 - 2024 — McMap. All rights reserved.