Solved

PHP Sytax error on Undefined Variable

Posted on 2013-11-21
8
358 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 58

Expert Comment

by:Gary
ID: 39667782
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
ID: 39667810
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
ID: 39667818
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:chtullu135
ID: 39667873
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
 
LVL 110

Assisted Solution

by:Ray Paseur
Ray Paseur earned 50 total points
ID: 39667890
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
ID: 39667898
It's missing from the sample or just from your code?
0
 

Author Comment

by:chtullu135
ID: 39667910
It was missing from the sample
0
 

Author Closing Comment

by:chtullu135
ID: 39667913
Thanks for the help.  I am also reading the links
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
This article discusses how to implement server side field validation and display customized error messages to the client.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

749 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