Solved

PHP Sytax error on Undefined Variable

Posted on 2013-11-21
8
350 Views
Last Modified: 2013-11-21
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
Comment
Question by:chtullu135
  • 4
  • 3
8 Comments
 
LVL 58

Expert Comment

by:Gary
Comment Utility
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
 

Author Comment

by:chtullu135
Comment Utility
I went ahead and checked and it looks like I am closing it on line 25
0
 
LVL 58

Accepted Solution

by:
Gary earned 450 total points
Comment Utility
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
 

Author Comment

by:chtullu135
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 50 total points
Comment Utility
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
 
LVL 58

Expert Comment

by:Gary
Comment Utility
It's missing from the sample or just from your code?
0
 

Author Comment

by:chtullu135
Comment Utility
It was missing from the sample
0
 

Author Closing Comment

by:chtullu135
Comment Utility
Thanks for the help.  I am also reading the links
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now