How do i open .php files?

How do i open .php files?

A file with the .php file extension is a plain-text file that contains the source code written in the PHP (it’s a recursive acronym meaning PHP: Hypertext Preprocessor) programming language. PHP is often used to develop web applications that are processed by a PHP engine on the web server.

PHP was created in 1994 by Rasmus Lerdorf as a simple set of scripts written in the C programming language. Its primary purpose was to track visitors who viewed his online resume. He initially called these scripts “Personal Home Page Tools” (PHP Tools) and would later rename them to FI (Forms interpreter), and then PHP/FI, before finally deciding on its current recursive name. PHP is used by 78.9% of all websites whose server-side programming language is known.

PHP files are processed by web servers using an interpreter, which executes the code and then combines the results (which can be any kind of data, like queries from a database or images) with dynamically generated HTML to form the webpage you see. This prevents any of the PHP code from actually being viewed by the user, even when looking at a page’s source code.

Often when you fill out a form online or submit contact details to a website the backend code will send that information to a server using a script inside the PHP file. WordPress is heavily based using PHP files.

How Do I Open One?

Since PHP files are plain-text files that are human-readable, all you need to view one is a simple text editor like Notepad, Notepad++, Sublime Text, Vi, and so on.

RELATED: How To Replace Notepad with Another Text Editor in Windows

If you only need to take a quick look inside a file, you can use Notepad and not have to download any other software. If you plan on editing the code, we recommend using an editor that correctly formats PHP code. I’ll be using Notepad++ on Windows in my example.

By default, when you install a text editor like Notepad++ it will associate most text/programming file extensions automatically, so double-clicking the file should open it up inside the program.

How do i open .php files?

If that doesn’t work, then you can right-click the file and choose your favorite text editor from the “Open With” list provided.

How do i open .php files?

The same holds true on other platforms like macOS and Linux.


If you’re trying to run or execute PHP files, you will need to download and install PHP on your computer to compile the code. For this, you can use a local server such as Varying Vagrant Vagrants, WampServer, or XAMPP.

RELATED: How to Install PHP on IIS 7 for Windows Server 2008

READ NEXT

  • › Don’t Replace an Old Computer, Put an SSD In It
  • › The Best Gaming Microphones of 2022
  • › How Cold Call Refund Scams Work
  • › Bad Wi-Fi Signal on Your PC? Do This to Fix It
  • › Do You Even Need a Note-Taking App?
  • › Apple AirPods Pro (2nd Gen) Review: The Best Earbuds for Apple Fans

How-To Geek is where you turn when you want experts to explain technology. Since we launched in 2006, our articles have been read more than 1 billion times. Want to know more?


In this chapter we will teach you how to open, read, and close a file on the server.


PHP Open File - fopen()

A better method to open files is with the fopen() function. This function gives you more options than the readfile() function.

We will use the text file, "webdictionary.txt", during the lessons:

AJAX = Asynchronous JavaScript and XML
CSS = Cascading Style Sheets
HTML = Hyper Text Markup Language
PHP = PHP Hypertext Preprocessor
SQL = Structured Query Language
SVG = Scalable Vector Graphics
XML = EXtensible Markup Language

The first parameter of fopen() contains the name of the file to be opened and the second parameter specifies in which mode the file should be opened. The following example also generates a message if the fopen() function is unable to open the specified file:

Example

<?php
$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
echo fread($myfile,filesize("webdictionary.txt"));
fclose($myfile);
?>

Run example »

Tip: The fread() and the fclose() functions will be explained below.

The file may be opened in one of the following modes:

ModesDescription
r Open a file for read only. File pointer starts at the beginning of the file
w Open a file for write only. Erases the contents of the file or creates a new file if it doesn't exist. File pointer starts at the beginning of the file
a Open a file for write only. The existing data in file is preserved. File pointer starts at the end of the file. Creates a new file if the file doesn't exist
x Creates a new file for write only. Returns FALSE and an error if file already exists
r+ Open a file for read/write. File pointer starts at the beginning of the file
w+ Open a file for read/write. Erases the contents of the file or creates a new file if it doesn't exist. File pointer starts at the beginning of the file
a+ Open a file for read/write. The existing data in file is preserved. File pointer starts at the end of the file. Creates a new file if the file doesn't exist
x+ Creates a new file for read/write. Returns FALSE and an error if file already exists


PHP Read File - fread()

The fread() function reads from an open file.

The first parameter of fread() contains the name of the file to read from and the second parameter specifies the maximum number of bytes to read.

The following PHP code reads the "webdictionary.txt" file to the end:

fread($myfile,filesize("webdictionary.txt"));


PHP Close File - fclose()

The fclose() function is used to close an open file.

It's a good programming practice to close all files after you have finished with them. You don't want an open file running around on your server taking up resources!

The fclose() requires the name of the file (or a variable that holds the filename) we want to close:

<?php
$myfile = fopen("webdictionary.txt", "r");
// some code to be executed....
fclose($myfile);
?>


PHP Read Single Line - fgets()

The fgets() function is used to read a single line from a file.

The example below outputs the first line of the "webdictionary.txt" file:

Example

<?php
$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
echo fgets($myfile);
fclose($myfile);
?>

Run example »

Note: After a call to the fgets() function, the file pointer has moved to the next line.


PHP Check End-Of-File - feof()

The feof() function checks if the "end-of-file" (EOF) has been reached.

The feof() function is useful for looping through data of unknown length.

The example below reads the "webdictionary.txt" file line by line, until end-of-file is reached:

Example

<?php
$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
// Output one line until end-of-file
while(!feof($myfile)) {
  echo fgets($myfile) . "<br>";
}
fclose($myfile);
?>

Run example »


PHP Read Single Character - fgetc()

The fgetc() function is used to read a single character from a file.

The example below reads the "webdictionary.txt" file character by character, until end-of-file is reached:

Example

<?php
$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
// Output one character until end-of-file
while(!feof($myfile)) {
  echo fgetc($myfile);
}
fclose($myfile);
?>

Run example »

Note: After a call to the fgetc() function, the file pointer moves to the next character.


Complete PHP Filesystem Reference

For a complete reference of filesystem functions, go to our complete PHP Filesystem Reference.


PHP Exercises



What programs will open a PHP file?

Programs that open PHP files.
File Viewer Plus..
Adobe Dreamweaver 2021..
Eclipse PHP Development Tools..
Zend Studio..
MPSoftware phpDesigner..
ES-Computing EditPlus..
Blumentals WeBuilder..
Microsoft Visual Studio Code..

How do I open a PHP file in Chrome?

Step by step instructions:.
Download and install XAMPP – The installation is quite simple and straightforward. ... .
Starting XAMPP – Once installed, you need to open the XAMPP Control Panel. ... .
Create your PHP page. ... .
Place the PHP file on the server. ... .
Find the path to your PHP page in your Chrome browser..

How do I open a PHP file in a browser?

You can open current file in browser using following methods: Click the button Open In Browser on StatusBar. In the editor, right click on the file and click in context menu Open PHP/HTML/JS In Browser. Use keybindings Shift + F6 to open more faster (can be changed in menu File -> Preferences -> Keyboard Shortcuts )

How do I open a PHP file in Windows?

You just follow the steps to run PHP program using command line..
Open terminal or command line window..
Goto the specified folder or directory where php files are present..
Then we can run php code using the following command: php file_name.php..

Tải thêm tài liệu liên quan đến bài viết How do i open .php files?