CodeIgniter Forums
using varibales as a function name - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: using varibales as a function name (/thread-4749.html)



using varibales as a function name - El Forum - 12-13-2007

[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?


using varibales as a function name - El Forum - 12-13-2007

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



using varibales as a function name - El Forum - 12-13-2007

[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'


using varibales as a function name - El Forum - 12-13-2007

[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