Home > Back-end >  PHP Problem : 8th and 9th entrance of the highway doesn't work
PHP Problem : 8th and 9th entrance of the highway doesn't work

Time:10-31

so i have a problem with my PHP code. It's simply the highway calculator, when you entrance in the highway, you receive a ticket with 4 numbers : the first two is the number of the entrance of the highway (0 to 9), and the last two is the vehicle (10 for motorbike, 11 for car and 12 for truck). When i enter the ticket 0711, i have this : Ticket highway. But when i enter the ticket 0811 or 0911, i have this : Ticket highway 2 and i don't know why please guys help me. (i'm very bad in english i'm french so i'm sorry if you don't understand anything).

This is my code :

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        table{
        border-collapse: collapse;
        background-color:lightblue;
        }

        th, td{
        border: 1px solid black;
        padding: 10px;
        }

    </style>
</head>
<body>
<?php
        if (isset($_POST['ticket']))
        {
            $ticket=$_POST['ticket'];
            $xx=substr($ticket,0,-2);
            $yy=substr($ticket,2,4);

            if ($xx==00)
            {
                $km=200;
            }
            elseif ($xx==01)
            {
                $km=180;
            }
            elseif ($xx==02) 
            {
                $km=160;
            }
            elseif ($xx==03) 
            {
                $km=140;
            }
            elseif ($xx==04) 
            {
                $km=120;
            }
            elseif ($xx==05) 
            {
                $km=100;
            }
            elseif ($xx==06) 
            {
                $km=80;
            }
            elseif ($xx==07) 
            {
                $km=60;
            }
            elseif ($xx==08) 
            {
                $km=40;
            }
            elseif ($xx==09) 
            {
                $km=20;
            }

            if ($yy==10)
            {
                $prix=0.05*$km*0.5;
            }
            elseif ($yy==11)
            {
                $prix=0.05*$km*1;
            }
            elseif ($yy==12) 
            {
                $prix=0.05*$km*1.2; 
            }

            echo "type de véhicule : $yy<BR>";
        }
    ?>
    <table>
        <tr>
            <td>
                n° entrée de péage : 
            </td>
            <td>
                <?php echo $xx; ?>
            </td>
        </tr>
        <tr>
            <td>
                Kilomètres : 
            </td>
            <td>
                <?php echo "$km kms"; ?>
            </td>
        </tr>
        <tr>
            <td>
                Catégorie véhicule : 
            </td>
            <td>
                <?php 
                    if ($yy==10)
                    {
                        echo "Moto";
                    }
                    elseif ($yy==11) 
                    {
                        echo "Voiture";
                    }
                    elseif ($yy==12) 
                    {
                        echo "Camion";
                    }
                ?>
            </td>
        </tr>
        <tr>
            <td>
                Prix à payer : 
            </td>
            <td>
                <?php echo "$prix €"; ?>
            </td>
        </tr>
    </table>
</body>
</html>

CodePudding user response:

The reason this was happening is because $xx is of type string, and you are comparing against 08 and 09 which are "Invalid numeric literals" according to PHP. The number 09 is different than 9, PHP tries to treat it as an octal number. What you need to do, is compare against "08", or type string. It is also a good idea to separate your PHP code with the presentation of output.

Please see the example below:

<?php

$km_map = array(
    "00" => "200",
    "01" => "180",
    "02" => "160",
    "03" => "140",
    "04" => "120",
    "05" => "100",
    "06" => "80",
    "07" => "60",
    "08" => "40",
    "09" => "20"
);

if (isset($_POST['ticket']))
{
    $ticket = $_POST['ticket'];
    $xx = substr($ticket,0,-2);
    $yy = substr($ticket,2,4);

    $km = $km_map[$xx];

    $prix = 0.0;
    $vehicle = "";

    switch ($yy)
    {
        case "10":
            $prix = 0.05 * $km * 0.5;
            $vehicle = "Moto";
            break;

        case "11":
            $prix = 0.05 * $km * 1;
            $vehicle = "Voiture";
            break;

        case "12":
            $prix = 0.05 * $km * 1.2;
            $vehicle = "Camion";
            break;
    }
}

?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        table{
        border-collapse: collapse;
        background-color:lightblue;
        }

        th, td{
        border: 1px solid black;
        padding: 10px;
        }

    </style>
</head>
<body>
    <table>
        <tr>
            <td>
                n° entrée de péage : 
            </td>
            <td>
                <?php echo $xx; ?>
            </td>
        </tr>
        <tr>
            <td>
                Kilomètres : 
            </td>
            <td>
                <?php echo "$km kms"; ?>
            </td>
        </tr>
        <tr>
            <td>
                Catégorie véhicule : 
            </td>
            <td>
                <?php echo $vehicle; ?>
            </td>
        </tr>
        <tr>
            <td>
                Prix à payer : 
            </td>
            <td>
                <?php echo "$prix €"; ?>
            </td>
        </tr>
    </table>
</body>
</html>
  • Related