2

I need to create some global variable in PHP through function dynamically to be used out of function

I tried

<?php
function setthis(newglobal){
  global $_.newglobal._1;
  $_.newglobal._1 = "pampam";
}
setthis();

 echo $_newglobal_1;
?>

and I am getting this error

Line : 2, Error type : 4

Message : syntax error, unexpected ')', expecting '&' or variable (T_VARIABLE)

I also tried wrap them in quoted the variable like

<?php
function setthis(newglobal){
  global $_.'newglobal'._1;
  $_newglobal_1 = "pampam";
}
setthis();

 echo $_newglobal_1;
?>

an I am getting error Message

: syntax error, unexpected ')', expecting '&' or variable (T_VARIABLE)

can you please let me know if this is doable in PHP? or what I am doing wrong?

Update

<?php
function setthis($name){
    $GLOBALS[$name];
    $name = "pampam";
}
setthis();
echo $name;
?>
Suffii
  • 5,694
  • 15
  • 55
  • 92

2 Answers2

7

Your error is always in the function header:

function setthis(newglobal){
               //^^^^^^^^^ needs a '$' in front of it

Then simply use the $GLOBALS array, e.g.

function setthis($name, $value = "pampam"){
    $GLOBALS[$name] = $value;
}
Rizier123
  • 58,877
  • 16
  • 101
  • 156
  • why _always_ in the function header? – Kenney Sep 17 '15 at 21:06
  • @Kenney Because in both attempts OP forgot the dollar sign to indicate a variable, so that's why he got the error in both code examples. – Rizier123 Sep 17 '15 at 21:07
  • I don't follow. `$_.newglobal._1` also looks like it should not compile. – Kenney Sep 17 '15 at 21:11
  • Thanks but I am still getting error on sample. Can you please take a look at update? – Suffii Sep 17 '15 at 21:11
  • I do not pass the value on the function header since I need to calculate it from a database query – Suffii Sep 17 '15 at 21:12
  • @Suffii The just write it in the function: `function setthis(){ $GLOBALS["_newglobal_1"] = "pampam"; }` – Rizier123 Sep 17 '15 at 21:14
  • 1
    http://sandbox.onlinephpfunctions.com/code/09aa68e62d26475be5914f4fb6625f0513d52718 – AbraCadaver Sep 17 '15 at 21:14
  • @Kenney Yes if OP wants to solve it by using the `global` keyword, he would have to do: `global $_newglobal_1; $_newglobal_1 = "pampam";` – Rizier123 Sep 17 '15 at 21:15
  • I don't see a proper argument or local variable declaration in OP's code, maybe that can be covered too.. there are multiple errors, and the 'always' just seems out of place. The error is usually where the parser finds it. – Kenney Sep 17 '15 at 21:19
2

You can set dynamic variable names by using brackets:

$name = 'key';
${$name} = 'value';
echo $key; // Will echo 'value'

As described here: Dynamic variable names in PHP

Your code would look something like:

<?php

function setthis($global_key, $global_value){
  global ${"_" . $global_key . "_1"};

  ${"_" . $global_key . "_1"} = $global_value;
}

setthis('newglobal', 'pampam value');

echo $_newglobal_1; // Will echo 'pampam value'

?>

Or you may use PHP's GLOBALS array, which is available (globally):

<?php

function setthis($global_key, $global_value){
  $GLOBALS[ $global_key ] = $global_value;
}

setthis('newglobal', 'pampam value');

echo $GLOBALS['newglobal']; // Will echo 'pampam value'

?>

Info on all of these are in the PHP docs: http://php.net/manual/en/language.variables.scope.php

Community
  • 1
  • 1
ChrisJohns.me
  • 174
  • 2
  • 6