Page 1 of 1

Simple/silly question

Posted: Sun Oct 24, 2010 4:18 am
by Dakke
Can someone tell me (being new) why this does not work:
I have a test.php in same dir, a test.js in same dir and the js works, the php does not. I tried the test.php and that actually prints. The test.php has the very simple lines:

Code: Select all

<?php
     
     echo("Hello World");
?>

Code: Select all

<html>
<head>
</head>

<body style="background-color:#fff;margin:0">
<div style="border-top:1px solid #404040">
1. JS: source file <br />
<script type="text/javascript" src="test2.js"/>
</script>
<br />----<br />

2. PHP: source file<br />
<script type="text/javascript" src="test.php"/>
</script>
<br />----<br />

3. JS: script inside<br />
<script type="text/javascript">
   
   document.write('<b></br></br>Hello World</br></b>');
    
</script>
<br />----<br />

4. PHP: script inside <br />
<script type="text/php">
   
   echo("Hello World");
   print "hello world";
</script>

<br />----<br />


</div></div></body></html>

Re: Simple/silly question

Posted: Sun Oct 24, 2010 6:56 am
by requinix
#1 and #3 are fine.

#2 does not work. You told the browser to get "test.php" and execute it as if it was JavaScript code. Since the PHP will be executed on the server you're left with "Hello World", which is totally not valid JavaScript code.

#4 does not work for two reasons:
1. Your browser cannot execute PHP.
2. PHP will only intercept <script>s that begin with

Code: Select all

<script language="php">
and even then I'm not 100% sure of that.

There are only two methods you should use: <?php and <?. The former always works and is much easier to type out than a <script> tag. Get yourself an editor with PHP support and there's no reason for a <script>. The latter works sometimes (depending on the PHP configuration) and can be used as a handy shortcut.

Re: Simple/silly question

Posted: Sun Oct 24, 2010 7:22 am
by s.dot
tasairis wrote:2. PHP will only intercept <script>s that begin with

Code: Select all

<script language="php">
and even then I'm not 100% sure of that.
Yep, <script language="php"> is as portable as <?php - it always works, and yet i never see it used and i never use it.. us lazy people :D
tasairis wrote:There are only two methods you should use: <?php and <?. The former always works and is much easier to type out than a <script> tag. Get yourself an editor with PHP support and there's no reason for a <script>. The latter works sometimes (depending on the PHP configuration) and can be used as a handy shortcut.
Wow, did not expect this :D one should only use <?php (unless they're feeling typey and want to use the script tag)

Re: Simple/silly question

Posted: Sun Oct 24, 2010 7:58 am
by requinix
s.dot wrote:Wow, did not expect this :D one should only use <?php
Yeah, I used to think that. I still do, really, but I've become more open-minded to the short tags. Since they can be enabled at just about any point the issue of having it configured isn't such an issue. And besides, which would you rather have:

Code: Select all

<input type="checkbox" name="option[<?php echo $option_name; ?>]" value="<?php echo $option_value; ?>" /> <?php echo $option_label; ?>

Code: Select all

<input type="checkbox" name="option[<?=$option_name?>]" value="<?=$option_value?>" /> <?=$option_label?>
Syntax highlighting makes the second much easier on (at least my) eyes. No "php" in one color, "echo" in another, and variable names in a third.
I've also learned that as much as I may tell people to avoid short tags, if they want to use them then they will. Can't help that.

So yeah, I've joined the Dark Side. But you know what? It's nice here. We've got cookies and we even validate parking.

Re: Simple/silly question

Posted: Sun Oct 24, 2010 8:24 am
by Eran
So yeah, I've joined the Dark Side. But you know what? It's nice here. We've got cookies and we even validate parking.
Yeah, but are those dark cookies? you think about that

Personally I find the short-tag syntax a bit less readable because it's easy for me to miss it inside the HTML syntax. The <?php syntax with the spacing is easier for me to catch.

Re: Simple/silly question

Posted: Sun Oct 24, 2010 9:46 am
by Dakke
Hmmm.

So supposedly (I'd be happy to make another noob error), this should work:

Code: Select all

<script language="php">
   
   echo("Hello World");
   print "hello world";
   
</script>
Yet, it does not on my mamp local install. The goal here is to run PHP scripts and port them to desktop and mobile, using appcelerator. Yet, trying it out on a local index.html before delving into appcelerator.

An example of two files that work, would be highly appreciated. :)

Re: Simple/silly question

Posted: Sun Oct 24, 2010 9:49 am
by s.dot
save it as index.php
If your web server is set up correctly it will work. It works on mine.

Re: Simple/silly question

Posted: Sun Oct 24, 2010 9:55 am
by Dakke
Yes, I know that, yet the appcelerator will only do .html (as far as I know).

Is there a way to use the .html and not go to .php?

Re: Simple/silly question

Posted: Sun Oct 24, 2010 9:58 am
by s.dot
On a webserver, yes, using apache's mod rewrite module.
However, I'm not familiar with appcelerator so I don't know.
Maybe someone else will be familiar with it.

Re: Simple/silly question

Posted: Sun Oct 24, 2010 11:51 pm
by requinix
Don't even need mod_rewrite for it (though it can certainly be done that way).

Code: Select all

<FilesMatch "\.html?$">
    SetHandler application/x-httpd-php
</FilesMatch>