Simple/silly question

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
Dakke
Forum Newbie
Posts: 24
Joined: Fri Aug 10, 2007 6:34 pm

Simple/silly question

Post 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>
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Simple/silly question

Post 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.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: Simple/silly question

Post 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)
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Simple/silly question

Post 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.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Simple/silly question

Post 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.
Dakke
Forum Newbie
Posts: 24
Joined: Fri Aug 10, 2007 6:34 pm

Re: Simple/silly question

Post 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. :)
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: Simple/silly question

Post by s.dot »

save it as index.php
If your web server is set up correctly it will work. It works on mine.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Dakke
Forum Newbie
Posts: 24
Joined: Fri Aug 10, 2007 6:34 pm

Re: Simple/silly question

Post 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?
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: Simple/silly question

Post 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.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Simple/silly question

Post 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>
Post Reply