passing parameters to php include/require construct

前端 未结 4 718
隐瞒了意图╮
隐瞒了意图╮ 2021-02-12 11:01

I\'ve read quite a few posts that are very similar to the question I\'m about to ask, but I just wanted to be sure that there wasn\'t a more sophisticated way to do this. Any f

4条回答
  •  离开以前
    2021-02-12 11:10

    There isn't a way to pass parameters to include or require.

    However the code that is included joins the program flow at the point where you include it, so it will inherit any variables that are in scope. So for example if you set $myflag=true immediately before the include, your included code will be able to check what $myflag is set to.

    That said, I wouldn't suggest using that technique. Far better for your include file to contain functions (or a class) rather than code that gets run straight off. If you've included a file containing functions then you can call your functions with whatever parameters you want at any point in your program. It's much more flexible, and generally a better programming technique.

    Hope that helps.

提交回复
热议问题