Page 1 of 2
echoing link...
Posted: Tue Aug 12, 2003 5:55 pm
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
Posted: Tue Aug 12, 2003 6:05 pm
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
Posted: Tue Aug 12, 2003 6:06 pm
by qartis
Code: Select all
echo "<A HREF="http://mysite.com/index.php?name={$name}" TARGET="_self">Refresh</A>";
Posted: Tue Aug 12, 2003 6:10 pm
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.
Posted: Tue Aug 12, 2003 9:14 pm
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>';
?>
Posted: Wed Aug 13, 2003 9:37 am
by McGruff
thanks alot
Posted: Wed Aug 13, 2003 6:52 pm
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:
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
Posted: Wed Aug 13, 2003 6:55 pm
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)
Posted: Wed Aug 13, 2003 6:59 pm
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."
Posted: Wed Aug 13, 2003 7:01 pm
by qartis
Posted: Wed Aug 13, 2003 7:02 pm
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...
Posted: Wed Aug 13, 2003 7:08 pm
by McGruff
Code: Select all
$sentence = $noun1 . ' ' . $verb1;
Posted: Wed Aug 13, 2003 7:09 pm
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.
Posted: Wed Aug 13, 2003 7:22 pm
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
Posted: Wed Aug 13, 2003 7:28 pm
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 */