GET作为PHP函数中的参数

GET作为PHP函数中的参数

本文介绍了$ _GET作为PHP函数中的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有同样的问题,但...我根据if语句使用标题将用户重定向到通过函数构造的动态页面。为了使该功能正常工作,它需要在头部的GET部分中传递的参数。



根据提供的答案,这是一个不好的做法。我应该怎么做呢?

 功能页面($ title,$ msg){
$ title = $ _GET [ '标题'];
$ msg = $ _GET ['msg'];
echo'< h1>'。$ title。'< / h1>';

echo'< p>';
switch($ msg){
case 1:
echo'dwasdwadawdwadwa';
休息;
案例2:
echo'wasdadwadwdad';
休息;
默认值:
echo'wadasdasd';
休息;
}
echo'< / p>';

ps:随意指出你看到的任何错误。



我发现了,但它并不真正帮助我。

解决方案

您链接的问题的答案表明函数不应该依赖任何外部如全球)变量。 $ _ GET $ _ POST (等等)是'超级全局',PHP的语言特性使它们可用在任何范围内。这意味着它们可能会在脚本中的任何地方被意外修改。



避免使用超级全局变量的一种方法是,避免在方法中使用超类全局变量 - 作为另一个问题表明 - 是需要参数的变量,否则你会从超级全局变量。



例如,而不是:

 函数add_user(){
$ user = $ _GET ['user'];
// ...
}
add_user();

您可以使用:

  function add_user($ user){
// ...
}
add_user($ _ GET ['user']);

在你的情况下,你想要的是:

 函数页面($ title,$ msg){
echo'< h1>'。$ title。'< / h1>';
echo'< p>';
switch($ msg){
case 1:
echo'dwasdwadawdwadwa';
休息;
案例2:
echo'wasdadwadwdad';
休息;
默认值:
echo'wadasdasd';
休息;
}
echo'< / p>';
}

然后,当您调用页面 $ b $ $ p $ $ $ c $ page $($ _ GET ['title'],$ _GET ['msg']) );


I have the same question but...I'm redirecting the user depending on an if statement using headers to a dynamic page that is constructed through a function. For that function to work properly, it needs the parameters passed in the GET portion of the headers.

According to what to the answers provided, this is a bad practice. What way should I be doing it?

function page($title,$msg){
    $title = $_GET['title'];
    $msg = $_GET['msg'];
    echo '<h1>'.$title.'</h1>';

    echo '<p>';
    switch($msg){
        case 1:
            echo 'dwasdwadawdwadwa';
        break;
        case 2:
            echo 'wasdadwadwdad';
        break;
        default:
            echo 'wadasdasd';
        break;
    }
    echo '</p>';
}

ps: feel free to point out anything else you see wrong.

I found this but it doesn't really help me.

解决方案

The answer to the question you linked suggests that functions should not rely on any external (e.g. global) variables. $_GET and $_POST (amongst others) are 'super globals', a language feature of PHP that makes them available in any scope. This means they may be unexpectedly modified from anywhere in your scripts.

One way to help avoid this is to avoid using super globals in methods and instead - as the answer to the other question suggests - is to instead require parameters for the variables you would otherwise get from the super globals.

E.g., instead of:

function add_user() {
  $user = $_GET['user'];
  // ...
}
add_user();

You would use:

function add_user($user) {
  // ...
}
add_user($_GET['user']);

In your situation, what you would want is:

function page($title, $msg){
  echo '<h1>'.$title.'</h1>';
  echo '<p>';
  switch($msg){
    case 1:
      echo 'dwasdwadawdwadwa';
    break;
    case 2:
      echo 'wasdadwadwdad';
    break;
    default:
      echo 'wadasdasd';
    break;
  }
  echo '</p>';
}

Then, when you call page you would call it as:

page($_GET['title'], $_GET['msg']);

这篇关于$ _GET作为PHP函数中的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-03 18:46