Hello friends. I am using this code.. thats the first time ever i wrote a long php code myself.. but it doesnt works it gives " unexpected T_STRING " error. can anyone please tell me why is it giving error?? <?php $thisPage="Home"; ?> <html><head>......</head><body> <?php if ($thisPage!="Home") echo "<div class="footer"> <a href='javascript:history.go(-1)'> [ Back ]</a> </div>"; ?> </body> </html>
<?php if ($thisPage!="Home") { echo "<div class=\"footer\"> <a href='javascript:history.go(-1)'> [ Back ]</a> </div>"; } ?>
That tells PHP to ignore those quotes. Without them PHP tries to parse them and therefore the script errors out.
the { } tells PHP that everything inside the curly brackets should happen if ($thispage != "Home") This is useful if you want to have more than one command in your if statement. In this case it's not really NEEDED because there's only one command after the IF statement. But it's good coding practice to use it.
but we can use elseif syntax to have more commands.. why did u write { } ?? is there any special use of it?
Technically you do not need them for a single line, but they are a good coding practice. The brackets tell the PHP parser where to start and stop, in this case the if statement. For good clean clode it is best to use line breaks to separate the code. <?php $thisPage="Home"; ?> <html> <head>......</head> <body> <?php if ($thisPage!="Home") { echo "<div class=\"footer\"> <a href='javascript:history.go(-1)'> [ Back ]</a> </div>"; } ?> </body> </html> Code (markup):
if you had more complicated code, it would be useful. else or elseif commands only happen if the condition in your IF statement is not true. so for example: <?php if ($thisPage!="Home") { echo "<div class=\"footer\"> <a href='javascript:history.go(-1)'> [ Back ]</a> </div>"; $some_variable = 0; } else { echo "Next >>"; $some_variable = 1; } ?> PHP:
yeah i understood what you mean.. but what will happen if i dont write the brackets?? will the code work ?
Oh, I see... yes, your code should work both ways, but it's bad coding practice to not use the brackets. It's a bad habit that could cause confusion for you or anyone reading your code in the future!
hey this code isnt working.. i wrote : <?php $thisPage="Home"; ?> <html><head><body> <?php if ($thisPage!="Home") { echo "<div class=\"footer\"> <a href='javascript:history.go(-1)'> [ Back ]</a> </div>"; $page = "not home"; } else { echo "Next >>"; $page = "Home"; } ?> <?php echo "$page"; ?> </body></html> PHP: and it gave output as while it should be showing Next's link becuz i defined the page as home..
hmmmm... that's strange. try this: <?php $thisPage = "Home"; ?> <html><head><body> <? if ($thisPage != "Home") { echo "<div class=\"footer\"> <a href='javascript:history.go(-1)'> [ Back ]</a></div>"; $page = "not home"; } else { echo "Next >>"; $page = "Home"; } ?> <? echo $page; ?> </body></html> PHP: It's exactly the same as what you posted, just with some spaces added in the code... I'm not sure why that wouldn't work. Anyone else?
wait.. why did u put off the " " from <? echo $page; ?> ? ok wait.. that code was working fine.. i forgot to save the file so why did u put off the "" ? this code is working fine but it is with " " . i wanted to ask why did u put them off? <?php $thisPage = "Homefg"; ?> <html><head><body> <? if ($thisPage != "Home") { echo "<div class=\"footer\"> <a href='javascript:history.go(-1)'> [ Back ]</a></div>"; $page = "not home"; } else { echo "Next >>"; $page = "Home"; } ?> <? echo "$page"; ?> </body></html> PHP: