Jump to content

[SOLVED] embedding variable into function name not working???


darksniperx

Recommended Posts

<?php
$name = 'Entry';
include 'test.php';-> I would like to change it to-> include '$name.php';
show.$name.();
?>

 

I would like to automate my php code and reduce at the same time. I pass one variable that contains text value, so that php would use that variable name inside include and function name. It should be possible?

You can easily use a variable to include a file....

 

<?php

  $name = 'Entry';
  include $name . '.php';

?>

 

But dynamically calling a function is a little more difficult....

 

<?php

  $name = 'Entry';
  include $name . '.php';
  call_user_func('show' . $name);

?>

<?php
function funcA ()
{
    echo "<p>A called</p>";
    
}

function funcB ()
{
    echo "<p>B called</p>";
    
}

if (isset($_GET['func']))
{
    $x = 'func'.$_GET['func'];
    
    $x();                               // call chosen dynamic function
}
?>
<form>
Call <input type="radio" name="func" value="A"> A <input type="radio" name="func" value="B"> B
<input type="submit" name="sub" value="Call">
</form>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.