Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.php > #8108 > unrolled thread
| Started by | cate <catebekensail@yahoo.com> |
|---|---|
| First post | 2013-01-21 16:06 -0800 |
| Last post | 2013-01-22 19:50 -0500 |
| Articles | 19 — 14 participants |
Back to article view | Back to comp.lang.php
define ('NL', "<br />"); can I define a constant var ? cate <catebekensail@yahoo.com> - 2013-01-21 16:06 -0800
Re: define ('NL', "<br />"); can I define a constant var ? Adam Harvey <usenet@adamharvey.name> - 2013-01-22 00:44 +0000
Re: define ('NL', "<br />"); can I define a constant var ? BobMCT <r.mariotti@fdcx.net> - 2013-01-21 21:16 -0500
Re: define ('NL', "<br />"); can I define a constant var ? Kim André Akerø <kimandre@nospambetadome.com> - 2013-01-22 03:50 +0100
Re: define ('NL', "<br />"); can I define a constant var ? denisb <newdb@no-log.org> - 2013-01-22 08:17 +0100
Re: define ('NL', "<br />"); can I define a constant var ? Arno Welzel <usenet@arnowelzel.de> - 2013-01-22 09:39 +0100
Re: define ('NL', "<br />"); can I define a constant var ? Luuk <luuk@invalid.lan> - 2013-01-22 19:43 +0100
Re: define ('NL', "<br />"); can I define a constant var ? Tim Streater <timstreater@greenbee.net> - 2013-01-22 18:58 +0000
Re: define ('NL', "<br />"); can I define a constant var ? Luuk <luuk@invalid.lan> - 2013-01-22 20:08 +0100
Re: define ('NL', "<br />"); can I define a constant var ? "M. Strobel" <sorry_no_mail_here@nowhere.dee> - 2013-01-23 01:35 +0100
Re: define ('NL', "<br />"); can I define a constant var ? Arno Welzel <usenet@arnowelzel.de> - 2013-01-23 10:59 +0100
Re: define ('NL', "<br />"); can I define a constant var ? The Natural Philosopher <tnp@invalid.invalid> - 2013-01-22 19:40 +0000
Re: define ('NL', "<br />"); can I define a constant var ? Arno Welzel <usenet@arnowelzel.de> - 2013-01-23 10:55 +0100
Re: define ('NL', "<br />"); can I define a constant var ? Jørn Andersen <jorn@jorna.dk> - 2013-01-22 23:51 +0100
Re: define ('NL', "<br />"); can I define a constant var ? Shake <alex.estevezQUENO@QUENOfilnet.es> - 2013-01-23 00:08 +0100
Re: define ('NL', "<br />"); can I define a constant var ? Jørn Andersen <jorn@jorna.dk> - 2013-01-23 00:51 +0100
Re: define ('NL', "<br />"); can I define a constant var ? "Peter H. Coffin" <hellsop@ninehells.com> - 2013-01-22 19:03 -0600
Re: define ('NL', "<br />"); can I define a constant var ? Shake <alex.estevezQUENO@QUENOfilnet.es> - 2013-01-23 03:42 +0100
Re: define ('NL', "<br />"); can I define a constant var ? Jerry Stuckle <jstucklex@attglobal.net> - 2013-01-22 19:50 -0500
| From | cate <catebekensail@yahoo.com> |
|---|---|
| Date | 2013-01-21 16:06 -0800 |
| Subject | define ('NL', "<br />"); can I define a constant var ? |
| Message-ID | <bfe0fbed-6727-437f-9c62-552c03dd4692@b11g2000yqh.googlegroups.com> |
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?
[toc] | [next] | [standalone]
| From | Adam Harvey <usenet@adamharvey.name> |
|---|---|
| Date | 2013-01-22 00:44 +0000 |
| Message-ID | <kdkne1$pt3$1@dont-email.me> |
| In reply to | #8108 |
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
[toc] | [prev] | [next] | [standalone]
| From | BobMCT <r.mariotti@fdcx.net> |
|---|---|
| Date | 2013-01-21 21:16 -0500 |
| Message-ID | <bitrf892um88s7hgdkks5c9og1fk2d4q9g@4ax.com> |
| In reply to | #8109 |
On Tue, 22 Jan 2013 00:44:49 +0000 (UTC), Adam Harvey
<usenet@adamharvey.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";
[toc] | [prev] | [next] | [standalone]
| From | Kim André Akerø <kimandre@nospambetadome.com> |
|---|---|
| Date | 2013-01-22 03:50 +0100 |
| Message-ID | <op.wrakuj12xyqyr5@neonk2> |
| In reply to | #8110 |
På Tue, 22 Jan 2013 03:16:28 +0100, skrev BobMCT <r.mariotti@fdcx.net>:
> On Tue, 22 Jan 2013 00:44:49 +0000 (UTC), Adam Harvey
> <usenet@adamharvey.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@NOSPAMbetadome.com
(remove NOSPAM to contact me directly)
[toc] | [prev] | [next] | [standalone]
| From | denisb <newdb@no-log.org> |
|---|---|
| Date | 2013-01-22 08:17 +0100 |
| Message-ID | <kdleda$q9q$1@speranza.aioe.org> |
| In reply to | #8108 |
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 !
’ `) /
|\_ ==”
[toc] | [prev] | [next] | [standalone]
| From | Arno Welzel <usenet@arnowelzel.de> |
|---|---|
| Date | 2013-01-22 09:39 +0100 |
| Message-ID | <50FE502B.2070708@arnowelzel.de> |
| In reply to | #8108 |
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
[toc] | [prev] | [next] | [standalone]
| From | Luuk <luuk@invalid.lan> |
|---|---|
| Date | 2013-01-22 19:43 +0100 |
| Message-ID | <6833t9-kme.ln1@luuk.invalid.lan> |
| In reply to | #8113 |
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";
[toc] | [prev] | [next] | [standalone]
| From | Tim Streater <timstreater@greenbee.net> |
|---|---|
| Date | 2013-01-22 18:58 +0000 |
| Message-ID | <timstreater-4F2B13.18585922012013@news.individual.net> |
| In reply to | #8114 |
In article <6833t9-kme.ln1@luuk.invalid.lan>, Luuk <luuk@invalid.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
[toc] | [prev] | [next] | [standalone]
| From | Luuk <luuk@invalid.lan> |
|---|---|
| Date | 2013-01-22 20:08 +0100 |
| Message-ID | <jm43t9-b9f.ln1@luuk.invalid.lan> |
| In reply to | #8115 |
On 22-01-2013 19:58, Tim Streater wrote: > In article <6833t9-kme.ln1@luuk.invalid.lan>, Luuk <luuk@invalid.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 ?
[toc] | [prev] | [next] | [standalone]
| From | "M. Strobel" <sorry_no_mail_here@nowhere.dee> |
|---|---|
| Date | 2013-01-23 01:35 +0100 |
| Message-ID | <am8pj2F17dgU1@mid.uni-berlin.de> |
| In reply to | #8116 |
Am 22.01.2013 20:08, schrieb Luuk: > On 22-01-2013 19:58, Tim Streater wrote: >> In article <6833t9-kme.ln1@luuk.invalid.lan>, Luuk <luuk@invalid.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.
[toc] | [prev] | [next] | [standalone]
| From | Arno Welzel <usenet@arnowelzel.de> |
|---|---|
| Date | 2013-01-23 10:59 +0100 |
| Message-ID | <kdocal$jur$1@dont-email.me> |
| In reply to | #8116 |
Am 22.01.2013 20:08, schrieb Luuk: > On 22-01-2013 19:58, Tim Streater wrote: >> In article <6833t9-kme.ln1@luuk.invalid.lan>, Luuk <luuk@invalid.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
[toc] | [prev] | [next] | [standalone]
| From | The Natural Philosopher <tnp@invalid.invalid> |
|---|---|
| Date | 2013-01-22 19:40 +0000 |
| Message-ID | <kdmpvl$sgn$4@news.albasani.net> |
| In reply to | #8115 |
On 22/01/13 18:58, Tim Streater wrote:
> In article <6833t9-kme.ln1@luuk.invalid.lan>, Luuk <luuk@invalid.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.
[toc] | [prev] | [next] | [standalone]
| From | Arno Welzel <usenet@arnowelzel.de> |
|---|---|
| Date | 2013-01-23 10:55 +0100 |
| Message-ID | <kdoc2a$i16$1@dont-email.me> |
| In reply to | #8114 |
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
[toc] | [prev] | [next] | [standalone]
| From | Jørn Andersen <jorn@jorna.dk> |
|---|---|
| Date | 2013-01-22 23:51 +0100 |
| Message-ID | <sr5uf89fourrm1gdmqr442ttf1pk9hnqbu@4ax.com> |
| In reply to | #8113 |
On Tue, 22 Jan 2013 09:39:07 +0100, Arno Welzel <usenet@arnowelzel.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
[toc] | [prev] | [next] | [standalone]
| From | Shake <alex.estevezQUENO@QUENOfilnet.es> |
|---|---|
| Date | 2013-01-23 00:08 +0100 |
| Message-ID | <kdn665$tn7$1@dont-email.me> |
| In reply to | #8118 |
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.
[toc] | [prev] | [next] | [standalone]
| From | Jørn Andersen <jorn@jorna.dk> |
|---|---|
| Date | 2013-01-23 00:51 +0100 |
| Message-ID | <5o8uf85qi8jltuv68s8j22ds05mvs93esu@4ax.com> |
| In reply to | #8119 |
On Wed, 23 Jan 2013 00:08:49 +0100, Shake <alex.estevezQUENO@QUENOfilnet.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
[toc] | [prev] | [next] | [standalone]
| From | "Peter H. Coffin" <hellsop@ninehells.com> |
|---|---|
| Date | 2013-01-22 19:03 -0600 |
| Message-ID | <slrnkfudmq.m9f.hellsop@nibelheim.ninehells.com> |
| In reply to | #8120 |
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
[toc] | [prev] | [next] | [standalone]
| From | Shake <alex.estevezQUENO@QUENOfilnet.es> |
|---|---|
| Date | 2013-01-23 03:42 +0100 |
| Message-ID | <kdnimh$uth$1@dont-email.me> |
| In reply to | #8120 |
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.
[toc] | [prev] | [next] | [standalone]
| From | Jerry Stuckle <jstucklex@attglobal.net> |
|---|---|
| Date | 2013-01-22 19:50 -0500 |
| Message-ID | <kdnc41$182$1@dont-email.me> |
| In reply to | #8118 |
On 1/22/2013 5:51 PM, Jørn Andersen wrote: > On Tue, 22 Jan 2013 09:39:07 +0100, Arno Welzel <usenet@arnowelzel.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@attglobal.net ==================
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.php
csiph-web