0

I can't understand how they operate global variables in this language, I read the documentation and everything is explained clearly, but when I go to apply via this code does not work. Probably something I'm wrong, help me to correct.

I declare a variable within the spreadsheet php, I call the variable $pubblica at this point I'm going inside a function to insert the content in this way:

function add()
{
    $GLOBALS['pubblica'] = "insert name";
}

I imagine that the content of the variable $pubblica now is: "insert name" I therefore the need to use the variable with this content inside another function like this:

function esplore()
{
    echo "Contents of variables is $pubblica";
}

Should print me this content: "insert name"; But I get a blank message, and do not understand why. What's wrong with that?

UPDATE QUESTION:

<?php

$GLOBALS['pubblica'];

function add()
{
   $GLOBALS['pubblica'] ="insert name";
}

function esplore()
{
   echo "Contents of variables is " . $GLOBALS['pubblica'];
}

?>

the add function is activated when you press a button, and within this is called esplore

  • 1
    Read this page http://php.net/manual/en/language.variables.scope.php – Jelle Keizer Nov 22 '14 at 19:52
  • 1
    Variables are always local-scoped in PHP, unless you invite them per `global` keyword (second function), or `$GLOBALS[]` lookup (did that in your `add` only). Quick reminder: enable `error_reporting` whenever something doesn't work. – mario Nov 22 '14 at 19:52
  • I get this: Notice: Undefined variable: pubblica –  Nov 22 '14 at 19:54
  • Before starting using global, I suggest that you read http://stackoverflow.com/questions/5166087/php-global-in-functions, because as explained there, globals are evil – M. Page Nov 22 '14 at 19:58
  • According to yourlast sentence, it seems you call another script with the button, right ? Then you have to implement different techniques of passing the data, global variables are global only in the current script. $_GET or $_POST may be the solution for you, bu please show us the code including the "button". – John Nov 22 '14 at 20:58

2 Answers2

1

What you are looking for is:

<?php
function add()
{
    $GLOBALS['pubblica'] = "insert name";
}

function esplore()
{
    global $pubblica;
    echo "Contents of variables is $pubblica";
}

add();
esplore();
?>

If you don't use global $pubblica; the esplore() function doesn't know that $pubblica is a global variable and tries to find it in the local scope.

A different story would be:

function esplore()
{
    echo "Contents of variables is " . $GLOBALS['pubblica'];
}

In this case it's obvious that you are addressing a (super-) global variable and no additional scope hinting is required.

  • How to pass the global variable as a parameter? –  Nov 22 '14 at 20:01
  • @Heisenberg The same way as every other variable, but if you have a global variable you don't have to pass it since IT IS GLOBAL and can be used from everywhere (unless it gets overwritten in the local scope). –  Nov 22 '14 at 20:03
  • Well, this solution in my code doesn't working.. the contents of $pubblica is blank –  Nov 22 '14 at 20:09
  • @Heisenberg What? The code I posted definately works (http://ideone.com/ViUzbU). Of course you have to call `add()` first as I did. –  Nov 22 '14 at 20:12
  • I updated the question, I think I've successfully implemented its working code. The problem is that the variable is always empty. –  Nov 22 '14 at 20:23
  • @Heisenberg Of course it is always empty if you never assign any value to your global variable. You have to call `add()` first and then `esplore()`. Otherwise your global variable is empty/not set. You have to actually look at my code and not just assume it's the same as your code. –  Nov 22 '14 at 20:26
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/65442/discussion-between-heisenberg-and-dennis). –  Nov 22 '14 at 20:28
0

The problem is you use form to pass the data. So Global variables get lost. You need to retrieve your FORM variables.

The script below will let you enter your variable in the input field and on submit will keep it and display the result.

<?php
$publicca=(isset($_POST['publicca']) ? $_POST['publicca'] : "");
// we check if there is the variable publicca posted from the form, if not $publicca is empty string
?>

<form action="#" method="post">
<input type="text" name="pubblica" value="<? echo $publicca; ?>" placeholder="insert name">
<!-- this input can be hidden, or visible or given by user, different formats, this is an example of user input -->
<button type="submit"> Pres to send the values </button>

<?

function esplore()
{
    global $pubblica;
    if (!empty($publicca)) echo "Contents of variables is $pubblica";
}
esplore();
?>
John
  • 123
  • 1
  • 10