CH 3 - Part 1
CH 3 - Part 1
& 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
• 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).
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).
• 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.
• Examples:
echo rand();
echo rand(1, 100);