defining variables

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
keithd
Forum Newbie
Posts: 2
Joined: Mon Sep 02, 2002 10:45 pm

defining variables

Post by keithd »

I'm trying to define a variable to contain the following (literally):

<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>
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!
ritesh_shah777
Forum Newbie
Posts: 1
Joined: Mon Sep 02, 2002 11:53 pm
Location: India

Post 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;

?>
User avatar
gite_ashish
Forum Contributor
Posts: 118
Joined: Sat Aug 31, 2002 11:38 am
Location: India

nested quotes !!

Post 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:

:idea: http://www.php.net/manual/en/language.types.string.html
:idea: http://www.php.net/manual/en/ref.strings.html


regards,
keithd
Forum Newbie
Posts: 2
Joined: Mon Sep 02, 2002 10:45 pm

Post by keithd »

Thanks guys! I new there had to be a simple solution out there, I just didn't know where to find it.

:D
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

There's also (if you're brave it can be quite tempramental) heredoc format:

Code: Select all

$myvar =&lt;&lt;&lt;END
&lt;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;"&gt; 
	&lt;IMG SRC="http://images.paypal.com/images/lgo/logo1.gif" BORDER="0" ALT="Send and accept payment through PayPal!, the #1 online payment service!"&gt; 
&lt;/A&gt; 
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
User avatar
gotDNS
Forum Contributor
Posts: 217
Joined: Tue May 07, 2002 5:53 pm
Location: West Chester, PA

Post by gotDNS »

gite_ashish wrote:
hi,

You can use backslash (\) character to escape the quotes inside quotes.

Code:

Code: Select all

&lt;?php 

    $str = '&lt;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;"&gt; 
&lt;IMG SRC="http://images.paypal.com/images/lgo/logo1.gif" BORDER="0" ALT="Send and accept payment through PayPal!, the #1 online payment service!"&gt;&lt;/A&gt;'; 

?&gt;
Instead:

Code: Select all

&lt;?php

    $str = "&lt;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;'&gt; 
&lt;img src='http://images.paypal.com/images/lgo/logo1.gif' border='0' alt='Send and accept payment through PayPal!, the #1 online payment service!'&gt;&lt;/a&gt;"; 

?&gt;
later on, -Brian
User avatar
Sevengraff
Forum Contributor
Posts: 232
Joined: Thu Apr 25, 2002 9:34 pm
Location: California USA
Contact:

Post 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.
User avatar
phice
Moderator
Posts: 1416
Joined: Sat Apr 20, 2002 3:14 pm
Location: Dallas, TX
Contact:

Post 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

&lt;a href="link.html"&gt;link name&lt;/a&gt;
but if you wanted a string to have the same value, you would do:

Code: Select all

&lt;? $string = "&lt;a href="link.html"&gt;link name&lt;/a&gt;"; ?&gt;
If you need further help, just ask. :)
Image Image
Post Reply