3
1.  $d = unserialize(base64_decode($params['customfields']));
2.  global $username = $d['Username'];
3.  global $password = $d['Password'];

I get an error:

Parse error: syntax error, unexpected '=', expecting ',' or ';' in line 2

whats wrong with my code? the PHP version is the latest

Saty
  • 22,443
  • 7
  • 33
  • 51

5 Answers5

2

Declare the variable as global

global $username;
global $password;

Then assign value for it

$username = $d['Username'];
$password = $d['Password'];
Anto S
  • 2,448
  • 6
  • 32
  • 50
  • thank you very much!, accept in 7 minutes –  Dec 21 '15 at 07:48
  • @BlurryScript glad to help you. Please do accept :-) – Anto S Dec 21 '15 at 07:50
  • 1
    Even if this is correct, I don't recommend to use global variables. It may be dangerous in big applications.. – Mihai Matei Dec 21 '15 at 08:18
  • @MateiMihai whats the risk of using it? Please clarify? Also, if you dont mind on oyur profile view page `about me` section looks good. But first two lines not included in code view. I mean `class Person { protected $nam...` – Anto S Dec 21 '15 at 08:26
  • Thanks for noticing me. The risk is that you may define a variable as global at some point and modify it later in the code. I don't say it creates bugs but you may spend some time to debug a wrong behavior which might be created by the usage of the global variables. http://stackoverflow.com/questions/12445972/stop-using-global-in-php – Mihai Matei Dec 21 '15 at 08:41
  • 1
    @MateiMihai thanks for updating me :-) Profile seems good too :-) – Anto S Dec 21 '15 at 08:58
1

No it cant work like that you must need to make it as global variable than use it.

global $username;
$username = $d['Username'];
devpro
  • 16,184
  • 3
  • 27
  • 38
1

global keyword is used to declare variable as global. You cannot use it with assignment operator. You can use $GLOBALS instead:

global $username,$password;
$username = $d['Username'];
$password = $d['Password'];

OR

$GLOBALS['username']= $d['Username'];
$GLOBALS['password']= $d['password'];

Both are appropriate methods, it's upto you what method you choose.

Disha V.
  • 1,834
  • 1
  • 13
  • 21
0

Typically you would use global within a function to reference a variable declared outwith the function. eg

$username='fred';

function blah(){
    global $username;
    echo $username;
}

Perhaps what you are trying to achieve should be done using the $GLOBALS array, eg:

$GLOBALS['username']=$d['username'];

which can then be referenced, as you would expect, pretty much anywhere eg: echo $GLOBALS['username']

Professor Abronsius
  • 33,063
  • 5
  • 32
  • 46
0

You should assign a value to global variable in the next line after declaring it as global.

$d = unserialize(base64_decode($params['customfields']));
global $username;
$username = $d['Username'];
global $password;
$password = $d['Password'];
Prafulla Kumar Sahu
  • 9,321
  • 11
  • 68
  • 105