douyun7285 2015-06-03 08:02
浏览 27

如何创建一个从代码中获取参数并返回字符串的函数?

So, I've been trying to learn PHP. I've got an assignment where I'm supposed to make a list of products with prices, and depending on what day it is, it's supposed to give you a discount. That works so far. However, one thing i'm trying to do is to not echo the result directly in the code; I want to create a function that takes the necessary parameters (Taste, Price, Amount) and returns a formatted string. I haven't been able to do this, despite trying a lot. Here's the code I have. This is the first time i'm using Stack Overflow, so forgive me if i didn't type it in correctly.

<?php
    $donuts = array(
        array("Strawberry", 10),
        array("Chocolate", 20),
        array("Vanilla", 30)
    );
    $weekday = date('N');
    $week = date('W');
    $hour = date('G');

    if ($weekday == 4) {
        echo "Hi";
    }
?>
<?php
    $today = date("l F j, Y");
        if ($weekday % 2 == 1 && $week % 2 == 1 && $hour >= 14 && $hour < 17){              
            echo "Congratulations! You get a 5% discount, and your wares will be delivered tomorrow!";  
        }   
    PRINT "$today";
?>
<table border="1">
        <tr>
            <th>Taste</th>
            <th>Price</th>
            <th>Discount</th>
            <th>Amount</th>
        </tr>
    <?php
        for($i = 0; $i<count($donuts); $i++) {
        echo "<tr>";
        echo "<td>".$donuts[$i][0]."</td>"; 
        if( $weekday == 1){
            echo"<td>".getPricePercent($donuts[$i][1], .5)." kr</td>";
            }           
            if( $weekday == 3){
                echo"<td>".getPricePercent($donuts[$i][1], 1.1)." kr</td>";     
            }           
            $pris = $donuts[$i][1];
                if ($weekday == 5 && $pris > 20){
                    echo"<td>".($pris-1)." kr</td>";
                }           
                else { echo"<td>".getPrice($donuts[$i][1], .9)." kr</td>";}
    ?>
            <td><input type="number" name="donuts-<?php echo $i; ?>" /></td>
            <?php
                echo "</tr>";
        }
            ?>
</table>
<?php
    function getPrice($price, $percent) {
        return ($price);
    }
    function getPricePercent($price, $percent){
        return ($price * $percent); 
    }   
?>
  • 写回答

2条回答 默认 最新

  • duadlkc5762218 2015-06-03 08:15
    关注

    If you want to concat the strings, you can achieve it by using something like this:

    echo "Price" . $price . "Amount" . $amount;
    
    评论

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