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".
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 www.php.net/manual/en/ref.filesystem.php 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:
<HTML>
<BODY>
<PRE>
<?
$fp = fopen("data.txt","r");
fpassthru($fp);
?>
</PRE>
</BODY>
</HTML>
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 www.php.net/manual/en/function.fopen.php).
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:
$i=1;
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.
Read up on the latest ideas and technologies from companies that sell hardware, software and services. How to improve employee productivity in small and medium businesses
The state of Middleware
IT Service Management Needs and Adoption Trends: An Analysis of a Global Survey of IT Executives
Making the Business Case for IT Consolidation
Controlling storage costs with Oracle database 11g
Look before you leap | Key considerations for moving to 802.11n
The Case for an Untethered Enterprise
Zones provide focussed content from Computerworld and leading technology partners.Discover how SOA can create smarter outcomes for your business.
Attend and learn:
- How SOA is helping leading companies to become more agile
- Where you should be applying SOA processes in your company
- The top SOA implementation mistakes to avoid
Click here for more information.
- +
Computerworld Live Podcast #97: The Future of Enterprise Networking 25/07/2008 09:45:36
This week CW Live chats with Mark Thompson, global sales and marketing manager for HP ProCurve, on the future of the enterprise networking. Mark discusses the trends we can expect to see in the near future and how the right infrastructure can ensure your enterprise network is secure. - +
Computerworld Live Podcast #96: Security at the Edge 11/06/2008 09:22:22
CW Live speaks with Amol Mitra, HP ProCurve Director of Marketing for Asia Pacific and Japan. Today's topic: how enterprises are starting to shift away from simply controlling security via server logins, firewalls and moving to more adaptive security frameworks. - +
Data Management Edition #10: Multi-Petascale Systems 02/05/2008 09:12:33
This week we look at sustainability and the development of multicore technologies to build multi-petascale systems. - +
IT Security Edition #11: How to poison the Storm botnet 01/05/2008 08:51:55
This week CW Live presents a case study on how to poison the notorious Storm botnet . Plus we take a look at Cisco's plans for Ironport. - +
IT Security Edition #10: Cyber-battles fought and won 24/04/2008 11:09:47
Vendors bow to end user pressure to improve product security, and we take a look at the latest concepts shaping the cyber-battlefield of the future.
AOC Launches 18.5” Widescreen Green 16:9 LCD Monitor in Australia and New Zealand 2008-12-03 15:30:00+11
FrontRange Solutions eases software license management with new License Manager 3.0 2008-12-03 14:56:00+11
Progress Software's Cure for Managing Services-based Applications 2008-12-03 14:42:00+11
S3 Graphics Unleashes Full OpenGL® 3.0 API Support with Beta Driver for Chrome 500 Series GPUs 2008-12-03 14:08:00+11
Informatica Powercenter added to Nec Infoframe Solution Suite 2008-12-03 11:36:00+11
IT Service Management Needs and Adoption Trends: An Analysis of a Global Survey of IT Executives
IT executives face the need to improve service delivery with limited resource increases. Two common strategies for achieving this are network and systems management tools and datacenter consolidation. Read on to disocover how you can make a strong business case for IT Consolidation.













Comments
Strings in a PHP is only
Strings in a PHP is only properl;y understood by a high level language programmer.
------------------------
sea plants...sea grapes...plant roots...phytoplanktons...sea plant pictures...seagrass...seaweed...easy aquarium plants...deep sea plants...Limu Moui...Landscaping...End of sea plants...underwater plants...Sea coral...Sea crabs