Problems with error messaging

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Icethrill
Forum Newbie
Posts: 13
Joined: Fri Jan 02, 2009 9:55 am

Problems with error messaging

Post by Icethrill »

So I am learning php at the moment. I am following a tutorial. I am programming a simple CMS system for a website. I have a form so you can edit some rows in the database. Like changing the name, position and if its visible or not. Atm I am fairly gotten into the error messaging. And I want to recieve an error message if I leave the menu_name field empty. But it changes the name to nothing and the form succeeds.

Here is the code thats giving me problems:

Code: Select all

if(!isset($_POST[$fieldname]) || (empty($_POST[$fieldname]) && $_POST[$fieldname] != 0)){
            $errors[] = $fieldname;
        }
(empty($_POST[$fieldname]) && $_POST[$fieldname] != 0) It works fine if I delete the $_POST[$fieldname] != 0 but I really need that condition with the &&. I am 100% sure I have written the code correctly since I trippled checked with the tutorial. I just dont understand whats the problem. Anyone having an idea? Have I missed something in the condition?
User avatar
kaszu
Forum Regular
Posts: 749
Joined: Wed Jul 19, 2006 7:29 am

Re: Problems with error messaging

Post by kaszu »

Checking any value against 0 with != is useless since it will evaluate to true only if value will be 0, false, empty or null, which in this case will be duplication of what empty() function does (almost). So basically

Code: Select all

empty($_POST[$fieldname]) && $_POST[$fieldname] != 0
is almost the same as

Code: Select all

empty($_POST[$fieldname]) && !empty($_POST[$fieldname])
So it never evaluates to true and is useless, so only part of condition which ma yevaluate to true is isset(...)

$_POST value is always a string or null (not defined), so maybe instead of '!= 0' you mean '!= "0"' ?
In that case condition will make sense. If it's not the case please explain why != 0 is needed.
Icethrill
Forum Newbie
Posts: 13
Joined: Fri Jan 02, 2009 9:55 am

Re: Problems with error messaging

Post by Icethrill »

kaszu wrote:Checking any value against 0 with != is useless since it will evaluate to true only if value will be 0, false, empty or null, which in this case will be duplication of what empty() function does (almost). So basically

Code: Select all

empty($_POST[$fieldname]) && $_POST[$fieldname] != 0
is almost the same as

Code: Select all

empty($_POST[$fieldname]) && !empty($_POST[$fieldname])
So it never evaluates to true and is useless, so only part of condition which ma yevaluate to true is isset(...)

$_POST value is always a string or null (not defined), so maybe instead of '!= 0' you mean '!= "0"' ?
In that case condition will make sense. If it's not the case please explain why != 0 is needed.
Thank you! It worked when I used "0". :) Weird that the other thing worked for the dude that made the video tutorial.
Post Reply