Hướng dẫn what are files in php? - tập tin trong php là gì?

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 <?php and ?> 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 <?php and ?> tags is executed on the server side. In the case of the above example, the output looks like this after PHP code execution:

<b>Hello World</b>

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.

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

<b>Hello World</b>
2 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

<b>Hello World</b>
6 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+!

Or if you want to learn how to use PHP for WordPress, check out our free course.

Hướng dẫn what are files in php? - tập tin trong php là gì?

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.

Hướng dẫn what are files in php? - tập tin trong php là gì?
Hướng dẫn what are files in php? - tập tin trong php là gì?
Hướng dẫn what are files in php? - tập tin trong php là gì?

Here are a few of the best-selling and up-and-coming PHP scripts available on CodeCanyon for 2020.

Bạn có thấy bài đăng này hữu ích?

Hướng dẫn what are files in php? - tập tin trong php là gì?

Kỹ sư phần mềm, FSPL, Ấn Độ

Tôi là một kỹ sư phần mềm chuyên nghiệp, và tôi đã thực hiện kỹ thuật khoa học máy tính. Đã khoảng 14 năm tôi đã làm việc trong lĩnh vực phát triển trang web và công nghệ nguồn mở. Chủ yếu, tôi làm việc trên các dự án và khung dựa trên PHP và MySQL. Trong số đó, tôi đã làm việc trên các khung web như Codeignitor, Symfony và Laravel. Ngoài ra, tôi cũng có cơ hội làm việc trên các hệ thống CMS khác nhau như Joomla, Drupal và WordPress và các hệ thống thương mại điện tử như Magento, Opencart, WooC Commerce và Drupal Commerce. Tôi cũng muốn tham dự các hội nghị công nghệ cộng đồng, và là một phần của điều đó, tôi đã tham dự Hội nghị Thế giới Joomla 2016 được tổ chức tại Bangalore (Ấn Độ) và 2018 Drupalcon được tổ chức tại Mumbai (Ấn Độ). Ngoài ra, tôi thích đi du lịch, khám phá những địa điểm mới và nghe nhạc!

Các tệp PHP được gọi là gì?

Một tệp có phần mở rộng tệp PHP là tệp mã nguồn PHP có chứa mã tiền xử lý siêu văn bản.Chúng thường được sử dụng làm tệp trang web thường tạo HTML từ động cơ PHP chạy trên máy chủ web.PHP source code file that contains Hypertext Preprocessor code. They are often used as web page files that usually generate HTML from a PHP engine running on a web server.

Tại sao các tệp được sử dụng trong PHP?

Hệ thống tệp PHP cho phép chúng tôi tạo tệp, đọc từng dòng, đọc ký tự tệp theo ký tự, ghi tệp, phụ lục tệp, xóa tệp và đóng tệp.allows us to create file, read file line by line, read file character by character, write file, append file, delete file and close file.

Tệp và thư mục trong PHP là gì?

Các chức năng thư mục PHP như tên của chúng cho thấy là một tập hợp các chức năng được sử dụng để truy xuất các chi tiết, sửa đổi chúng và tìm nạp thông tin trên các thư mục hệ thống tệp khác nhau và nội dung cụ thể của chúng.a set of functions used in retrieving details, modifying them and fetching information on various file system directories and their specific contents.

Có bao nhiêu loại tệp trong PHP?

Hàm fileType () chấp nhận tên tệp dưới dạng tham số và trả về một trong bảy loại tệp thành công và sai về lỗi.Bảy giá trị trả về có thể của hàm filetype () là: Tệp: Tệp thông thường.seven file types on success and False on failure. The seven possible return values of the filetype() function are: file: regular file.