• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 378
  • Last Modified:

PHP Sytax error on Undefined Variable

Hello,

I am a rank beginner in PHP.  I've just started to teach myself PHP after many years in vba and some vb.net.  I am getting an undefined variable error when I first run the program.  From what I've read this is because the variables are not set when the application is started.  I'm trying to use the ISSET function to check the value of  variable.  If they are set, I would then like to execute the code. It looks like I'm still getting an error on the </html> tag.  It's some sort of syntax error but I'm not finding it.

<!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>Future Value Calculator</title>
    <link rel="stylesheet" type="text/css" href="main.css"/>
</head>

<body>
    <div id="content">
    <h1>Future Value Calculator</h1>
    <?php if (!empty($error_message)) { ?>
        <p class="error"><?php echo $error_message; ?></p>
   
    <form action="display_results.php" method="post">

        <div id="data">
            <label>Investment Amount:</label>
            <input type="text" name="investment"

//this is where I put the isset function ----------------------------

            <?php if(isset($investment)) { ?>
                   value="<?php echo $investment; ?>"/><br />
            <?php } ?>


            <label>Yearly Interest Rate:</label>
            <input type="text" name="interest_rate"
                   value="<?php echo $interest_rate; ?>"/><br />

            <label>Number of Years:</label>
            <input type="text" name="years"
                   value="<?php echo $years; ?>"/><br />
        </div>

        <div id="buttons">
            <label>&nbsp;</label>
            <input type="submit" value="Calculate"/><br />
        </div>

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

Open in new window

0
chtullu135
Asked:
chtullu135
  • 4
  • 3
2 Solutions
 
GaryCommented:
You have an opening if block
<?php if (!empty($error_message)) { ?>

But you never close it

<?php }?>

p.s.
If you are just starting out then stick with the HTML5 doctype - no point using old doctypes
<!DOCTYPE html>
0
 
chtullu135Author Commented:
I went ahead and checked and it looks like I am closing it on line 25
0
 
GaryCommented:
No, line 25 is closing the block you open on line 23
On line 12 you open an if block with no closure - I'm not sure where you want it to be closing but at a guess I would say just after line 42
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
chtullu135Author Commented:
Oh I see.  I'm working from a sample file in the book I'm using.  I've rechecked the original sample file and that closing php tag is missing.
0
 
Ray PaseurCommented:
Some good resources for getting started in PHP are in this article.  Not only will this point you in the right direction, but if you read and follow the guidance carefully it will help you avoid the many simply terrible examples of PHP code that litter the internet!
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11769-And-by-the-way-I-am-new-to-PHP.html

And as a complement, here is a partial list of what to avoid.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_12293-AntiPHPatterns-and-AntiPHPractices.html

As a general rule, the intermix of PHP and HTML is just a bad idea and therefore to be minimized.  The more times you jump in and out of the language, the more tangled and unmaintainable your code becomes (witness not being able to follow the control structures).  I find that just getting the logic right -- inputs yield correct outputs -- is the best first step.

See: http://www.laprbass.com/RAY_temp_chtullu135.php

<?php // RAY_temp_chtullu135.php
error_reporting(E_ALL);

// http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28301011.html
// FUTURE VALUE OF A ONE-TIME INVESTMENT, ANNUAL COMPOUNDING

// IF THERE IS A POST-REQUEST
if (!empty($_GET))
{
    // ASSIGN VALUES OR DEFAULT VALUES
    $orig = !empty($_GET['prin']) ? $_GET['prin']         : 0.0;
    $rate = !empty($_GET['rate']) ? $_GET['rate'] / 100.0 : 0.0;
    $term = !empty($_GET['term']) ? (int)$_GET['term']    : 0;

    // IF THE DATA IS PRESENT AND USABLE
    if ( ($orig * $rate * $term) > 0.00001)
    {
        // SAVE THE STARTING PRINCIPAL
        $prin = $orig;

        // A RANGE OF YEARS
        $years  = range(1, $term);
        $out[0] = $prin;

        // COMPUTE THE VALUE FOR EACH SUCCEEDING YEAR
        foreach ($years as $year)
        {
            $out[$year] = $prin + $prin * $rate;
            $prin  = $out[$year];
        }

        // THE LAST ELEMENT ON THE ARRAY IS THE FUTURE VALUE
        $fval = array_pop($out);

        // MAKE PRINTABLE VALUES
        $or_p = number_format($orig, 2);
        $rt_p = number_format($_GET['rate']) . '%';
        $fv_p = number_format($fval, 2);

        // REPORT THE OUTCOME OF THE CALCULATIONS
        echo PHP_EOL
        . "PRINCIPAL: $or_p"
        . PHP_EOL
        . "RATE: $rt_p"
        . PHP_EOL
        . "TERM: $term"
        . PHP_EOL
        . "FUTURE VALUE: $fv_p"
        ;
    }
    else
    {
        echo PHP_EOL . "VALUES ARE TOO SMALL OR MISSING";
    }
}

// CREATE THE HTML FORM USING HEREDOC NOTATION
$form = <<<ENDFORM
<form>
COMPUTE FUTURE VALUE WITH ANNUAL COMPOUNDING
<br>PRINCIPAL: <input name="prin" placeholder="Initial Principal" />
<br>%INTEREST: <input name="rate" placeholder="Percent Annual Interest" />
<br>YEARSTERM: <input name="term" placeholder="Years" />
<br><input type="submit" value="Compute!" />
</form>
ENDFORM;

echo $form;

Open in new window

Best of luck with your PHP adventures, ~Ray
0
 
GaryCommented:
It's missing from the sample or just from your code?
0
 
chtullu135Author Commented:
It was missing from the sample
0
 
chtullu135Author Commented:
Thanks for the help.  I am also reading the links
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now