global关键字的效用是什么?
是否有任何理由更喜欢一种方法?
方法1:
function exempleConcat($str1, $str2)
{
return $str1.$str2;
}
Run Code Online (Sandbox Code Playgroud)
方法2:
function exempleConcat()
{
global $str1, $str2;
return $str1.$str2;
}
Run Code Online (Sandbox Code Playgroud)
什么时候使用有意义global?
对我来说,它似乎很危险 ......但它可能只是缺乏知识.我感兴趣的是记录(例如代码示例,文档链接......)技术原因.
提前致谢!
这是关于这个主题的一个很好的一般性问题,我(@Gordon)正在提供奖励以获得更多答案.无论您的答案是否与我的答案一致或给出不同的观点都无关紧要.由于该global主题时不时出现,我们可以使用一个很好的"规范"答案来链接.
我有两个PHP文件.在第一个我基于$_GET值设置cookie ,然后调用一个函数,然后将该值发送到另一个文件.这是我在join.php中使用的一些代码:
include('inc/processJoin.php');
setcookie("site_Referral", $_GET['rid'], time()+10000);
$joinProc = new processJoin();
$joinProc->grabReferral($_COOKIE["site_Referral"]);
Run Code Online (Sandbox Code Playgroud)
然后,另一个文件(processJoin.php)将此值(以及其他文件)发送到将处理数据并将数据插入数据库的其他文件.
我遇到的问题是,当grabReferral()调用processJoin.php中的函数时,该$referralID变量未在全局范围内定义 - 其他函数processJoin.php似乎无法访问它以发送到其他文件/进程.
我在processJoin.php中试过这个:
grabReferral($rid) {
global $ref_id;
$ref_id = $rid;
}
someOtherFunction() {
sendValue($ref_id);
}
Run Code Online (Sandbox Code Playgroud)
但someOtherFunction似乎无法访问或使用该$ref_id值.我也尝试使用define()无济于事.我究竟做错了什么?
以下是代码段:
function something() {
$include_file = 'test.php';
if ( file_exists($include_file) ) {
require_once ($include_file);
// global $flag;
// echo 'in main global scope flag='.$flag;
test();
}
}
something();
exit;
//in test.php
$flag = 4;
function test() {
global $flag;
echo '<br/>in test flag="'.$flag.'"';
if ($flag) {
echo 'flag works';
//do something
}
}
Run Code Online (Sandbox Code Playgroud)
上面的代码片段正确地回应了'全局范围'$ flag值但是没有识别值为4的$ flag,假定$ flag为null值.请指出访问该$ flag全局变量有什么问题.
在此先感谢,Anitha
我正在写一个脚本,它会多次从输入文件中的单词中选择一个随机单词.现在调用file()多次似乎效率低下,所以我想要为文件中的单词设置一个全局数组,并将一个函数加载到数组中(在选择随机单词之前调用).为什么不起作用?
global $words;
function init_words($file)
{
$words = file($file);
$count = count($words);
echo "$count words<br>\n"; // "3 words"
}
init_words("/foo/bar");
$count = count($words);
echo "$count words<br>\n"; // "0 words"
Run Code Online (Sandbox Code Playgroud) 我的功能有问题.我在config/funkcije.php中写下它们.在文件夹配置/我有连接到数据库等的db.php ...当我在浏览器中打开config/funkcije.php时,该页面上没有任何显示,但它应该回显一些来自DB的结果.
这是我的功能:
include 'db.php';
function prikaz_posebne_ponude()
{
$sql = "SELECT * FROM posebna_ponuda ORDER BY id DESC LIMIT 1";
$sql1 = $mysqli->query("$sql");
while ($row = $sql1->fetch_assoc()) {
$glavni_naziv = $row[$lang];
if (empty($glavni_naziv)) {
echo '';
} else {
echo "<div class='row-banner'>";
echo "<h3><span>" . $langArray['rezervacija_smjestaja'] . "</span></h3>";
echo "<p>" . $glavni_naziv . "</p>";
echo "</div>";
}
}
}
Run Code Online (Sandbox Code Playgroud)
但是当我在最后一行删除函数prikaz_posebne_ponude(){和}时,一切正常.为什么会这样?
在Drupal模块回调函数中,有一个简单的自定义函数可以进入数组.
当我在Drupal模块回调函数中定义输入数组时,自定义函数正确执行.但是,当我在根级别(全局)定义输入数组时,Drupal模块回调函数中的自定义函数将失败.
作为测试,我使自定义函数只是将输入数组的内容输出为字符串.第一种方法正确输出,而第二种方法没有任何输出.理想情况下,我想在全局级别定义数组,以便其他函数可以使用它.
思考?
<?php
// ** Placement of array for method 2
$mapping = array(
0 => "name",
1 => "match"
);
function mymodule_menu() {
$items = array();
$items['mymodule'] = array(
'title' => 'MyModule',
'page callback' => 'myModule_main',
'access callback' => TRUE,
'type' => MENU_NORMAL_ITEM
);
return $items;
}
function myModule_main() {
// ** Placement of array for method 1
$mapping = array(
0 => "name",
1 => "match"
);
$output = myFunction($mapping);
echo $output; // ** Returned to …Run Code Online (Sandbox Code Playgroud)