File handling is a very important part of programming when it comes to making applications. We might require reading input from a file or writing output into a file. File handling in PHP is similar to other programming languages. There are predefined functions that can be used to accomplish the specified task. Check out the Basics of File Handling to know about the functions.
$<variable_name> = fopen(<file source path>,<access mode>)
Difference in the fopen modes r+, rw+ and w+ in PHP
- r+: Opens a file in read and write mode. File pointer starts at the beginning of the file.
- w+: Opens a file in read and write mode. It creates a new file if it does not exist, if it exists, it erases the contents of the file and the file pointer starts from the beginning.
- rw+: Opens a file in read and write mode. File pointer starts at the beginning of the file. This mode does not exists in the PHP documentation but it works well.
Example: For a better understanding, let us observe the parsing function for fopen.
- It checks for the first character of the argument, i.e. mode. Depending upon it being r,w,a or x, corresponding mode is identified.
- It looks for the presence of ‘+’ in the mode argument. If present, it sets the appropriate flags.
Therefore, “r+” and “w+” has a difference in the mode of opening the file and placing the file pointer. The “r+” and “rw+” are the same. PHP only cares that the string starts with “r” and has a “+”. The “w” is ignored in “rw+”. Hence, they work the same.
- Difference between Indirect and Immediate Addressing Modes
- Difference between Direct and Immediate Addressing Modes
- Difference between Indirect and Implied Addressing Modes
- Difference between Direct and Implied Addressing Modes
- Difference between PC relative and Base register Addressing Modes
- PHP | fopen( ) (Function open file or URL)
- Difference between Memory based and Register based Addressing Modes
- Web 1.0, Web 2.0 and Web 3.0 with their difference
- Difference between ASP and ASP.NET
- Difference Between Blu-Ray and DVD
- What is the difference between (NaN != NaN) & (NaN !== NaN)?
- What is the difference between GUI and CUI?
- Difference Between JSP and PHP
- Difference between “!==” and “==!” in PHP
- Difference between JSP and ASP
- What is the difference between MEAN.js and MEAN.io?
- Difference between T-SQL and PL-SQL
- Difference between ReactJS and Vue.js
- Difference between self::$bar and static::$bar in PHP
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.