Welcome Guest, Not a member yet? Register   Sign In
using varibales as a function name
#1

[eluser]A.M.F[/eluser]
hello,

i am using a foreach loop to show several functions. so i want to do it something like this:

Code:
foreach($fields as $k=>$v)
{
     echo $this->validation->$v[0]._error;
}

but this is the message i get:
Quote:Message: Use of undefined constant _error - assumed '_error

why?
#2

[eluser]A.M.F[/eluser]
nvm i used it like this:
Code:
$function_name = $v[0].'_error';
$this->validation->$function_name;
#3

[eluser]Nick Husher[/eluser]
Code:
foreach($fields as $k=>$v) { echo $this->validation->$v[0]._error; }

It's because you didn't put quotes around error in the first example: $this->validation->$v[0].'_error'
#4

[eluser]tonanbarbarian[/eluser]
actually im not sure the quotes would work
Code:
echo $this->validation->{$v[0]._error};
this should work, although I personally use the method AMF showed more often than not despite it meaning more code to run




Theme © iAndrew 2016 - Forum software by © MyBB