Introduction to PHP - II

featured image

Functions:

The real power of PHP comes from its functions; it has more than 1000 built-in functions. We'll see how to create user-defined functions in php.


User Defined Functions:

A user-defined function starts with the word function:

Syntax:
         function functionName(){
                  #code goes here....
         }

Note: A function name can start with a letter or underscore (not a number).

Tip: Give the function a name that reflects what the function does!


Example:
Lets create a function that can sum two numbers.

         <?php
         function sum($number1 , $number2){
                  $sum = $number1 + $number2;
                  return $sum;
         }

         echo sum(4,5);
         ?>

The above function prints out the sum of 4 and 5 in the browser.

Self Study Topics:
  • Conditional statements (if else, switch)
  • Sorting Functions in PHP
    • sort( ) - sort arrays in ascending order.
    • rsort ( ) - sort arrays in desending order
    • asort ( ) - sort associative arrays in ascending order, according to the value.
    • ksort ( ) - sort associative arrays in ascending order, according to the key.
    • arsort ( ) - sort associative arrays in descending order, according to the value.
    • krsort ( ) - sort associative arrays in descending order, according to the key.
Documentation:
         Please prefer the documentation for studying the functions or any related topic in php. Here's the link for you.


if you're still confused about functions and how they're used then you can refer to the new boston channel video about functions posted below:



And Also this one:




if you've any questions feel free to ask in comments i'll be happy to respond. 

No comments:

Post a Comment