A function in PHP is a piece of code that takes in input as a parameter, processes it, and returns a value. Functions can be used repeatedly in a program. A function is defined using the following syntax: A valid function name in PHP starts with a letter or underscore, followed by any number of letters, numbers, or underscores. A function contains a set of instructions to be executed, defined by using the keyword function followed by the name of the function, the parentheses which contain the parameters, and finally the curly braces which contain the code block. To call a function, you... Show more A function in PHP is a piece of code that takes in input as a parameter, processes it, and returns a value. Functions can be used repeatedly in a program. A function is defined using the following syntax: A valid function name in PHP starts with a letter or underscore, followed by any number of letters, numbers, or underscores. A function contains a set of instructions to be executed, defined by using the keyword function followed by the name of the function, the parentheses which contain the parameters, and finally the curly braces which contain the code block. To call a function, you can: Write its name followed by parentheses () For example, `function myMessage() ( echo `Hello world!''; ) myMessage(); You can pass information to functions through arguments. The return keyword is used to return value back to the part of program, from where it was called. The returning value may be of any type including the arrays and objects. The return statement also marks the end of the function and stops the execution after that and returns the value. PHP has a wide range of built-in functions, including: String functions: Manipulate strings strlen(): Calculates string length strtoupper(): Converts strings to uppercase substr(): Extracts a portion of a string Mathematical functions: Perform mathematical operations sqrt(): Calculates the square root pow(): Raises a number to a power rand(): Generates random numbers Date and time functions: Deal with dates and times Related test: PHP Programming Basics Practice Test: PHP Arrays and Functions Show less
A function in PHP is a piece of code that takes in input as a parameter, processes it, and returns a value. Functions can be used repeatedly in a program. A function is defined using the following syntax:
A valid function name in PHP starts with a letter or underscore, followed by any number of letters, numbers, or underscores. A function contains a set of instructions to be executed, defined by using the keyword function followed by the name of the function, the parentheses which contain the parameters, and finally the curly braces which contain the code block.
To call a function, you can: Write its name followed by parentheses () For example, `function myMessage() ( echo `Hello world!''; ) myMessage(); You can pass information to functions through arguments. The return keyword is used to return value back to the part of program, from where it was called. The returning value may be of any type including the arrays and objects. The return statement also marks the end of the function and stops the execution after that and returns the value.
PHP has a wide range of built-in functions, including: String functions: Manipulate strings strlen(): Calculates string length strtoupper(): Converts strings to uppercase substr(): Extracts a portion of a string
Mathematical functions: Perform mathematical operations sqrt(): Calculates the square root pow(): Raises a number to a power rand(): Generates random numbers Date and time functions: Deal with dates and times
Related test: PHP Programming Basics Practice Test: PHP Arrays and Functions
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.