Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old July 4th, 2008, 03:15 AM
Twayne
Guest
 
Posts: n/a
Default echo format Q

Hi,

Irrelevant question time, probably:

Is there any advantage/reason to use one format over the other for the
following two types of echo statements?

1. echo "error is " . $error;
2. echo "error is $error";

Obviously I'm talking about longer statements than the samples above,
but they do create identical outputs.

Line 1 is how most of the code snippets/samples I find are written.
But line 2 is a lot less typing and for me at least, easier to keep
track of.

Maybe it's my gross inexperience showing but the only time I see the
...." . concatenator useful is if I want to print something like
"$error is 0"; then it's just echo '$error is ' . $error; but as
always, there are still other ways to accomplish it, which I didn't mean
to get into here.

If you consider this a waste of your time, OK; I understand. But if
have an opinion I'd be interested to hear whether there is any advantage
of one over the other. Mostly, so that I pick up the preferred habit if
nothing else.

TIA,

Twayne


  #2  
Old July 4th, 2008, 05:25 AM
Jerry Stuckle
Guest
 
Posts: n/a
Default Re: echo format Q

Twayne wrote:
Quote:
Hi,
>
Irrelevant question time, probably:
>
Is there any advantage/reason to use one format over the other for the
following two types of echo statements?
>
1. echo "error is " . $error;
2. echo "error is $error";
>
Obviously I'm talking about longer statements than the samples above,
but they do create identical outputs.
>
Line 1 is how most of the code snippets/samples I find are written.
But line 2 is a lot less typing and for me at least, easier to keep
track of.
>
Maybe it's my gross inexperience showing but the only time I see the
..." . concatenator useful is if I want to print something like
"$error is 0"; then it's just echo '$error is ' . $error; but as
always, there are still other ways to accomplish it, which I didn't mean
to get into here.
>
If you consider this a waste of your time, OK; I understand. But if
have an opinion I'd be interested to hear whether there is any advantage
of one over the other. Mostly, so that I pick up the preferred habit if
nothing else.
>
TIA,
>
Twayne
>
>
>
Google "Premature optimization". Then don't worry about the difference.

All a matter of style. Pick the one which suits you best. Because of
you ask 100 programmers, you'll get 25 different reasons as to which to
use and why. And you'll also get another 75 different answers as to
which is the "better" way to do it :-)

Seriously - consistency is more important in this case.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

  #3  
Old July 4th, 2008, 12:45 PM
Michael Fesser
Guest
 
Posts: n/a
Default Re: echo format Q

..oO(Twayne)
Quote:
>Irrelevant question time, probably:
>
>Is there any advantage/reason to use one format over the other for the
>following two types of echo statements?
>
>1. echo "error is " . $error;
>2. echo "error is $error";
>
>Obviously I'm talking about longer statements than the samples above,
>but they do create identical outputs.
3. echo 'error is ', $error;
4. printf('error is %s', $error);
5. ...

Use whatever you like. I prefer embedded variables or printf() if I have
to embed multiple variables or complex expressions into a string. Of
course with 2. you should use an editor with good syntax highlighting.

Micha
  #4  
Old July 4th, 2008, 04:15 PM
Twayne
Guest
 
Posts: n/a
Default Re: echo format Q

Hi,
Quote:
>
Irrelevant question time, probably:
>
Is there any advantage/reason to use one format over the other for the
following two types of echo statements?
>
1. echo "error is " . $error;
2. echo "error is $error";
>
Obviously I'm talking about longer statements than the samples above,
but they do create identical outputs.
>
Line 1 is how most of the code snippets/samples I find are written.
But line 2 is a lot less typing and for me at least, easier to keep
track of.
>
Maybe it's my gross inexperience showing but the only time I see the
..." . concatenator useful is if I want to print something like
"$error is 0"; then it's just echo '$error is ' . $error; but as
always, there are still other ways to accomplish it, which I didn't
mean to get into here.
>
If you consider this a waste of your time, OK; I understand. But if
have an opinion I'd be interested to hear whether there is any
advantage of one over the other. Mostly, so that I pick up the
preferred habit if nothing else.
>
TIA,
>
Twayne
Thanks guys; clear/concise responses there, actually . One mentioned
consistancy, which was really the root of my question I think. I know
enough to almost be dangerous now and am settling in on some "do it this
way" things after a long spiel of trying most every different way I
could find to do things; which I'm aware I didn't find all of <g>.

Thanks much, & Regards,

Twayne



  #5  
Old July 4th, 2008, 05:15 PM
Paul Lautman
Guest
 
Posts: n/a
Default Re: echo format Q

