3.2: Creating PHP Files
- Page ID
Like the HTML and CSS files we have already created, PHP also uses a special file format to identify its contents. When you want to use PHP in a file, even if it was already .htm or .html, you will need to set (or create) the file format as .php.
If you do not have access to a server with PHP, you can follow along this section of the text by using writecodeonline.com/php/ to try the examples and write your own code.
Technically, you could insert PHP code into HTML files (or other formats) and have it run, by changing settings on your server for how it serves and interacts with the file extension in question. You could also do the same with HTML in a text file, or other combinations. The drawback is this could also affect other files on your server, and makes your site less portable to other servers.
Long, Short Tags
As you begin to work with PHP, you will undoubtedly see code examples that begin with
<?while both will end with
?>. These are tags, just like in HTML, and are used to mark the start and end of a section of code that contains PHP (we could even use
<?php is longhand writing, while
<? Is considered shorthand. By default, all PHP capable servers will recognize longhand while shorthand is an option that must be enabled. For best support of your code, and to better recognize what language is being used, always use longhand when writing your code.