darksniperx Posted October 21, 2007 Share Posted October 21, 2007 <?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? Link to comment https://forums.phpfreaks.com/topic/74243-solved-embedding-variable-into-function-name-not-working/ Share on other sites More sharing options...
trq Posted October 21, 2007 Share Posted October 21, 2007 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); ?> Link to comment https://forums.phpfreaks.com/topic/74243-solved-embedding-variable-into-function-name-not-working/#findComment-375061 Share on other sites More sharing options...
darksniperx Posted October 21, 2007 Author Share Posted October 21, 2007 nice!!!!, I have one ,more question, it is possible to pass a parameter with call_user_func('show' . $name); to be something like this showEntry($value); or I have to hard code to pass a parameter? Link to comment https://forums.phpfreaks.com/topic/74243-solved-embedding-variable-into-function-name-not-working/#findComment-375066 Share on other sites More sharing options...
derwert Posted October 21, 2007 Share Posted October 21, 2007 http://www.php.net/manual/en/function.call-user-func.php <?php $name = 'Entry'; include $name . '.php'; call_user_func('show' . $name, $value); ?> Link to comment https://forums.phpfreaks.com/topic/74243-solved-embedding-variable-into-function-name-not-working/#findComment-375068 Share on other sites More sharing options...
darksniperx Posted October 21, 2007 Author Share Posted October 21, 2007 I did check php.net, I guess I have missread. Anyways, thx, everething works now. Link to comment https://forums.phpfreaks.com/topic/74243-solved-embedding-variable-into-function-name-not-working/#findComment-375069 Share on other sites More sharing options...
Barand Posted October 21, 2007 Share Posted October 21, 2007 <?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> Link to comment https://forums.phpfreaks.com/topic/74243-solved-embedding-variable-into-function-name-not-working/#findComment-375071 Share on other sites More sharing options...
darksniperx Posted October 21, 2007 Author Share Posted October 21, 2007 interesting, havent thought about it this way, I guess php is very robust. Just hope it will work with php 4, I'll work with both of them, each one has a very unique use for me. Link to comment https://forums.phpfreaks.com/topic/74243-solved-embedding-variable-into-function-name-not-working/#findComment-375073 Share on other sites More sharing options...
Barand Posted October 21, 2007 Share Posted October 21, 2007 Works in 4.3.9 and 5.1.2 Link to comment https://forums.phpfreaks.com/topic/74243-solved-embedding-variable-into-function-name-not-working/#findComment-375076 Share on other sites More sharing options...
darksniperx Posted October 21, 2007 Author Share Posted October 21, 2007 I should have 4.3.9 or something higher, I worked when I tested it out. Link to comment https://forums.phpfreaks.com/topic/74243-solved-embedding-variable-into-function-name-not-working/#findComment-375078 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.