Simple php problem

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
Matthewlikespie
Forum Newbie
Posts: 4
Joined: Fri Nov 27, 2009 5:56 pm

Simple php problem

Post 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
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Re: Simple php problem

Post by daedalus__ »

you forgot to escape your double quotes.
Matthewlikespie
Forum Newbie
Posts: 4
Joined: Fri Nov 27, 2009 5:56 pm

Re: Simple php problem

Post by Matthewlikespie »

escape my double quotes? :(

i dont understand, where do i have to do something with my double quotes?
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Re: Simple php problem

Post by daedalus__ »

Matthewlikespie
Forum Newbie
Posts: 4
Joined: Fri Nov 27, 2009 5:56 pm

Re: Simple php problem

Post by Matthewlikespie »

telling me to google it isnt helping...
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Re: Simple php problem

Post by daedalus__ »

well the very first result has the answer.
Matthewlikespie
Forum Newbie
Posts: 4
Joined: Fri Nov 27, 2009 5:56 pm

Re: Simple php problem

Post by Matthewlikespie »

i'm sorry, but where in that *huge* document is the answer? :banghead:

Edit: thank you, it works now that i changed the double quotes to single quotes. I don't know why though.
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Re: Simple php problem

Post by daedalus__ »

no problem!

Image
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Re: Simple php problem

Post 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.
Post Reply