basic php

Discuss how to write good code, break bad code, your current pet projects, or the best way to approach novel problems

basic php

Post by Mortecai4 on Wed Apr 30, 2008 5:24 pm
([msg=1751]see basic php[/msg])

While reading up on PHP, i came across 2 different ways to code the PHP script into the html document.

The regular:
Code: Select all
<?php
$txt = "HELLO WORLD!";
echo $txt;
?>


Other way:
Code: Select all
<script language="php">
$txt = "HELLO WORLD!";
echo $txt;
</script>


Which way should i be using? Are they both correct in syntax?
Last edited by ScrAm on Sun May 04, 2008 12:04 pm, edited 1 time in total.
Reason: Changed <code></code> to [code][/code]
Mortecai4
New User
New User
 
Posts: 44
Joined: Wed Apr 23, 2008 2:14 pm
Blog: View Blog (0)


Re: basic php

Post by BhaaL on Wed Apr 30, 2008 5:48 pm
([msg=1754]see Re: basic php[/msg])

Stick to the first one, the second is rather confusing for some browsers (yes, still!). Apart from that, in some cases, it isnt even interpreted as it should be, as opposed to the <?php ... ?> notation (depending on configuration).

And just to keep you off bad coding styles...
-> Use <?php rather than shorttag <? (because it could be disabled, and it makes problems when using xml/xhtml types).
-> Hands off the short-echo <?= syntax (which doesnt work either if shorttag is disabled).

:)
BhaaL
Poster
Poster
 
Posts: 270
Joined: Sun Apr 13, 2008 11:16 am
Blog: View Blog (0)


Re: basic php

Post by comperr on Sun May 04, 2008 8:45 am
([msg=2065]see Re: basic php[/msg])

BhaaL wrote:the second is rather confusing for some browsers (yes, still!).

not true - PHP parses it
http://www.php.net/manual/en/language.basic-syntax.php wrote:The <script language="php"> tag won't be read by browsers; it will be parsed by PHP just like <?php and ?> are.
User avatar
comperr
Poster
Poster
 
Posts: 373
Joined: Mon Apr 07, 2008 6:52 pm
Location: /dev/null
Blog: View Blog (0)


Re: basic php

Post by BhaaL on Fri May 09, 2008 10:09 am
([msg=2160]see Re: basic php[/msg])

comperr wrote:not true - PHP parses it

This only goes for .php files (and others configured to run thru the PHP processor). For .htm/l and such, it will certainly not work.
And even for .php files, I think it was possible to disable that from php.ini...you just got me confused on that, so I'm not actually sure about it either anymore.
BhaaL
Poster
Poster
 
Posts: 270
Joined: Sun Apr 13, 2008 11:16 am
Blog: View Blog (0)



Return to Programming

Who is online

Users browsing this forum: No registered users and 0 guests