I'm trying to call a function with a php variable variable. You'll see in my code in function mainFunction(). If it's not possible to do it this way, is there a better way to do it, that avoids any more code? I wish it would work this way.
<?php
$a = 1;
$b = 1;
if ( $a == $b ) {
$exampleFunction = 'exampleOne';
} else {
$exampleFunction = 'exampleTwo';
}
//----------------------------------------------
mainFunction();
function mainFunction() {
global $exampleFunction;
echo 'This is mainFunction <br>';
$$exampleFunction();//Here's where I'm stuck.
}
function exampleOne() {
echo 'This is example one <br>';
}
function exampleTwo() {
echo 'This is example two <br>';
}
?>
A way to solve this problem would be to use PHP's call_user_func function. Here is the modified code (it also removes the global variable):
Code Example
<?php
$a = 1;
$b = 1;
// I'm just using this to hold the function name,
// to get rid of the global keyword. It will be passed
// as an argument to our mainFunction()
$exampleFunction = '';
if ($a == $b) {
$exampleFunction = 'exampleOne';
} else {
$exampleFunction = 'exampleTwo';
}
//----------------------------------------------
mainFunction($exampleFunction);
function mainFunction($func) {
echo 'This is mainFunction <br>';
// Use PHP's call_user_func. We are also checking to make sure
// the function exists here.
if (function_exists($func)) {
// This will call the function.
call_user_func($func);
}
}
function exampleOne() {
echo 'This is example one <br>';
}
function exampleTwo() {
echo 'This is example two <br>';
}
Output
When I run this code, it produces the following output:
This is mainFunction
This is example two
Try like
if ( $a == $b ) {
$exampleFunction = exampleOne();
} else {
$exampleFunction = exampleTwo();
}
and your functions should return like
function exampleOne() {
return 'This is example one <br>';
}
function exampleTwo() {
return 'This is example two <br>';
}
OR if you want to call them through the variable try to replace like
function mainFunction() {
global $exampleFunction;
echo 'This is mainFunction <br>';
$exampleFunction();
}
Try with $exampleFunction(); instead of $$exampleFunction();
OR
use call_user_func($exampleFunction)
check this way :-
function mainFunction() {
global $exampleFunction;
echo 'This is mainFunction <br>';
$exampleFunction();
}
Use just $exampleFunction, without $$:
<?php
function mainFunction() {
global $exampleFunction;
echo 'This is mainFunction <br>';
$exampleFunction();
}
?>
See manual of variable functions, not variable variables.
P.S.: Also, I suggest $exampleFunction to be an argument of mailFunction, rather than use globals.
Related
Sorry for the rather poor title, but hopefully I can explain it with some code.
So lets say I have the following functions:
<?php
function helloTest()
{
echo 'hello';
}
function worldTest()
{
echo 'world';
}
function helloworld()
{
// call all functions with 'Test'
}
?>
Is it possible for the helloworld function to call all functions which are named at the end as 'Test'?
$funcs = get_defined_functions();
foreach( $funcs['user'] as $f ) {
if( strstr($f, 'Test') )
call_user_func($f);
}
You should use Get All methods php function to get all methods like below
function helloTest()
{
echo 'hello';
}
function worldTest()
{
echo 'world';
}
function helloworld()
{
// call all functions with 'Test'
$methods = get_defined_functions();
$user_defined_methods = $methods['user'];
foreach ($user_defined_methods as $method_name)
{
//check with regular expressions if it's having 'Test' at end of $method_name then call that function using call_user_func($method_name)
}
}
for more info check this link
This is my first program in oop php. Its very simple where i would like to add a numerical value to a variable. And program must output 2.
<?php
class MyClass
{
public $a = 1;
public function abc()
{
if ($a=1){
$a+1;
}
}
}
$obj = new MyClass;
echo $obj->abc;
?>
In addition to gview's answer:
if ($a=1){
$a+1;
}
Should be:
if ($a == 1){
$a = $a + 1;
}
The = operator is for assignment, not for comparisons.
The abc() function does not return anything. Thus you get no output. If you add:
return $a;
You'll get something in the echo.
You aren't returning your results
public function abc()
{
if ($a==1){
$a++;
}
return $a;
}
I think you forgot to return the value from abc()
public function abc()
{
if ($a=1){
$a+1;
}
return $a;
}
function nothing() {
echo $variableThatIWant;
}
You can put "global" before the variable you want to use, Like this :
<?php
$txt = "Hello";
function Test() {
global $txt;
echo $txt;
}
Test();
?>
OR :
you can passed it as parameter, Like this :
<?php
$txt = "Hello";
function Test($txt) {
echo $txt;
}
Test($txt);
?>
source : http://browse-tutorials.com/tutorial/php-global-variables
The better way is to pass it as an argument.
function nothing($var) {
echo $var;
}
$foo = 'foo';
nothing($foo);
The evil way, and I dont know why I'm even showing you this, is to use global.
function nothing() {
global $foo;
echo $foo;
}
$foo = 'foo';
nothing();
You have to use global.
$var = 'hello';
function myprint()
{
global $var;
echo $var;
}
You can also use a class property (or member variable) if you are inside a class:
<?php
$myClass = new MyClass();
echo $myClass->nothing();
class MyClass {
var $variableThatIWant = "something that I want";
function nothing() {
echo $this->variableThatIWant;
}
}
Codepad example
You can pass it by reference if you want to modify it inside the function without having to return it:
$a = "hello";
myFunction($a);
$a .= " !!";
echo $a; // will print : hello world !!
function myFunction(&$a) {
$a .= " world";
}
Codepad example
I need a function that will take a string as an argument, then check to see if a variable named the same thing as that string is set.
This works...
$foo = 'foosuccess';
$property = 'foo';
if(isset($$property)){
echo $$property;
}
This doesn't, because within test(), $$property2 is the wrong scope.
$huh = 'huhsuccess';
$huh = test("huh");
function test($property2){
if(isset($$property2)){
echo $$property2;
}
}
How can I fix the function so $$property2 refers to the same scope as the caller's context? Is that possible?
Thanks in advance....
try this:
$huh = 'huhsuccess';
test("huh");
function test($property2) {
global $$property2;
if(isset($$property2)) {
echo $$property2;
}
}
<?php
function test($s)
{
return isset($GLOBALS[$s]);
}
ok, i think i figured it out for my purposes (if anyone's interested...)
//uncomment to get success
//$huh = 'huhsuccess';
$huh = test($huh);
echo $huh;
function test(&$property2) {
if(isset($property2)) {
return $property2;
} else {
return 'not set!';
}
}
die;
This can be done with eval():
$foo = 'foosuccess';
$property = 'foo';
if(eval('isset($'.$property.')'){
echo $$property;
}
I want to be able to call a function, that will set one or more local variables in the calling function. For instance:
function someFunc () {
loadTranslatedStrings($LOCALS, "spanish");
echo $hello; // prints "hola";
}
function loadTranslatedStrings (&$callerLocals, $lang) {
if ($lang == 'spanish')
$callerLocals['hello'] = 'hola';
else if ($lang == 'french')
$callerLocals['hello'] = 'bonjour';
else
$callerLocals['hello'] = 'hello';
}
(I'm guessing it is impossible to do this, but might as well ask...)
You could do this...
function someFunc () {
loadTranslatedStrings($lang, "spanish");
extract($lang);
echo $hello; // prints "hola";
}
CodePad.
The closest I think you could get is by using extract:
function someFunc()
{
extract(loadStrings('french'));
echo $hello;
}
function loadStrings($lang)
{
switch($lang)
{
case 'spanish':
return array('hello' => 'hola');
case 'french':
return array('hello' => 'bonjour');
}
}
You can do that using $GLOBALS: $GLOBALS['hello'] = 'hola';