Twayne wrote:
Quote:
Hi,
>
Irrelevant question time, probably:
>
Is there any advantage/reason to use one format over the other for the
following two types of echo statements?
>
1. echo "error is " . $error;
2. echo "error is $error";
>
Obviously I'm talking about longer statements than the samples above,
but they do create identical outputs.
>
Line 1 is how most of the code snippets/samples I find are written.
But line 2 is a lot less typing and for me at least, easier to keep
track of.
>
Maybe it's my gross inexperience showing but the only time I see the
..." . concatenator useful is if I want to print something like
"$error is 0"; then it's just echo '$error is ' . $error; but as
always, there are still other ways to accomplish it, which I didn't
mean to get into here.
>
If you consider this a waste of your time, OK; I understand. But if
have an opinion I'd be interested to hear whether there is any
advantage of one over the other. Mostly, so that I pick up the
preferred habit if nothing else.
>
TIA,
>
Twayne
Rather than
2. echo "error is $error";

you should get used to using:
2. echo "error is {$error}";

And
1. echo "error is " . $error;

is better as:
1. echo 'error is ' . $error;


  #6  
Old July 4th, 2008, 06:25 PM
Twayne
Guest
 
Posts: n/a
Default Re: echo format Q

Twayne wrote:
Quote:
Quote:
>Hi,
>>
>Irrelevant question time, probably:
>>
>Is there any advantage/reason to use one format over the other for
>the following two types of echo statements?
>>
>1. echo "error is " . $error;
>2. echo "error is $error";
>>
....
Quote:
Quote:
>... Mostly, so that I pick up the
>preferred habit if nothing else.
>>
>TIA,
>>
>Twayne
>
Rather than
2. echo "error is $error";
>
you should get used to using:
2. echo "error is {$error}";
>
And
1. echo "error is " . $error;
>
is better as:
1. echo 'error is ' . $error;


OHHHHHhhh, now you've gone and done it! I told myself I'd not get into
that one yet as it was going to make me look even stupider (is that a
word?<g>) than I've already made myself look! :^)

OK, I understand:
-- Curly braces define begin/end and thus helps avoid spaces etc.;
extends a line to multiple lines; keeps things related to each other;
and in my case still confuse me;
-- because, although I understand echo "error is {$error}";
, I do NOT clearly understand, taking a different sample of, for
instance:
echo "He drank some {$beer}s";
All the explanations note that 's' is a valid character for variable
names, but ... so what? If $beers='beers', then, OH! ... Are these
examples trying to say that one cannot make a var plural of, say,
$beer='beer' by simply adding the "s" to it? Is THAT what it's
trying to point out? Boy, if that's the case, they do a lousy job of
it! <g>.

I considered deleting the foregoing and rewording it, then decided
against deleting it so you can see where my head is.

Is the following correct?
IF $beer = 'beer'
then printing/echoing "beers" is {$beer}s, and NOT $beers ;
correct? That seems suspicious to me because I can't see why anyone
would try to do it the wrong way in that case. So though I'm not
comfortable with that interpretation right now, it's the only one that
seems to make sense to me.

If that's not the case then I'm still lost: Net.php, w3schools and
several tuts all seem to copy each other and explain this part in the
same way.

So if I'm wrong, then can you explain a differenct significance of
the "s" in the examples?

I've even seen other forums where they talk about being sure they
didn't use an "s" and still don't understand why something didn't work.
They seem to talk about "s" as though it's some
super-secret-reserved-word or something and for whatever reason never
mention any other letter. But it doesn't make sense to me that way
either.

lol, if I haven't made a completely *clear's mud* post out of this, any
clarification/verification would be most appreciated.
Also, if you were just trying to confuse me with (['({fact}s'])), then
_shame_ on you<g>! Yeah yeah, I know, all newbies have to pay their
dues, but ... .

TIA,

Twayne


  #7  
Old July 4th, 2008, 07:05 PM
Michael Fesser
Guest
 
Posts: n/a
Default Re: echo format Q

..oO(Twayne)
Quote:
>OHHHHHhhh, now you've gone and done it! I told myself I'd not get into
>that one yet as it was going to make me look even stupider (is that a
>word?<g>) than I've already made myself look! :^)
>
>OK, I understand:
>-- Curly braces define begin/end and thus helps avoid spaces etc.;
>extends a line to multiple lines; keeps things related to each other;
>and in my case still confuse me;
>-- because, although I understand echo "error is {$error}";
>, I do NOT clearly understand, taking a different sample of, for
>instance:
>echo "He drank some {$beer}s";
This would output "He drank some ", followed by the content of the
variable $beer, followed by a literal "s". But what about this:

