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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