Page 1 of 1
Simple php problem
Posted: Fri Nov 27, 2009 6:00 pm
by Matthewlikespie
One of my first times using php, and it says theres a parse error when i try to run this.
Code: Select all
<?php
$random = rand(1,3);
if($random == 1)
echo "<h1 style="color:red;font-size:40;">Random Page</h1>";
elseif($random == 2)
echo "<h1 style="color:blue;font-size:40;">Random Page</h1>";
else
echo "<h1 style="color:green;font-size:40;">Random Page</h1>";
?>
What i want is to sometimes when loading hte page show as red, blue, or green text. Right now, it doesn't work at all and i have no idea why. Thanks in advance
-Matthew
Re: Simple php problem
Posted: Fri Nov 27, 2009 6:17 pm
by daedalus__
you forgot to escape your double quotes.
Re: Simple php problem
Posted: Fri Nov 27, 2009 7:09 pm
by Matthewlikespie
escape my double quotes?
i dont understand, where do i have to do something with my double quotes?
Re: Simple php problem
Posted: Fri Nov 27, 2009 8:07 pm
by daedalus__
Re: Simple php problem
Posted: Fri Nov 27, 2009 8:32 pm
by Matthewlikespie
telling me to google it isnt helping...
Re: Simple php problem
Posted: Fri Nov 27, 2009 8:34 pm
by daedalus__
well the very first result has the answer.
Re: Simple php problem
Posted: Fri Nov 27, 2009 8:45 pm
by Matthewlikespie
i'm sorry, but where in that *huge* document is the answer?
Edit: thank you, it works now that i changed the double quotes to single quotes. I don't know why though.
Re: Simple php problem
Posted: Fri Nov 27, 2009 8:57 pm
by daedalus__
no problem!

Re: Simple php problem
Posted: Fri Nov 27, 2009 9:00 pm
by daedalus__
oh its because
you open a string like so
$the_jacka = "tear gas is a dope album!
then you close it with "
so what happens when you say
$the_jacka = "said "my boy got botched on his first case, hate to see the look on his mommas face". it was such a dope verse!";
?
you opened, closed, then later re-opened the string leaving the parts in the middle outside of it.
so if you want to print the same string character you enclose it in inside the literal you have to escape it with a backslash
to avoid this i always use two things, syntax highlighting editor (notepad++) and if i think i might use double quotes in a string i always use a single quote to open it.