0

I'm currently learning php using Murach (the book, published in 2010). One of the exercises has these statements:

<label>Investment Amount:</label> <input type="text" name="investment"
value="<?php echo $investment; ?>"/><br />
<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 />

The whole gist is that the value attributes above with the echo statements have variables which have not been assigned any value at first, so the textbox is supposed to be empty according to the book. But later on the exercise this same page will be called again and the variables will now have values, thus they will be printed on the textbox. Here's the book's explanation:

These echo statements display the variables that contain the data that the user has previously entered into the form. The first time this page is displayed, though, these variables won’t contain data so the text boxes will be empty.

However, upon running the program for the first time, the textboxes are in fact not empty:

These textboxes are supposed to be empty the first time around says the book

Since this book was published 5 years ago I'm guessing either they worked then but do not now, or the code itself is wrong. Can anyone tell me if this is just obsolete code or is it really broken to begin with? And how can I fix it to get the desired effect of an empty textbox using a null variable?

Thanks!

codehowser
  • 41
  • 1
  • 1
  • 5
  • you could set the error_reporting so that this NOTICE isn't displayed: [php docs](http://php.net/manual/en/function.error-reporting.php) like: error_reporting(E_ALL & ~E_NOTICE); – Jacob A. Feb 10 '15 at 08:57
  • 2
    @Igoel that's an awful solution ,you should always solve the errors,not ignoring them – Gal Sisso Feb 10 '15 at 08:59
  • "awful" is a little harsh to say.....not the best ok, but I think when you are learning something new its better to achieve something ;) – Jacob A. Feb 10 '15 at 09:03

3 Answers3

7

You should check if they are defined.

i.e.

<?php echo (isset($years)) ? $years : ''; ?>

Also, if you turn off display_errors in your php.ini this won't happen, however this would be an ill-advised solution

swifty
  • 1,127
  • 1
  • 10
  • 23
2

The most important way of programming is programming in such a way, that someone else can also understand it. So in that case, if you plan to use this variable, declare and comment it before:

$investment = ''; // Future post investement variable, on startup empty
$interest_rate = ''; // Future interest_rate variable, on startup empty
$years = ''; // Future years variable, on startup empty

In that case everyone is sure what each variable is, and what it will contain. And no undefined error will occur.


Also notice that turning off warning display, as mentioned in comments and answers, isnt a good idea. When you write nice code, no warnings should be displayed. And turning them off is of course not a solution.

The only MUST do is to turn off warnings and errors on production, to not give hackers any possible clue, even if something goes wrong. And save them in some sort of error logger.


If you plan to use this variable with post, I suggest doing something like that:

$investement = (isset($_POST['investment'])) ? safety($_POST['investment']) : '';

Where safety is your own safety check function (remove special characters, and prevent mysql injection if you plan to echo / store data). It is the same as writing:

if (isset($_POST['investment'])) {
    $investement = safety($_POST['investment']);
}
else {
    $investement = ''; // init value
}
Jacek Kowalewski
  • 2,761
  • 2
  • 23
  • 36
  • Hi Jacek, these variables are actually assigned values in another php page which will then call this page using the include function. The book was trying to say that if you echo a variable which has no value assigned to it, the textbox is supposed to turn up empty. They even had pictures that showed the textboxes empty. I'm only curious if they made a mistake or it really was like this in the past. The version used was 5.3 – codehowser Feb 10 '15 at 09:11
  • Thx for comment. include works like a copy paste in notepad. So if those variables are really declared before that in included file, you shouldn't get any error or warning. Maybe the include doesn't work? Since 5.3 nothing changed, so it should work as described in your book :). – Jacek Kowalewski Feb 10 '15 at 09:13
  • 1
    I think you may have forgot the semicolon at the end? $investment = ''; – drooh Feb 17 '16 at 20:38
0

A Notice is what it is, a notice, it doesn't prevent your code from executing as such. Of course if the variable / code generating the notice is used elsewhere you can run into trouble.

So, the book is somewhat right, but I would say that it's bad practice since that you should always aim to have 0 warnings / notices, which means that you should do as @Swifty mentioned and check whether the variable is set.

Epodax
  • 1,828
  • 4
  • 27
  • 32