Presentation is loading. Please wait.

Presentation is loading. Please wait.

PHP Programming.

Similar presentations


Presentation on theme: "PHP Programming."— Presentation transcript:

1 PHP Programming

2 Topics Functions File Handling

3 Functions and Parameters
PHP functions need to be defined with key word function It can have zero or more values (parameters) Functions may or may not return values If a function need to return value, the last statement of the function should be return return value;

4 Functions Parameter less function <?php function sayHi() {
echo “hi”; } ?> This can be called as <?php sayHi(); ?> in the program

5 Functions Parameterized function <?php function greet($name) {
echo “Hello “ . $name; } ?> This can be called <?php greet(‘Ram’);?> This gives an output Hello Ram

6 Functions Function returning value <?php function add($a,$b) {
return ($a + $b); } ?> When called like <?php echo add(1,2);?> we will get an output 3 in the browser.

7 File Handling This involves 5 tasks Opening a file
Reading data from a file Displaying the read data Writing contents to another file Closing a file

8 Opening a file $fp = fopen(‘filename’,’mode’);
Eg $fp = fopen(‘c:\abc.txt’,’r’); This opens a file abc.txt in read only mode Available modes: r – read only w – write only w+ - read write A – append – adding to the end

9 Reading a file Several methods are available
fread(filepointer,no of bytes to read) fgetc(filepointer) – Reads character by character fgets(filepointer) – Reads line by line The read content can be stored in a variable $data = fread($fp,10) – this reads 10 characters from file pointed by file pointer $fp and stores in $data If we want to read characters till end, we need to use a loop with condition checking for End of File

10 Writing to file We can use echo $data, to print the contents read from the file to browser Or we can open another file in write mode and put the contents to that file using either of these methods fwrite(filepoiner,data); fputc(filepointer,char); - writes character by character fputs(filepointer,line); - writes line by line Eg - fwrite($fpw,$data);

11 Closing a file feof(fp) – Checks for end of file.
Returns –1 if EOF is reached. Otherwise returns 0 To close a file use fclose(filepointer) method Eg. fclose($fp); This closes the file pointed by $fp.


Download ppt "PHP Programming."

Similar presentations


Ads by Google