FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


OR for empty posts?





alalex
I tried to do a OR thing in php so if any of the form fields was empty, return an error. But I don't know how to use itl... Here what I wrote:
Code:
...elseif(empty($_POST['name']) || (empty($_POST['error'])) || (empty($_POST['email'])){...


(It is just a piece)
And it says that there is a problem with $BOOLEAN_OR something...

Help!!
kv
Code:

...elseif(empty($_POST['name']) or (empty($_POST['error'])) or (empty($_POST['email'])){...


Very Happy
Rhysige
I believe 'or' is less correct than ||
Code:
...elseif(empty($_POST['name']) || empty($_POST['error']) || empty($_POST['email'])){...

The idea is if(all conditions go here){ etc} you do not need to divide the conditions into seperate brackets.
kv
I checked php manual. We can use both "or" and "||". I don't know how one of them will become "less" correct when both of them achieve the same thing (because || is used in more languages than "or"?).

BTW, looks like the error is because of incorrect usage of paranthesis.

Correct code:
Code:

...elseif(empty($_POST['name']) || empty($_POST['error']) || empty($_POST['email']) ){...
Stubru Freak
You could use parentheses like he does (altough it isn't useful), but he has forgotten to close one at the end

Code:
...elseif(empty($_POST['name']) || (empty($_POST['error'])) || (empty($_POST['email']))){...


Will work. That's three: one for the empty(), one for the one you opened for fun, and one to end the elseif()

And there is a difference between || and or.

Try this code:
Code:
echo true || true ? 'You used ||' : 'FALSE';

And this one which just has or instead of ||:
Code:
echo true or true ? 'You used ||' : 'FALSE';


First one will echo 'You used ||', second one will echo 1

If you want to know why check this page:
http://www.php.net/manual/en/language.operators.php#language.operators.precedence

But in general the difference is minimal, certainly if you are properly using parentheses.
alalex
OK, thanks to all of you, I will try what you said!
Laughing
Rhysige
Stubru.. WTF! Your code is more incorrect then what the guy originally posted...
Stubru Freak
Rhysige wrote:
Stubru.. WTF! Your code is more incorrect then what the guy originally posted...


I just tried and my code works as expected. It will activate the code in the elseif block if one of the array indices is empty.

I agree that it's a strange way to write it, as the parentheses are there without reason. But if you want to write it that way his code will give an error and my code will not. So I would say my code is more correct.

I agree however, that extra parentheses don't make any sense, and the page may take a little longer to load (but not that much, not even a millisecond I think).

I just showed my code to prove that the problem was not that he used the extra parentheses, but that he forgot to close one. So when he has good reasons to do it, he can do it.
BlackSkad
Rhysige wrote:
Stubru.. WTF! Your code is more incorrect then what the guy originally posted...


Would please stop doing this? You've done it once on one of my posts too, while the code in the post is completely valid and will work! It's not because it's not exactly the way you would write it, that it is incorrect.
One of the only rules when using parenthese, is that you need to close them correctly! The code below will have the same result as the one of StuBruFreak and yours, although there are more parentheses. Those reduce the readability of the code, but that's just a mather of taste!
Code:
...elseif(((empty($_POST['name'])) || (empty($_POST['error']))) || (empty($_POST['email']))){...

And StuBruFreak seems to have done more research on the topic than you did. In this case (as elseif-condition) both or and || will have the same result, although most people use ||.

BlackSkad

EDIT: seems like StuBruFreak was a little faster.
Rhysige
It is still less correct so no I will not stop doing it.
The way hes done it is for GROUPING the or's. For example
Code:

if($pie == true || ($tim == true && $scott == true)){ Perform action }

Using the grouping on single parameters is not proper coding.
BlackSkad
It doesn't matter in this case. Grouping 'or'-s doesn't change the behaviour of the complete condition, and nor does grouping single values. It is indeed not proper coding, and might slow down the parsing-process, but some people don't really care. Why worry about the beauty of your script, when it doesn't even work?

In the end, the condition of StuBruFreak IS correct. There's no point saying that the solution of StuBruFreak is even worse than the original code. StuBru Freak's solution worked while the starting-code didn't. That was my main objection. You insult people for something that works, by saying it doesn't. And saying that something can be done better, certainly doesn't need curses (like WTF!). Just stop saying WTF, and say it in a normal (and polite) way.
Rhysige
Would you prefer a spelt that word out for you so that you can comprehend it properly?
The point should also be made 2 sets of code that worked had already been posted. The code he posted WAS incorrect unfortunatly this is the way I have been taught, the guys that taught me PHP where so strict with coding standards that I have adopted the same stance. Things may WORK but it doesnt make the CORRECT. Optimisation plays a huge role in web coding, sure you will get away with it if thats all he does on the page then fine but its best to get used to completly correct code to prevent slow load times when the pages get more complex.
I am involved in the development of a large PHP based game and with the traffic we get and the complexity of the code you must optimise everything and 0.01 seconds extra loading time that can be taken off SHOULD be taken off.
Stubru Freak
Rhysige wrote:
Would you prefer a spelt that word out for you so that you can comprehend it properly?
The point should also be made 2 sets of code that worked had already been posted. The code he posted WAS incorrect unfortunatly this is the way I have been taught, the guys that taught me PHP where so strict with coding standards that I have adopted the same stance. Things may WORK but it doesnt make the CORRECT. Optimisation plays a huge role in web coding, sure you will get away with it if thats all he does on the page then fine but its best to get used to completly correct code to prevent slow load times when the pages get more complex.
I am involved in the development of a large PHP based game and with the traffic we get and the complexity of the code you must optimise everything and 0.01 seconds extra loading time that can be taken off SHOULD be taken off.


Quote:
You could use parentheses like he does (altough it isn't useful), but he has forgotten to close one at the end


This is what I said about the code I posted. As you can see I said it isn't useful. I never said my code was 100% correct.

The reason I said it, was that your comments may give him the impression you can't use parentheses in an if.

I wrote that reaction to show that that isn't the error, but that the error is just that he forgot to close one. So he will not be scared to use parentheses when he needs them.
alalex
Code:
 }elseif(empty($_POST['name']) || (empty($_POST['error'])) || (empty($_POST['email'])){


OK, I tried this, and now the error is
Quote:
Parse error: syntax error, unexpected '{' in /home/alalex/domains/webot.frih.net/public_html/CER_test/catalog.php on line 596

(http://webot.frih.net/CER_test/catalog.php)

I don't understand! Question
alalex
OK, it is working!!
I made this line for code:
Code:
}elseif((empty($_POST['name']) || (empty($_POST['error'])) || (empty($_POST['email'])))){


Guys, thanks a lot!!! Very Happy
You can see the result in here: http://webot.frih.net/CER_test/catalog.php it was the error check form.

Thanks!!
mathiaus
Glad this is solved. Closing to avoid a war breaking out Shame on you

-close-
Related topics
Guest posts
Exceeded the alloted amount of posts ???
question abt posts/mth
5 posts only..?
Delete all over 3 week old posts
Suggestion: Posts per page
useless posts
how much posts to have forever-account?
MP3-Host
Software Forums - based on phpBB
Deleting Blog Posts
Add number of posts on the forums index page
Posts suddenly get empty ...
Custom Operating System in C#, C++, or Java
This topic is locked: you cannot edit posts or make replies.    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.