470,619 Members | 1,545 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,619 developers. It's quick & easy.

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
Jul 4 '08 #1
11 1705
Twayne wrote:
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.
js*******@attglobal.net
==================

Jul 4 '08 #2
..oO(Twayne)
>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
Jul 4 '08 #3
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
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

Jul 4 '08 #4
Twayne wrote:
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;
Jul 4 '08 #5
Twayne wrote:
>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";
....
>... 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
Jul 4 '08 #6
..oO(Twayne)
>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.
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
>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.
>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.
>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
Jul 4 '08 #7
.oO(Twayne)
>
>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! :^)
....
>
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.

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

....
>
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.
>
$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

Jul 4 '08 #8
..oO(Twayne)
>[...]

Once more, thanks for your time & patience, & best regards,
You're welcome.

Micha
Jul 4 '08 #9
Message-ID: <LOxbk.275$9W.114@trndny04from Twayne contained the
following:
>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
Jul 5 '08 #10
Twayne wrote:
>Twayne wrote:
>>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";

...
>>... 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?-
Jul 6 '08 #11
....
>
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

Jul 6 '08 #12

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by entoone | last post: by
2 posts views Thread by RT | last post: by
4 posts views Thread by hamil | last post: by
14 posts views Thread by Khai | last post: by
9 posts views Thread by Jon Slaughter | last post: by
8 posts views Thread by mandanarchi | last post: by
32 posts views Thread by Request-1 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.