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
Switch to threaded view of this topic Create a new topic Submit Reply
define ('NL', "<br />"); can I define a constant var ? [message #180174] Tue, 22 January 2013 00:06 Go to next message
cate is currently offline  cate
Messages: 12
Registered: January 2012
Karma: 0
Junior Member
I can see why this doesn't work:

define ('NL', '<br />');
echo "This is NL another line";

Is there a way to define a constant variable in PHP?
Re: define ('NL', "<br />"); can I define a constant var ? [message #180175 is a reply to message #180174] Tue, 22 January 2013 00:44 Go to previous messageGo to next message
Adam Harvey is currently offline  Adam Harvey
Messages: 25
Registered: September 2010
Karma: 0
Junior Member
On Mon, 21 Jan 2013 16:06:46 -0800, cate wrote:
> I can see why this doesn't work:
>
> define ('NL', '<br />');
> echo "This is NL another line";
>
> Is there a way to define a constant variable in PHP?

Not as such — you can define constants with define() or (in 5.3.0+) the
const keyword, but they aren't variables and hence aren't interpolated
into strings, as you've discovered.

As an alternative, there's always:
echo "This is ".NL." another line";

Adam
Re: define ('NL', "<br />"); can I define a constant var ? [message #180176 is a reply to message #180175] Tue, 22 January 2013 02:16 Go to previous messageGo to next message
r.mariotti is currently offline  r.mariotti
Messages: 17
Registered: December 2011
Karma: 0
Junior Member
On Tue, 22 Jan 2013 00:44:49 +0000 (UTC), Adam Harvey
<usenet(at)adamharvey(dot)name> wrote:

> On Mon, 21 Jan 2013 16:06:46 -0800, cate wrote:
>> I can see why this doesn't work:
>>
>> define ('NL', '<br />');
>> echo "This is NL another line";
>>
>> Is there a way to define a constant variable in PHP?
>
> Not as such — you can define constants with define() or (in 5.3.0+) the
> const keyword, but they aren't variables and hence aren't interpolated
> into strings, as you've discovered.
>
> As an alternative, there's always:
> echo "This is ".NL." another line";
>
> Adam


Or perhaps an easier string approach:

echo "this is \n another line";
Re: define ('NL', "<br />"); can I define a constant var ? [message #180177 is a reply to message #180176] Tue, 22 January 2013 02:50 Go to previous messageGo to next message
Kim Andr Aker is currently offline  Kim Andr Aker
Messages: 17
Registered: September 2010
Karma: 0
Junior Member
På Tue, 22 Jan 2013 03:16:28 +0100, skrev BobMCT <r(dot)mariotti(at)fdcx(dot)net>:

> On Tue, 22 Jan 2013 00:44:49 +0000 (UTC), Adam Harvey
> <usenet(at)adamharvey(dot)name> wrote:
>
>> On Mon, 21 Jan 2013 16:06:46 -0800, cate wrote:
>>> I can see why this doesn't work:
>>>
>>> define ('NL', '<br />');
>>> echo "This is NL another line";
>>>
>>> Is there a way to define a constant variable in PHP?
>>
>> Not as such — you can define constants with define() or (in 5..3.0+) the
>> const keyword, but they aren't variables and hence aren't interpolated
>> into strings, as you've discovered.
>>
>> As an alternative, there's always:
>> echo "This is ".NL." another line";
>>
>> Adam
>
>
> Or perhaps an easier string approach:
>
> echo "this is \n another line";

Alternatively:

echo nl2br("This is \n another line");

http://php.net/nl2br


--
Kim André Akerø
- kimandre(at)NOSPAMbetadome(dot)com
(remove NOSPAM to contact me directly)
Re: define ('NL', "<br />"); can I define a constant var ? [message #180178 is a reply to message #180174] Tue, 22 January 2013 07:17 Go to previous messageGo to next message
denisb is currently offline  denisb
Messages: 1
Registered: January 2013
Karma: 0
Junior Member
Le 22/01/13 01:06, cate a écrit :
> define ('NL', '<br />');
> echo "This is NL another line";


echo 'This is ',NL,' another line';



--
@@@@@
E -00 comme on est very beaux dis !
’ `) /
|\_ ==”
Re: define ('NL', "<br />"); can I define a constant var ? [message #180179 is a reply to message #180174] Tue, 22 January 2013 08:39 Go to previous messageGo to next message
Arno Welzel is currently offline  Arno Welzel
Messages: 317
Registered: October 2011
Karma: 0
Senior Member
cate, 2013-01-22 01:06:

> I can see why this doesn't work:
>
> define ('NL', '<br />');
> echo "This is NL another line";
>
> Is there a way to define a constant variable in PHP?

Maybe you want to look at the documentation:

<http://php.net/manual/en/language.constants.php>

So - yes the define is OK.

But your echo will not work, since NL is not automatically treated as a
placeholder. You have to write:

echo "This is ".NL." another line";

With variables this would be easier though:

$NL = '<br />';
echo "This is $NL another line";


hth

--
Arno Welzel
http://arnowelzel.de
http://de-rec-fahrrad.de
Re: define ('NL', "<br />"); can I define a constant var ? [message #180180 is a reply to message #180179] Tue, 22 January 2013 18:43 Go to previous messageGo to next message
Luuk is currently offline  Luuk
Messages: 329
Registered: September 2010
Karma: 0
Senior Member
On 22-01-2013 09:39, Arno Welzel wrote:
> cate, 2013-01-22 01:06:
>
>> I can see why this doesn't work:
>>
>> define ('NL', '<br />');
>> echo "This is NL another line";
>>
>> Is there a way to define a constant variable in PHP?
>
> Maybe you want to look at the documentation:
>
> <http://php.net/manual/en/language.constants.php>
>
> So - yes the define is OK.
>
> But your echo will not work, since NL is not automatically treated as a
> placeholder. You have to write:
>
> echo "This is ".NL." another line";
>
> With variables this would be easier though:
>
> $NL = '<br />';
> echo "This is $NL another line";
>

Why do you use single quotes on the first line,
and double on the second?

Why not simply use double-quotes everywhere.....

$NL = "<br />";
echo "This is $NL another line";
Re: define ('NL', "<br />"); can I define a constant var ? [message #180181 is a reply to message #180180] Tue, 22 January 2013 18:58 Go to previous messageGo to next message
Tim Streater is currently offline  Tim Streater
Messages: 328
Registered: September 2010
Karma: 0
Senior Member
In article <6833t9-kme(dot)ln1(at)luuk(dot)invalid(dot)lan>, Luuk <luuk(at)invalid(dot)lan>
wrote:

> On 22-01-2013 09:39, Arno Welzel wrote:
>> cate, 2013-01-22 01:06:

>> With variables this would be easier though:
>>
>> $NL = '<br />';
>> echo "This is $NL another line";
>>
>
> Why do you use single quotes on the first line,
> and double on the second?
>
> Why not simply use double-quotes everywhere.....
>
> $NL = "<br />";
> echo "This is $NL another line";

Because the interpreter has to do more work with the string:

<http://php.net/manual/en/language.types.string.php>

If you do:

$NL = '<br />';
echo 'This is ' . $NL . 'another line';

then you avoid the double-quote altogether.

--
Tim

"That excessive bail ought not to be required, nor excessive fines imposed,
nor cruel and unusual punishments inflicted" -- Bill of Rights 1689
Re: define ('NL', "<br />"); can I define a constant var ? [message #180182 is a reply to message #180181] Tue, 22 January 2013 19:08 Go to previous messageGo to next message
Luuk is currently offline  Luuk
Messages: 329
Registered: September 2010
Karma: 0
Senior Member
On 22-01-2013 19:58, Tim Streater wrote:
> In article <6833t9-kme(dot)ln1(at)luuk(dot)invalid(dot)lan>, Luuk <luuk(at)invalid(dot)lan>
> wrote:
>
>> On 22-01-2013 09:39, Arno Welzel wrote:
>>> cate, 2013-01-22 01:06:
>
>>> With variables this would be easier though:
>>>
>>> $NL = '<br />';
>>> echo "This is $NL another line";
>>>
>>
>> Why do you use single quotes on the first line,
>> and double on the second?
>>
>> Why not simply use double-quotes everywhere.....
>>
>> $NL = "<br />";
>> echo "This is $NL another line";
>
> Because the interpreter has to do more work with the string:
>
> <http://php.net/manual/en/language.types.string.php>
>
> If you do:
>
> $NL = '<br />';
> echo 'This is ' . $NL . 'another line';
>
> then you avoid the double-quote altogether.
>

and who is going to notice those msecs ?
Re: define ('NL', "<br />"); can I define a constant var ? [message #180183 is a reply to message #180181] Tue, 22 January 2013 19:40 Go to previous messageGo to next message
The Natural Philosoph is currently offline  The Natural Philosoph
Messages: 993
Registered: September 2010
Karma: 0
Senior Member
On 22/01/13 18:58, Tim Streater wrote:
> In article <6833t9-kme(dot)ln1(at)luuk(dot)invalid(dot)lan>, Luuk <luuk(at)invalid(dot)lan>
> wrote:
>
>> On 22-01-2013 09:39, Arno Welzel wrote:
>>> cate, 2013-01-22 01:06:
>
>>> With variables this would be easier though:
>>>
>>> $NL = '<br />';
>>> echo "This is $NL another line";
>>>
>>
>> Why do you use single quotes on the first line,
>> and double on the second?
>>
>> Why not simply use double-quotes everywhere.....
>>
>> $NL = "<br />";
>> echo "This is $NL another line";
>
> Because the interpreter has to do more work with the string:
>
> <http://php.net/manual/en/language.types.string.php>
>
> If you do:
>
> $NL = '<br />';
> echo 'This is ' . $NL . 'another line';
>
> then you avoid the double-quote altogether.
>
Or easier yet

printf("This is <BR>a new line");
Or
$NL="<BR>";
printf("This is %s a new line",$NL);

--
Ineptocracy

(in-ep-toc’-ra-cy) – a system of government where the least capable to
lead are elected by the least capable of producing, and where the
members of society least likely to sustain themselves or succeed, are
rewarded with goods and services paid for by the confiscated wealth of a
diminishing number of producers.
Re: define ('NL', "<br />"); can I define a constant var ? [message #180184 is a reply to message #180179] Tue, 22 January 2013 22:51 Go to previous messageGo to next message
Jrn Andersen is currently offline  Jrn Andersen
Messages: 2
Registered: January 2013
Karma: 0
Junior Member
On Tue, 22 Jan 2013 09:39:07 +0100, Arno Welzel <usenet(at)arnowelzel(dot)de>
wrote:

> With variables this would be easier though:
>
> $NL = '<br />';
> echo "This is $NL another line";

A question from someone relatively new to PHP:
Why use constants at all?
I always use variables - have I missed something?

Best,
Jørn, Copenhagen
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 next message
Shake is currently offline  Shake
Messages: 40
Registered: May 2012
Karma: 0
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.
Re: define ('NL', "<br />"); can I define a constant var ? [message #180186 is a reply to message #180185] Tue, 22 January 2013 23:51 Go to previous messageGo to next message
Jrn Andersen is currently offline  Jrn Andersen
Messages: 2
Registered: January 2013
Karma: 0
Junior Member
On Wed, 23 Jan 2013 00:08:49 +0100, Shake
<alex(dot)estevezQUENO(at)QUENOfilnet(dot)es> wrote:

> 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
>

Thanks for explaing.
I do understand the basics about scope, so that makes sense - and I
would probably have used constants a bit more if I had been aware that
constants work across scopes ...

Is there any reason why it is better/worse to use constants instead of
global $var in functions?


Best,
Jørn, Copenhagen

--
Jørn Andersen
http://socialister.dk
http://marxisme.dk
Re: define ('NL', "<br />"); can I define a constant var ? [message #180187 is a reply to message #180182] Wed, 23 January 2013 00:35 Go to previous messageGo to next message
M. Strobel is currently offline  M. Strobel
Messages: 386
Registered: December 2011
Karma: 0
Senior Member
Am 22.01.2013 20:08, schrieb Luuk:
> On 22-01-2013 19:58, Tim Streater wrote:
>> In article <6833t9-kme(dot)ln1(at)luuk(dot)invalid(dot)lan>, Luuk <luuk(at)invalid(dot)lan>
>> wrote:
>>
>>> On 22-01-2013 09:39, Arno Welzel wrote:
>>>> cate, 2013-01-22 01:06:
>>
>>>> With variables this would be easier though:
>>>>
>>>> $NL = '<br />';
>>>> echo "This is $NL another line";
>>>>
>>>
>>> Why do you use single quotes on the first line,
>>> and double on the second?
>>>
>>> Why not simply use double-quotes everywhere.....
>>>
>>> $NL = "<br />";
>>> echo "This is $NL another line";
>>
>> Because the interpreter has to do more work with the string:
>>
>> <http://php.net/manual/en/language.types.string.php>
>>
>> If you do:
>>
>> $NL = '<br />';
>> echo 'This is ' . $NL . 'another line';
>>
>> then you avoid the double-quote altogether.
>>
>
> and who is going to notice those msecs ?

Nobody. It's a question of style, and showing if you know what you are doing.

/Str.
Re: define ('NL', "<br />"); can I define a constant var ? [message #180188 is a reply to message #180184] Wed, 23 January 2013 00:50 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 1/22/2013 5:51 PM, Jørn Andersen wrote:
> On Tue, 22 Jan 2013 09:39:07 +0100, Arno Welzel <usenet(at)arnowelzel(dot)de>
> wrote:
>
>> With variables this would be easier though:
>>
>> $NL = '<br />';
>> echo "This is $NL another line";
>
> A question from someone relatively new to PHP:
> Why use constants at all?
> I always use variables - have I missed something?
>
> Best,
> Jørn, Copenhagen
>

Actually, a more correct answer would be the same reason you use
constants in any language. They can't be changed.

You can get some very hard to find problems when something which is
supposed to be a constant gets the wrong value.


--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: define ('NL', "<br />"); can I define a constant var ? [message #180189 is a reply to message #180186] Wed, 23 January 2013 01:03 Go to previous messageGo to next message
Peter H. Coffin is currently offline  Peter H. Coffin
Messages: 245
Registered: September 2010
Karma: 0
Senior Member
On Wed, 23 Jan 2013 00:51:20 +0100, J?rn Andersen wrote:
> I do understand the basics about scope, so that makes sense - and I
> would probably have used constants a bit more if I had been aware that
> constants work across scopes ...
>
> Is there any reason why it is better/worse to use constants instead of
> global $var in functions?

Well, the other thing is that constants are ... well, constant. They
don't change, nothing in the code running can affect them. They won't
get screwed up by logical or programming errors, like mistaking = for
==.

--
I think it's a beautiful day to go to the zoo and feed the ducks.
To the lions.
-- Brian Kantor
Re: define ('NL', "<br />"); can I define a constant var ? [message #180190 is a reply to message #180186] Wed, 23 January 2013 02:42 Go to previous messageGo to next message
Shake is currently offline  Shake
Messages: 40
Registered: May 2012
Karma: 0
Member
For who has answered explaining that a constant is... a constant.

Peter and Jerry. Of course, they have reason.

I made the mistake of forgetting the first and primordial :)

Rgds.
Re: define ('NL', "<br />"); can I define a constant var ? [message #180191 is a reply to message #180180] Wed, 23 January 2013 09:55 Go to previous messageGo to next message
Arno Welzel is currently offline  Arno Welzel
Messages: 317
Registered: October 2011
Karma: 0
Senior Member
Am 22.01.2013 19:43, schrieb Luuk:

> On 22-01-2013 09:39, Arno Welzel wrote:
>> cate, 2013-01-22 01:06:
>>
>>> I can see why this doesn't work:
>>>
>>> define ('NL', '<br />');
>>> echo "This is NL another line";
>>>
>>> Is there a way to define a constant variable in PHP?
>>
>> Maybe you want to look at the documentation:
>>
>> <http://php.net/manual/en/language.constants.php>
>>
>> So - yes the define is OK.
>>
>> But your echo will not work, since NL is not automatically treated as a
>> placeholder. You have to write:
>>
>> echo "This is ".NL." another line";
>>
>> With variables this would be easier though:
>>
>> $NL = '<br />';
>> echo "This is $NL another line";
>>
>
> Why do you use single quotes on the first line,
> and double on the second?

To avoid substitutions and to make clear, that the string is a literal.

> Why not simply use double-quotes everywhere.....

Because then $foobar would be treated as a variable or /n would be
treated as newline and not as literal '/n'.


--
Arno Welzel
http://arnowelzel.de
http://de-rec-fahrrad.de
Re: define ('NL', "<br />"); can I define a constant var ? [message #180192 is a reply to message #180182] Wed, 23 January 2013 09:59 Go to previous message
Arno Welzel is currently offline  Arno Welzel
Messages: 317
Registered: October 2011
Karma: 0
Senior Member
Am 22.01.2013 20:08, schrieb Luuk:

> On 22-01-2013 19:58, Tim Streater wrote:
>> In article <6833t9-kme(dot)ln1(at)luuk(dot)invalid(dot)lan>, Luuk <luuk(at)invalid(dot)lan>
>> wrote:
[...]
>>> Why not simply use double-quotes everywhere.....
>>>
>>> $NL = "<br />";
>>> echo "This is $NL another line";
>>
>> Because the interpreter has to do more work with the string:
>>
>> <http://php.net/manual/en/language.types.string.php>
>>
>> If you do:
>>
>> $NL = '<br />';
>> echo 'This is ' . $NL . 'another line';
>>
>> then you avoid the double-quote altogether.
>
> and who is going to notice those msecs ?

You my not notice any difference when testing a script on your own. But
imagine a webserver with thousands of requests every hour - every
additional delay will put more load to the machine and then it *is*
important to avoid inefficient code.


--
Arno Welzel
http://arnowelzel.de
http://de-rec-fahrrad.de
  Switch to threaded view of this topic Create a new topic Submit Reply
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 30 09:55:14 GMT 2024

Total time taken to generate the page: 0.02760 seconds