Page 1 of 1
defining variables
Posted: Mon Sep 02, 2002 10:45 pm
by keithd
I'm trying to define a variable to contain the following (literally):
but I can't seem to get it to work because the text must contain both single and double quotes, so I can't use either. How can this be done?
Please help!
Posted: Mon Sep 02, 2002 11:53 pm
by ritesh_shah777
// Just Escape " with \" see how it work below by running this script in browser
<?php
echo $myVar = " <A HREF=\"
https://www.paypal.com/affil/pal=ng%40mail.utexas.edu\" target=
\"_blank\" onmouseover=\"window.status='http://www.paypal.com';return true;\" onmouseout=
\"window.status=' ';return true;\"> <IMG SRC=\"
http://images.paypal.com/images/lgo/logo1.
gif\" BORDER=\"0\" ALT=\"Send and accept payment through PayPal!, the #1 online payment s
ervice!\"></A> ";
echo "The Result is :<BR> ".$myVar;
?>
nested quotes !!
Posted: Mon Sep 02, 2002 11:53 pm
by gite_ashish
hi,
You can use backslash (\) character to escape the quotes inside quotes.
Code: Select all
<?php
$str = '<HREF="https://www.paypal.com/affil/pal=ngmail.utexas.edu" target="_blank" onmouseover="window.status=''http://www.paypal.com'';return true;" onmouseout="window.status='' '';return true;">
<IMG SRC="http://images.paypal.com/images/lgo/logo1.gif" BORDER="0" ALT="Send and accept payment through PayPal!, the #1 online payment service!"></A>';
?>
See also php man's string related sections:
http://www.php.net/manual/en/language.types.string.html
http://www.php.net/manual/en/ref.strings.html
regards,
Posted: Mon Sep 02, 2002 11:56 pm
by keithd
Thanks guys! I new there had to be a simple solution out there, I just didn't know where to find it.

Posted: Tue Sep 03, 2002 1:47 am
by twigletmac
There's also (if you're brave it can be quite tempramental) heredoc format:
Code: Select all
$myvar =<<<END
<A HREF="https://www.paypal.com/affil/pal=ng%40mail.utexas.edu" target="_blank" onmouseover="window.status='http://www.paypal.com'; return true;" onmouseout="window.status=' '; return true;">
<IMG SRC="http://images.paypal.com/images/lgo/logo1.gif" BORDER="0" ALT="Send and accept payment through PayPal!, the #1 online payment service!">
</A>
END;
There's a bit of information on it in the documentation for
echo():
php docs wrote:echo <<<END
This uses the "here document" syntax to output
multiple lines with $variable interpolation. Note
that the here document terminator must appear on a
line with just a semicolon no extra whitespace!
END;
I like it because it means I can cut and paste HTML without worrying about backslashes and it retains the formatting (tabs, linebreaks) that I give it so my source code looks pretty. But it can be a pain sometimes because it can produce odd parse errors if you get things slightly wrong. The main things to look out for are spaces after <<<END and END; (and any before END; too).
Mac
Posted: Wed Sep 04, 2002 5:22 pm
by gotDNS
gite_ashish wrote:
hi,
You can use backslash (\) character to escape the quotes inside quotes.
Code:
Code: Select all
<?php
$str = '<HREF="https://www.paypal.com/affil/pal=ngmail.utexas.edu" target="_blank" onmouseover="window.status=''http://www.paypal.com'';return true;" onmouseout="window.status='' '';return true;">
<IMG SRC="http://images.paypal.com/images/lgo/logo1.gif" BORDER="0" ALT="Send and accept payment through PayPal!, the #1 online payment service!"></A>';
?>
Instead:
Code: Select all
<?php
$str = "<a href='https://www.paypal.com/affil/pal=ngmail.utexas.edu' target='_blank' onmouseover='window.status='http://www.paypal.com';return true;' onmouseout='window.status=' ';return true;'>
<img src='http://images.paypal.com/images/lgo/logo1.gif' border='0' alt='Send and accept payment through PayPal!, the #1 online payment service!'></a>";
?>
later on, -Brian
Posted: Wed Sep 04, 2002 11:10 pm
by Sevengraff
is it possible to do an inline thing? like....
Code: Select all
<?PHP
$var = " ?>
<A HREF="https://www.paypal.com/affil/pal=ng%40mail.utexas.edu" target="_blank" onmouseover="window.status='http://www.paypal.com';return true;" onmouseout="window.status=' ';return true;">
<IMG SRC="http://images.paypal.com/images/lgo/logo1.gif" BORDER="0" ALT="Send and accept payment through PayPal!, the #1 online payment service!"></A>
<?
";
echo("$var");
?>
because i know that you can do stuff like that for other things.
Posted: Wed Sep 04, 2002 11:34 pm
by phice
All you have to do is if you want to have certain quotes (") inside your variable, then just put a foreward-slash (\) infront of each one. In example; if you wanted the code (in HTML)...
Code: Select all
<a href="link.html">link name</a>
but if you wanted a string to have the same value, you would do:
Code: Select all
<? $string = "<a href="link.html">link name</a>"; ?>
If you need further help, just ask.
