Functions With Arguments in Php Programming Languages. This tutorial is about functions passing arguments to functions.Functions are useful in code to shorten the amount of code to write and you can refer back this functions too which is known as calling of functions and it will process the same code every-time you call.
Code basic Structure of functions (Screenshot1):-
Screenshot1 |
<?php function name() { /// here } name(); ?>
This is basic and standard structure of functions.
Code for adding numbers by functions (Screenshot2):-
Code for adding numbers by Functions Screenshot2 |
<?php function add($number1, $number2) { echo $number1 + $number2; } add(); ?>
In this example,I have created a function adding two numbers.You may do programming as it is simply done to add two numbers. You may take user input as number1 and number2 and for that you may need functions to add to numbers together. Always get practiced of using functions instead of structures as its lot better then doing things on the fly. By calling functions specially with arguments you save a lot of time,better and as well as easier to read.
In this example, I am making function add and inside the function specify those arguments which are in the form of variables.Here my variables are $number1 and $number2. Here the variables type are unspecified that whether they are numbers or string which actually doesn't matter.That depends on how you deals everything inside functions.Now echo out that particular results where $number1 will refer to functions variable. The functional arguments must match that is used in the functions. Here let me show you calling functions without arguments to show you errors, and how to read errors.
In output Screenshot3
Now you can see in output warning coming up which is missing argument 1 for add(). Its telling us first argument is missing for function add, it tells us in which line code is and it also tell us where the functions has been declared. And also its missing argument 2 for add() function and code in same line and function is defined in same line and actually it gives the results.
Code for adding two numbers in functions with arguments(Screenshot4):-
Code for adding two numbers in functions with arguments Screenshot4 |
<?php $inumber1 = 10; $inumber2 = 5; function add($number1, $number2) { echo $number1 + $number2; } add($inumber1,$inumber2); ?>
Output (Screenshot5):-
Screenshot5 |
In this example of adding two number we are including some numbers. Taking two variables like inumber1 and inumber2. Now as we created variables $number1= 10 and $number2= 5 so, result is 15.Now when we call function we need to specify that two variables ,this two variables will pass as $inumber1 will be pass through function variable $number1 and $inumber2 will be pass through function variable $number2. That the only arguments we need to pass through functions.Now calling in add() that variables will be interpreted, then inside functions they are processed.then we echo out the results.And you can see results 15.
Code for adding two numbers in functions with arguments(Screenshot6):-
Screenshot6 |
<?php $inumber1 = 22; $inumber2 = 8; function add($number1, $number2) { echo $number1 + $number2; } add($inumber1,$inumber2); ?>
Output (Screenshot7):-
Screenshot7 |
In this examples codes are same just changed variables to different integer.
Code for functions with arguments to display date (Screenshot8):-
Screenshot8 |
<?php function displayDate($day, $date, $year) { echo $day.' '.$date.' '.$year; } displayDate('Monday',31,2011); ?>
Output (Screenshot9):-
Screenshot10 |
In this example,I am making functions to print date.Making functions of displaying date will feed in the day , the date and the year.Then echo out the result into the functions.Here our day will be string then date will be integer and year will be integer.Now on calling the function displayDate(), passing variables string as well as integer will be displayed.In this way arguments can be pass in php.