2014-01-28 42 views
0

你好,大家好我已经看到它使用$ _ POST作为变量.The代码的代码是

private 
$something 

function example() 
if($_POST) $_POST = $this->$something-1; 
if($_SESSION) $_SESSION = $this+1; 
if($_COOKIES) $_ COOKIES = $this->something 

在这段代码中我已经看到$ _ POST作为一个变量。可摆在我们$ _ POST作为一个变量,而不是

$code = $_POST['code'] 

我很抱歉,如果我问这个问题。请错帮我.. :) .Thanks提前

+0

这是您的代码吗? – putvande

+5

'$ something = $ _POST;''稍后会让你能够使用'$ something ['code']',我发现它完全没用 –

+0

你应该尝试遵循着名的做法,否则其他开发人员将难以遵循你的代码。 –

回答

0

这已经被问了几次,一般的共识似乎是没有问题的参考直接使用$_POST变量。事实上,通过使用该行

$code = $_POST['code'] 

你实际上为自己添加更多的代码和更多的工作。

但这完全取决于你如何使用$_POST,无论哪种方式都很好。

更新

你不一定需要以使用$ _POST发布到网页数据见下文

echo '<pre>'; 
print_r($_POST); //return blank array as nothing has been posted to the page 
echo '</pre>'; 

$_POST[] = 1; //even though no data has been posted we can use $_POST as a variable if we wish 

echo '<pre>'; 
print_r($_POST); // This will print an array where $_POST[0] is equal to 1 
echo '</pre>'; 

$_POST[0] = $_POST[0] + 1; 
echo '<pre>'; 
print_r($_POST); // This will print an array where $_POST[0] is equal to 2 as we added one earlier 
echo '</pre>'; 

所以在这里我们可以看到使用的$_POST。在第三次印刷中,我们改变了$_POST[0]的值。您也可以如您的代码所示,使$_POST代表单个值而不是默认的数组。

因此,如果我们说

$_POST = 1+1; 
echo '<pre>'; 
print_r($_POST); // This will print 2 
echo '</pre>'; 

这将打印2.

就像你可以用$ _POST作为变量名的任何变量,虽然我不建议这一点,除非处理发布的数据。

+0

没有得到你的答案? – user3220407

+0

我的问题是,我们可以使用$ _post作为像$ _POST = $ somthing + $ code这样的varibale。我们可以像那样使用吗? – user3220407

+0

@ user3220407我已更新答案。 –

1

一个合法使用$ _ POST没有关键

$_POST['email'] 

会...

is_array($_POST) 

这是不使用$ _ POST作为一个变量,但它是一个使用$ _POST标识符而不尝试访问其元素之一的示例。根据你的例子,这个可能是是每个人在使用$ _POST之前应该做的事情吗?