Re: define ('NL', "<br />"); can I define a constant var ? [message #180185 is a reply to message #180184] |
Tue, 22 January 2013 23:08 |
Shake
Messages: 40 Registered: May 2012
Karma:
|
Member |
|
|
Dans son message précédent, Jørn Andersen a écrit :
>
> A question from someone relatively new to PHP:
> Why use constants at all?
> I always use variables - have I missed something?
Because variables have 'Scope':
http://php.net/manual/en/language.variables.scope.php
<?php
define('NL',"\n");
$NL = '<br />';
function sayHello() {
echo 'Hello 2'.$NL; // $NL is not in this Scope, don't work.
echo NL; // NL is in every Scope, works
}
sayHello();
echo 'Hello 1'.$NL;
echo NL;
?>
Constants do not have this behaivor.
If you Execute the code above you get:
-------------
Hello 2Hello 1<br />
-------------
Rgds.
|
|
|