I'm a complete beginner so sorry if the way I word this is confusing. I'm working on my online computer science course right now and we're practising functions. What we're supposed to do is set three sentences to output three different variables inside the sentence
x is y years old and is z
Here's what I mean:
Obama is 50 years old and is president of the United States.
Bill Gates is 60 years old and is Founder of Microsoft.
Jacob is 20 years old and is a student.
This is the code that I currently have:
<?php
function displayStory($name) {
}
function displayAge($age) {
}
function displayJob($job) {
echo $name . " is " . $age . " and is " . $job . ".<br />";
}
displayStory("Obama");
displayAge("50");
displayJob("the President of The United States");
displayStory("Bill Gates");
displayAge("60");
displayJob("the founder of Microsoft");
displayStory("Jacob");
displayAge("20");
displayJob("a student");
?>
I'm sure there's an easier way to complete this and I know other ways to complete this, but we're supposed to use the function DisplayX to complete this.
Thank you in advance :)
What you are looking for is user-defined functions.
A function is basicly a set of instructions, these instructions are generalized with a name. To create one single function, you use the function
keyword followed by a space and the name of the function.
In this case, this functionality you need could be achieved by just one function. You can name it something like displayInformation
.
The function is gonna need 3 parameters, which are the 3 things you are wanting to display. The name, the age and the job of the person. Parameters should be defined in ( )
's as variables which comes after the function name.
To create this function, it looks something like this:
function displayInformation($name, $age, $job) {
}
The context of the function can now simply be the echo'ing of the data like you did in one of your functions.
echo $name . " is " . $age . " and is " . $job . ".<br />";
The final result of this code would be:
<?php
function displayInformation($name, $age, $job) {
echo $name . " is " . $age . " and is " . $job . ".<br />"
}
displayInformation("Obama", 50, "president of the United States");
displayInformation("Bill Gates", 60, "Founder of Microsoft");
displayInformation("Jacob", 20, "a student");
For more information on user-defined functions, you can read the documentation.
Function design
As for personal preference, best practices and this specific case, you can design your function in another way.
When using PHP functions, it is always going to return
a value. This can be a number, some text, a object, you name it.
You could remove the functionality of the function to already "display" the personal information, and just make it return
the personal information as a string. This could be done by the return
keyword like this:
function getInformation($name, $age, $job) {
return $name . " is " . $age . " and is " . $job . ".<br />"
}
Now you only have to use echo
to display the information.
echo getInformation("Obama", 50, "president of the United States");
echo getInformation("Bill Gates", 60, "Founder of Microsoft");
echo getInformation("Jacob", 20, "a student");
More information about returning values can be found here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With