input calculate number

Discussion in 'PHP' started by walidsu, Apr 29, 2012.

  1. #1
    Hi, i made a simple php code,

    im not getting the incorrect answer,

    
    <?
    
    $num = "12";
    $num2 = "3";
    
    $cal = $num - $num2;
    
    echo $cal ;
    
    ?>
    <title><?php echo $cal?></title>
    <form action="page.php" method="POST">
        what is <?php echo $num . " - " .$num2?> ? <input type="text" name="cap">
        <input type="submit" value="Submit">
    
    </form>
    
    PHP:
    
    <?
    
    $num = "12";
    $num2 = "3";
    
    $cal = $num - $num2;
    
    $cap = $_POST['cap'];
    
    if ($cal !== $cap){
        echo "yes 12 - 3 is ". $cap;
    }else{
        "No, the answare is incorect";
    }
    ?>
    
    PHP:

     
    walidsu, Apr 29, 2012 IP
  2. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #2
    If you are going to use strict variable comparisons (===. !===) the best way to test why the variables do not match is by using var_dump($var). Which will show you the variable type.

    You either have to typecast the values or just use normal comparison:

    
    <?php
    $num = 12;
    $num2 = 3;
    
    $cal = $num - $num2;
    
    $cap = (int) $_POST['cap'];
    
    if ($cal !== $cap) {
        echo "yes 12 - 3 is ". $cap;
    } else {
        "No, the answare is incorrect";
    }
    
    PHP:
    Or:
    
    $num = "12";
    $num2 = "3";
    
    $cal = $num - $num2;
    
    $cap = $_POST['cap'];
    
    if ($cal != $cap) {
        echo "yes 12 - 3 is ". $cap;
    } else {
        "No, the answare is incorrect";
    }
    
    PHP:
     
    ThePHPMaster, Apr 29, 2012 IP
  3. walidsu

    walidsu Member

    Messages:
    82
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    28
    #3
    im not good whit php
    but
    i meant, if you enter the wrong number its still saying that yes 12 - 3 is any number .

    so in first/index page it says what is 12 - 3 ? if you enter the wrong/ any number its saying that yes 12 - 3 is displays any number you have entered.
     
    walidsu, Apr 29, 2012 IP