CH-3 WORKING WITH FORM DATA
& FUNCTIONS
WHAT IS FUNCTION?
• A function is a block of code to perform some specific task.
• PHP provides us with two major types of functions:
• Built-in functions
• User Defined Functions
• Why should we use functions?
• Reusability
• Easier error detection
• Easily maintained
FUNCTION..(CONTINUE)
• User Defined Functions • Example:
• Syntax function welcome()
function function_name(){ {
//code;
echo "This is function";
}
• Rules }
• A function name always begins with • // Calling the function
the function keyword. welcome();
• A function name cannot start with a
number. It can start with an alphabet or
underscore.
• To call a function we just need to write
its name followed by the parenthesis.
• A function name is not case-sensitive.
FUNCTION..(CONTINUE)
• Function Parameters or Arguments
• Function Parameters
• Function parameters are the variables defined in the function definition.
• These parameters act as placeholders that will receive values when the function is called.
• Syntax
function function_name($first_parameter, $second_parameter) {
// code;
}
• Example:
• // Function definition with parameters $a and $b
function add_numbers($a, $b) {
return $a + $b;
}
FUNCTION..(CONTINUE)
• Function Arguments
• Function arguments are the actual values or data passed to the function when it is called.
• These values are assigned to the corresponding parameters in the function.
• Example:
add_numbers(5, 3);
// Function call with arguments 5 and 3
FUNCTION..(CONTINUE)
• Returning Values from Functions
• Functions can also return values to the part of program from where it is called.
• The return keyword is used to return value back to the part of program, from where it was
called.
• The return statement also marks the end of the function and stops the execution and exits the
function immediately. after that returns the value.
FUNCTION..(CONTINUE)
• Built-in function
• Built-in functions in PHP are pre-defined functions provided by the PHP language.
• These functions are readily available for use without requiring the user to define them
manually.
• PHP offers a wide range of built-in functions that cater to various needs, such as string
manipulation, array handling, mathematical calculations, date.
FUNCTION..(CONTINUE)
• Date and Time Functions
• The computer stores dates and times in a format called UNIX Timestamp, which measures time as a
number of seconds since the beginning of the Unix epoch: January 1, 1970, 00:00:00 GMT).
• date() Function: The PHP date() function converts timestamp to a more readable date and time
format.
• Syntax: date(format, timestamp)
• The format parameter in the date() function specifies the format of returned date and time.
• The timestamp is an optional parameter, if it is not included then the current date and time will be
used.
• Example: date(“d/m/Y”);
FUNCTION..(CONTINUE)
• Formatting options available in date() function
• d: Represents day of the month; two digits with leading zeros (01 or 31).
• D: Represents day of the week in the text as an abbreviation (Mon to Sun).
• m: Represents month in numbers with leading zeros (01 or 12).
• M: Represents month in text, abbreviated (Jan to Dec).
• y: Represents year in two digits (08 or 14).
• Y: Represents year in four digits (2008 or 2014).
• Example: echo date(“d/m/Y”);
• Example: echo date(“d-m-Y/D”);
FUNCTION..(CONTINUE)
• The following characters can be used along with the date() function to format the time string:
• h: Represents hour in 12-hour format with leading zeros (01 to 12).
• H: Represents hour in 24-hour format with leading zeros (00 to 23).
• i: Represents minutes with leading zeros (00 to 59).
• s: Represents seconds with leading zeros (00 to 59).
• a: Represents lowercase antemeridian and post meridian (am or pm).
• A: Represents uppercase antemeridian and post meridian (AM or PM).
• Example: echo date(“h:i:s”);
• Example: echo date(d-m-Y h:i:A);
FUNCTION..(CONTINUE)
• getdate() function
• The getdate() function returns date/time information of a timestamp or the current local date/time.
• Syntax: getdate(timestamp)
• The timestamp is an optional parameter, if it is not included then the current date and time will be
used.
• Return value
• Returns an associative array with information related to the timestamp.
• Example: print_r(getdate());
FUNCTION..(CONTINUE)
• DateTime::setDate() function
• The DateTime::setDate() function is an inbuilt function in PHP which is used to reset the
current date of DateTime object with the given date-time object.
• Syntax:
• DateTime::setDate( int $year, int $month, int $day )
• It takes three parameters: the year, the month, and the day.
• Return Value: This function returns a new DateTime object on success or False on failure.
FUNCTION..(CONTINUE)
• Example:
• <?php
// Creating a new DateTime() object
$datetime = new DateTime();
• $Year = '2025';
• $Month = '01';
• $Day = '25';
// Calling the setDate() function
$datetime->setDate($Year, $Month, $Day);
echo $datetime->format('Y-m-d');
?> OUTPUT:- 2025-01-25
FUNCTION..(CONTINUE)
• checkdate() function
• The checkdate() function is used to validate a Gregorian calendar date. The function accepts three mandatory
parameters.
• Syntax: checkdate(month, day, year)
• Return Value
• Returns True if the date is valid.
• Return False if the date in invalid.
• Example:- $month = 2;
• $day = 30;
• $year = 2025;
• if (checkdate($month, $day, $year)) {
echo "The date $month/$day/$year is valid."; }
else { echo "The date $month/$day/$year is invalid."; }
?> OUTPUT:- The date 2/30/2025 is invalid.
FUNCTION..(CONTINUE)
• PHP Time function
• The time() function is used to get the current time as a Unix timestamp (the number of seconds since the
beginning of the Unix epoch: January 1, 1970, 00:00:00 GMT).
• h: Represents hour in 12-hour format with leading zeros (01 to 12).
• H: Represents hour in 24-hour format with leading zeros (00 to 23).
• i: Represents minutes with leading zeros (00 to 59).
• s: Represents seconds with leading zeros (00 to 59).
• a: Represents lowercase antemeridian and post meridian (am or pm).
• A: Represents uppercase antemeridian and post meridian (AM or PM).
• $timestamp =time();
• echo (date(F d, Y h:i:s a, $timestamp));
FUNCTION..(CONTINUE)
• mktime() function
• The mktime() function is used to create a Unix timestamp for a specific date and time.
• Syntax: mktime(hour, minute, second, month, day, year)
• Return Value
• Returns an integer Unix timestamp. FALSE on error
• Example:
• $timestamp = mktime(10, 30, 0, 12, 25, 2025);
• echo date("Y-m-d H:i:s", $timestamp); // Outputs the date and time
FUNCTION..(CONTINUE)
• PHP Math Functions
• The predefined math functions in PHP are used to handle the mathematical operations within the
integer and float types.
• 1) abs() function
• The abs() function is an inbuilt function in PHP which is used to return the absolute (positive)
value of a number.
• Parameters
• The abs() function accepts single parameter value which holds the number whose absolute value
you want to find.
• Syntax: abs(value)
• abs(-7)
• OUTPUT: 7
FUNCTION..(CONTINUE)
• 2) ceil() function
• In mathematical problems to round up a decimal number to next greater integral value.
• PHP provides us with a built-in function ceil() to perform such operation.
• The ceil() function is a built-in function in PHP and is used to round a number to the nearest
greater integer.
• Parameters: The ceil() function accepts a single parameter value which represents the number
which you want to round up to the nearest greater integer.
• Syntax: ceil(value)
• ceil(0.80)
• OUTPUT: 1
• Ceil(6)
• OUTPUT: 6
FUNCTION..(CONTINUE)
• 3) floor()function
• Rounds a number down to the nearest smaller integer.
• Syntax: floor(value)
• Example: echo floor(4.7); //OUTPUT: 4
• 4) round() function
• Rounds a number to the nearest integer or to a specified number of decimal places. It follows standard
rounding rules (rounds up if .5 or higher, otherwise rounds down).
• precision: (Optional) The number of decimal places to round to. Default is 0 (round to the nearest
integer).
• Syntax: round($number, $precision);
• Example: echo round(4.4); //OUTPUT: 4.4
echo round(4.5); //OUTPUT:- 5
echo round(4.555, 2); // Output: 4.56
FUNCTION..(CONTINUE)
• fmod() • pow() – Power
• Returns the remainder of a division • Raises a number to the power of another
(modulus). number.
• Syntax: pow($base, $exponent);
• Syntax: fmod($dividend, $divisor);
• Parameters:
• Parameters: dividend: The number to be
divided. • base: The base number that will be raised.
• divisor: The number by which to divide. • exponent: The power to which the base will
be raised.
• Example: echo fmod(10, 3); // Output: 1
echo fmod(15, 4); // Output: 3 • Examples:
echo pow(2, 3); // Output: 8 (2^3)
• echo pow(5, 2); // Output: 25 (5^2)
FUNCTION..(CONTINUE)
• min() - Minimum Value • max() - Maximum Value
• Finds the smallest value from a list of • Finds the largest value from a list of values.
values. • Parameters: value1, value2, ...: A list of
• Parameters: value1, value2, ...: A list of numbers from which the largest value will be
numbers from which the smallest value will returned.
be returned. • Syntax:
• Syntax: max($value1, $value2, ...);
• min($value1, $value2, ...);
• Examples:
echo max(5, 10, 1, 7); // Output: 10
• Examples:
echo min(5, 10, 1, 7); // Output: 1 • echo max(100, 150, 90); // Output: 150
• echo min(100, 150, 90); // Output: 90
FUNCTION..(CONTINUE)
• sqrt() - Square Root • rand() - Random Integer
• Returns the square root of a number. • Generates a random integer between two
• Syntax:
optional values.
sqrt($number); • Syntax: rand($min, $max);
• Examples: • Parameters:
echo sqrt(16); // Output: 4
• echo sqrt(25); // Output: 5 • min: (Optional) The minimum value for the
random number. Default is 0.
• max: (Optional) The maximum value for the
random number.
• Examples:
echo rand();
echo rand(1, 100);