dongxun7301 2014-02-18 00:11
浏览 58
已采纳

PHP在函数中声明全局变量

I've been at this for two hours, perhaps I could ask some help.

Alright, so I have a basic $_POST variable that a user submits. As you can see below, the code first checks if a value was submitted at all, and if not sets it to a default value. If the user did submit a value, it sets a variable (later used) to the value submitted. You can see my code below.

if(!isset($_POST['pSize'])) {
    $pSize = "16"; 
}
else {
    $pSize = ($_POST['pSize']);
};

echo $pSize;

The problem with the above is that I might have 50 or so different areas that the user will submit, and it'd be much nicer to just have myFunction('name','default','value'); than to write out the above for each area. However, I've been running into a problem. Here's a few examples of things I've tried. (excuse any minor errors; I don't have the actual code I tried, just the jist of what I was getting at. The problems of the code I had were not syntax errors).

function newFunction($title, $default, $value)
    if(!isset($_POST[$value])) {
        $title = $default; 
    }
    else {
        $title = ($_POST[$value]);
    };

newFunction('pSize','16,'24');
echo $pSize;

I soon learned that the above won't work due to the fact that variables in a function won't be able to be used outside of that function unless they're global. That makes sense, since if the variable $title could be used, it would be set to many different things depending on how many times I called the function. These things in mind, I tried to set global variables.

function newFunction($title, $default, $value)
    if(!isset($_POST[$value])) {
        global $title . 'Title' = $title; 

...

newFunction('pSize','16,'24');
echo $pSizeTitle;

I lastly tried to set the global variable to the name of the $title I supplied for the function with the string 'Title', creating the new global variable pSizeTitle, so I could echo that variable out. And this probably would have actually worked, except for the fact that I cannot define a global variable with something appended to the end, only with a simple name, and that will not work for me since I need a new global variable for every title item I have.

Hopefully this is clear, sorry if this is an absolute noob problem, I just really can't get past this.

  • 写回答

2条回答 默认 最新

  • dseax40600 2014-02-18 00:16
    关注

    I think the major problem is that you are not returning any data in your function, try to do it

    function newFunction($title, $default, $value)
    {
        if(!isset($_POST[$value])) {
            $title = $default; 
        } else {
            $title = ($_POST[$value]);
        }
        return $title;
        //return was missed
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。