Deprecated: Assigning the return value of new by reference is deprecated in /home/fann/kksou.com/php-gtk2/includes/patTemplate/patTemplate.php on line 1420
PHP-GTK2 Cookbook Forum
Welcome, Guest
Please Login or Register.    Lost Password?

Deprecated: Assigning the return value of new by reference is deprecated in /home/fann/kksou.com/php-gtk2/components/com_fireboard/template/default/view.php on line 535

Deprecated: Assigning the return value of new by reference is deprecated in /home/fann/kksou.com/php-gtk2/components/com_fireboard/template/default/view.php on line 1063
Re:Global variables in user-definend function? (1 viewing) (1) Guest
Go to bottom Post Reply Favoured: 0
TOPIC: Re:Global variables in user-definend function?


#6161
nounours (User)
Fresh Boarder
Posts: 5
graphgraph
User Offline Click here to see the profile of this user
Global variables in user-definend function? 7 Months ago Karma: 0  
Hi!

Is it possible to use global variables with DirectPHP? I expected so, but it does not work.

I have a script something like:

<?php
function test($var) {
global $a;
echo $a;
}

$a = 77;
test('gugus');

?>

If I run it directly, the scripts outputs 77 as expected.

If I run it from DirectPHP, the output is empty. Therefore I conclude that "global" is ignored and $a acts like a normal local variable.

Do I oversee something obvious?

thanks, nounours


P.S.: For the curious, my real application looks like

In different articles, I'm using the same script, but with different parameters. So these articles look all like:


<?php

$param1=11;
$param2=99;

require("/phpscripts/mycomplicatedscript.php");

?>

I need the values in the function, so I want to use "global" to pass them into the function (the php-script in the article and the require-script share the same variable scope, so it's no problem, they are avaible in the "mycomplicatedscript.php". they should also in the function, but in DirectPHP they seem not to.

And the file mycomplicatedscript.php


function dosomething($var) {
global $param1, $param2;

do something with $var, $param1, $param2
}

dosomethingelse

dosomething("jejejh");

?>


P.S.: If somebody is wondering why I do not pass the $param1 & 2 as an argument in the function call??? well, it's actually a callback function, so i think i can't - can I? (this would be an easy work around to the above problem)
 
 
Last Edit: 2014/04/24 08:58 By nounours. Reason: i forgot an argument
  The administrator has disabled public write access.

#6162
kksou (Admin)
Admin
Posts: 1728
graph
User Online Now Click here to see the profile of this user
Re:Global variables in user-definend function? 7 Months ago Karma: 27  
Hi,

Try adding one more global $a;
before $a = 77;
as shown below:

Code:
function test($var) {
global $a;
echo $a;
}

global $a;
$a = 77;
test('gugus');
Let me know if it works, ok? Warm Regards, /kksou
 
 
Last Edit: 2014/04/25 00:16 By kksou.
  The administrator has disabled public write access.

#6200
nounours (User)
Fresh Boarder
Posts: 5
graphgraph
User Offline Click here to see the profile of this user
Re:Global variables in user-definend function? 5 Months ago Karma: 0  
YES!! This works! Thank you very much!!
 
  The administrator has disabled public write access.



Go to top Post Reply
Powered by FireBoardget the latest posts directly to your desktop

Blog - Forum - Privacy Policy - Contact Us
Copyright © 2006-2012. kksou.com. All Rights Reserved