Reputation: 115
I am having trouble understanding the file extension .phtml .From what i have read, it is supposed to be a way of using html and php together(please correct me if i am mistaken). What i am trying to do is very simple, I have a php variable, and if it fulfils a certain condition, some html code should be executed, if not some other html code should be executed. The code i have is:
<html>
<head>
</head>
<body>
<?php
$code = "dk";
?>
<?php if($code == "dk"): ?>
<p>
1</p>
</br>
<?php else: ?>
<p>
2</p>
</br>
<?php endif; ?>
</body>
</html>
The output I get when I save and run it as a .php file is only "1", but with .phtml I get "1 2". Can someone explain me why?
Upvotes: 3
Views: 5969
Reputation: 26732
You need to configure to run .php/.phtml
extensions, so
AddType application/x-httpd-php .php .phtml .html in your httpd.conf
file to run phtml file
else you will see the whole code block like if you save the above code in xyz.phtml
and run it via http://localhost/xyz.phtml
EDIT
After URL provided in the comments I checked the link and found your PHP code is actually rendered in the View source..please check yourself by viewing the source of this page, so 1 2
is rendered as a part of HTML u mentioned but since the server is not able to understand it silently induced the PHP logical code as well without going into the logical implementation part, hope it makes sense to you.
Upvotes: 2
Reputation: 15603
Try this:
<p>
<?php echo (($code == "dk") ? "1" :"2"); ?>
</p>
</br>
This will work on both .php and .phtml
Don't know why you are doing code redundant this is optimize code also.
Upvotes: 0
Reputation: 4446
There is no difference between .phtml and .php files. They are both intended to be a php code, which might have some HTML parts.
Your problem is that your server does not know, how to treat .phtml files. You can change the file extension to php (usually after installing the PHP changes your server settings automatically), or accept swapnesh's or J Griffiths's answer.
In fact PHP can parse any file with any extension, it might be even .html or .jpg, if you like. This does not matter*), as the header is most important.
Your code is correct from the PHP point of view, you need to change the server settings.
*) it can in some older Internet Explorer browsers
Upvotes: 1
Reputation: 729
One of PHP files' strengths / weaknesses is that you can easily mix PHP and HTML.
In your case, I think your server is not configured to interpret as PHP files that end in .phtml, so the PHP is entirely ignored. Have a look at the page source of the page that outputs "1 2" to see if it's true.
You can have any extension you want be interpreted as PHP, even .html, but that will cause extra overhead of course. If you're running Apache, and want .phtml to be parsed too, adding a line like
AddType application/x-httpd-php .php .phtml
Should do the trick. Choosing a different extension can help hide that you're using PHP, if you're not already using mod_rewrite.
Upvotes: 2