New Post has been published on www.eduonix.com - Implementing functions and file inclusion in php
Implementing functions and file inclusion in php
In today’s session we will study the php function and learn to implement file inclusion in php.
To study php function and file inclusion property follow the steps given below :
Install Xampp server in your machine, if it is already installed then go to xampp folder .
In this Xampp folder you will find htdocs folder.
Create a new folder say s5 in this htdocs .
In this s5 folder create an index.php file .
To implement function write the following code in index.php
<?php include('functions.php'); ?> <!DOCTYPE html> <html> <head> <title>PHP Functions</title> </head> <body> <?php echo add(5,5); ?> </body> </html>
Now in the same folder i.e s5 create a function.php file and open it with notepad++ .
Now to implement function write the given code in function.php file :
<?php function add($num1,$num2) $sum = $num1 + $num2; return $sum; echo add(5,5); ?>
So now your notepad++ window will have the following look :
Run the program by entering the following url in the address bar http://localhost/s5/index.php
The output is shown below :
Now lets study the include and require functions :
In the s5 folder create another folder say s6
Now in this s6 folder create two files called as index.php and function.php .
In the index.php write the code given below :
<?php include 'function.php'; echo 'I am in the index.php'; ?>
In the index.php file we have included the function.php file , so now lets write the code in function.php file .
<?php echo 'I am in the function.php file '; echo '<br />'; ?>
Now start the server from your Xampp control pannel
Now enter the following url in the browser to run your program : http://localhost/s5/s6/index.php
Thus you will have the following output :
Suppose now if incase in index.php file we include function1.php that does not exists then in the output it will display the error message and will not display its content but it will display the message from the index file.
<?php include 'function1.php'; //such function1.php does not exists. echo 'I am in the index.php'; ?>
After executing the program you will have the following output :
That is the content of index.php file still gets executed .
But if we use require instead of include keyword it will display only error messages i.e it will not execute the index.php file which is shown below :
To study some of the built in functions in php follow the steps given below :
In s5 folder create a new file called as builtfunction.php and write the following code in it :
<html> <body> <h1>This is the string replace function</h1> <?php $phrase = 'I like to eat apples'; $phrase = str_replace('apples','oranges',$phrase); echo $phrase; ?> <br /> <h1>This is the implode function</h1> <?php $name_array = array('Brad','Bob','Jim','Sarah'); $name_string = implode('-',$name_array); echo $name_string; ?> <br /> <h1>This is the explode function</h1> <?php $car_string = 'toyota,ford,nissan,dodge'; $car_array = explode(',',$car_string); print_r($car_array); ?> <br /> <h1>This is the string conversion function</h1> <?php echo ucwords('hello world'); echo '<br>'; echo strtolower('HELLO WORLD'); ?> <br /> <h1>This is the sorting of the array element</h1> <?php $car_string = 'toyota,ford,nissan,dodge'; $car_array = explode(',',$car_string); sort($car_array); print_r($car_array); ?> </body> </html>
Save the file and enter the url in the browser as : http://localhost/s5/builtfunction.php
Thus you will have the output as shown below :
Thus we have successfully studied the implementation of include , require and also some built in functions in php .