473,411 Members | 1,991 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

use of constants in an interpreter environment

what's the use of having constants in an interpreted language like
php, when there's no compile cycle?

Other than enforcing programmer discipline or affording a convinience
for the programmers, I don't see any.

php gurus tell.

Aug 14 '07 #1
7 1189
Rik
On Tue, 14 Aug 2007 22:49:50 +0200, Water Cooler v2 <wt*****@yahoo.com>
wrote:
what's the use of having constants in an interpreted language like
php, when there's no compile cycle?

Other than enforcing programmer discipline or affording a convinience
for the programmers, I don't see any.
1. Constants cannot be changed, overwritten, or unset (either deliberately
or by accident). Certainly on projects with more programmers this really
pays off.

2. Constants are always in scope.

Then again, that's all about enforcing discipline and convenience
offcourse.
--
Rik Wasmus
Aug 14 '07 #2
2. Constants are always in scope.

You mean I can't have a global constant? Hmm! There's no syntax for
it.

global define ("foo", "bar");

Aug 14 '07 #3
Rik
On Tue, 14 Aug 2007 23:01:54 +0200, Water Cooler v2 <wt*****@yahoo.com
wrote:
>2. Constants are always in scope.

You mean I can't have a global constant? Hmm! There's no syntax for
it.
No, exactly the opposite: constants are kind of 'superglobals': they are
always in scope, in every include, function, method etc. they can be used.

<?php
define('FOO','bar');
$foz = 'baz';
function test(){
echo FOO;
echo $foz;
}
test();
?>
--
Rik Wasmus
Aug 14 '07 #4
<?php
define('FOO','bar');
$foz = 'baz';
function test(){
echo FOO;
echo $foz;}

test();
?>


I tried it on my machine. Shows a blank page. Is your argument that
variables are limited by scope whereas constants have global (and not
superglobal) scope?

Aug 14 '07 #5
Rik
On Tue, 14 Aug 2007 23:14:40 +0200, Water Cooler v2 <wt*****@yahoo.com
wrote:
>
><?php
define('FOO','bar');
$foz = 'baz';
function test(){
echo FOO;
echo $foz;}

test();
?>

I tried it on my machine. Shows a blank page.
There's something wrong with your machine. It should return 'bar',
followed by either nothing or an error message $foz is not defined,
depending on error settings.
Is your argument that
variables are limited by scope whereas constants have global (and not
superglobal) scope?
The term 'superglobal' indicates that a constant is automatically
global--i.e., automatically available in every scope, without the use of
either the global keyword or the $GLOBALS array. A variable can be global,
PHP has some variable superglobals (like $_SERVER,$_POST,$_GET, etc.), but
there is no way to make a normal user-defined variable a superglobal.
Putting variables in global scope (so not available in any scope, just the
global) it pretty easy, so easy it's done to death is scripts of starters.
--
Rik Wasmus
Aug 14 '07 #6
On 14.08.2007 22:49 Water Cooler v2 wrote:
what's the use of having constants in an interpreted language like
php, when there's no compile cycle?
php is not an interpreted language in a strict sense, it's first
compiled to bytecodes and then executed.
>
Other than enforcing programmer discipline or affording a convinience
for the programmers, I don't see any.
Programming languages are all about discipline and convenience.
Otherwise, we would be still programming in machine codes like 50 years ago.

--
gosha bine

makrell ~ http://www.tagarga.com/blok/makrell
php done right ;) http://code.google.com/p/pihipi
Aug 15 '07 #7
Rik wrote:
there is no way to make a normal user-defined variable a superglobal.
Except the PHP runkit extension.

--
Toby A Inkster BSc (Hons) ARCS
[Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
[OS: Linux 2.6.12-12mdksmp, up 55 days, 12:04.]

Fake Steve is Dead; Long Live Fake Bob!
http://tobyinkster.co.uk/blog/2007/08/13/fake-bob/
Aug 15 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Chris Gonnerman | last post by:
A while back (a long while actually) I was a participant in a long argument over decimal and rational numbers, and their inclusion into the core of Python. Last night in a dream I came up with...
46
by: Jon Perez | last post by:
Can one run a 1.5 .pyc file with the 2.x version interpreters and vice versa? How about running a 2.x .pyc using a 2.y interpreter?
0
by: Atul Kshirsagar | last post by:
I am embedding python in my C++ application. I am using Python *2.3.2* with a C++ extention DLL in multi-threaded environment. I am using SWIG-1.3.19 to generate C++ to Python interface. Now to...
5
by: Rafal Kleger-Rudomin | last post by:
Hello, I'm looking for a command to reset interpreter's environment i.e. unload all modules, delete variables etc. Regards, Rafal
5
by: RajaSrinivasan | last post by:
I have seen some previous messages about such a problem. I have this problem but it is not clear what the solution really was. I am running FC2, python 2.3.3 the script i have sock.py runs if...
13
by: Rolf Magnus | last post by:
Hi, I would like to embed a python interpreter within a program, but since that program would be able to automatically download scripts from the internet, I'd like to run those in a restricted...
61
by: km | last post by:
Hi all, is true parallelism possible in python ? or atleast in the coming versions ? is global interpreter lock a bane in this context ? regards, KM
11
by: sturlamolden | last post by:
Python has a GIL that impairs scalability on computers with more than one processor. The problem seems to be that there is only one GIL per process. Solutions to removing the GIL has always...
5
by: Erik Hahn | last post by:
I'm looking for a standalone Javascript interpreter like the ones of Perl and Python. Does such a thing exist for Linux? Thanks in advance. -Erik --...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.