PHP
  Home arrow PHP arrow PHP 101 (part 5) - The Wonderland Factor
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

PHP 101 (part 5) - The Wonderland Factor
By: Vikram Vaswani and Harish Kamath, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 6
    2000-09-06


    Table of Contents:
  • PHP 101 (part 5) - The Wonderland Factor
  • So Who Are You, Anyway?
  • Graffiti For The Masses
  • Calling Godzilla
  • Q
  • Arguments And Responses
  • The Wonderland Factor

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    PHP 101 (part 5) - The Wonderland Factor
    ( Page 1 of 7 )

    This concluding article in the series illustrates PHP's file functions, with examples of how to read and write files on the system. It also includes an explanation of user-defined functions, return values and function arguments, together with some not-so-real-life examples. If you've been following along, you already know how PHP can be used to extract data from a database...it's as easy as apple pie. But in the real world, data isn't always stored in neat rows and columns, and you're quite likely to come across situations where the data you need is actually stored in a bunch of ASCII text files.

    You have three options here. You could con someone into the mind-numbing task of inserting the textual data into a database. You could throw up your hands in despair and look for another job. Or you could use PHP's built-in file functions to extract and format the data.

    The first option requires guile and cunning. The second is unacceptable. And the third requires this issue of PHP 101.

    Over the next few pages, we'll be discussing the basics of PHP's file functions, together with some interesting examples. And then we'll explore the murky tunnels of user-defined functions, and delve deep into the mysteries of local and global variables, return values and function parameters. Pack some warm clothes, bring enough food and get someone to feed the dogs - this is one journey you don't want to miss!{mospagebreak title=Windy Nights} From the days of C, programmers have had the ability to create files on the system, read them and perhaps even execute them via the shell. And, like all good programming languages, PHP comes with a bunch of functions which allow you to read and write files with minimum effort.

    Let's get straight into the nitty-gritty of reading a file. Create a text file and populate it with some text - here's what our file, which we've named "random.txt", looks like:

    It was a dark and stormy evening. Outside the pub, the wind wailed and moaned, singing dirges to long-lost sailors and making the timbers creak. Inside, all was silent except for a brave nightlight that kept the ghosts awake.

    Oh, what I wouldn't give for a slug of ale just about now!
    Before you can use PHP to read the contents of this file, you need to ensure that you have permission to read it. On *NIX, this is accomplished via the "chmod" command.- try

    $ chmod 744 random.txt
    to make it world-readable.

    And here's some simple PHP code that reads the file and returns the contents and file size:

    <?php // read file $bytes = readfile("random.txt"); // display size echo "<br>File is $bytes bytes in size."; ?>
    And here's the output:

    It was a dark and stormy night. Outside the pub, the wind wailed and moaned, singing dirges to long-lost sailors and making the timbers creak. Inside, all was silent except for a brave nightlight that kept the ghosts awake. Oh, what I wouldn't give for a slug of ale just about now! File is 286 bytes in size.
    As you can see, readfile() doesn't do much - it simply reads the file and displays the contents. It also returns the size of the file in bytes - we've displayed this using an echo() above.

    A far more useful function, and one which lets you format the extracted data, is the file() function, which allows you to read the file into a regular PHP array. Each element of the array represents one line of the file, and therefore, the size of the array is representative of the number of lines in the file. Take a look:

    <?php // set filename $filename = "random.txt"; // read file into array $contents = file($filename); // get array length $length = sizeof($contents); echo "<b>File $filename contains $length line(s)</b><p>"; // display each line with "for" loop for($counter=0; $counter<$length; $counter++) { echo "$contents[$counter]<br>"; } ?>
    In this case, we've used the file() function to read the contents of the entire file into an array called $contents. Using the sizeof() function, we can obtain the length of the array, which is the same as the number of lines present in the text file. The "for" loop is used to read and display each element of the array.

    And here's the output:

    File random.txt contains 3 line(s) It was a dark and stormy night. Outside the pub, the wind wailed and moaned, singing dirges to long-lost sailors and making the timbers creak. Inside, all was silent except for a brave nightlight that kept the ghosts awake. Oh, what I wouldn't give for a slug of ale just about now!


     
     
    >>> More PHP Articles          >>> More By Vikram Vaswani and Harish Kamath, (c) Melonfire
     

       

    PHP ARTICLES

    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    Stay green...Green IT