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 =EoSQL
SELECT
foo AS Foo
FROM Bar
WHERE baz='quux'
EoSQL;


--
Peter Ford, Developer phone: 01580 89 fax: 01580 893399
Justcroft International Ltd.  www.justcroft.com
Justcroft House, High Street, Staplehurst, Kent   TN12 0AH   United Kingdom
Registered in England and Wales: 2297906
Registered office: Stag Gates House, 63/64 The Avenue, Southampton SO17 1XS

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

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

Output - {XYZ}

Attempt 2:
?php
define ('XYZ','ABC');
echo {{XYZ}}\n;
?

Output - {{XYZ}}

No luck there. I did encounter one oddity though:

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


echo XYZ  . \n;



--Curtis


--
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 dai_bac...@hotmail.com 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):
 ?php
 define ('XYZ','ABC');
 echo {XYZ}\n;
 ?

 Output - {XYZ}

 Attempt 2:
 ?php
 define ('XYZ','ABC');
 echo {{XYZ}}\n;
 ?

 Output - {{XYZ}}

 No luck there. I did encounter one oddity though:

 ?php
 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 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 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:
?php
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



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:
 ?php
 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.
script src=?php echo DIR_JAVA . 'jquery-1.5.1.js';?/script

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.
script src=?php echo DIR_JAVA . 'jquery-1.5.1.js';?/script

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 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:
 ?php
 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.
 script
src=?php echo DIR_JAVA .
'jquery-1.5.1.js';?/script
 

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 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.
 script src=?php echo DIR_JAVA . 'jquery-1.5.1.js';?/script
 
 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 EOT
Your constant for the JavaScript path is {{DIR_JAVA}}
EOT; 

I fully understand the syntax but I do not understand the advantages of using 
either.
Is it just a writing style, or is there an advantage to the way it processes, 
speed or something?





 


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

?php

$accountCreationSuccessfulMessage = EndSuccess
Pellentesque habitant morbi tristique senectus et netus et malesuada fames
ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget,
tempor sit amet, ante.

Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae
est.

Mauris placerat eleifend leo. Quisque sit amet est et sapien ullamcorper
pharetra. Vestibulum erat wisi, condimentum sed, commodo vitae, ornare sit
amet, wisi. Aenean fermentum, elit eget tincidunt condimentum, eros ipsum
rutrum orci, sagittis tempus lacus enim ac dui. Donec non enim in turpis
pulvinar facilisis. Ut felis.
EndSuccess



$accountCreationFailedMessage = EndFailure
Donec placerat. Nullam nibh dolor, blandit sed, fermentum id, imperdiet
sit amet, neque. Nam mollis ultrices justo. Sed tempor. Sed vitae tellus.
Etiam sem arcu, eleifend sit amet, gravida eget, porta at, wisi. Nam non
lacus vitae ipsum viverra pretium. Phasellus massa.

Fusce magna sem, gravida in, feugiat ac, molestie eget, wisi. Fusce
consectetuer luctus ipsum. Vestibulum nunc. Suspendisse dignissim
adipiscing libero. Integer leo. Sed pharetra ligula a dui. Quisque ipsum
nibh, ullamcorper eget, pulvinar sed, posuere vitae, nulla. Sed varius
nibh ut lacus. Curabitur fringilla.

Nunc est ipsum, pretium quis, dapibus sed, varius non, lectus. Proin a
quam. Praesent lacinia, eros quis aliquam porttitor, urna lacus volutpat
urna, ut fermentum neque mi egestas dolor.
EndFailure



?

Of course, this is arguably as clean:

?php

$accountCreationSuccessfulMessage = 
Pellentesque habitant morbi tristique senectus et netus et malesuada fames
ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget,
tempor sit amet, ante.

Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae
est.

Mauris placerat eleifend leo. Quisque sit amet est et sapien ullamcorper
pharetra. Vestibulum erat wisi, condimentum sed, commodo vitae, ornare sit
amet, wisi. Aenean fermentum, elit eget tincidunt condimentum, eros ipsum
rutrum orci, sagittis tempus lacus enim ac dui. Donec non enim in turpis
pulvinar facilisis. Ut felis.
; //$accountCreationSuccessfulMessage

$accountCreationFailedMessage = 
Donec placerat. Nullam nibh dolor, blandit sed, fermentum id, imperdiet
sit amet, neque. Nam mollis ultrices justo. Sed tempor. Sed vitae tellus.
Etiam sem arcu, eleifend sit amet, gravida eget, porta at, wisi. Nam non
lacus vitae ipsum viverra pretium. Phasellus massa.

Fusce magna sem, gravida in, feugiat ac, molestie eget, wisi. Fusce
consectetuer luctus ipsum. Vestibulum nunc. Suspendisse dignissim
adipiscing libero. Integer leo. Sed pharetra ligula a dui. Quisque ipsum
nibh, ullamcorper eget, pulvinar sed, posuere vitae, nulla. Sed varius
nibh ut lacus. Curabitur fringilla.

Nunc est ipsum, pretium quis, dapibus sed, varius non, lectus. Proin a
quam. Praesent lacinia, eros quis aliquam porttitor, urna lacus volutpat
urna, ut fermentum neque mi egestas dolor.
; //$accountCreationFailedMessage

?

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 

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


?php

$blah = _
Lorem ipsum dolor sit amet, consectetur adipisicing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna
aliqua. Ut enim ad minim veniam, quis nostrud exercitation
ullamco laboris nisi ut aliquip ex ea commodo consequat.
_;

?

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