在PHP中使用全局变量在函数中使用Javascript的方式 [英] Using global vars within a function in PHP the way you do it in Javascript

查看:171
本文介绍了在PHP中使用全局变量在函数中使用Javascript的方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用大量全局变量和数组的函数,例如。

  $ a = 1; 
$ b [0] ='a';
$ b [1] ='b';
$ c ='你好';

函数foo(){
echo$ a
$ b [0]
$ b [1]
$ c;
}

据我所知,与JS相反,你必须包括变量调用函数:

  function foo($ a,$ b,$ c)

但是由于我在函数中使用了许多不同的变量和数组,并且由于大多数变量的主要来源是$ _GET数组(在使用extract($ _ GET)之后),我不知道该怎么做。



有没有办法让PHP函数的行为像JS函数你可以使用global关键字:

<$ p

解决方案

$ p> $ a =Hello World;
$ b =Hello World;

函数outputStrings(){
global $ a,$ b;
echo $ a。 - 。$ b;
}

$ b =再见世界;

outputStrings(); // ouputsHello World - Goodbye World

然而,最好不要使用这种结构。它通常令人困惑,并会使您的代码难以维护。 Wordpress在他们的代码库中使用了这种方法,这使得非常棘手的调试成为可能。其他插件和代码可以插入和修改全局变量,从而改变脚本的输出。



最好是:



为您的web应用程序使用OOP结构。



这样您就可以使用对象而不是随机的全局变量。这解决了在脚本过程中意外覆盖全局变量的问题。它也有助于正确组织变量,因此所有与用户有关的变量都可以在 User 类中。

  class User {
private $ firstName;
私人$ secondName;
私人$性别;

public function __construct($ fname,$ sname,$ gend){
$ this-> firstName = $ fname;
$ this-> secondName = $ sname;
$ this-> gender = $ gend;


public function outputDetails(){
echo $ this-> firstName。。$ this-> secondName。is。$ this->性别;



$ user = new User(Thomas,Clayson,Male);
$ user-> outputDetails();



将变量传递给函数



就像你已经在你的例子中显示。这是这样做的普遍接受的标准方式。你应该总是像这样传递变量,它可以帮助你定义范围和适当的结构。它也意味着你知道变量的价值是什么,因为你拥有它们并将它们传递给函数,而不仅仅是从全局范围中抽取变量。

I have a function that uses lots of global vars and arrays - e.g.

$a=1;
$b[0]='a';
$b[1]='b';
$c='Hello';

function foo() {
 echo "$a 
       $b[0] 
       $b[1] 
       $c";
}

I understand that as opposed to JS, you have to include the vars when you call the function:

function foo($a,$b,$c)

but since I'm using lots of different vars and arrays in the function, and since the main origin of most of the vars is the $_GET array (after using extract($_GET)), I'm not sure how to do this.

is there a way to make a PHP function behave like a JS function for that manner?

解决方案

You can use the global keyword:

$a = "Hello World";
$b = "Hello World";

function outputStrings(){
  global $a, $b;
  echo $a." - ".$b;
}

$b = "Goodbye World";

outputStrings(); // ouputs "Hello World - Goodbye World"

However, its best not to use this structure. Its generally confusing and will make your code difficult to maintain. Wordpress uses this approach a lot in their code base and it makes for very tricky debugging. Other plugins and code can interject and modify global variables, changing the output of your script.

What would be better would be to either:

Use an OOP structure for your web app.

This way you can use objects instead of just random global variables. This gets around the issue of you accidentally overwriting a global variable in the course of a script. It also helps to organise the variables correctly, so all variables concerning users can be in the User class. It makes more sense to structure it like that.

class User {
  private $firstName;
  private $secondName;
  private $gender;

  public function __construct($fname, $sname, $gend){
    $this->firstName = $fname;
    $this->secondName = $sname;
    $this->gender = $gend;
  }

  public function outputDetails(){
    echo $this->firstName." ".$this->secondName." is ".$this->gender;
  } 
}

$user = new User("Thomas", "Clayson", "Male");
$user->outputDetails();

Pass variables into functions

Just like you've shown in your example. This is the generally accepted standard way of doing this. You should always pass in variables like this, it helps you define scopes and a proper structure. Also it means you know what the value of variables is, as you own them and pass them to functions, rather than just plucking variables from global scope.

这篇关于在PHP中使用全局变量在函数中使用Javascript的方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