Use a variable to define a PHP function
What you can do is
$thing = 'some_function';
$$thing = function() {
echo 'hi';
};
$some_function();
In php < 5.3, you'll have to use create_function
instead of the anonymous function:
// Use this in < 5.3
$thing = 'some_function';
$$thing = create_function('',
'echo "hi";'
);
$some_function();
That being said, defining functions with dynamic names is a bad idea. Instead, create an array of functions you call in to, or use polymorphism.
How can I define a local variable in a function in PHP?
function ha(){
global $user;
$user2 = 'abc'; // No prefix whatsoever
echo $user; // Prints global $user
echo $user2; // Prints local $user2
}
how can I define a static value of variable in PHP that is read from file config.php and only access the value in the function?
It's all about variable scope. If you want a variable to be defined in the configuration file and only be readable from within a function, then your current approach is incorrect.
Anything declared in the main scope (such as the configuration file loaded in the main scope) will be accessible from nearly anywhere in the code. If you don't want variables to be accessed otherwise than through ha()
then they need to be defined within ha()
.
PHP define a function output as variable
If you have no access to modify the function get_title
, you can always use output buffers.
<?php
ob_start();
get_title()
$title = ob_get_contents();
The clear solution would be returning the value of title instead of echoing it.
<?php
function get_title() {
$title = // black box //;
return $title;
}
$title = get_title();
As you pointed out, if you don't want the function to output anything onto the screen, you can use ob_end_clean() to destroy any output that get_title() may have.
Use Variable as Function Name in PHP
Declaring a function with a variable but arbitrary name like this is not possible without getting your hands dirty with eval()
or include()
.
I think based on what you're trying to do, you'll want to store an anonymous function in that variable instead (use create_function()
if you're not on PHP 5.3+):
$variableA = function() {
// Do stuff
};
You can still call it as you would any variable function, like so:
$variableA();
PHP function use variable from outside
Add second parameter
You need to pass additional parameter to your function:
function parts($site_url, $part) {
$structure = 'http://' . $site_url . 'content/';
echo $structure . $part . '.php';
}
In case of closures
If you'd rather use closures then you can import variable to the current scope (the use
keyword):
$parts = function($part) use ($site_url) {
$structure = 'http://' . $site_url . 'content/';
echo $structure . $part . '.php';
};
global
- a bad practice
This post is frequently read, so something needs to be clarified about global
. Using it is considered a bad practice (refer to this and this).
For the completeness sake here is the solution using global
:
function parts($part) {
global $site_url;
$structure = 'http://' . $site_url . 'content/';
echo($structure . $part . '.php');
}
It works because you have to tell interpreter that you want to use a global variable, now it thinks it's a local variable (within your function).
Suggested reading:
- Variable scope in PHP
- Anonymous functions
PHP: define functions with variable names
If you really had to you could declare the function within an eval
block:
foreach ($a as $functionname)
eval('
function '.$functionname.' () {
print 123;
}
');
But that incurs some extra parsing time speed penalty over just declaring the functions in a file.
Having a PHP function use a global variable as a default?
You could use a constant. Define it at the top of a file and let your functions use that. E.g.
define('CUSTOM_TIMEZONE', 'Mountain');
function tell_time($values, $timezone = CUSTOM_TIMEZONE) {
// Your code here
}
Just change the constants value and it's changed everywhere.
Use a variable to define PHP function parameters
How you are wanting to do it, you can't. However, look in to call_user_func_array
as it may solve what you are trying to do. Demonstration follows:
function blabla($param1, $param2){
echo "my cool code $param1 $param2";
}
$params = 'does,work';
call_user_func_array('blabla', explode(',', $params));
Related Topics
PHP File Listing Multiple File Extensions
Can't Send Email to Addresses at My Own Domain
Storing Form Data as a Session Variable
How to Use Open Id as Login System
How to Increment Letters Like Numbers in PHP
Upload 1Gb Files Using Chunking in PHP
Dependency Hell - How Does One Pass Dependencies to Deeply Nested Objects
How to Read Large Worksheets from Large Excel Files (27Mb+) with PHPexcel
$_Server["Remote_Addr"] Gives Server Ip Rather Than Visitor Ip
PHP - Convert Multidimensional Array to 2D Array with Dot Notation Keys
Search for PHP Array Element Containing String
Laravel: Connect to Databases Dynamically
Simplest Way to Increment a Date in PHP