0

This is my first post. So sorry if I get the format wrong :)

Im trying to build a program, that asks for a input using html, sets that input as a php variable, and then calculates that variable. Everything is ready except for the input part, which I find extremely challenging. My code is as follows:

<?php
$x = 1;
$answer = ($x = 1);
if($answer) {
    echo "The answer is: True"; 
} else {
    echo "The answer is: False";    
}
?>

The variable I'm trying to set is the ($x = 1) part. The codes purpose is to see if a mathematical calculation is true or false. The code has already been tested.

I have already searched the internet for an answer, and sadly I only saw answers for questions that were way different.

executable
  • 3,365
  • 6
  • 24
  • 52
  • 2
    `($x = 1)` since `=` is an assignment operator rather than a comparison operator, `$answer` will always be *truthy* – CD001 Nov 06 '18 at 10:45
  • Look at this link on how to check for equality. At the moment you are just assigning a value - https://stackoverflow.com/a/80649/2570277 – Nick Nov 06 '18 at 10:50
  • Also if($answer) will always be true no matter what. Basically you check if the variable exists you don't compare it with anything – pr1nc3 Nov 06 '18 at 10:50

6 Answers6

0

For that you need to have a form which can submit values to calculation script or same page if script is in same page.

Also in your script you are not comparing anything as the condition is always true as answer is always 1 and condition is always satisfied, so for that you can compare the user input to answer as I did in example.

For example calc.php

<?php 
echo"<form method="POST" action="">Your answer:<input type="text" name="val" /><br><input type="submit" /></form>";
@$val=$_POST['val'];
if($val){
    $answer = 1;
    if($answer==$val) {
    echo "The answer is: True"; 
    } 
    else {
        echo "The answer is: False";    
    }
}
?>
Avinash Karhana
  • 659
  • 4
  • 16
0

In PHP '=' means assignment operator. Equal check operator should be '=='.

So, your code should be refactored as:

    <?php

    $x = 1;

    $answer = ($x == 1); // this will return true or false

    if($answer) {
       echo "The answer is: True";    
    } else {
       echo "The answer is: False";
    }


    ?>

You can also use ternary operator to shorten your code instead of if/else statement like this:

    <?php

    $x = 1;

    $answer = ($x == 1); // this will return true or false

    echo $answer ? 'The answer is: true' : 'The answer is: false';


    ?>
Gabriel
  • 970
  • 7
  • 20
0

you need to create form to get input from html. there is many different approaches to create form.

I think your are begginer, so simply you can create form on the same page.

you can separate php code from html form write your php code on the top of the page like example below

    <?php
if(isset($_POST['submit'])){
    $x = $_POST['myinput'];

    $answer = $x;

    if($answer) {
    echo "The answer is: True"; 

     } else {
        echo "The answer is: False";    
     } 
    }   
     ?>


    <form method="POST" action="">
    <input type="Text" name="myinput" >
    <inpu type="submit" name="submit" value="submit button">
    </form>

action="" is used for same page. if you want to use separate page for html part you can give action="yourPHPPage.php"

Sayed Mohd Ali
  • 2,156
  • 3
  • 12
  • 28
0

Did u mean this?

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>

<body>
    <?php
      if (isset($_POST['submit'])) {

        $answer = $_POST['input'];

        if($answer == 1) {
        echo "The answer is: True"; 

        } else {
            echo "The answer is: False";    

        }
    }

    ?>
    <form action="" method="post">
        Enter value: <input name="input" type="text" />
        <input name="submit" type="submit" />
    </form>
</body>

</html>
0

There's one small issue with your current code: You're using = to compare a value to another, which will not work, single = is used to assign a variable. You should use == instead.

When you want to use a user's input, you will need something called $_POST, this is a method that you can set in a form using the method="POST" attribute. When the form is submitted it will create an array with the values in the form.

You can then access these values using a certain key, which is equal to the name="" attribute of the input, select or textarea in the form.

Example


Consider this form, with some PHP code:

<form method="post">
    <input type="text" name="myName" placeholder="Enter your name!">
    <input type="submit" value="Submit">
</form>

<?php
// When the server gets a $_POST request
if($_SERVER['REQUEST_METHOD'] == 'POST') {

    // Set the variable name to whatever the user put in
    $name = $_POST['myName']; // equal to the name="" attribute in the form

    echo "Hello, " . $name . "!";

}
?>

If I submit the form with my name, it will echo: Hello, rpm192!


For your situation, it would look something like this:

<form method="post">
    <input type="number" name="answer" placeholder="Your answer">
    <input type="submit" value="Submit answer">
</form>

<?php

// When the server gets a $_POST request
if($_SERVER['REQUEST_METHOD'] == 'POST') {

    // Set the variable X to whatever the user put in
    $x = $_POST['answer'];

    $answer = ($x = 1);

    // Check if $answer is true or false
    if($answer) {
        echo "True!";
    } else {
        echo "False!";
    }

}

?>
rpm192
  • 2,630
  • 3
  • 20
  • 38
  • First of all, thank you! Its very nice of you to help me. And yeah, my programming teacher was puzzled too, but we totally forgot the == instead of = haha, thanks again, ill try the code – Knight Deus Vult Nov 08 '18 at 10:54
-1

As the purpose is to check if a mathematic function's value is True or False, I propose a simple CLI script.

<?php
 print ((isset($argv[2]) && ($argv[2] === 1)) ? "\nAnswer is True\n\n" : "\nAnswer is false\n\n");

Save this as myfile.php Open command line navigate to the folder and type in

>php myfile.php 1 // Returns Answer is True

So if you want to change the input you can do that also..

>php myFile.php 4 // Answer is False

Note that if you're passing String "1", this would also return True.

  • Did you even read the question? "Having a html input as a variable for php", your solution doesn't include any HTML whatsoever.... – rpm192 Nov 06 '18 at 14:02
  • @rpm192 - Did you read _ The codes purpose is to see if a mathematical calculation is true or false._ Also did you notice the first line of my answer? – Vimal Maheedharan Nov 07 '18 at 04:16