我倾向于PHP5类,实例我是新的。但是我试图在foreach
内使用Class Methods
循环来获取此人的性别,但它只显示第一个人的权利,但没有为第二个人显示第3个等等。
这是我的代码:
class Person{
// atteributes or property of class
var $first_name;
var $last_name;
var $legs = 2;
var $hand = 2;
var $gender;
function say_hello(){
echo "Hello inside the class from " . get_class($this) . "<br />";
}
// full name function
function full_name(){
echo $this->first_name ." ". $this->last_name;
}
// Gender
function gender(){
$names = array("Hamza","Ali","Ibrar","Muqeet","Hassan","Umer","Shaveer");
foreach($names as $name){
if($this->first_name == $name){
echo $this->gender = "Male";
break;
}else{
echo $this->gender = "Female";
break;
}
}
}
}
$person = new Person();
$person->first_name = "Hamza";
$person->last_name = "Nisar";
$person->full_name();
echo "<br />";
$person->gender();
echo "<br />";
/*---------------------*/
$person2 = new Person();
$person2->first_name = "Ibrar";
$person2->last_name = "Ahmed";
$person2->full_name();
echo "<br />";
$person2->gender();
echo "<br />";
这就是结果:
Hamza Nisar
Male
Ibrar Ahmed
Female
它必须显示男性也是第二个人
答案 0 :(得分:2)
使用foreach循环并检查每种可能性似乎是实现此目的的一种奇怪方式。我建议使用in_array函数替换foreach函数,该函数在if语句中使用时允许您使用较少的代码执行相同的操作。
示例:强>
<?php
class Person{
// atteributes or property of class
var $first_name;
var $last_name;
var $legs = 2;
var $hand = 2;
var $gender;
function say_hello(){
echo "Hello inside the class from " . get_class($this) . "<br />";
}
// full name function
function full_name(){
echo $this->first_name ." ". $this->last_name;
}
// Gender
function gender(){
$names = array("Hamza","Ali","Ibrar","Muqeet","Hassan","Umer","Shaveer");
if(in_array($this->first_name, $names)){
echo $this->gender = "Male";
}
else {
echo $this->gender = "Female";
}
}
}
$person = new Person();
$person->first_name = "Hamza";
$person->last_name = "Nisar";
$person->full_name();
echo "<br />";
$person->gender();
echo "<br />";
/*---------------------*/
$person2 = new Person();
$person2->first_name = "Ibrar";
$person2->last_name = "Ahmed";
$person2->full_name();
echo "<br />";
$person2->gender();
echo "<br />";
?>
答案 1 :(得分:1)
<?php
class Person{
// atteributes or property of class
var $first_name;
var $last_name;
var $legs = 2;
var $hand = 2;
var $gender;
function say_hello(){
echo "Hello inside the class from " . get_class($this) . "<br />";
}
// full name function
function full_name(){
echo $this->first_name ." ". $this->last_name;
}
// Gender
function gender($newname){
$names = array("Hamza","Ali","Ibrar","Muqeet","Hassan","Umer","Shaveer");
if(in_array($this->first_name, $names)){
echo $this->gender = "Male";
}else{
echo $this->gender = "Female";
}
}
}
$person = new Person();
$person->first_name = "Hamza";
$person->last_name = "Nisar";
$person->full_name();
echo "<br />";
$person->gender($person->first_name);
echo "<br />";
/*---------------------*/
$person2 = new Person();
$person2->first_name = "Ibrar";
$person2->last_name = "Ahmed";
$person2->full_name();
echo "<br />";
$person2->gender($person2->first_name);
echo "<br />";
?>
答案 2 :(得分:1)
这些答案都没有告诉您为什么您的代码无效。当first_name =“Ibrar”:
时,让我们遵循foreach的逻辑$names = array("Hamza","Ali","Ibrar","Muqeet","Hassan","Umer","Shaveer");
foreach($names as $name){
if($this->first_name == $name){
echo $this->gender = "Male";
break;
}else{
echo $this->gender = "Female";
break;
}
}
Loop1:IF语句为false,如“Ibrar”!==“Hamza”。因此,使用ELSE,打印女性和休息。
其他答案都是很好的修复。但是,假设您仍然希望使用 foreach 循环。你可以这样做:
$this->gender = 'Female';
$names = array("Hamza","Ali","Ibrar","Muqeet","Hassan","Umer","Shaveer");
foreach($names as $name){
if ($this->first_name === $name) {
$this-gender = 'Male';
break;
}
}
echo $this->gender;
答案 3 :(得分:1)
您需要关注的是中断关键字。如果要获得预期结果,则需要跟踪代码流。根据你的代码,这就是它的内容:
我将从你做错的代码部分开始:
对于$person
个对象,
$person->gender()
将按以下方式执行:
迭代1:
foreach($names as $name)
结果$name = "Hamza"
$this->first_name
是"Hamza"
,== $name
如果条件为真,则打印“男性”。
break;
突破了foreach循环。
没有其他迭代。
输出结果为:
Male
对于$person2
个对象,
$person2->gender()
将按以下方式执行:
迭代1:
foreach($names as $name)
结果$name = "Hamza"
$this->first_name
是"Ibrar"
,!= $name
如果条件在这里不为真,即它是假的,那么打印“女性”。
break;
突破了foreach循环。
没有其他迭代。
输出结果为:
Female
您的代码没有说出您想要的内容。
它说:
如果数组的第一项具有我们想要的名称,请打印“Male”,否则打印“Female”。只有在检查数组中的第一项后,我们才完成并退出foreach循环。我们不需要检查同一个数组中的其他项目。
所以这就是结果:
对于类Person
的每个对象,您创建的$first_name
属性值不为"Hamza"
,"Female"
将从对成员函数{{}的调用中打印出来1}}。
解决方案是编写代码:
如果条件为gender()
,则不应突破循环,因为必须使用false
数组中的每个项目检查$this->first_name
。如果$names
数组中的所有项都返回$names
,则false
属性值为“女性”也会打印出来。
解决方案代码: 在其他答案中已经提到过。
答案 4 :(得分:0)
没有foreach requred change和性别功能如下所示,并在函数调用之前写入echo。
function gender(){
$names = array("Hamza","Ali","Ibrar","Muqeet","Hassan","Umer","Shaveer");
if(in_array($this->first_name,$names)){
return $this->gender = "Male";
}else{
return $this->gender = "Female";
}
}