使用PHP内部的基本功能

时间:2016-05-10 12:39:08

标签: php html function

我是一个完全的初学者,很抱歉,如果我这样说的方式令人困惑。 我正在研究我的在线计算机科学课程,我们正在练习各种功能。我们应该做的是设置三个句子在句子中输出三个不同的变量

  

x y 岁,是 z

这就是我的意思:

  

奥巴马 50 岁,美国总统

     

比尔盖茨 60 岁,是微软的创始人

     

Jacob 20 岁,学生

这是我目前的代码:

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

?>

我确信有更简单的方法来完成此操作,我知道其他方法可以完成此操作,但我们应该使用函数 DisplayX 来完成此操作。

提前谢谢你:)

3 个答案:

答案 0 :(得分:6)

您正在寻找的是用户定义的功能。

一个函数基本上是一组指令,这些指令用一个名称来概括。要创建一个函数,请使用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");

有关用户定义函数的更多信息,您可以read the documentation

功能设计

至于个人偏好,最佳做法和这种特殊情况,您可以用另一种方式设计您的功能。

使用PHP函数时,总是会return一个值。这可以是数字,某些文本,对象,您可以命名。

您可以删除该功能的功能已经显示&#34;显示&#34;个人信息,只需将个人信息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");

有关返回值的更多信息,请访问here

答案 1 :(得分:3)

如果您只想使用某个功能,那么您可以将功能简化为以下内容:

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岁,是微软的创始人

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

答案 2 :(得分:-1)

如果你不认识下面的内容,那么公平。但谷歌的一些话题和读物是:

  • 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包含文件构建,通常不在它们使用的文件中。