|
Posted by Norman Peelman on July 6, 2008, 11:02 am
Please log in for more thread options 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 ";
>>
>> 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 ";
> , I do NOT clearly understand, taking a different sample of, for
> instance:
> echo "He drank some 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 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 (['(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 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?-
|