Page 1 of 1

(beginner)code not working

Posted: Wed Jul 01, 2009 3:40 am
by lipun4u
I hv just started to learn PHP. Here is my first code snippet where I hv embedded php in html.

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Script Two</title>
</head>
 
<body>
    <b> <? print "This"; ?></b>
    <font color='red'> <? print "is"; ?> </font>
    <i> <? print "Line"; ?> </i>
    <font color='blue'> <?= "Of"; ?> </font>
    <h1 <? = "text"; ?> </h1>
</body>
 
</html>
But when I run it in browser, only the title changes and nothing is rendered.
And the source file in browser is as follows,

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Script Two</title>
</head>
 
<body>
    <b> <? print "This"; ?></b>
    <font color='red'> <? print "is"; ?> </font>
    <i> <? print "Line"; ?> </i>
 
    <font color='blue'> <?= "Of"; ?> </font>
    <h1 <? = "text"; ?> </h1>
</body>
 
</html>
It shows that the dynamic HTML is not generated.

please help ???

Re: (beginner)code not working

Posted: Wed Jul 01, 2009 3:41 am
by BornForCode
I suppose you have set an web server for this, right?

Re: (beginner)code not working

Posted: Wed Jul 01, 2009 3:48 am
by lipun4u
yes

I have installed apache and configured it for php.

Re: (beginner)code not working

Posted: Wed Jul 01, 2009 3:52 am
by Jammerious
You can check if your webserver with PHP is running correctly with this simple script:

Code: Select all

<?php 
phpinfo(); 
?>
A page with your server settings, installed modules etc. should appear.

Then, make sure you declare the script type, so use <?php ?> instead of just <? ?> . If I am not mistaken, the latter could resolve to ASP or something too.
Next, again, if I am not mistaken, the in-line return of the variable is deprecated, so instead of <?=$myVar ?> use <?php echo $myVar; ?>

Re: (beginner)code not working

Posted: Wed Jul 01, 2009 3:57 am
by lipun4u
its working by using...

<?php
phpinfo();
?>


I used the tag <?php ?> instead of <? ?>. But its not working.

Re: (beginner)code not working

Posted: Wed Jul 01, 2009 3:58 am
by Jammerious
Hm. Did you save your file as .php ?

Re: (beginner)code not working

Posted: Wed Jul 01, 2009 4:04 am
by lipun4u
Thanx for helping. Now its working when I replaced print by echo.

Can anyone suggest me why echo worked ???