Reputation: 49
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 :)
Upvotes: 4
Views: 153
Reputation: 1757
If you want to just use a function then you can simplify your function to the following:
function display($name, $age, $job) {
echo $name . " is " . $age . " and is " . $job . ".<br />"
}
display("Obama", 50, "president of the United States");
display("Bill Gates", 60, "Founder of Microsoft");
display("Jacob", 20, "a student");
This will echo out:
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
However you should look at creating classes which are easier to maintain in the long run.
class personInformation {
public $name;
public $age;
public $job;
public function __construct($name, $age, $job) {
$this->name = $name;
$this->age= $age;
$this->job= $job;
}
public function display() {
return $this->name . ' is ' . $this->age . ' and is ' . $this->job . '.<br />';
}
}
Then using this class you can do:
/* Start a new instance of the class */
$obama = new personInformation("Obama", 50, "president of the United States");
$bill = new personInformation("Bill Gates", 60, "Founder of Microsoft");
$jacob = new personInformation("Jacob", 20, "a student");
/* Display the sentences */
echo $obama->display();
echo $bill->display();
echo $jacob->display();
the __construct
function is the same as setting each value seperately however it does it when you call a new instance of the class. So rather doing this:
$obama = new personInformation();
$obama->name = "obama";
$obama->age= 50;
$obama->job= "president of the United States";
You create the __construct function
however note that you must fill in all the information in the __construct
else it will error so you cannot do:
$obama = new personInformation("Obama", 50);
You can add further functions to the class for example:
public function hello() {
return $this->name . ' says hello. <br />';
}
Upvotes: 3
Reputation: 2210
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.
Upvotes: 6
Reputation: 22760
If you don't recognise what's below, fair enough. But some topics to Google and read about are:
So my solution is that you generate a class which handles all the data for the chosen person in an object orientated fashion:
class sentence {
private $name;
private $age;
private $job;
//this is the sentence string that will be
//populated with your data, currently given a default value.
public $output = $this->name . " is " . $this->age . " and is " . $this->job . "!";
/***
Within the class you can create methods which is another
name for functions.
***/
public function setName($name){
$this->name = $name;
}
public function setAge($age){
$this->age = (int)$age; //forces to integer type.
}
public function setJob($job){
//you can fill this one in.
}
} //close class.
So that is the class, how do you use it, well you first create the class as so:
$one = new sentence();
And then add data to it using the functions we set above to save the data parts to the corresponding values.
$one->setAge("50");
$one->setName("george");
And then display the output as follows, note that calling a variable inside a class, rather than a method, means you do not use brackets. So:
//output
print $sentence->output;
This will output
George is 50 and is !
Classes are typcally built in PHP include files and not usually in the file they are usedin.
Upvotes: -1