(beginner)code not working

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
lipun4u
Forum Commoner
Posts: 82
Joined: Wed Jul 01, 2009 3:35 am
Location: Mumbai
Contact:

(beginner)code not working

Post 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 ???
BornForCode
Forum Contributor
Posts: 147
Joined: Mon Feb 11, 2008 1:56 am

Re: (beginner)code not working

Post by BornForCode »

I suppose you have set an web server for this, right?
lipun4u
Forum Commoner
Posts: 82
Joined: Wed Jul 01, 2009 3:35 am
Location: Mumbai
Contact:

Re: (beginner)code not working

Post by lipun4u »

yes

I have installed apache and configured it for php.
User avatar
Jammerious
Forum Commoner
Posts: 59
Joined: Sat Jun 27, 2009 11:30 am
Location: Slovenia (EU)

Re: (beginner)code not working

Post 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; ?>
lipun4u
Forum Commoner
Posts: 82
Joined: Wed Jul 01, 2009 3:35 am
Location: Mumbai
Contact:

Re: (beginner)code not working

Post by lipun4u »

its working by using...

<?php
phpinfo();
?>


I used the tag <?php ?> instead of <? ?>. But its not working.
User avatar
Jammerious
Forum Commoner
Posts: 59
Joined: Sat Jun 27, 2009 11:30 am
Location: Slovenia (EU)

Re: (beginner)code not working

Post by Jammerious »

Hm. Did you save your file as .php ?
lipun4u
Forum Commoner
Posts: 82
Joined: Wed Jul 01, 2009 3:35 am
Location: Mumbai
Contact:

Re: (beginner)code not working

Post by lipun4u »

Thanx for helping. Now its working when I replaced print by echo.

Can anyone suggest me why echo worked ???
Post Reply