"> ">

Presentation is loading. Please wait.

Presentation is loading. Please wait.

PHP 4 Files, Functions. Opening a File The fopen() function is used to open files in PHP. The first parameter of this function contains the name of the.

Similar presentations


Presentation on theme: "PHP 4 Files, Functions. Opening a File The fopen() function is used to open files in PHP. The first parameter of this function contains the name of the."— Presentation transcript:

1 PHP 4 Files, Functions

2 Opening a File The fopen() function is used to open files in PHP. The first parameter of this function contains the name of the file to be opened and the second parameter specifies in which mode the file should be opened in (r, r+, w, w+, a, a+, x, x+): <?php $f=fopen("welcome.txt","r"); ?>

3 Opening a File <?php if (!($f=fopen("welcome.txt","r"))) exit("Unable to open file!"); ?>

4 Closing a File The fclose() function is used to close a file. fclose($f);

5 Reading from a File The feof() function is used to determine if the end of file is true. The fgetc() function is used to read a single character from a file. The fputs($f,sting) function is used to write. <?php if (!($f=fopen("welcome.txt","r"))) exit("Unable to open file."); while (!feof($f)) { $x=fgetc($f); echo $x; } fclose($f); ?>

6 Reading from a File string fgets ( resource $handle [, int $length ] ) reads a file from the file string fread ( resource $handle, int $length ) reads up to length bytes from the file pointer referenced by handle. Reading stops as soon as: –length bytes have been read –EOF (end of file) is reached –a packet becomes available (for network streams) –8192 bytes have been read (after opening userspace stream)

7 Reading from a File <?php // Get a file into an array: HTML source of a URL. $lines = file ('http://www.example.com/'); // Loop through our array: html + line numbers. foreach ($lines as $line_num => $line) { echo "Line # {$line_num} : "; echo htmlspecialchars($line). " \n"; }... //htmlspecialchars - Convert special characters to //HTML entities

8 Error Control Operators PHP supports one error control operator: the at sign (@). When prepended to an expression in PHP, any error messages that might be generated by that expression will be ignored. $my_file = @file ('non_existent_file') or die ("Failed opening file: error was '$php_errormsg'"); $value = @ $cache[$key]; // will not issue a notice if the index $key doesn't exist.

9 Strings string substr ( string $string, int $start [, int $length ] ) returns the portion of string specified by the start and length parameters. $username = substr($username,0,30); //((Mid)) int strlen ( string $string ) - Returns the length of the given string trim(string $string ) – truncates the string str_replace (mixed $lookfor, mised $replacewith, mixed $stringToSearchIn) - Replace all occurrences of the search string with the replacement string strstr - Find first occurrence of a string : for example: strstr($email, '@');

10 Functions

11 Functions

12 Functions In PHP 3, functions must be defined before they are referenced. No such requirement exists in PHP 4. Except when a function is conditionally defined

13 Conditionally defined functions <?php $makefoo = true; // We can't call foo() bar(); if ($makefoo) { function foo () { echo "I don't exist until program execution reaches me.\n"; } } if ($makefoo) foo(); function bar() { echo "I exist immediately upon program start.\n"; } ?>

14 The global keyword <?php $a = 1; $b = 2; function Sum() { $GLOBALS["b"] = $GLOBALS["a"] + $GLOBALS["b"]; } Sum(); echo $b; ?> <?php $a = 1; $b = 2; function Sum() { global $a, $b; $b = $a + $b; } Sum(); echo $b; ?>


Download ppt "PHP 4 Files, Functions. Opening a File The fopen() function is used to open files in PHP. The first parameter of this function contains the name of the."

Similar presentations


Ads by Google