Jump to content

Recommended Posts

I think you are misunderstanding the meaning of constant. It's not constant throughout your whole application, it's constant during the runtime of the script in which it is define'd. A constant is a set value, and if you try to change the value of a constant that has already been set you will get an error at runtime.

They are commonly used to store values that never or rarely change. e.g.

 

 

define('_VAT_', 0.175);


$cost = 10;
$cost_with_vat = $cost + ($cost * _VAT_);


echo 'Cost excluding VAT: £' . number_format($cost, 2) . '<br >';
echo 'Cost including VAT: £' . number_format($cost_with_vat, 2);

 

 

The value of VAT changes rarely, but you can still change it, as it has been changed recently:

 

 

 

define('_VAT_', 0.2);


$cost = 10;
$cost_with_vat = $cost + ($cost * _VAT_);


echo 'Cost excluding VAT: £' . number_format($cost, 2) . '<br >';
echo 'Cost including VAT: £' . number_format($cost_with_vat, 2);

---------------------

A session is a value which is stored on the server, and a session id stored in a cookie on the client's machine is sent to the server in each HTTP request; to identify weather they have a session stored on the server (and the location of the file that it's stored in, if they do)and this array of values (specific to each client), can be retrieved through out the sessions lifetime, so long as the code has session_start before any header's, including output, are sent.

 

 

page1.php

session_start();


$_SESSION['username'] = 'Andy-H';

page2.php

session_start();


echo $_SESSION['username']; //String - 'Andy-H'

 

 

Link to comment
https://forums.phpfreaks.com/topic/206837-site-wide-variables/#findComment-1082708
Share on other sites

Thanks, Andy...  I managed to get my code working using sessions.

 

What I've created is a language selector, either English or Vietnamese.  Depending on which language the user selects it stores a session variable for it and then calls a PHP file where I have defined all my constants.

 

I originally just assigned variables ( $user = "Vaulx"; ), but that didn't seem to work with another PHP script I downloaded to use as a form mailer.  I had to "define" all of my variables/constants.  This part I haven't figured, but I'm getting there.

 

The following is the small piece of code I created to do three things:

 

1 - Checks if user selected the language button. 

2 - Determines which language to use.

3 - Sets default language to English if first time visit.

 

<?php

// Check if user selected a language button and assign session variable.
if ($_GET[lan] == 'vn')
  { $_SESSION['lan'] = 'vn'; }
elseif ($_GET[lan] == 'en')
  { $_SESSION['lan'] = 'en'; }
elseif (!isset($_SESSION['lan']))
  { $_SESSION['lan'] = 'en'; }

// Include the appropriate language file depending upon session variable set in previous step.
if ($_SESSION['lan'] == 'vn')
  { include ('/.../language/vietnamese.php'); }
elseif ($_SESSION['lan'] == 'en')
  { include ('/.../language/english.php'); }

?>

Link to comment
https://forums.phpfreaks.com/topic/206837-site-wide-variables/#findComment-1082841
Share on other sites

By the way, I don't have session_start() in this code and it still works.  I think it's because this script is being called from another script where session_start() already exists.

 

 

Indeed it is ;)

 

 

Also, super-global arrays ($_POST, $_GET, $_SESSION, etc.) should have their keys quoted as strings, ie

 

 

$_GET[lan]; // look at the syntax highlighting, lan is treated as a constant.


$_GET['lan']; // correctly quoted and treated as a string.

Link to comment
https://forums.phpfreaks.com/topic/206837-site-wide-variables/#findComment-1083118
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.