--- On Wed, 11/12/08, brucewhealton <[EMAIL PROTECTED]> wrote:
> Ok, I'm a bit confused on the single and double quotes. If single
> quotes does not replace the variable, then how would this work:
>
> echo('<img src="images/topbanner' . $number. '.jpg" />');
>
> The first choice from below? Would that work with the single quotes?
> I see that this would work:
>
> print "<img src='images/topbanner$number.jpg' />";
>
> I thought I was going to have to worry about escaping the < > and "
> characters but none of these solutions require that. Why?
> Thanks,
> Bruce
$x = 123;
print "The value of x is $x"; // The value of x is 123
print 'The value of x is $x'; // The value of x is $x
I don't know of any HTML browsers which care if you do your element properties
with single or double quotes. For XHTML your mileage may vary.
<img src='pic.jpg'>
<img src="pic.jpg">
Combining this, if you want to output HTML with variables, it's best to contain
the entire thing in double quotes so that the variables (if simple ones) are
replaced by their value:
print "<img src='pic$x.jpg'>"; // <img src='pic123.jpg'>
This will not work as expected:
print '<img src="pic$x.jpg">'; // <img src="pic$x.jpg">
When you are breaking things out into individual constant strings and variables
which are concatenated together with the dot operator (.) then it doesn't
matter if the constant strings are enclosed with single or double quotes.
Single quotes are called "strong" quotes because they do not evaluate anything
which looks like a variable. Double quotes are "soft" quotes and they do.
Single and double quotes are also an issue for special characters you want to
be evaluated like \n or \r or \t.
You can write things like this but it is ugly and easy to make mistakes:
print "<img src=\"pic$x.jpg\">"; // <img src="pic123.jpg">
The backslashes before the double quotes "escapes" them so that they do not
have their normal meaning to the PHP interpreter: end the string which started
with a double quote.
The angle brackets (< >) are not special to PHP so they don't need to be
escaped.
I mentioned simple variable names (like $x). If you have an array like
$a['abc'] and you try to put it in a double quoted string, you can get unwanted
results:
$a['abc'] = 789;
print "Value of a is $a['abc']"; // PHP Parse error: syntax error, unexpected
T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING
If you have just $a:
print "Value of a is $a"; // Value of a is Array
But enclosing the variable in curly braces gives PHP a hint to know where the
variable name begins and ends.
print "Value of a is ${a['abc']}"; // Value of a is 789
James
ps. Always give a useful subject line that describes your problem.