In this quick post, we’re going to discuss the ins and outs of a PHP file. If you're new to PHP programming, this article should help you to understand various aspects of a PHP file.
PHP is a server-side scripting language which is mostly used to build web-based applications. These may range from a very simple blog website to a full-fledged eCommerce website for selling products online. In fact, PHP is one of the most popular server-side scripting languages, with a very healthy market share.
So when you start learning PHP programming, the first thing you should do is make yourself familiar with .php files.
What Is a PHP File?
When it comes to learning the PHP scripting language, you'll be creating and coding files with the .php extension. So what exactly is a PHP file?
Generally speaking, a PHP file is a plain-text file which contains code written in the PHP programming language. Since PHP is a server-side (back-end) scripting language, the code written in the PHP file is executed on the server. The PHP engine on the web server converts all the code written in PHP to HTML, so the resulting web page contains only HTML code when it’s sent to the client side for rendering in the user browser.
In fact, a PHP file may contain plain text, HTML tags, or code as per the PHP syntax. When you access any PHP-based web page, the request is sent to the server, where the PHP code is interpreted and executed by the PHP engine. The PHP code is converted to HTML, and it’s merged with the other plain text and HTML in the file. Finally, the completed HTML is sent to the client after the page executes.
If you view the source of any PHP file in a browser, you’ll never see the original PHP code in it, since it’s already executed and converted to HTML by the server.
That's an overview of how a PHP file works. In the next section, we’ll see an example of what a PHP file looks like.
Structure of a PHP File
In this section, we’ll quickly go through the structure of a PHP file. Let’s have a look at an example PHP file:
<?php echo "<b>Hello World</b>"; ?>
In the above example, the code which is wrapped by
?> tags is a piece of PHP code. And as we discussed in the previous section, a PHP file may contain code other than PHP, and it may be plain text or HTML tags. The code which is wrapped by
?> tags is executed on the server side. In the case of the above example, the output looks like this after PHP code execution:
In fact, the structure of a PHP file may look like:
<?php //PHP code ?> <div>other HTML tags</div> plain text <?php // more PHP code ?> <p>more HTML</p>
So as you can see, a PHP file can mix code, HTML, and plain text.
In the previous section, we saw that the PHP code is wrapped by
?> tags in a PHP file. However, the php.ini configuration file provides a directive named
short_open_tag, which allows you to use a short form of PHP’s open tag.
If you enable the
short_open_tag directive, you can wrap your PHP code between
?> instead of
?>, and it’ll work perfectly. You can set this and other directives in the php.ini configuration file. I explain how to find and use this in another post.
There is one catch—if you use the short open tag, you need to be careful when combining PHP code with XML code, since the syntax of the opening XML tag
<?xml may create issues, and that XML code would be detected as PHP.
In fact, it’s considered a best practice to avoid using a short form of PHP’s open tag.
And with that, we’ve reached the end of this quick tutorial. I hope this post has helped you to understand the basics of a PHP file. You can learn more about PHP programming in the courses and tutorials here on Envato Tuts+!
FREEWordPressLearn PHP for WordPressRachel McCollin
- PHPHow to Use PHP in HTMLSajal Soni
- PHPWhere Is php.ini, the PHP Configuration File?Sajal Soni
Or if you want to learn how to use PHP for WordPress, check out our free course.
The Best PHP Scripts on CodeCanyon
CodeCanyon is a marketplace for plugins and scripts. You can explore thousands of the best and most useful PHP scripts ever created on CodeCanyon. With a low-cost one-time payment, you can purchase these high-quality WordPress themes and improve your website experience for you and your visitors.
Here are a few of the best-selling and up-and-coming PHP scripts available on CodeCanyon for 2020.
- PHP11 Best PHP Event Calendar and Booking Scripts... and 3 Free OptionsMonty Shokeen
- PHP13 Best PHP URL Shortener Scripts (Free and Premium)Monty Shokeen
- PHP18 Best Contact Form PHP Scripts for 2021Monty Shokeen
- PHPComparing the 5 Best PHP Form Builders (And 4 Free Scripts)Monty Shokeen
- PHPCreate Beautiful Forms With PHP Form BuilderAshraff Hathibelagal
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.Update me weekly
Envato Tuts+ tutorials are translated into other languages by our community members—you can be involved too!Translate this post