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
returnstatement.Example:
function cube($n) { return $n * $n * $n; } echo "Cube of 3 is: " . cube(3);
Built-in Functions
Math Functions
Function | Description | Example | Output |
|---|---|---|---|
| Returns the absolute value of a number |
|
|
| Returns arc cosine of a number (range: -1 to 1) |
|
|
| Rounds a number up to the nearest integer |
|
|
| Rounds a number down to the nearest integer |
|
|
| Returns the highest value in an array or specified values |
|
|
| Returns the lowest value in an array or specified values |
|
|
| Returns |
|
|
| Returns the square root of a number |
|
|
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; specifyactionandmethodattributes.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
$_COOKIEsuperglobal.
Sessions: Store data on the server.
Use
session_start()to begin a session.Data stored in
$_SESSIONsuperglobal.
Object-Oriented Programming in PHP
Classes and Objects: Define classes with properties and methods; create objects from classes.
Exceptions: Use
try...catchto 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.