Solved

PHP Sytax error on Undefined Variable

Posted on 2013-11-21
8
362 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
Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

 

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

WordPress Tutorial 1: Installation & Setup

WordPress is a very popular option for running your web site and can be used to get your content online quickly for the world to see. This guide will walk you through installing the WordPress server software and the initial setup process.

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to dynamically set the form action using jQuery.
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…

623 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