FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » define ('NL', "<br />"); can I define a constant var ?
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: define ('NL', "<br />"); can I define a constant var ? [message #180185 is a reply to message #180184] Tue, 22 January 2013 23:08 Go to previous messageGo to previous message
Shake is currently offline  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.
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: Passing complex data back when using cURL
Next Topic: my blog
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Sat Nov 23 16:48:33 GMT 2024

Total time taken to generate the page: 0.04068 seconds