Php: about echoing a null variable

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:

这本书第一次说这些文本框应该是空的

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!


You should check if they are defined.

ie

<?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


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
}

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.

链接地址: http://www.djcxy.com/p/96558.html

上一篇: 声明类变量的问题

下一篇: Php:关于回显一个null变量