Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsGeneralPHPASPPerlColdFusionFlashHTML, CSS, ScriptsBrowsers

Webmaster Forum / PHP / General PHP Topics / July 2008



Tip: Looking for answers? Try searching our database.

echo format Q

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Twayne - 04 Jul 2008 03:12 GMT
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
Jerry Stuckle - 04 Jul 2008 05:17 GMT
> Hi,
>
[quoted text clipped - 27 lines]
>
> 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.

Signature

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

Michael Fesser - 04 Jul 2008 12:36 GMT
.oO(Twayne)

>Irrelevant question time, probably:
>
[quoted text clipped - 6 lines]
>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
Twayne - 04 Jul 2008 16:14 GMT
> Hi,
>
[quoted text clipped - 27 lines]
>
> 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
Paul Lautman - 04 Jul 2008 17:11 GMT
> Hi,
>
[quoted text clipped - 27 lines]
>
> 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;
Twayne - 04 Jul 2008 18:16 GMT
>> Hi,
>>
[quoted text clipped - 5 lines]
>> 1.  echo "error is " . $error;
>> 2.  echo "error is $error";

...

>> ... Mostly, so that I pick up the
>> preferred habit if nothing else.
[quoted text clipped - 14 lines]
> 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
Michael Fesser - 04 Jul 2008 19:00 GMT
.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
[quoted text clipped - 8 lines]
>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/language.types.string.php#language.types.string.par
sing.complex


>I considered deleting the foregoing and rewording it, then decided
>against deleting it so you can see where my head is.
[quoted text clipped - 3 lines]
> 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
[quoted text clipped - 7 lines]
>   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
Twayne - 04 Jul 2008 23:50 GMT
> .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/language.types.string.php#language.types.string.par
sing.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
Michael Fesser - 04 Jul 2008 23:58 GMT
.oO(Twayne)

>[...]
>
>Once more, thanks for your time & patience, & best regards,

You're welcome.

Micha
Geoff Berrow - 05 Jul 2008 10:19 GMT
Message-ID: <LOxbk.275$9W.114@trndny04> from 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...

Signature

Geoff Berrow  0110001001101100010000000110
001101101011011001000110111101100111001011
100110001101101111001011100111010101101011

Norman Peelman - 06 Jul 2008 16:02 GMT
>>> Hi,
>>>
[quoted text clipped - 79 lines]
>
> 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.

Signature

Norman
Registered Linux user #461062
-Have you been to www.php.net yet?-

Twayne - 06 Jul 2008 18:08 GMT
...

>   Just keep in mind that the curly braces isolate variables from the
> rest of the string, and if not using concatenation '.' are required in
[quoted text clipped - 16 lines]
>
> 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
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.