Re: [PHP] Constants in strings

2011-07-08 Thread Pete Ford

On 06/07/11 17:33, Robert Williams wrote:

Where I've made most use of heredocs is when I want to do nothing but define a 
bunch of
long strings in one file.


I find the most useful thing about heredocs is that they don't care about 
quotation marks, so I often use them for SQL statements where I might have a 
mixture of ' and "


eg.

$sql =<

Re: [PHP] Constants in strings

2011-07-06 Thread Robert Cummings

On 11-07-06 02:59 PM, Jim Giner wrote:

I LOVE the heredocs tool.  I only learned about it a couple of months ago -
what a find!  It makes generating my html for my web pages so much
easier and allows me to include my php vars within the html with much less
confusion and simplifies the intermixing of html and php vars - no more
single quote, double quote and dot stuff in an html tag.
Sure you have to put the closing tag in column 1 - a mere blip against the
pros.


With respect to putting the closing tag in column 1... I've found the 
following to be fairly unobtrusive:




Cheers,
Rob.
--
E-Mail Disclaimer: Information contained in this message and any
attached documents is considered confidential and legally protected.
This message is intended solely for the addressee(s). Disclosure,
copying, and distribution are prohibited unless authorized.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Constants in strings

2011-07-06 Thread Jim Giner
I LOVE the heredocs tool.  I only learned about it a couple of months ago - 
what a find!  It makes generating my html for my web pages so much 
easier and allows me to include my php vars within the html with much less 
confusion and simplifies the intermixing of html and php vars - no more 
single quote, double quote and dot stuff in an html tag.
Sure you have to put the closing tag in column 1 - a mere blip against the 
pros. 



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Constants in strings

2011-07-06 Thread Robert Williams
On 2011-07-6 08:09, "ad...@buskirkgraphics.com"
 wrote:



>I use constants in my OOP and I never use the heredoc syntax. Now I am
>fearing that I have not taken advantage of something.
>My understanding of heredoc syntax as of 5.3 is just a string quoting
>right?
>Is there an advantage of using the heredoc syntax over single quoted or
>double quoted?

I don't believe that a heredoc will perform significantly differently than
a double-quoted string, as, from the parser's POV, they're essentially the
same thing once you get past the step of extracting the entire string from
the source. I've not verified this by reviewing the relevant source,
however, nor have I benchmarked it. But, I'm willing to bit that even if
there is a difference, it's so small that you're better off worrying about
which will lead to easier code maintenance than worrying about performance
(as is typically the case with such micro-optimization choices).

In my view, what's important is how you use them. In particular, a heredoc
can present a bit more cleanly when you're dealing with a large-ish chunk
of text, as in, say, an e-mail message template. The main downside is that
they will usually make a mess of code formatting, since the closing
delimiter must be against the left margin. For this reason, I tend to
prefer multi-line double-quoted strings over heredocs when I have
meaningful indentation, as in a function or class method. Where I've made
most use of heredocs is when I want to do nothing but define a bunch of
long strings in one file. For example, I might create a file to define a
set of related e-mail message templates:



Of course, this is arguably as clean:



With the latter, there is the catch that you end up with leading and
trailing line breaks, but those are easy enough to deal with, if desired.

As to the original topic of this thread, it's long annoyed me that there's
no easy way to use constants with interpolation. Since I find repeated
concatenation extremely ugly and prone to editing error (the same reason I
wouldn't use syntax any more complicated than {{foo}} even if it did
work), I end up taking the approach of defining a variable to use instead,
usually with a prefix to make clear that it's an understood constant
(e.g., $kUsername). And if I have to do this for one constant, I do it for
all related constants even if not needed, just for the sake of
consistency. This means that I frequently end up using variables where
constants would work just fine. With the prefix convention and a
disciplined team, this proves easily tenable, but it's not ideal.

-Bob


--
Robert E. Williams, Jr.
Associate Vice President of Software Development
Newtek Businesss Services, Inc. -- The Small Business Authority
http://www.thesba.com/


Notice: This communication, including attachments, may contain information that 
is confidential. It constitutes non-public information intended to be conveyed 
only to the designated recipient(s). If the reader or recipient of this 
communication is not the intended recipient, an employee or agent of the 
intended recipient who is responsible for delivering it to the intended 
recipient, or if you believe that you have received this communication in 
error, please notify the sender immediately by return e-mail and promptly 
delete this e-mail, including attachments without reading or saving them in any 
manner. The unauthorized use, dissemination, distribution, or reproduction of 
this e-mail, including attachments, is prohibited and may be unlawful. If you 
have received this email in error, please notify us immediately by e-mail or 
telephone and delete the e-mail and the attachments (if any).

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] Constants in strings

