[英]PHP declaring global variables in function
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. 好了,所以我有一个用户提交的基本$ _POST变量。 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');
上面的问题是,我可能有50个左右的不同区域要由用户提交,而只具有
myFunction('name','default','value');
会更好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. 这是有道理的,因为如果可以使用变量
$title
,则将根据我调用该函数的次数将其设置为许多不同的东西。 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. 我最后尝试将全局变量设置为我为字符串(标题)提供给函数的
$title
的名称,创建了新的全局变量pSizeTitle
,因此我可以将该变量回显出来。 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. 这可能实际上是可行的,除了以下事实:我无法定义仅在一个简单名称后附加到末尾的全局变量,而这对我不起作用,因为我需要为每个
title
项添加一个新的全局变量我有。
Hopefully this is clear, sorry if this is an absolute noob problem, I just really can't get past this. 希望这很清楚,如果这是一个绝对的菜鸟问题,对不起,我真的无法克服。
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
}
function checkDefault($index, $default)
{
return isset($_POST[$index]) ? $_POST[$index] : $default;
}
$title = checkDefault("title", "my default value");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.