Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 30th, 2006, 3:23 PM   #1
piercy
Programmer
 
Join Date: Apr 2006
Posts: 35
Rep Power: 0 piercy is on a distinguished road
Angry if statement example please

i feel like a right idiot keep having to ask you guys :mad: but im having trouble with my if statments. i have searched google and seen examples but mine look fairly similar but never work. i need it to include 2 or more functions.
something like:
 if ($a || $b || $c === 0) {
echo ("please fill in all fields");
print ("please fill in all fields");
} else {
echo ("please fill in all fields");
print ("please fill in all fields");
}
i know print and echo do the same thing but is there anything wrong with the above code? in my real code it just keeps executing the first part and not doing the second (nothing after the else).

please help with an example similar to mine.

thanks :banana:
__________________
this forum rules you guys are great!
thanks to all who help
piercy
piercy is offline   Reply With Quote
Old Apr 30th, 2006, 3:48 PM   #2
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Posts: 1,819
Rep Power: 5 Sane will become famous soon enough
Two things...

I'm not exactly sure what ...if ($a || $b || $c === 0)... is supposed to do, but I believe that it should be ...if ($a == 0 || $b == 0 || $c == 0) ... to check if all of them equal 0.

You also have to make sure that $a, $b and $c all equal something first.

Here's a working example of what you have:

[php]<?php

$a = 1;
$b = 1;
$c = 1;

if ($a == 0 || $b == 0 || $c == 0)
{
echo ("please fill in all fields");
print ("please fill in all fields");
}
else
{
echo ("all fields were filled in");
print ("all fields were filled in");
}

?>[/php]

And here's an article on the differences between "echo" and "print": http://www.faqts.com/knowledge_base/...l/aid/1/fid/40

I don't see why you need to use both here. *confused*
Sane is offline   Reply With Quote
Old Apr 30th, 2006, 4:01 PM   #3
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
You don't need to use both 'print' and 'echo'. Furthermore, since both branches had the same effect, how would you expect to know (aside from using a debugger) which branch was taken? Your expression of the logic is technically correct, presuming you know what you wanted. The expression, ($a), is the equivalent of ($a == true) (or, non-zero). It's almost certain that it's incorrect, though, as Jayme says, given the way you've written the expression. Each piece of the expression must be independent -- they can't share the same expression, such as "=== 0". That's a mental shortcut for you only because you know what you're thinking. The processor hasn't a clue, so it's a brick wall. I don't wish to denigrate what you are doing, but it's obvious you need to revisit some very basic material on the language.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Apr 30th, 2006, 4:21 PM   #4
piercy
Programmer
 
Join Date: Apr 2006
Posts: 35
Rep Power: 0 piercy is on a distinguished road
the above code was an example not my real code. basically i didnt want you guys just giving me a code i wanted an example which i could extend myself. the statement i wanted really (in word terms) was:

if firstname or lastname or emailadress or message was empty
then show form again and say please fill in all fields
otherwise
send the form

thanks to sanes post about each variable needing the question (as it be). i came up with a if statment like what i just put. i understand that echo and print do the same thing i just didnt realise that each part of an if statement needed a question. as i am still learning php and have gone through if staments it seems the book i am reading to help me learn was not very clear on this matter. all of the examples i saw where only designed for one variable.

but thankyou to you both as now i have sorted the problem.
all i cna hope now is that i can complete the rest of the project without help and maybe learn php to the standard you guys are at.

just to show you what i came up wiht heres a small example.
if ( empty($a) || empty($b) || empty($c)) {
require("form.php");
echo 'please fill in all fields.'
} else { 
mail(example@example.com);
INSERT INTO test.messages;
}

as my other computer has not got internet at the moment i cannot copy and paste the code onto here but that is the main of it but obviously slightly shorthanded.

thanks again
piercy
__________________
this forum rules you guys are great!
thanks to all who help
piercy
piercy is offline   Reply With Quote
Old Apr 30th, 2006, 4:28 PM   #5
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Posts: 1,819
Rep Power: 5 Sane will become famous soon enough
Looking good. And good luck to you and your project.
Sane is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 8:17 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC