Nick
Nick

Reputation: 2641

PHP print and echo not working

I am new to PHP and I was making this form and I wanted to print some data but it is not displaying. What is wrong with it? Here's the code:

<form name="input" action="check.php" method="get">
            Unit number: 
            <input type="number" name="unit" />
            <input type="submit" value="Submit" />
            </form>

            <table>
            <tr><td class="check-table">
            <?php
            if($_GET[unit] = null) $output="<p>Please Enter A Unit Number</p>";
            echo $output;
            ?>
            </td></tr></table>

Please Help?

Upvotes: 0

Views: 156

Answers (2)

zerkms
zerkms

Reputation: 254886

The better way would be:

if (empty($_GET['unit'])) {
    $output="<p>Please Enter A Unit Number</p>";
    echo $output;
}

The reasons:

  1. You check if variable exists
  2. You use ' quotes for array key name
  3. You output $output variable only if it is necessary. And in your case - you output it even if it doesn't exist
  4. You've also confused == (comparison operator) and = (assignment operator)

Upvotes: 4

Wern Ancheta
Wern Ancheta

Reputation: 23297

I think you missed the single quotes in the $_GET['unit']

<?php
            if($_GET['unit'] = null) $output="<p>Please Enter A Unit Number</p>";
            echo $output;
            ?>

Upvotes: 2

Related Questions