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

5
  • 1
    Read this page php.net/manual/en/language.variables.scope.php Commented Nov 22, 2014 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. Commented Nov 22, 2014 at 19:52
  • I get this: Notice: Undefined variable: pubblica Commented Nov 22, 2014 at 19:54
  • Before starting using global, I suggest that you read stackoverflow.com/questions/5166087/php-global-in-functions, because as explained there, globals are evil Commented Nov 22, 2014 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". Commented Nov 22, 2014 at 20:58

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

7 Comments

How to pass the global variable as a parameter?
@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).
Well, this solution in my code doesn't working.. the contents of $pubblica is blank
@Heisenberg What? The code I posted definately works (ideone.com/ViUzbU). Of course you have to call add() first as I did.
I updated the question, I think I've successfully implemented its working code. The problem is that the variable is always empty.
|
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();
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.