How to use static and global variables in PHP?

Asked

Viewed 3,392 times

4

I need the value of a variable in several functions to increment and decrement the value of the variable.

I have tried using static variable started with null value, but even changing its value, it gets null value.

How do I manipulate her?

  • 1

    It needs more information because depending on the situation there are different solutions. If, for example, you are implementing a legacy system with old codes, it is difficult to rewrite everything, and it is more expensive for the customer.. In these cases, the most practical and coarse way is used with the use of "global", although it is recommended not to use, it is still useful for this type of situation. I find all the answers below valid because the context of the question lacks more details.

  • 1

    Especially in legacy codes should not be used global. You can break all the code by doing this. globalshould never be used, but if it is it should be very well thought out, its use should have a huge control. It is so complicated to control this effectively that it is easier to use other solutions. Perhaps the best global is that people think it’s easy to use it.

3 answers

10


How to use global variable? Simple, do not use.

Global variables are problematic. It is difficult to ensure that it will not be confused with other variables. It’s hard to understand who and when she’s being altered.

Static variable

Simply declare the variable to static (static). This way it will have life throughout the application. If it is within a function its visibility will be only within the function and this is important. But it doesn’t stop her from having life throughout the execution.

When variable is declared static it is stored in a different region of memory than it is lost when function ends as is the case for local variables within a function.

Here’s a possible algorithm for your counter. You can certainly improve it. You can even make the increment happen within the function if that’s all you want. You can use creativity and do it in many different ways. The important thing is that now knows that it is possible to extend the life of the variable throughout the life of the application without exposing it to every application, which would be a beautiful of a bad practice.

function contador($valor) {
    static $contador;
    if (isset($valor)) {
        $contador = $valor;
    }
    return $contador;
}

echo contador(1) . "\n";
$contador = contador(null) + 1;
echo contador($contador);

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

That is to say global state. The ideal is not to use even this. But depending on the application it does not cause real problem.


Rafael Withoeft’s answer also gives another possible solution without using global or static. The only difficulty of it is that it would have to pass the state variable throughout the application to use elsewhere. The variable has no extended lifespan outside the function where it was created. This can be bad but it can also be good. In some cases - this does not seem to be the case for AP - this may be the best option. This is a shape similar to the use of an object. Only the object has only one member inside, the value itself.

4

  • I did not understand the negative, is it because it did not speak in global and static? Because the solution solves the problem of AP in another good way.

  • I believe that’s why, but without knowing the code that he implemented only remained to put the way I thought more viable for him to implement...

  • On the other hand before positivizing I was analyzing everything, I think it doesn’t totally solve the problem. The variable that stores the value would have to be passed to other parts of the program to maintain its status. If you carefully analyze the question, this is undesirable. Even so, I think a negative would be an exaggeration. But it is the criterion of each one.

  • Actually, depending on the case it would be more laborious but you’re right so I’ll positive your answer.

2

Example of use of global variables !!

<?php
    $teste = "Variável 1";
    $teste2 = "teste2";
    function teste(){
        global $teste,$teste2;
        echo $teste; // Retorno será Variável 1
    }

    //Chamando a função
    teste();
    //Alterando valor da variável...
    $teste = "valor alterado...";
    teste();
?>
  • 1

    The answer is not wrong, but this is bad practice. Even so I did not negatively, rarely negative responses, especially when competing with my answer. And in this case I don’t say the answer is wrong, it just doesn’t recommend doing this.

  • Do not recommend to do? What bad practice ?

  • 2

    Declare global variables anyway.

  • Ah yes I understood, bad programming practice. I thought the answer was misspelled.

  • Thank you for your attention, I welcomed your reply...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.