$beer = 5;
echo "He drank some $beers";

Which variable would you want PHP to use - $beer or $beers? In fact PHP
would try to use $beers, which doesn't exist, so the code will throw a
notice.
Quote:
All the explanations note that 's' is a valid character for variable
>names, but ... so what? If $beers='beers', then, OH! ... Are these
>examples trying to say that one cannot make a var plural of, say,
>$beer='beer' by simply adding the "s" to it? Is THAT what it's
>trying to point out? Boy, if that's the case, they do a lousy job of
>it! <g>.
The {} inside a string just help to avoid ambiguities and allow more
complex expressions. See the examples in the manual.

http://www.php.net/manual/en/languag...arsing.complex
Quote:
>I considered deleting the foregoing and rewording it, then decided
>against deleting it so you can see where my head is.
>
>Is the following correct?
>IF $beer = 'beer'
then printing/echoing "beers" is {$beer}s, and NOT $beers ;
>correct?
No, as shown above. The parser sees $beers and tries to use that
variable. If you want it to use $beer instead, you have to use curly
braces.
Quote:
>That seems suspicious to me because I can't see why anyone
>would try to do it the wrong way in that case. So though I'm not
>comfortable with that interpretation right now, it's the only one that
>seems to make sense to me.
>
>If that's not the case then I'm still lost: Net.php, w3schools and
>several tuts all seem to copy each other and explain this part in the
>same way.
>
So if I'm wrong, then can you explain a differenct significance of
>the "s" in the examples?
It's not about the "s", it's about the variable names in general and how
the parser interprets the string:

$bar = 42;
print "foo $barr";
print "foo {$bar}r";

Two different things. The {} help to tell the parser exactly how you
want it to interpret the string.
Quote:
>lol, if I haven't made a completely *clear's mud* post out of this, any
>clarification/verification would be most appreciated.
>Also, if you were just trying to confuse me with (['({fact}s'])), then
>_shame_ on you<g>! Yeah yeah, I know, all newbies have to pay their
>dues, but ... .
;)

HTH
Micha
  #8  
Old July 4th, 2008, 11:55 PM
Twayne
Guest
 
Posts: n/a
Default Re: echo format Q

.oO(Twayne)
Quote:
>
Quote:
>OHHHHHhhh, now you've gone and done it! I told myself I'd not get
>into that one yet as it was going to make me look even stupider (is
>that a word?<g>) than I've already made myself look! :^)
....
Quote:
>
The {} inside a string just help to avoid ambiguities and allow more
complex expressions. See the examples in the manual.
>
http://www.php.net/manual/en/languag...arsing.complex
Excellent reference; many thanks. Duly bookmarked & saved.

