If you've taken our basic PHP course or
really done almost anything in PHP, you've already been reading files.
They include and require statements read additional files into the current file.
This is a great way to cut back and rewriting the same thing all the time.
We can write things once and include them wherever we want.
But this course is really about giving you control.
We want to be able to do something with those files,
besides just dumping them into the current file.
So, let's take a look at some of the other options we have for reading files.
We're going to create a form, and bring in country, and
state drop-downs from some other files.
If we click on the HTML countries.html, and view the page source.
You can see that this is a list of drop down options.
The other HTML files are formatted the same way, and
you can browse each of them if you'd like.
Back in work spaces create a new file name state_formed.php.
Let's add our form tag and then create a country drop down.
Country And then, our select.
For country, we're just going to include our countries file.
Let's view this in the browser.
We can see that their drop down is populated.
Let's go back and add a state drop down now using a little more control this time.
We'll add a label for our state.
And then our select.
This time we'll start by reading the file with the fopen function.
This function opens a connection to a file,
and returns a file handler that we can use to connect to the file.
This is the same kind of thing that we do when we set up a connection to a database.
To open a connection, we first tell it, which file we want to open, and
then tell it which mode we want to use.
What are we doing with the file?
Reading?
Writing?
Appending?
Or even creating a file if it doesn�t exist.
Check the notes associated with this video for mode options, and
additional features of fopen.
First we pass data/ html/states.html.
And then, since we want to read the file, we use r as the mode.
We'll wrap this in an F statement, so
we can perform our actions only if we're actually able to open the file.
We set our file handle equal to the fopen And now, we perform our actions.
The first action I want to add within the statement is the action to close the file.
Use fclose and pass the file handler, Before I close the file.
I want to loop over each line, we can create any kind of loop we want.
We could loop through only a specific number of lines, or
until we find a certain character.
I want to loop through all the lines in this file.
So, I will use the end of file function.
While not file, and a file, and our file handler.
To read each line, we use that fgets function.
This returns the current line as a string, and moves the pointer to the next line.
We can now echo that line.
Let's preview this in the browser again.
This time we see the state drop down as well.
But we haven't actually done anything more than include the file.
So, let's say, that I want to default the selection to Oregon.
Let's go back to Workspaces, and add some additional code.
If the string position
of line for Oregon.
This will check if my line contains the string Oregon.
If so, then I want to string replace.
Oregon, with
selected Oregon.
Else, we'll echo the line.
Let's check our browser again.
This time Oregon is selected by default.
We could make any comparisons, or manipulations we wanted to this line.
But let's check out another function for reading files.