1

I am new to programming and have some suspicions regarding the usage of global variables inside functions.

I am trying to separate the logic from the structure of my html form and here is where my question stems from.

The variable I have declared as global will only be used in one specific place from outside of the function, ie the html form.

function updatePosts(){
     $query= "...";
     $result= "...";
     $rows= mysqli_fetch_assoc($result);
     global $post_title;
     $post_title = $rows['post_title'] ;
}

updatePosts();

<form action="">
//use of global variable outside function
    <input type="text" name="" value="{$post_title}" class="form-control">
</form>

When i declared multiple global variables it raised some questions regarding what the impact of so many global variables might be. eg.

function updatePosts(){
     $query= "...";
     $result= "...";
     $rows= mysqli_fetch_assoc($result);
     global $post_title;
     global $post_author;
     global $post_content;
     global $post_tags;
     global $post_image;
     global $post_status;
     global $post_date;
     $post_title = $rows['post_title'] ;
     $post_author = $rows['post_author'] ;
     $post_content = $rows['post_content'] ;
     $post_tags = $rows['post_tags'] ;
     $post_image = $rows['post_image'] ;
     $post_status = $rows['status'] ;
     //and so on..
}

updatePosts();

<form action="">
<input type="text" name="" value="<?php echo $post_title ?>" class="form-control">
<input type="text" name="" value="<?php echo $post_author ?>" class="form-control">
<input type="text" name="" value="<?php echo $post_tags ?>" class="form-control">
<input type="text" name="" value="<?php echo $post_content ?>" class="form-control">
<input type="text" name="" value="<?php echo $post_status ?>" class="form-control">
//and so on...
</form>

Is this considered to be an acceptable usage of functions and global variables?

If not, what might be a more efficient way to separate the logic from the structure. Rather than encapsulating in a function would includes would be better suited for this task?

Any advice would be very much appreciated and would go a long way in helping a beginner on his journey in programming / php.

Samwyzz
  • 87
  • 1
  • 9
  • 12
    Global variables in PHP are [considered bad practice](https://stackoverflow.com/questions/1557787/are-global-variables-in-php-considered-bad-practice-if-so-why) - [Stop using global in PHP](https://stackoverflow.com/questions/12445972/stop-using-global-in-php) - [Why is it considered bad practice to use global reference inside functions?](https://stackoverflow.com/questions/8715897/why-is-it-considered-bad-practice-to-use-global-reference-inside-functions) - [PHP global in functions](https://stackoverflow.com/questions/5166087/php-global-in-functions) – GrumpyCrouton Nov 06 '17 at 16:50
  • 2
    Data should be passed back and forth across function boundaries by using function parameters and return values. It's hard to say from the excerpts you've posted, but you are probably looking for a function to return one or more posts (either as objects or just arrays), which you can then pass to your display layer, rather than setting them in global scope. – iainn Nov 06 '17 at 16:53
  • 2
    Return an array or object which bundles all those variables into one thing; probably you should just `return $rows` directly. – deceze Nov 06 '17 at 16:54
  • 1
    Shouldn't this be asked on [Code Review](https://codereview.stackexchange.com/)? Seems to me the question is about improving working code. – icecub Nov 06 '17 at 16:56
  • @GrumpyCrouton Thankyou, I will read through the links – Samwyzz Nov 06 '17 at 17:00
  • @iainn thankyou, I am fetching results from an array to assign to global variables, I was hoping globals would do the job. I understand, i am still learning and will read further on your suggestions, thankyou again – Samwyzz Nov 06 '17 at 17:04
  • @icecube Thankyou for your suggestion, my apologies if this is not the correct place to post this question, I was not familiar with code reviews and will take care in future to post to the correct forum – Samwyzz Nov 06 '17 at 17:07
  • @deceze I understand, an array sounds to be more efficient to what i am trying to achieve , thankyou ! – Samwyzz Nov 06 '17 at 17:10

1 Answers1

0

I personally would use Closures to grab variables in your scope for further use and yes the keyword global is bad practice and in the older version it was more useful in procedual coding but today you just build tiny Closures for quick use.

Just to give you a general idea.

$variable_from_outside = "hi!";
$closure = function() use ($variable_from_outside) {
    // do stuff with that variable inside this scope..
};

Closure Documentation

However, you may also just grab your variables inside your function as parameter

$variable = 'something';
function func($variable) {
    // do stuff with $variable
}

And in OOP, best way are Traits. Declare universal properties or methods and use them in classes whatever your wish is. Also it reduces performance when you take traits in a later derived class for example that way they don't have to be carried in your entire API all the way.

trait Example {
    public $stuff = 'Yey!';
    // or methods...
}

class someClass {
    use Example;
    function __construct() {
        echo $this->stuff; // Yey!
        // etc...
    }
}

Trait Documentation

x7ee1
  • 17
  • 5
  • Thankyou very much for your help, I am still learning and (with much anticipation) I am yet to reach to topics such as OOP and Closures. I will surely read up on these topics further. Thankyou for your time and the very insightful answer. I was hoping to read your explanation thoroughly, with understanding and then select it as the correct answer. – Samwyzz Nov 06 '17 at 17:40