Click to See Complete Forum and Search --> : PHP newbie question


Aronya1
12-16-2003, 04:38 PM
I'm just starting to study PHP using "PHP and MySQL for dynamic web sites" by Larry Ullman. He is using some syntax I don't follow, and I can't find any reference to what he is doing. Can someone help?

Here is an example:

echo 'You are purchasing <b>', $quantity, '</b> widget(s) at a cost of <b>$', $price. '</b> each. With tax, the total comes to <b>$, $total, '</b>.';

My question is this; What do the <b> tags do? Apparently, they don't create a line break, and I can't see any reason to have them in the code. I'm at work right now, and can't test the script without the tags to see what would happen, so I'm hoping somebody here can give me a simple explanation.

Another thing I just spotted that I haven't got my feeble old mind around yet; What is the period ( . ) for near the end of the first line? [ <b>$', $price. '</b> ]

TIA

pyro
12-16-2003, 04:48 PM
That is certainly not how I would have done that, but I'll go ahead and explain it:

The <b> is simple HTML to make the text bold, and the period at the end, is simple a period at the end of the sentence.

Now that we got that out of the way, here's how I'd do it:

Option 1 (use PHP's variable interpolation):

echo "You are purchasing <b>$quantity</b> widget(s) at a cost of <b>\\$$price</b> each. With tax, the total comes to <b>\\$$total</b>.";

Option 2 (Concatenating, with single quotes ):

[php]echo 'You are purchasing <b>'.$quantity.'</b> widget(s) at a cost of <b>$'.$price.'</b> each. With tax, the total comes to <b>$'.$total.'</b>.';

Aronya1
12-16-2003, 05:19 PM
Thanks, Pyro.

My eyes are crossing as I try to read this stuff. I'm looking at <b> but seeing <br>. Doh!!

But the period isn't at the end of the sentence. It's after the word 'price'.

Also, here's something that comes up with your examples. The book specifically states that using double dollar signs (as in your first example: $$price) would cause problems. Unfortunately, they don't elaborate on that point. Any idea what they're talking about?

Another question; Why use concatenation in your 2nd example? The explanation I've been given is that concatenation is essentially a way to add variables
together re: $first_name . $last_name

Thanks again

pyro
12-16-2003, 05:37 PM
1 - Look in your book, are you sure that's what they did? If so, I'd guess a typo, even though it will work.

2 - The $ should probably have been escaped with a \ (and now is, thanks to an edit).

3 - Yes, the . is the concatenation opperator. Which is why it works. ;) We are concatenating it all together into one string, so we can echo it out.

Aronya1
12-16-2003, 06:05 PM
I'm so confused ... !!! Gahhh! I need a drink.

They used the period that way twice. Go figure.

Escaping the $ makes more sense. Very good. That I can now understand.

Not getting the reason for concatenating in this example. You used the period almost exactly the way the book uses commas:

Book:echo 'You are purchasing <b>', $quantity, '</b> widget(s) at a cost of <b>$', $price. '</b> each. With tax, the total comes to <b>$, $total, '</b>.';
Pyro:echo 'You are purchasing <b>'.$quantity.'</b> widget(s) at a cost of <b>$'.$price.'</b> each. With tax, the total comes to <b>$'.$total.'</b>.';

Their explanation for the commas was that they are separating a series of arguments. Are your periods doing essentially the same thing here?

pyro
12-16-2003, 06:07 PM
Yes, in this case, the periods and commas are basically interchangable.

Aronya1
12-16-2003, 06:12 PM
OK, thanks much.

I'll be Bach...

pyro
12-16-2003, 06:12 PM
Happy to help. :)