Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call a function on a variable in PHP?

Tags:

php

I am having to do:

$sourceElement['description'] = htmlspecialchars_decode($sourceElement['description']);

I want to avoid that redundant mention of the variable name. I tried:

htmlspecialchars_decode(&$sourceElement['description']);

and

call_user_func('htmlspecialchars_decode', &$sourceElement['description']);

That did not work. Is this possible in PHP? Call a function on a variable?

like image 776
Aditya M P Avatar asked Dec 15 '12 22:12

Aditya M P


People also ask

How do you call a method with a variable name?

You could try something like: Method m = obj. getClass(). getMethod("methodName" + MyVar); m.

How can I access one function variable from another function in PHP?

You could set a return value from the first function which could either be passed as a parameter to the second function or declared, within the second function, as a global.

What is a variable function in PHP?

Variable functions ¶ This means that if a variable name has parentheses appended to it, PHP will look for a function with the same name as whatever the variable evaluates to, and will attempt to execute it. Among other things, this can be used to implement callbacks, function tables, and so forth.

Can we call a function inside a function in PHP?

PHP has a huge collection of internal or built-in functions that you can call directly within your PHP scripts to perform a specific task, like gettype() , print_r() , var_dump , etc.


1 Answers

You could create your own wrapper function that takes the variable by reference:

function html_dec(&$str) {$str = htmlspecialchars_decode($str);}

Then call:

html_dec($sourceElement['description']);
like image 69
Niet the Dark Absol Avatar answered Sep 27 '22 21:09

Niet the Dark Absol