|
Re: define ('NL', "<br />"); can I define a constant var ? [message #180175 is a reply to message #180174] |
Tue, 22 January 2013 00:44 |
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 |
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 |
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 #180180 is a reply to message #180179] |
Tue, 22 January 2013 18:43 |
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 #180183 is a reply to message #180181] |
Tue, 22 January 2013 19:40 |
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 #180185 is a reply to message #180184] |
Tue, 22 January 2013 23:08 |
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 #180187 is a reply to message #180182] |
Wed, 23 January 2013 00:35 |
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 |
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 |
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 #180191 is a reply to message #180180] |
Wed, 23 January 2013 09:55 |
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
|
|
|
|