1.) Functions refresher
print the number of characters in your name:
$length = strlen("Samuel");
echo $length;
2.) Functions Syntax
typical structure of a function:
function name(parameters) {
statement;
}
function helloWorld() {
echo "Hello world!";
}
3.) First Function
function displayName() {
echo "Samuel";
}
displayName();
5.) Returning Values
Instead of printing something to the screen, what if you want to make it the value that the function outputs so it can be used elsewhere in your program? In PHP, the return keyword does just that. It returns to us a value that we can work with. The difference between this and echo or print is that it doesn’t actually display the value.
function returnName() {
return "samuel";
}
6.) Parameters and Arguments
Functions wouldn’t be nearly as useful if they weren’t able to take in some input. This is where parameters or arguments come in. These are the variables or inputs that a function uses to perform calculations.
$name = "Samuel";
function greetings($name) {
echo "Greetings," .$name. "!";
}
greetings($name);
7.) Practice Defining multiple parameters
$name = "Samuel";
$age = "28";
function aboutMe($name, $age){
echo "Hello! My name is " .$name. " and I am " .$age. " years old";
}
aboutMe($name, $age);