....
Quote:
>
No, as shown above. The parser sees $beers and tries to use that
variable. If you want it to use $beer instead, you have to use curly
braces.
Great. I "know" (but didn't recall) that it'd look for what was
written, not necessarily what I "meant"<g>. First time I've hooked up
curly braces to it, so that's good lesson learned. Learning &
experiencing what one learned are two different things.

....
Quote:
>
It's not about the "s", it's about the variable names in general and
how the parser interprets the string:
THANK YOU! I needed to hear that.
Quote:
>
$bar = 42;
print "foo $barr";
print "foo {$bar}r";
>
Two different things. The {} help to tell the parser exactly how you
want it to interpret the string.
>
As usual when I'm getting started with something new, I overthunk it!
So much for the "ThiMk" sign over my desk, too.
I have a decent handle on it now, and a couple other things too, based
on the link you provided. I always look for phpnet and w3school links
when I'm searching but I guess my search terms weren't close enough this
time.

Once more, thanks for your time & patience, & best regards,

Twayne



  #9  
Old July 5th, 2008, 12:05 AM
Michael Fesser
Guest
 
Posts: n/a
Default Re: echo format Q

..oO(Twayne)
Quote:
>[...]
>
>Once more, thanks for your time & patience, & best regards,
You're welcome.

Micha
  #10  
Old July 5th, 2008, 10:25 AM
Geoff Berrow
Guest
 
Posts: n/a
Default Re: echo format Q

Message-ID: <LOxbk.275$9W.114@trndny04from Twayne contained the
following:
Quote:
Quote:
>It's not about the "s", it's about the variable names in general and
>how the parser interprets the string:
>
>THANK YOU! I needed to hear that.
I just love a good ker-ching moment...

--
Geoff Berrow 0110001001101100010000000110
001101101011011001000110111101100111001011
100110001101101111001011100111010101101011
  #11  
Old July 6th, 2008, 04:05 PM
Norman Peelman
Guest
 
Posts: n/a
Default Re: echo format Q

Twayne wrote:
Quote:
Quote:
>Twayne wrote:
Quote:
>>Hi,
>>>
>>Irrelevant question time, probably:
>>>
>>Is there any advantage/reason to use one format over the other for
>>the following two types of echo statements?
>>>
>>1. echo "error is " . $error;
>>2. echo "error is $error";
>>>
>
...
>
Quote:
Quote:
>>... Mostly, so that I pick up the
>>preferred habit if nothing else.
>>>
>>TIA,
>>>
>>Twayne
>Rather than
>2. echo "error is $error";
>>
>you should get used to using:
>2. echo "error is {$error}";
>>
>And
>1. echo "error is " . $error;
>>
>is better as:
>1. echo 'error is ' . $error;
>
>
>
OHHHHHhhh, now you've gone and done it! I told myself I'd not get into
that one yet as it was going to make me look even stupider (is that a
word?<g>) than I've already made myself look! :^)
>
OK, I understand:
-- Curly braces define begin/end and thus helps avoid spaces etc.;
extends a line to multiple lines; keeps things related to each other;
and in my case still confuse me;
-- because, although I understand echo "error is {$error}";
, I do NOT clearly understand, taking a different sample of, for
instance:
echo "He drank some {$beer}s";
All the explanations note that 's' is a valid character for variable
names, but ... so what? If $beers='beers', then, OH! ... Are these
examples trying to say that one cannot make a var plural of, say,
$beer='beer' by simply adding the "s" to it? Is THAT what it's
trying to point out? Boy, if that's the case, they do a lousy job of
it! <g>.
>
I considered deleting the foregoing and rewording it, then decided
against deleting it so you can see where my head is.
>
Is the following correct?
IF $beer = 'beer'
then printing/echoing "beers" is {$beer}s, and NOT $beers ;
correct? That seems suspicious to me because I can't see why anyone
would try to do it the wrong way in that case. So though I'm not
comfortable with that interpretation right now, it's the only one that
seems to make sense to me.
>
If that's not the case then I'm still lost: Net.php, w3schools and
several tuts all seem to copy each other and explain this part in the
same way.
>
So if I'm wrong, then can you explain a differenct significance of
the "s" in the examples?
>
I've even seen other forums where they talk about being sure they
didn't use an "s" and still don't understand why something didn't work.
They seem to talk about "s" as though it's some
super-secret-reserved-word or something and for whatever reason never
mention any other letter. But it doesn't make sense to me that way
either.
>
lol, if I haven't made a completely *clear's mud* post out of this, any
clarification/verification would be most appreciated.
Also, if you were just trying to confuse me with (['({fact}s'])), then
_shame_ on you<g>! Yeah yeah, I know, all newbies have to pay their
dues, but ... .
>
TIA,
>
Twayne
>
>
Just keep in mind that the curly braces isolate variables from the
rest of the string, and if not using concatenation '.' are required in
certain circumstances:

$array['car']['make']['color'] = 'silver';

echo "Joe drives a $array['car']['make']['color'] car."; //doesn't work
echo "Joe drives a $array[car][make][color] car."; //doesn't work

echo "Joe drives a {$array['car']['make']['color']} car."; //works

Points to remember:

Single quotes around array keys are not needed when the array variable
is embedded inside double quoted string. But they are required when
using curly braces to isolate the variable.

Single quotes around array key names are required inside curly braces.

Curly braces are required around multi-dimensional arrays.



--
Norman
Registered Linux user #461062
-Have you been to www.php.net yet?-
  #12  
Old July 6th, 2008, 06:15 PM
Twayne
Guest
 
Posts: n/a
Default Re: echo format Q

....
Quote:
>
Just keep in mind that the curly braces isolate variables from the
rest of the string, and if not using concatenation '.' are required in
certain circumstances:
>
$array['car']['make']['color'] = 'silver';
>
echo "Joe drives a $array['car']['make']['color'] car."; //doesn't
work echo "Joe drives a $array[car][make][color] car."; //doesn't work
>
echo "Joe drives a {$array['car']['make']['color']} car."; //works
>
Points to remember:
>
Single quotes around array keys are not needed when the array variable
is embedded inside double quoted string. But they are required when
using curly braces to isolate the variable.
>
Single quotes around array key names are required inside curly braces.
>
Curly braces are required around multi-dimensional arrays.

Hmm, that's wording I can remember and good clarification that's worthy
of my lists too.

Thanks, Norman

Twayne



 

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles