嵌套if语句需要Php帮助 [英] Php help needed with nested if statement

查看:91
本文介绍了嵌套if语句需要Php帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好编码员,

我正在练习foreach并在php中嵌套。

我有一个所谓的bug。代码没有按照应有的方式工作。

我有一系列汽车和foreach语句循环遍历这些数组元素。

现在我正在使用if语句,如果汽车是宝马,那么用世界上最好的汽车替换它,如果是大众汽车那么用国家汽车代替它。到目前为止它一直很好,直到宝马是最好的汽车,但它不会取代大众汽车与国家汽车,而是打印出其他声明,并打印出未找到的汽车。请检查以下代码。

还请告诉我,是否可以使用递归功能替换带有法拉利的奥迪?我的意思是,如果我调用一个递归函数,当找到audi时,它会用ferarri替换它。



我尝试过:



Hello coders,
I am practicing foreach and nested if in php.
I am having a so called bug. The code is not working the way it should.
I have an array of cars and foreach statement that loops through these array elements.
Now i am using if statement that, if the car is bmw then replace it with best car in the world and if it is volkswagen then replace it with nation car. So far it works good until bmw is best car but it does not replace volkswagen with the nation car and instead it prints out the else statement and prints out that the cars not found. Please check the code below.
Also please tell me that, is it possible to replace audi with ferrari using recursive function? I mean, if i call a recursive function and when the audi is found, it replaces it with ferarri.

What I have tried:

<?php

$anarray= array('bmw','volkswagen','volvo','audi','bmw');

foreach($anarray as $value){
    
    echo 'The car is: '.$value.'.'.' ';
    
    
}
if($value == 'bmw')
    {
        echo 'bmw is the best car in the world.'.' '.' ';
        
        if($value == 'volkswagen')
    {
        echo 'volkswagen is a  nation car';
    }
    else{
        echo 'cars not found.';
    }
    }
    
    
    
    


?>

推荐答案

anarray = array(' bmw'' volkswagen'' volvo'' audi'' bmw');

foreach(
anarray= array('bmw','volkswagen','volvo','audi','bmw'); foreach(


anarray as


value) {

echo ' 汽车是:'
value){ echo 'The car is: '.


这篇关于嵌套if语句需要Php帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