SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    May 2008
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Help me Dissect this PHP code

    Code:
    $opFile = "blogfile.txt"; 
    $fp = fopen($opFile,"r") or die("Error Reading File");
    So i know the above code opens the blogfile.txt and the text inside the brackets after 'die' show that message if the file cannot be opened, but what does the R in ($opFile,"r") mean and why is does it have quotes around it?

    Code:
    $data = fread($fp, filesize($opFile)); 
    fclose($fp);
    So the above code puts the blogfile.txt data into a variable named $data, but what is the code filesize($opFile) for and is it needed? And what is the code fclose for and is it needed?


    Code:
    $n++
    I know the $n is a variable but what is the plus plus(++) for?

    And in PHP what does the word explode do?

    Finally, if you use a Flat text file database instead of mysql, is there a maximum file size limit for a flat file db? Would it be possible to have a 100GB flat file db? because i think i read that on notepad in windows xp it can only open up a maximum 45KB .txt file. If there is a limit, how do you create an empty flat file db to takeover from the first db and how do you make them communicate with each other?

  2. #2
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    I really hate doing this, but all your questions (well, most of them. ) are answered here.

    Just type in the function name your inquisitive about.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  3. #3
    SitePoint Enthusiast
    Join Date
    Feb 2009
    Location
    Scotland
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by tim987 View Post
    Code:
    $opFile = "blogfile.txt"; 
    $fp = fopen($opFile,"r") or die("Error Reading File");
    So i know the above code opens the blogfile.txt and the text inside the brackets after 'die' show that message if the file cannot be opened, but what does the R in ($opFile,"r") mean and why is does it have quotes around it?
    specifies to open the file for read only
    Code:
    $data = fread($fp, filesize($opFile)); 
    fclose($fp);
    So the above code puts the blogfile.txt data into a variable named $data, but what is the code filesize($opFile) for and is it needed? And what is the code fclose for and is it needed?
    filesize() returns the size of the file in bytes, and it's needed in the fread() function so the function knows how much of the file to read.
    flcose() simply closes the file, so it can then be used by other scripts/programmes.
    Code:
    $n++
    I know the $n is a variable but what is the plus plus(++) for?
    $n++ is a basically a short cut for adding 1 to the existing string value. It's the same as using $n = $n+1.
    And in PHP what does the word explode do?
    Splits a string into an array.
    An example would be a list of keywords entered into a string, with a space between each word. Using explode and specifing a space as the delimiter, would split the string into an array containing the individual words.
    Finally, if you use a Flat text file database instead of mysql, is there a maximum file size limit for a flat file db? Would it be possible to have a 100GB flat file db? because i think i read that on notepad in windows xp it can only open up a maximum 45KB .txt file. If there is a limit, how do you create an empty flat file db to takeover from the first db and how do you make them communicate with each other?
    Flat files are very slow and innefficient way to hold any great amount of data.

    If you're running windows, wordpad will open files larger than what notepad is capable of opening.

    But as SilverBullet says, check the php site, and put whatever function you're not sure about into the search box. Admiteddly, some of the examples aren't very clear, so if you have any specific queries just ask.

  4. #4
    SitePoint Evangelist
    Join Date
    Aug 2007
    Posts
    566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    know the $n is a variable but what is the plus plus(++) for?
    There are several shortcuts like this.

    $n+=2 means take the value of $n, add 2 and store the result in $n
    $n/=3 is the same, divide $n by 3 and store the result in $n

    As you have $n++, which means "return the value of $n and then increment of 1", you have ++$n which means "increment the value of $n of one, and return it's value".
    Subtle difference, but difference there is.
    You can read about it in the PHP reference there: http://www.php.net/manual/en/languag....increment.php


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •