50r Posted November 27, 2012 Share Posted November 27, 2012 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. Link to comment https://forums.phpfreaks.com/topic/271252-do-constants-also-have-global-and-local-scopes/ Share on other sites More sharing options...
Beeeeney Posted November 27, 2012 Share Posted November 27, 2012 Well, think about it for a second. Why is a constant called a constant? Because it's constant. If you need to change it, use a variable. Link to comment https://forums.phpfreaks.com/topic/271252-do-constants-also-have-global-and-local-scopes/#findComment-1395579 Share on other sites More sharing options...
Muddy_Funster Posted November 27, 2012 Share Posted November 27, 2012 constants are global. you can't change that. it's a constant. variables can be assigned different scope but default to local. Link to comment https://forums.phpfreaks.com/topic/271252-do-constants-also-have-global-and-local-scopes/#findComment-1395582 Share on other sites More sharing options...
Barand Posted November 27, 2012 Share Posted November 27, 2012 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 https://forums.phpfreaks.com/topic/271252-do-constants-also-have-global-and-local-scopes/#findComment-1395643 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.