4

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 :)

8
  • Use a class->method->property call. It's a different way around to your current code but is the way it should be done for ease, clarifty and efficiency for both you and the machine. Commented May 10, 2016 at 12:43
  • 1
    You are creating empty functions displayStory and displayAge as there is no code within {} and you have written code in displayJobbut in that function there is only one parameter i.e $job what you need is a function like display_details($name,$age,$job){ ,I hope it helps Commented May 10, 2016 at 12:44
  • RTM, http://php.net/manual/en/language.functions.php Commented May 10, 2016 at 12:45
  • 6
    @Martin-op is a beginner and has has just started learning functions ,It will be a bit confusing for him to dive into classes/objects for now.. Commented May 10, 2016 at 12:45
  • @Shubanker true, but it's easier to learn from the start than to have to start by unlearning basic knowledge. For example each "function" will need the string passed to it or will need to be inserting the output into the HTML string, both are not industry standard and actually poor design patterns. Commented May 10, 2016 at 12:48

3 Answers 3

6

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Great, thank you so much. Worked and also helped me understand :)
You took the effort to explain the OP what a function is, instead of confusing him with classes. 1+
You eventualy might want to explain to the OP how to handle non mandatory parameters too? What if for example the age is not mentioned. It's useful later on...
@JeroenBellemans That's really outside the scope of the question (and there's other questions that answer that on SO)
3

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 />';
}

2 Comments

Thats why the function is at the top and an intro into classes for extra. He can use it or not. the answer still has what he wants.
Isn't the info at the top just copied from my answer?
-1

If you don't recognise what's below, fair enough. But some topics to Google and read about are:

  • PHP variable scope
  • Object Orientated Programming

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.

9 Comments

Classes again ?! what part of "I'm learning functions" you didn't get ?
@PedroLobito It's an option that works. Let's call it "Live and let live" ;-) the answer could serve others also, not just the OP. However, learning and using a class is the better method, far as I'm concerned.
@Fred-ii- I agree that classes are a better method, but the OP is simply starting on "and he needs to use functions for the "computer science course" not classes, as stated on its question : "but we're supposed to use the function DisplayX"
@PedroLobito The question's starting to get "too broad" at this point lol and possibly "opinion-based".
@Fred-ii- Totally, I'm out ;)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.