echoing link...

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

npodges
Forum Newbie
Posts: 12
Joined: Tue Aug 12, 2003 5:55 pm

echoing link...

Post by npodges »

Hey, I've a quick question...
I'm real new to PHP, and I made a random insult page, per se, just to try out php. It gets the name from the link like:
mysite.com/index.php?name=xxx
then, i have a refresh button, so once the person has gone on, and seen their insult, they can refresh the page, with their name still in there.
i tried this:

Code: Select all

echo "<A HREF='http://mysite.com/index.php?name=";
echo $name;
echo "' TARGET='_self'\>refresh.</A>";
this works fine, but i want to do it without using the single quotes in the HTML. When i use double quotes, i only get errors. how can i get around this?
thanks,
nick podges
Drachlen
Forum Contributor
Posts: 153
Joined: Fri Apr 25, 2003 1:16 am

Post by Drachlen »

place a backslash behind all quotes you dont want to end an echo:

Code: Select all

<?php
echo "<A HREF="http://mysite.com/index.php?name=$name" TARGET="_SELF">";
?>
You dont have to break it into 3 lines either
qartis
Forum Contributor
Posts: 271
Joined: Sat Dec 14, 2002 4:43 pm
Location: BC, Canada
Contact:

Post by qartis »

Code: Select all

echo "<A HREF="http://mysite.com/index.php?name={$name}" TARGET="_self">Refresh</A>";
Bennettman
Forum Contributor
Posts: 130
Joined: Sat Jun 15, 2002 3:58 pm

Post by Bennettman »

Use escaping slashes. When you use echo "string"; and have doublequotes inside the string, PHP thinks the dblquote in the string is the end of the command, and throws up an error because of the rest of the text.

Code: Select all

echo "<A HREF="http://mysite.com/index.php?name=" . $name . "" TARGET="_self">refresh.</A>";
Basically, everywhere there's a doublequote, put a backward slash in front of it. The example above separates $name from the rest of the string, but that isn't necessary. The below works too:

Code: Select all

echo "<A HREF="http://mysite.com/index.php?name=$name" TARGET="_self">refresh.</A>";
You can also use another method, which does away with having to use escaping slashes. However, you can't use arrays and commands, this is purely strings and variables:

Code: Select all

echo <<<sometext
<A HREF="http://mysite.com/index.php?name=$name" TARGET="_self">refresh.</A>
sometext;
"sometext" can be anything you like - I usually use "HTML". There must be a linebreak before the string starts, and underneath the string you must have that word with a semicolon after it, on it's own line.
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Or, if you use echo 'string'; you need to escape slashes on the single quotes.

Code: Select all

<?php
    echo '<A HREF="http://mysite.com/index.php?name='.$name.'" TARGET="_self">Refresh ''em</A>';
?>
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

npodges
Forum Newbie
Posts: 12
Joined: Tue Aug 12, 2003 5:55 pm

thanks alot

Post by npodges »

thanks alot guys, i got it working, and now i'm moving on...
to: a random sentence generator.
i've got several text files (nouns, verbs, adverbs, etc.) from which i make arrays to select a random word. one of these is called sentences. and has
the different possible sentence structures
ie. "$article $adj1 $snoun1 $verb1 $prep1 $article $pnoun1"
so i name all of the variables, so each of them has a random word, and i want to display the sentance.
i tried this:

Code: Select all

echo $sentence;
but that displays "$art $adj1 $snoun1 $verb1 $prep1 $art $pnoun1 "
how can i make is show the what the variables hold, not the variables themselves?
thanks,
nick podges
qartis
Forum Contributor
Posts: 271
Joined: Sat Dec 14, 2002 4:43 pm
Location: BC, Canada
Contact:

Post by qartis »

Are you ever using ' instead of "?

When asked to display '$variable', php won't interpret $variables for their values if surrounded by single-quotes (as opposed to double-quotes)
npodges
Forum Newbie
Posts: 12
Joined: Tue Aug 12, 2003 5:55 pm

Post by npodges »

no... let me give you an example:
$noun1 = dog
$verb1 = ate

$sentence = $noun1 $verb1

when i echo $sentence, it returns "$noun1 $verb1"
i want it to return "dog ate."
qartis
Forum Contributor
Posts: 271
Joined: Sat Dec 14, 2002 4:43 pm
Location: BC, Canada
Contact:

Post by qartis »

Try

Code: Select all

$sentence = "$noun1 $verb1";
npodges
Forum Newbie
Posts: 12
Joined: Tue Aug 12, 2003 5:55 pm

Post by npodges »

i cant, i am importing $sentence from a text file. i want to have a random set of sentence structures. if i just wanted one structure, with random words, i'd do it that way...
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

Code: Select all

$sentence = $noun1 . ' ' . $verb1;
Last edited by McGruff on Wed Aug 10, 2005 10:34 pm, edited 1 time in total.
qartis
Forum Contributor
Posts: 271
Joined: Sat Dec 14, 2002 4:43 pm
Location: BC, Canada
Contact:

Post by qartis »

so you have a string, like '$verb1 $noun1', and you want to substitute the proper variables? Try this:

Code: Select all

$string = '$variable1 $variable2';
$variable1 = "Sexy";
$variable2 = "beast";
$string = "echo "".$string."";";
ob_start();
eval($string);
$output_string = ob_get_clean();
And now you have the filled-in $output_string.
npodges
Forum Newbie
Posts: 12
Joined: Tue Aug 12, 2003 5:55 pm

Post by npodges »

thanks, i did that, and its working now, but i still get an error... heres what i get when i open the page:

the beautiful lamp threw above the snacks
Fatal error: Call to undefined function: ob_get_clean() in /host/g/o/o/p/o/r/goomba300.port5.com/sentence.php on line 49

do you know why i got this error? if not, could you explain the whole ob_get_clean function?
thanks,
nick podges
qartis
Forum Contributor
Posts: 271
Joined: Sat Dec 14, 2002 4:43 pm
Location: BC, Canada
Contact:

Post by qartis »

Sorry, ob_get_contents() is only in php version 4.3.0 and higher. Try this instead:

Code: Select all

//Snip
ob_start(); /* This line turns on output buffering, which stores all of the script's output in a buffer */
eval($string); /* This line evalates $string as pure php code, and sends the output to the buffer */
$output_string = ob_get_contents(); /* This line stores the buffer's contents (the result of eval($string)) as $output_string */
ob_end_clean(); /* This line clears the buffer, so no output from eval() is sent to the browser */
Post Reply