开发者

while mysql_fetch_array adding letters before or after the result?

开发者 https://www.devze.com 2023-04-02 19:14 出处:网络
I\'d like to display a price in different currencies, starting with only the numerical value and then appending the currency. I tried this way:

I'd like to display a price in different currencies, starting with only the numerical value and then appending the currency. I tried this way:

echo $ $row['price']
// or
echo £ $row['price']

but I receive 开发者_如何学Pythonan error, why?


First, what I suggest is reading the error and trying to understand what it's trying to tell you. Often, errors don't just break everything, but most importantly try to give you clues as to why you might be wrong! Your code is speaking to you, listen to it!

Second, and I think more importantly, try reading introduction documentation before asking a question here. While we love helping out newbies, we can't read the documentation for them, else we would be doing it all day.

The PHP documentation is extremely well made, so you should begin there. For example, you might want to read on variables and basic syntax. You would have learned that every instruction in PHP needs a semi-colon at the end, and that everything you want to be printed out on the screen needs to be marked as a string.

Let's take a look at your code:

echo $ $row['price']

Returns the error: Parse error: syntax error, unexpected $end, expecting ',' or ';' in [...]

It says "expecting ;". It should give you a clue to put a semi-colon at the end of your line.

Now you know about strings and semi-colons, you should be writing:

echo '$' $row['price']

But oh, another error!

Parse error: syntax error, unexpected T_VARIABLE, expecting ',' or ';' in

"Unexpected variable". Hmm, looks like PHP didn't like getting a second variable didn't it? Let's take a look at the documentation. Every time you want to know more about something in PHP, just go to php.net/[word]. Let's try with php.net/echo. Turns out, you have to either pass your string to echo as one or two arguments. Either separate them with a comma to make them two arguments, or merge them in one string! Your choice!

And tada,

echo '$'.$row['price'];

it should work!


Very easy:

echo '$' . $row['price'];
echo '£' . $row['price'];


Read this so you know exactly what can (and can't) be done with PHP strings.

You need to do this:

echo '$ '.$row['price'];

...or this...

echo "£ {$row['price']}";


This is pretty basic string concatenation...

echo "$ " . $row['price'];


This any good?

echo "${$row['price']}"
or
echo "£{$row['price']}";
0

精彩评论

暂无评论...
验证码 换一张
取 消