Strings and PHP

If you read the 'PHP Hypertext Preprocessor' column, you might have been wondering about some of the peculiarities of string handling with PHP. Strings, simply, consist of one or more characters in sequence. In PHP, $s = "string" creates a string variable 's' that contains the string "string".

Python vs. PHP: Choosing your next project's language

What about the more complicated strings in last month's example? One string was "The time is ".date('D M d H:i:s T Y')."\n"; in fact, this is three sub-strings.

How does this work? PHP allows you to append strings to other strings using the '.' (full stop) character. When the PHP script gets executed and our example above runs, PHP reads it as follows: build a string from the text "The time is", append the results of a call to the date() function, then append '\n' (a new line) to that. This can be really useful when your programs get information from several sources and you need to collate it.

Interacting with files

One of the best applications for PHP is delivering content stored in files or databases. In the first case, PHP makes available a range of functions to use when interacting with files (see for a complete list). One of the easiest ways to retrieve data from a file with PHP is to use the function fpassthru(); an example follows. First, put some random text into a file and call it 'data.txt'. Then create this script:

$fp = fopen("data.txt","r");
Save the script as fpassthru.php in the same directory as data.txt on your PHP-enabled Web server (for PHP 4 and Apache Web server software and installation details, see the cover CD). Requesting the page from your Web server will give you the contents of the file in an HTML document.

How does this work? The fopen() function opens the file data.txt for reading at the beginning of the file (you can open files in other modes; find a full list at

The fopen() function returns a file pointer, which is a variable needed to tell the other file system functions which file you wish to manipulate. In our example, we supply fpassthru() with the file pointer for data.txt. As its name suggests, fpassthru() reads each line from the file, and outputs it. Notice that we do not need to close the file; fpassthru() does that.

Another way to replicate the functionality of fpassthru, but to have more control over what is read from the file, is to use fgets(). This function returns a line, or a portion thereof, as a string and moves the current position in the file forward one line. For example, when you call fopen() in read mode, the current position is set to the first line. By calling fgets($fp,100), the first 100 characters of the file pointed to by $fp are returned (or the whole line if that line is fewer than 100 characters long), and the current position in the file is set to the second line. To see what this allows us to do, replace the call to fpassthru() with the following:

while(!feof($fp)) { echo "Line $i: ".fgets($fp,1024); $i++; } fclose($fp);

Save this as fgets.php and request it from your Web server. You will see each line of your data.txt file displayed on the line number on the left margin.

New features

fgets.php introduces some new aspects of PHP. One is the while loop, which continues to execute the code inside its curly braces - { } - while the argument to while() remains true.

Like most programming languages, PHP is based on bivalent evaluation - truth and falsity. In PHP, the value 0 denotes the latter, and any other value the former. The bang (or exclamation mark) reverses the truth or falsity of the expression following it. For example, !1, !-1, !1234 are all false, while !0 is true. The bang is used in fgets.php to reverse the value returned by feof() - a function that indicates if the current file position is the end of the file. So, the while loop condition reads 'loop while the current line is not the end of the file'.

The last new feature of PHP revealed in fgets.php is that of incrimination. The line $i++ is equivalent to $i = $i + 1. The '++' simply means 'increment by one'.

There are many other ways to read data from files with PHP, exploration of which is left as an exercise.

Join the PC World newsletter!

Error: Please check your email address.

Our Back to Business guide highlights the best products for you to boost your productivity at home, on the road, at the office, or in the classroom.

Keep up with the latest tech news, reviews and previews by subscribing to the Good Gear Guide newsletter.

Gavin Sherry

PC World
Show Comments

Most Popular Reviews

Latest News Articles


PCW Evaluation Team

Azadeh Williams

HP OfficeJet Pro 8730

A smarter way to print for busy small business owners, combining speedy printing with scanning and copying, making it easier to produce high quality documents and images at a touch of a button.

Andrew Grant

HP OfficeJet Pro 8730

I've had a multifunction printer in the office going on 10 years now. It was a neat bit of kit back in the day -- print, copy, scan, fax -- when printing over WiFi felt a bit like magic. It’s seen better days though and an upgrade’s well overdue. This HP OfficeJet Pro 8730 looks like it ticks all the same boxes: print, copy, scan, and fax. (Really? Does anyone fax anything any more? I guess it's good to know the facility’s there, just in case.) Printing over WiFi is more-or- less standard these days.

Ed Dawson

HP OfficeJet Pro 8730

As a freelance writer who is always on the go, I like my technology to be both efficient and effective so I can do my job well. The HP OfficeJet Pro 8730 Inkjet Printer ticks all the boxes in terms of form factor, performance and user interface.

Michael Hargreaves

Windows 10 for Business / Dell XPS 13

I’d happily recommend this touchscreen laptop and Windows 10 as a great way to get serious work done at a desk or on the road.

Aysha Strobbe

Windows 10 / HP Spectre x360

Ultimately, I think the Windows 10 environment is excellent for me as it caters for so many different uses. The inclusion of the Xbox app is also great for when you need some downtime too!

Mark Escubio

Windows 10 / Lenovo Yoga 910

For me, the Xbox Play Anywhere is a great new feature as it allows you to play your current Xbox games with higher resolutions and better graphics without forking out extra cash for another copy. Although available titles are still scarce, but I’m sure it will grow in time.

Featured Content

Latest Jobs

Don’t have an account? Sign up here

Don't have an account? Sign up now

Forgot password?