2011-07-06 Thread admin
> -Original Message-
> From: Ashley Sheridan [mailto:a...@ashleysheridan.co.uk]
> Sent: Wednesday, July 06, 2011 10:49 AM
> To: ad...@buskirkgraphics.com; 'Dave Wilson'; php-general@lists.php.net
> Subject: RE: [PHP] Constants in strings
> 
> 
> 
> >
> >define('DIR_JAVA', '/js/');
> >
> >When you need to use the JavaScript directory you can do this.
> >
> >
> >There is no true need for the curly brackets to echo out the value of
> >the constant.
> >
> 
> Except for when you're using heredoc, much like in the OPs first
> post...
> 
> Thanks,
> Ash
> http://www.ashleysheridan.co.uk
> --
> Sent from my Android phone with K-9 Mail. Please excuse my brevity.


Ash,
I have a few questions.
I use constants in my OOP and I never use the heredoc syntax. Now I am fearing 
that I have not taken advantage of something.
My understanding of heredoc syntax as of 5.3 is just a string quoting right?
Is there an advantage of using the heredoc syntax over single quoted or double 
quoted?

Examples:

Echo 'your constant for the javascript path is '.DIR_JAVA;

Echo <<http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] Constants in strings

2011-07-06 Thread Curtis Maurand



Yeah, that was my answer and I was rebuked for that.

