Home » PHP access global variable in function

PHP access global variable in function

Today, We want to share with you PHP access global variable in function.In this post we will show you PHP $GLOBALS (super global) variable, hear for Accessing a Global Variable Inside a Function we will give you demo and example for implement.In this post, we will learn about How to assign global variable value in a function in php? with an example.

PHP access global variable in function

There are the Following The simple About PHP access global variable in function Full Information With Example and source code.

Read Also:  Laravel DataTables Server side Processing Example

As I will cover this Post with live Working example to develop how to use global variable inside function in php, so the php global variable in function for this example is following below.

Way : 1 php access variable outside function

// global scope
$product_data = "Old Product Data Value";

function product_function($data) {
   global $product_data;
   $product_data = "New Data value";
}

function category_function(){
    global $product_data;
    product_function("test test test");
}

Way : 2 php set global variable in function

function foo(){
  $GLOBALS['product_id'] = 'product_id';
}
function bar(){
  echo $GLOBALS['product_id'];
}
foo();
bar();

Way : 3 php global variable across files

<?php

//this is old value
$product_data = "Old Product Data Value";

//need to change the value of global variable
product_function($product_data);
echo $product_data;

function product_function(&$data) {
    $data = "New value";
}
?>

Way : 4 Declaring a global variable inside a function

// global scope
$product_id = 1;

product_function($rid){
   global $product_id;
   $product_id = $rid;
}

category_function(){
    global $product_id;
    sendValue($product_id);
}

Way : 5 PHP Global Variable Example: Playing with Scope in PHP

$global = "I am a global variable because I wasn't defined inside a function.";

product_function();
category_function($global);
function product_function() { 
     global $global; 
     echo $global;
}

function category_function($global) {
     echo $global;
}

function category_else_global() {
    global $category_global;
    $category_global = "I'm global becuase the word global was used inside a function.";
}
category_else_global();
echo $category_global;

Web Programming Tutorials Example with Demo

Read :

Read Also:  Export data mysql table to csv file using PHP

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about PHP access global variable in function.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.


Related FAQ

Here are some more FAQ related to this Article:

  1. Read Also:  php application global config and settings - php superglobals
  2. Read Also:  How to remove undefined value from Laravel PHP array?
  3. Read Also:  PHP Creating Dynamic Directory (Folder) Example
  4. Read Also:  PHP MySQL Connect Database Script
  5. Read Also:  javascript timed redirect with countdown
  6. Read Also:  Laravel Eloquent Get Last Inserted ID using PHP
  7. Read Also:  PHP Functions - MY SQL Connects and Global Variable
  8. Read Also:  PHP access Global-Superglobals array Variables
  9. Read Also:  PHP woocommerce_after_shop_loop_item Examples
  10. Read Also:  How to Check Whether a Variable is Set or Not in laravel blade?

Leave a Comment

Your email address will not be published. Required fields are marked *