How to Use Arguments in PHP Functions - Coding Cyber

How to Use Arguments in PHP Functions

How to Use Arguments in PHP Functions

Other Articles in PHP User Defined Functions Section

Quickly Navigate to Other Sections in PHP Tutorials

PHP Basics
PHP Data Types
PHP Advanced Data Types
PHP Logical Statements
PHP Operators
PHP Loops
User Defined Functions in PHP
Useful PHP Functions

PHP Function with Arguments

Till now, we have worked on simple function without any arguments. Arguments are the data we provide to the function to process or to execute.

Syntax of PHP Function with Arguments
PHP Function with Arguments are same like PHP functions, instead we pass arguments in the braces. These arguments will be used in the statements inside the function.

function functionName($arguments) {
code to be executed with $arguments;
}

Example of PHP Function with Arguments
Here in this below example code, we are using variable name in the braces. This name variable will be used to write the message.

function HelloName($name) {
echo “Hello $name”;
}

HelloName(“vivek”);

This is another example with two arguments, which makes it add more functionality. Here in this function I’m passing two variables one is name and the other one is year which is date of birth.

function HelloName($name, $year) {
echo “$name Born in $year
“;
}

HelloName(“vivek”, “1988”);

Output will be “vivek Born in 1988”

PHP Functions with Default Argument Value

PHP functions can be executed with default Argument value, what if there is no argument passed in the function. In such type of cases we can use default argument value.

Example of PHP Functions with Default Argument Value
function HelloName($name = “vivek”) {
echo “Hello $name”;
}

Related Posts
No related posts for this content
Vivek Vengala
 

Vivek Vengala is a Online Entrepreneur, Web Developer from Hyderabad India.

Click Here to Leave a Comment Below 0 comments

Leave a Reply: