473,385 Members | 1,769 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 1823
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: entoone | last post by:
I have a form that has a text box, i.e. text area that takes 200 characters max. When I use my update script and it brings up the information in the database, it displays as a single line. How can...
2
by: RT | last post by:
How would I format a timestamp which is used to show when a quote was created in a record set. This is the echo - <?php echo($row_rsquote); ?> I tried doing this - <?php echo date('D,...
4
by: hamil | last post by:
I wrote a VB.net "echo" program using the Tcp client/listener class to determine how fast the transactions would occur. I got a speed of about 160 echoes per second. A friend did the same test...
14
by: Khai | last post by:
I'm an extreme newbie, and have been nosing around the PHP.Net site for a few days. Only, the search function returns alot of information that's not truly relevant due to Coding samples at the...
4
by: weirdstuff | last post by:
Hi. I have this simple code: =========================================== ->Database query here (.. some code) $row=mysql_fetch_array($res); (...)
6
by: ken | last post by:
is there a way to make a php page only output content that is generated from php statements, such as print, or echo? ie: if I have whitespace (or other text) outside my <?php .... ?block i do not...
9
by: Jon Slaughter | last post by:
I have to output a lot of html stuff mixed with php and I'm curious if its better to echo the html code or stop the php parser temporarily to got into "html mode"? Is there any drawbacks to either...
8
by: mandanarchi | last post by:
<?php $td_sku="1088963"; //Following the XML data $xmldata="<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r <OnlineCheck>\r <Header>\r <BuyerAccountId>----------</BuyerAccountId>\r...
32
by: Request-1 | last post by:
hi folks, html coder here, new and terrified * of php!! aaaaaa! i'm trying to bury a JS script to rotate a photo, in a page i converted from html to php. the conversion went well, it was to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.