It’s Festive Season! Enjoy 30% Off

Use code FESTIVE30 at checkout

Code has been added to clipboard!

PHP File: Definition, Explanation, Examples

Reading time 4 min
Published Aug 8, 2017
Updated Oct 2, 2019

Before you can consider yourself a coding professional, you need to pay attention to the fundamental aspects of PHP. You should know what is a PHP file, how to use it, and, of course, how to edit PHP files yourself.

A PHP file can be defined simply as a file, containing the PHP code and ending with a .php extension. If you are using PHP 5 or a newer version, working with PHP files will be easy, as there are inbuilt functions for that. We will analyze them in this tutorial.

PHP File: Main Tips

  • Before you learn how to edit PHP files, you need to know what they are and how they work.
  • The most important things in creating a PHP file is using PHP tags and a .php extension.
  • You can read a PHP file into either a string or an array.
  • PHP scripts can also be used to open external files.

A Basic PHP File: Example

What is a PHP file? Well, if you save the script below as a file with a .php extension, you will have the most basic PHP file example:

Example
<html>
 <head>
  <title>Testing Testing</title>
 </head>
 <body>
 <?php echo '<p>Happy days</p>'; ?> 
 </body>
</html>

While this does look very similar to a simple HTML file, there are two things you should note:

  • The .php extension: without it, the server will not send it to PHP.
  • The PHP tags (<?php and ?>). They let you enter and leave PHP mode as needed.

We'd also advise you to learn how PHP functions in your web server. To do that, create a simple script as you see below:

Example
<?php phpinfo(); ?>

Save it with a .php extension, upload it to your server, and open it using your browser. If you did everything correctly, the script will call a phpInfo() function to show a lot of information about the PHP environment you're using (such as configuration settings and module version).

Reading a PHP file

There are two functions used for reading a PHP file: file() and file_get_contents.

Using file(), you can read a PHP file into an array. The syntax is rather simple:

file(path,include_path,context)

As you can see, there are three parameters:

Parameter Description
path Required. Defines a path of the file or the directory to check.
include_path Optional. Looks for a file in include_path.
context Optional. Defines a custom context.

To read a whole PHP file into a string, you can use a built-in PHP file_get_contents function. The syntax for it is as follows:

file_get_contents(path, include_path, context, start, max_length)

The first three parameters are the same as they were for PHP file(). However, there are two optional ones that are unique to PHP file_get_contents:

  • start defines the starting point for reading the file.
  • max_length defines the amount of bytes to read.

Opening External Files

While you might come across a term of PHP file types, it's important to understand what it means, as there's an easy mistake to make. PHP files themselves only come in one type - an executable script. However, you can use it to open external files, and there is a wide variety of those.

There are two functions you can use to get the type of the file you specify:

  • filetype() will get the PHP file type.
  • mime_content_type will get the MIME type of the file.

There is only one parameter you need to define for both of these functions, which is the path to the file. Working with external files in PHP is covered more fully in this tutorial.

Tip: to make PHP copy file, use copy(source, destination). It lets you make a copy of a file located in a source and place it into a destination.

PHP File: Summary

  • Before you learn how to edit PHP files, make sure you fully understand what is a PHP file.
  • Using PHP tags and a .php extension are vital in creating a PHP file.
  • With different functions, you can read a PHP file into a string or an array.
  • You can also make PHP scripts open external files.