Jump to content

Do Constants Also Have Global And Local Scopes?


50r

Recommended Posts

Hey freaks i want to be sure on this, do are constants binded to global and local scopes like valuables? i see one code of my friend using constants in a class method but i dont see where he declairs them global. but they are defined from an included config file.

Edited by 50r
Link to comment
Share on other sites

Here's a little snippet to show scope constant scope

 

<?php
define ('ACONST', 42);

if (defined('BCONST'))
   echo BCONST . ' BCONST exists 1<br />';
else
   echo 'BCONST not yet defined<br />';

echo test();

if (defined('BCONST')) echo BCONST . ' (BCONST exists globally now)<br />';

function test()
{
   define('BCONST', ACONST * 2);
   return BCONST . ' BCONST from function<br />';
}
?>

RESULTS
------------------------------
BCONST not yet defined
84 BCONST from function
84 (BCONST exists globally now)

Link to comment
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.