ad...@buskirkgraphics.com wrote:
>> -Original
Message-
>>
From: Dave Wilson
[mailto:dai_bac...@hotmail.com]
>> Sent: Wednesday, July 06,
2011 10:11 AM
>> To: php-general@lists.php.net
>>
Subject: Re: [PHP] Constants in strings
>>
>> On
Wed, 06 Jul 2011 12:56:21 +0100, Stuart Dallas wrote:
>> >
My guess is that the preceding $ causes PHP to interpret the next
>> token
>> > "{XYZ}" as a variable or a
constant, but without that preceding $ it
>> has
>>
> no way to know you're trying to use a constant. As Curtis points
out,
>> > the only way to insert a constant into a string is
through
>> > concatenation.
>> >
>>
> -Stuart
>>
>> OK. I should have made myself
clearer - I was making an observation
>> with
>>
regards to constant parsing in strings rather than looking for advice.
>> My
>> bad.
>>
>> My third
example showed that "{${XYZ}}" would echo the value of the
>> variable called the value of XYZ:
>> > define ('XYZ','ABC');
>>
>>
$ABC="huh!";
>>
>> echo
"{${XYZ}}\n";
>> ?>
>> Output - huh!
>>
>> We could easily re-write the 'echo' line above to
be:
>> echo "{${constant('XYZ'}}\n";
>>
>> But my example shows that PHP *is* accessing the value of a
constant
>> without any jiggery-pokery or hacks (e.g.
http://www.php.net/manual/en/
>>
language.types.string.php#91628) as it is retrieving the value of ABC
>> from the XYZ constant and then looking for a variable of that
name.
>>
>> I admit that I'm no C coder but it may
be possible (note, the word
>> "may")
>>
that a change of code within the PHP source tree will allow us to use
>> something like echo "{{XYZ}}" to access the constant
value.
>>
>> Cheers
>>
>>
Dave
>>
>>
>> --
>> PHP
General Mailing List (http://www.php.net/)
>> To unsubscribe,
visit: http://www.php.net/unsub.php
> 
> 
> 
> define('DIR_JAVA', '/js/');
> 
> When you need to
use the JavaScript directory you can do this.
> 
> 
>
There is no true need for the curly brackets to echo out the value of
the
> constant.
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
>

> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
> 
>


RE: [PHP] Constants in strings

2011-07-06 Thread Ashley Sheridan


>
>define('DIR_JAVA', '/js/');
>
>When you need to use the JavaScript directory you can do this.
>
>
>There is no true need for the curly brackets to echo out the value of
>the constant.
>

Except for when you're using heredoc, much like in the OPs first post...

Thanks,
Ash
http://www.ashleysheridan.co.uk
--
Sent from my Android phone with K-9 Mail. Please excuse my brevity.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] Constants in strings

2011-07-06 Thread admin
> -Original Message-
> From: Dave Wilson [mailto:dai_bac...@hotmail.com]
> Sent: Wednesday, July 06, 2011 10:11 AM
> To: php-general@lists.php.net
> Subject: Re: [PHP] Constants in strings
> 
> On Wed, 06 Jul 2011 12:56:21 +0100, Stuart Dallas wrote:
> > My guess is that the preceding $ causes PHP to interpret the next
> token
> > "{XYZ}" as a variable or a constant, but without that preceding $ it
> has
> > no way to know you're trying to use a constant. As Curtis points out,
> > the only way to insert a constant into a string is through
> > concatenation.
> >
> > -Stuart
> 
> OK. I should have made myself clearer - I was making an observation
> with
> regards to constant parsing in strings rather than looking for advice.
> My
> bad.
> 
> My third example showed that "{${XYZ}}" would echo the value of the
> variable called the value of XYZ:
>  define ('XYZ','ABC');
> 
> $ABC="huh!";
> 
> echo "{${XYZ}}\n";
> ?>
> Output - huh!
> 
> We could easily re-write the 'echo' line above to be:
> echo "{${constant('XYZ'}}\n";
> 
> But my example shows that PHP *is* accessing the value of a constant
> without any jiggery-pokery or hacks (e.g. http://www.php.net/manual/en/
> language.types.string.php#91628) as it is retrieving the value of ABC
> from the XYZ constant and then looking for a variable of that name.
> 
> I admit that I'm no C coder but it may be possible (note, the word
> "may")
> that a change of code within the PHP source tree will allow us to use
> something like echo "{{XYZ}}" to access the constant value.
> 
> Cheers
> 
> Dave
> 
> 
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php



define('DIR_JAVA', '/js/');

When you need to use the JavaScript directory you can do this.


There is no true need for the curly brackets to echo out the value of the 
constant.











--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Constants in strings

2011-07-06 Thread Dave Wilson
On Wed, 06 Jul 2011 12:56:21 +0100, Stuart Dallas wrote:
> My guess is that the preceding $ causes PHP to interpret the next token
> "{XYZ}" as a variable or a constant, but without that preceding $ it has
> no way to know you're trying to use a constant. As Curtis points out,
> the only way to insert a constant into a string is through
> concatenation.
> 
> -Stuart

OK. I should have made myself clearer - I was making an observation with 
regards to constant parsing in strings rather than looking for advice. My 
bad.

My third example showed that "{${XYZ}}" would echo the value of the 
variable called the value of XYZ:

Output - huh!

We could easily re-write the 'echo' line above to be:
echo "{${constant('XYZ'}}\n";

But my example shows that PHP *is* accessing the value of a constant 
without any jiggery-pokery or hacks (e.g. http://www.php.net/manual/en/
language.types.string.php#91628) as it is retrieving the value of ABC 
from the XYZ constant and then looking for a variable of that name.

I admit that I'm no C coder but it may be possible (note, the word "may") 
that a change of code within the PHP source tree will allow us to use 
something like echo "{{XYZ}}" to access the constant value.

Cheers

Dave


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Constants in strings

2011-07-06 Thread Ashley Sheridan


>> Any ideas?
>>
>echo XYZ  . "\n";
>
>
>
>--Curtis
>
>
>--
>PHP General Mailing List (http://www.php.net/)
>To unsubscribe, visit: http://www.php.net/unsub.php

Which doesn't answer the original question Dave asked...

Thanks,
Ash
http://www.ashleysheridan.co.uk
--
Sent from my Android phone with K-9 Mail. Please excuse my brevity.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Constants in strings

2011-07-06 Thread Stuart Dallas
On Wed, Jul 6, 2011 at 12:07 PM, Dave Wilson  wrote:

> Hi all,
>
> OK. We all know that constants cannot be accessed directly via their name
> in double-quoted or heredoc strings. I knew this already but a read of
> the PHP manual got me thinking.
>
> The manual states that to get the $$ value of a variable, the form
> "{${var}}" should be used. Therefore, I wondered if something similar
> would work for constants.
>
> Attempt 1 (just to be sure):
>  define ('XYZ','ABC');
> echo "{XYZ}\n";
> ?>
>
> Output - {XYZ}
>
> Attempt 2:
>  define ('XYZ','ABC');
> echo "{{XYZ}}\n";
> ?>
>
> Output - {{XYZ}}
>
> No luck there. I did encounter one oddity though:
>
>  define ('XYZ','ABC');
> echo "{${XYZ}}\n";
> ?>
>
> Output:
> PHP Notice: Undefined variable: ABC in /home/wilsond/testScripts/l7.php
> on line 3
>
> Which appears to mean that PHP is able to pick up the value of the
> constant and try to access a variable with that name.
>
> Any ideas?
>

My guess is that the preceding $ causes PHP to interpret the next token
"{XYZ}" as a variable or a constant, but without that preceding $ it has no
way to know you're trying to use a constant. As Curtis points out, the only
way to insert a constant into a string is through concatenation.

-Stuart

-- 
Stuart Dallas
3ft9 Ltd
http://3ft9.com/


Re: [PHP] Constants in strings

2011-07-06 Thread Curtis Maurand

On 7/6/2011 7:07 AM, Dave Wilson wrote:

Output - {XYZ}

Attempt 2:


Output - {{XYZ}}

No luck there. I did encounter one oddity though:



Output:
PHP Notice: Undefined variable: ABC in /home/wilsond/testScripts/l7.php
on line 3

Which appears to mean that PHP is able to pick up the value of the
constant and try to access a variable with that name.

Any ideas?


echo XYZ  . "\n";



--Curtis


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php