Using basic functions inside PHP

I'm a complete newbie, so sorry if I say this, this is confusing. I am currently working on my online computer science course, and we practice the features. We have to make three sentences to output three different variables inside the sentence

x - y years and has z

Here is what I mean:

Obama is 50 years old and the president of the United States .

Bill Gates is 60 years old and the founder of Microsoft .

Jacob is 20 years old and a student .

This is the code I 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 am sure there is an easier way to complete this, and I know other ways to complete this, but we must use the DisplayX function to do this.

Thanks in advance:)

+4
3

, , .

- , . , function, .

, , . - displayInformation.

3 , 3 , . , . ( ) , .

, :

function displayInformation($name, $age, $job) {

}

- , .

echo $name . " is " . $age . " and is " . $job . ".<br />";

:

<?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");

.


, , -.

PHP return . , , , .

, "" , return . return :

function getInformation($name, $age, $job) {
    return $name . " is " . $age . " and is " . $job . ".<br />"
}

echo .

echo getInformation("Obama", 50, "president of the United States");
echo getInformation("Bill Gates", 60, "Founder of Microsoft");
echo getInformation("Jacob", 20, "a student");

.

+6

, :

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");

:

50 .

60 Microsoft

20

, .

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

, , :

/* 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();

__construct , , . :

$obama = new personInformation();
$obama->name = "obama";
$obama->age= 50;
$obama->job= "president of the United States";

__construct function , __construct , :

$obama = new personInformation("Obama", 50);

, :

public function hello() {
    return $this->name . ' says hello. <br />';
}
+3

, , . Google :

  • PHP

, , , :

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. 

, , , :

$one = new sentence();

, , , .

$one->setAge("50");
$one->setName("george");

: , , , , . :

//output
print $sentence->output;

- 50 !

PHP, , , .

-1
source

All Articles