Web Programming TutorialsImplementing 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 :

  1. Install Xampp server in your machine, if it is already installed then go to xampp folder .
  2. In this Xampp folder you will find htdocs folder.
  3. Create a new folder say s5 in this htdocs .
  4. v1

  5. In this s5 folder create an index.php file .
  6. 1

  7. To implement function write the following code in index.php
  8. <?php include('functions.php'); ?>
    <!DOCTYPE html>
    <html>
    <head>
    	<title>PHP Functions</title>
    </head>
    <body>
    <?php
    	echo add(5,5);
    ?>
    </body>
    </html>

  9. Now in the same folder i.e s5 create a function.php file and open it with notepad++ .
  10. 2

  11. Now to implement function write the given code in function.php file :
  12. <?php
    	function add($num1,$num2){
    		$sum = $num1 + $num2;
    		return $sum;
    	}
    
    	echo add(5,5);
    ?>

  13. So now your notepad++ window will have the following look :
  14. v3

  15. Run the program by entering the following url in the address bar http://localhost/s5/index.php
  16. The output is shown below :
  17. v5

    • Now lets study the include and require functions :

    1. In the s5 folder create another folder say s6
    2. Now in this s6 folder create two files called as index.php and function.php .
    3. In the index.php write the code given below :
    4. <?php 
      
      include 'function.php';
      
      echo 'I am in the index.php';
      ?>

    5. In the index.php file we have included the function.php file , so now lets write the code in function.php file .
    6. <?php
      
      echo 'I am in the function.php file ';
      echo '<br />';
      
      ?>

    7. Now start the server from your Xampp control pannel
    8. Now enter the following url in the browser to run your program : http://localhost/s5/s6/index.php
    9. Thus you will have the following output :
    10. v6

    11. 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.
    12. <?php 
      
      include 'function1.php';   //such function1.php does not exists.
      
      echo 'I am in the index.php';
      ?>

    13. After executing the program you will have the following output :
    14. v7

    15. That is the content of index.php file still gets executed .
    16. 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 :
    17. V8

    • To study some of the built in functions in php follow the steps given below :

    1. In s5 folder create a new file called as builtfunction.php and write the following code in it :
    2. <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>

    3. Save the file and enter the url in the browser as : http://localhost/s5/builtfunction.php
    4. Thus you will have the output as shown below :
    5. v9

      • Thus we have successfully studied the implementation of include , require and also some built in functions in php .

2 COMMENTS

  1. I got this web page from my buddy who told me concerning this web site and at the moment this time I am visiting this
    web site and reading very informative articles or reviews at this place.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Exclusive content

- Advertisement -

Latest article

21,501FansLike
4,106FollowersFollow
106,000SubscribersSubscribe

More article

- Advertisement -