0

How to put PHP string in a function name?

for ($i=1;$i<10;$i++) { 
    function my_function_$i() {
    //Parse error: syntax error, unexpected T_VARIABLE, expecting '('        
        include($i.'.php');
    }
}

UPDATE:

OK. closed this question, I shall study for more.

9
  • explain a bit more.. what you are really looking for? Commented Oct 27, 2011 at 13:17
  • 1
    @user973254, I do not want to copy parse the same code. just a little difference. why a down vote? if my question is easier for you, why not help me out? Commented Oct 27, 2011 at 13:17
  • 2
    Please add some more of your code, or describe how you've structured it and how you think the above will help you. Where will you want to call those functions? What are you trying to achieve? In what way does writing my_function_1() involve less copy-pasting than writing include('1.php'), or my_function(1)? Commented Oct 27, 2011 at 13:36
  • 1
    @Nico because those who new to language insists on their wrong setup and refuse to learn the right way. Commented Oct 27, 2011 at 13:38
  • 2
    @ Nico: I downvoted because the question is not likely to be useful for future visitors, quite the contrary, they may get an idea that this is the right way to do something. In it's current form the question is nearly useless. If the OP could say what he was trying to achieve people could really help him and future visitors with similar problems. In that case I would change my vote. Commented Oct 27, 2011 at 14:51

4 Answers 4

8

there is something utterly wrong with your architecture if you come to a question like this.
it seems you do not understand what functions are for.

  1. there should be no functions like my_function_$i() but one function my_function($i)
  2. There should be no enumerated includes as well. What are these php files for?
Sign up to request clarification or add additional context in comments.

6 Comments

so, it is impossible write string in a a function name?
it is possible but you don't need it for sure. and you have to learn regular functions first.
I have not found a example to write regular in function name. excuse me for my hard head.
cj333: The point Col. Shrapnel is making is that you probably shouldn't be calling separate functions, but in stead calling the same function with different arguments.
@cj333 It sounds like you would be better off asking a more general question about the architecture of your code.
|
1

Check this out - maybe what you're looking for

http://php.net/manual/en/function.create-function.php

from that page:

<?php
$newfunc = create_function('$a,$b', 'return "ln($a) + ln($b) = " . log($a * $b);');
echo "New anonymous function: $newfunc\n";
echo $newfunc(2, M_E) . "\n";
// outputs
// New anonymous function: lambda_1
// ln(2) + ln(2.718281828459) = 1.6931471805599
?>

6 Comments

Anonymous functions would work, but it's probably just going to lead to more confusion and frustration for someone who's learning the basics of the language..
You need a hug Col. Shrapnel? :)
@Col - Shame about your comments... Maybe we're all learning here. No need to be rude. I'm sure you don't know everything about everything ... isn't that the point?
it is not about knowledge. it is just about common sense. Almost every answer on this site is written mechanically, triggered by some keyword in the question. You see the "variable function name" - here comes an answer. But why noone tries to get THE MEANING of the question? To consider if the question okay to answer?
@Nico - It's not about points or ranking... there maybe a situation where this could be needed does Col know everything about everything? Stackoverflow is about helping others... or did I get that wrong?
|
0

Firstly, this is a horrible thing to do. Consider using closures, or create_function(), or passing $i as an argument.

Secondly - the only way I can think of to do this (but for Christ's sake, don't) is with eval():

for ($i = 1; $i < 10; $i++) { 
    eval("function my_function_$i() {
        include('$i.php');
    }");
}

3 Comments

+1, eval can work, but still little error Parse error: syntax error, unexpected T_DNUMBER in line include('$i.php');. I modify to include(".'$i.".php'); to avoid the fault, but not sure it is safe or no...
@cj333 It will work but it's a BAD solution. If you go down that path you are probably shooting yourself in the foot.
@cj333 Agreed with Nico, I'm not saying do it (indeed, I specifically said don't do it) I'm just saying that it can be done. As soon as you see eval(), it's probably a sign of something you shouldn't do.
-3

Maybe you can use eval for do like this:

for ($i=1;$i<10;$i++) { 
    eval('function myfunc_'.$i.'(){echo '.$i.';}'); 

}

myfunc_5();

//Output
//5

1 Comment

Please don't... That would be a horrible, horrible way to solve a fairly simple problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.