php global数组,php中的Global关键字和数组有什么区别(What is difference between Global keyword and array in php)...

php中的Global关键字和数组有什么区别(What is difference between Global keyword and array in php)

我必须更改脚本以在禁用全局变量的服务器上运行。

所以我想知道这是否有效:

function setData(){

echo $GLOBALS['data'];

}

这会有用吗?

function setData(){

global $data;

echo $data;

}

如果不是,解决方案是什么?

I have to change a script to run on a server with Global Variables disabled.

so I am wondering if this is not working:

function setData(){

echo $GLOBALS['data'];

}

Will this work ?

function setData(){

global $data;

echo $data;

}

If no, what is the solution ?

原文:https://stackoverflow.com/questions/4365163

更新时间:2019-12-24 09:46

最满意答案

你在谈论register_globals吗? 这不会禁用所有全局变量,它只是禁用从请求参数设置全局变量。

这意味着,如果有人进入这样的页面: index.php?admin=1然后在index.php中全局变量$admin将被设置为1 。 你通常不想要的。 它不会阻止全局变量的运行。

正如Col. Shrapnel在评论中所说, $_GLOBALS不起作用,因为数组的名称是$GLOBALS 。

Are you talking about register_globals? That doesn't disable all globals, it just disables the setting of global variables from request parameters.

What that means is, if someone goes to a page like this: index.php?admin=1 then in index.php the global variable $admin will be set to 1. Which you generally don't want. It doesn't stop global variables working.

As Col. Shrapnel said in the comments, $_GLOBALS won't work, because the name of the array is $GLOBALS.

2010-12-06

相关问答

从技术上讲, global并不是一个关键词:它是一个所谓的“上下文关键词”。 这些仅在有限的程序环境中具有特殊含义,并且可以用作该环境之外的标识符。 global可以并且应该在不明确或隐藏成员时使用。 从这里开始 : class TestApp

{

// Define a new class called 'System' to cause problems.

public class System { }

// Define a constant called 'Con

...

从文档 : 在函数外部使用全局关键字不是错误。 如果文件包含在函数中,则可以使用它。 基本上,你可以让你的函数有一个不同于你的函数声明的文件。 这些胆量将被纳入该功能。 如果单独查看使用global函数外部用户的用户,这会给人一种印象,但事实是,当解释此代码时,它将从函数内部进行解释。 $name = "Jonathan";

function doSomething () {

include( 'functionGuts.php' );

}

在我们的functionGuts.php文件的内

...

将变量声明为全局变量 global $username;

global $password;

然后为它分配值 $username = $d['Username'];

$password = $d['Password'];

Declare the variable as global global $username;

global $password;

Then assign value for it $username = $d['Username'];

$password = $d['P

...

你的例子有点简化。 为了获得差异,尝试将示例代码包装到另一个函数中,从而在内部回调周围创建一个附加范围,这不是全局性的。 在以下示例中, $useRandom在排序回调中始终为null ,因为没有名为$useRandom全局变量。 您将需要使用use从外部作用域访问一个不是全局作用域的变量。 function test()

{

$test = array( "hello", "there", "what's up" );

$useRandom = "random";

$r

...

根据字符串的大小,您可以使用哈希来加快速度。 首先迭代文本。 对于每个单词,将其分配给一个数组: foreach (preg_split("/\s/", $text) as $word)

{

$string[$word] = 1;

}

然后迭代检查$ string的关键字: foreach ($keywords as $keyword)

{

if (isset($string[$keyword]))

{

// $keyword exist

...

这是PHP5类型提示的一个例子。 参数$data应该是一个数组。 可以将方法参数提示为object或array类型。 如果是对象,则可以将类的名称指定为hint关键字。 This is an example of PHP5's type hinting. The parameter $data is supposed to be an array. It is possible to hint method parameters as either object or array types. I

...

你在谈论register_globals吗? 这不会禁用所有全局变量,它只是禁用从请求参数设置全局变量。 这意味着,如果有人进入这样的页面: index.php?admin=1然后在index.php中全局变量$admin将被设置为1 。 你通常不想要的。 它不会阻止全局变量的运行。 正如Col. Shrapnel在评论中所说, $_GLOBALS不起作用,因为数组的名称是$GLOBALS 。 Are you talking about register_globals? That doesn't

...

拥有大量全球变量通常很糟糕。 它很快变得一团糟。 如果您确实需要全局变量,最好的方法是使用静态类变量 。 它们与其他产品一样全球化,但它们实施更好的命名和预先声明,因此最终结果更好一些。 如果你真的需要普通的全局变量,你也可以使用$GLOBALS变量,它在每个范围都可用,基本上是所有全局变量的数组。 global $variable_name;

do_something_with($variable_name);

是相同的 do_something_with($GLOBALS['variable

...

一个简单的preg_grep将完成这项工作: $arr = array(

'Canon sample printing text',

'Captain text',

'Canon EOS Kiss X4 (550D / Rebel T2i) + Double Zoom Lens Kit',

'Fresh sample Roasted Seaweed',

'Fresh sample text Seaweed'

);

$matched = preg_grep('

...

这可以使用array_count_values()来实现: $keywords = array();

foreach ($data as $key => $sub) {

foreach ($sub['data'] as $key => $value) {

$keywords[] = $value;

}

}

$result = array_count_values($keywords);

print_r($result);

$result将是一个包含所有值和出现

...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值