Wednesday, February 26, 2020

What is a PHP File?

In this quick post, we’re going to discuss the ins and outs of a PHP file. If you are a novice 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 a 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:

In the above example, the code which is wrapped by <?php and ?> tags is a PHP code. And as we discussed in the previous section, a PHP file may contain code other than PHP, and it may be a plain text or HTML tags. The code which is wrapped by <?php and ?> tags is executed on the server side. In the case of the above example, the output looks like following after PHP code execution.

In fact, the structure of a PHP file may look like:

So as you can see, a PHP file can mix code, HTML, and plain text.

The short_open_tag Tag

In the previous section, we saw that the PHP code is wrapped by <?php and ?> 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 <? and ?> instead of <?php and ?>, 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 as a best and recommended 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+!

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.


No comments:

Post a Comment