UNIT2 - WFS

PHP Functions

User-Defined Functions

  • Definition: User-defined functions are functions created by the user to perform specific tasks.

  • Syntax:

    function functionname(arguments) {  
        // code to be executed  
        return;  
    }  
  • Example:

    function sayHello() {  
        echo "Hello PHP Function";  
    }  
    sayHello(); //calling function  

Function Arguments

  • Passing Information: PHP functions can receive information through arguments, which are separated by commas.

  • Types of Arguments:

    • Call by Value (Default): Arguments are passed by value, meaning a copy of the variable is made.

      • Example:

      function sayHello($name) {  
          echo "Hello $name<br/>";  
      }  
      sayHello("Sonoo");  
    • Call by Reference: Use the ampersand (&) symbol before the argument name to pass by reference. Changes made inside the function affect the original variable.

      • Example:

      function adder(&$str2) {  
          $str2 .= 'Call By Reference';  
      }  
      $str = 'Hello ';  
      adder($str);  
      echo $str;  
    • Default Argument Values: You can specify a default value for an argument.

      • Example:

      function sayHello($name = "Sonoo") {  
          echo "Hello $name<br/>";  
      }  
      sayHello("Rajesh");  
      sayHello(); //passing no value  
      sayHello("John");  

Returning Values

  • Functions can return values using the return statement.

  • Example:

    function cube($n) {  
        return $n * $n * $n;  
    }  
    echo "Cube of 3 is: " . cube(3);  

Built-in Functions

Math Functions

Function

Description

Example

Output

abs()

Returns the absolute value of a number

abs(-3.5)

3.5

acos()

Returns arc cosine of a number (range: -1 to 1)

acos(-0.35)

1.92

ceil()

Rounds a number up to the nearest integer

ceil(3.35)

4

floor()

Rounds a number down to the nearest integer

floor(3.35)

3

max()

Returns the highest value in an array or specified values

max(2,4,6,8,10)

10

min()

Returns the lowest value in an array or specified values

min(2,4,6,8,10)

2

pow()

Returns x raised to the power of y

pow(2,4)

16

sqrt()

Returns the square root of a number

sqrt(9)

3

String Functions
  • chop(): Deletes whitespace/characters from end of a string.

  • implode() / join(): Converts elements of an array into a string.

  • ord(): Returns ASCII value of the first character of a string.

  • str_replace(): Replaces parts of a string.

Array Functions
  • array(): Creates an array.

  • array_push(): Inserts elements to the end of an array.

  • array_pop(): Deletes the last element of an array.

  • array_merge(): Merges multiple arrays into one.

Handling Forms

  • Form Structure: Use <form> tag; specify action and method attributes.

  • Input Types: Include <input>, <textarea>, <select> to gather user input.

  • Validation: Ensure data is valid (e.g., check for empty fields, valid email addresses).

Cookies and Sessions

  • Cookies: Store small amounts of data on the user's computer.

    • Use setcookie() to set cookies.

    • Access cookies via the $_COOKIE superglobal.

  • Sessions: Store data on the server.

    • Use session_start() to begin a session.

    • Data stored in $_SESSION superglobal.

Object-Oriented Programming in PHP

  • Classes and Objects: Define classes with properties and methods; create objects from classes.

  • Exceptions: Use try...catch to handle exceptions gracefully.

JSON Handling

  • JSON: Lightweight data interchange format.

  • Use json_encode() to encode PHP arrays/objects into JSON.

  • Use json_decode() to convert JSON into PHP arrays/objects.