user2080797
user2080797

Reputation:

JS, PHP, Html File Conventions

I have 3 questions, but I think the first 2 are very simple, so I'll ask them all here.

I normally work in C++ with SQL (and sometimes with VBA), and I'm trying to figure out the basics of JS, PHP & HTML (I've mostly got the jist of HTML and CSS).

I have 5 different reference books plus the net, but one thing I can't seem to find anything about are the file exts (.js, .php, .html). From my tests I have come to realize that you can usually run JS scripts in other file types, but PHP seems to require the .php ext.

So the questions are:

  1. Do I always have to use *.php for PHP scripting?
  2. In a SINGLE file, can I delay PHP execution by simply putting the code into a function?

eg

<?php
    function test() {echo "hello world";}
    //as opposed to:
    echo "hello world";
?>
  1. When using multiple files, are there any compelling reasons to (or not to) always put scripts in their corresponding file types (e.g. JS in *.js). Obviously this would make it easier to understand / read, especially as it grows BUT can this create problems?

Upvotes: 0

Views: 57

Answers (1)

Amarnasan
Amarnasan

Reputation: 15529

  1. No, you can use any extension you want. Even if you want, don't use extension at all. But then, tell your server what interpreter to use when he founds he has to parse a *.wtf file. I mean, you're running a the script "file.wtf" from the command line you can do it like this:

$>php file.wtf

but if the script is to be parsed by your favorite web server (like Apache) because it is part of (say) a web page, then you have to configure it to interpret .wtf files with the PHP library.

  1. By simply putting it in a function:no. But you really want to delay execution, use the sleep function

  2. Just what you said: You can mix html and javascript code in your php files, but that is very messy.

Upvotes: 1

Related Questions