PHP Notice: Undefined variable: help

I am working on some php code and came across two error messages that I can’t seem to figure out. I am new to php so this may be an easy fix but I am struggling. The two error messages that I am getting are:

Notice : Undefined variable: message in /opt/lampp/htdocs/ex_starts/ch10_ex1/date_tester.php on line 31
and
Notice : Undefined variable: due_date_message in /opt/lampp/htdocs/ex_starts/ch10_ex1/date_tester.php on line 54

They basically are the same error just on different lines. How can I go about fixing this issue.

The code for date_tester.php is

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Date Tester</title>
    <link rel="stylesheet" type="text/css" href="main.css"/>
</head>

<body>
    <div id="content">
        <h1>Date Tester</h1>
        <form action="." method="post">
        <input type="hidden" name="action" value="process_data"/>

        <label>Invoice Date:</label>
        <input type="text" name="invoice_date"
               value="<?php echo htmlspecialchars($invoice_date_s); ?>"/>
        <br />

        <label>Due Date:</label>
        <input type="text" name="due_date"
               value="<?php echo htmlspecialchars($due_date_s); ?>"/>
        <br />

        <label>&nbsp;</label>
        <input type="submit" value="Submit" />
        <br />

        </form>
        <h2>Message:</h2>
        <?php if ($message != ''): ?>
            
            <p><?php echo $message; ?></p>
        <?php else : ?>
        <table cellspacing="5px">
            <tr>
                <td>Invoice date:</td>
                <td><?php echo $invoice_date_f; ?></td>
            </tr>
            <tr>
                <td>Due date:</td>
                <td><?php echo $due_date_f; ?></td>
            </tr>
            <tr>
                <td>Current date:</td>
                <td><?php echo $current_date_f; ?></td>
            </tr>
            <tr>
                <td>Current time:</td>
                <td><?php echo $current_time_f; ?></td>
            </tr>
            <tr>
                <td>Due date message:</td>
                <td><?php echo $due_date_message ?></td>
            </tr>
        </table>
        <?php endif; ?>

    </div>
</body>
</html>

Any help to fix this issue is appreciated

Thanks.

the variables are undefined, so you can just define them. Or you capture their existence properly with isset() or empty().

So how would I do that. Do I add empty() to <?php if ($message != ''): ?> or do I need to do it another way.

Before you echo the variables (or do anything else with them) you need to check that they exist, otherwise you get the error message you are seeing. So a simple if() clause around each time you echo them would do. Or you could check to see if they exist before you echo them, and if they don’t, set them to an empty string, and then your echo will output nothing, but won’t throw an error.

<?php if (isset($due_date_message)): ?>
<tr>
                <td>Due date message:</td>
                <td><?php echo $due_date_message; ?></td>
</tr>
<?php endif; ?>

Also it’s proper form to end with a semicolon- your echo $due_date_message didn’t have one.

Or perhaps

<td><?php echo isset($due_date_message) ? $due_date_message : ""; ?></td>

There’s a shorter way in PHP 7, I think.

Ok thanks. I entered the code but the due date message doesn’t come back with the time before its due.
The code I have in my index.php file is
$time_span = $current_date_o ->diff($due_date_o); if ($due_date_o < $current_date_o){ $due_date_message = $time_span ->format('This Invoice is %y years, %m months, and %d days.');
I can provide the entire index.php code if its needed.

From memory and not tested:

// $due_date_message = isset($due_date_message) ? $due_date_message : ""; 

// Force empty string if not declared
$due_date_message = $due_date_message ?? '';

BEWARE: Tapped on a tablet and not validated.

Edit:

Just checked on the desktop and the syntax was correct :slight_smile:

1 Like

Some kind of clue as to what’s in $current_date_o and $due_date_o would be helpful in diagnosing the issue.

Your code will only show that message if the invoice is overdue, that is, if the due date is before the current date. If you want to show when it will become due, you need an ‘else’ clause and some change to the wording to denote whether it’s past due or not.

empty() will also check if the variable exists, so you can just use:

<?php if (!empty($message)): ?>

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.