-3

I'm trying to create a pet site and I want to create a virtual currency system for it. But I can't find an overall php code for it to that becomes a global variable. How do I make global codes on php?

Haran Rajkumar
  • 2,155
  • 16
  • 24
EchoDino
  • 35
  • 1
  • 10
  • `$GLOBALS['varname']='value';` ?? https://www.php.net/manual/en/reserved.variables.globals.php – Professor Abronsius Mar 27 '19 at 08:29
  • PHP has the `global` keyword, https://www.php.net/manual/en/language.variables.scope.php - but I am not to sure that’s actually what you are asking for (and it most likely isn’t what you should _use_ in the first place.) Your “question” lacks any proper explanation of what you actually want to achieve here. Please go read [ask], and then edit it accordingly. – 04FS Mar 27 '19 at 08:29
  • Also, please _tag_ appropriately - using `phpmyadmin` here seems to make very little sense. – 04FS Mar 27 '19 at 08:31

2 Answers2

1

Your question is kinda ambiguous for me. If you mean setting a variable from one file then making it available from all your php files you can put it in one file then include the file it in all your scripts. Put it on the top. Or you can put it inside an $_SESSION variable. This is the simplest solution I know. :)

Sample: Assuming all files are in the root directory

vars.php:

$globalVar = 2;
//or 
$_SESSION['global_var'] = 2; // if a session exists

otherfile.php:

include 'vars.php';
echo $globalVar; //outputs 2
//or
echo $_SESSION['global_var']; //outputs 2

I'm not sure if this is what you are trying to achieve :D

medard mandane
  • 549
  • 4
  • 6
1

To make a variable available from your whole code you have several solutions. I'll show several here, it's up to you to choose the solution that matches the best for your needs.

Environment variables.

Apache

Environment variables are a kind of global variables that you can find on your OS. For example, the variable $PATH is an environment variable.
That said, you can set environment variable and use them in your code. In this SO answer you can see how to set an environment variable in Apache to use it in your PHP.

From the answer:

<VirtualHost hostname:80>
   ...
   SetEnv VARIABLE_NAME variable_value
   ...
</VirtualHost

Then this SO answer show how to get the value.
Usually you use:

<?php

getenv('APP_ENV'); //Get the environment variable "APP_ENV"

See SetEnv documentation.

.env file

Depending on your host, your server and several things it might be difficult for you to use Apache to set your environment variables.
Like Ruby, PHP may now use a file to set environment variables. Using a library like phpdotenv you can define a file .env with variables in your project, and then use them with getenv().

.env

APP_ENV=dev
APP_SECRET=P3JGbB2neXL2vc4juepX3ZkFrNfszfEo

file.php

(new Dotenv())->load(__DIR__.'/.env'); //Load the .env
getenv('APP_ENV');

Variable definitions file

You can use a file to define global variables.

config.php

<?php
$appEnv = 'dev';

file.php

require('config.php');
echo $appEnv;
Anthony
  • 2,014
  • 2
  • 19
  • 29