The Church Media Community
Equipping Your Church to Communicate Effectively

Go PRO - Enjoy PRO Benefits!
 
Go Back   The Church Media Community > Website Design > Website Coding Discussions
Forgot Password?
                            Register

Website Coding Discussions PHP, MySQL, Java, Javascript, ASP, etc.

Reply
 
Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old Saturday, November 8th, 2008, 06:07 AM
New Church Media Member

 
 Join Date: Sep 2008 
 Last Online: Thursday, December 11th, 2008 
How to continue PHP source code to another line in editor

Thanks for reading, I hope this could be a help for someone else as well in the future as googling for an hour did not result more than one relative answer. Usually Google rocks, but this one was hard with the search words....

What are the options to deal with a rather long if() statement? I think I found one working solution after littlebit of testing, but as it's part of security script I would not like to rely just solely on my own testing.

Here's the example of long statement:
Code:
$a=1;
$b=2;
$c=3;
$d=4;
if($a==1 && $b==2 && $c==3 && $d==4) {
  print "Pass";
}
else print "Fail";
I found out the if() could be written to multiple lines and still works if I hit enter after every operator (leaving also a white space) like this:
Code:
$a=1;
$b=2;
$c=3;
$d=4;
if($a==1 && 
 $b==2 && 
 $c==3 && 
 $d==4) {
  print "Pass";
}
else print "Fail";
So my question to you is if that's the right way to do it or is there better/more secure ways to do it? I tested with $c="3"; and it will pass which is ok. Also $c=null; $c=""; $c=($a+$b); worked as expected. After some testing I was unable to make it fail, but haven't found it from php manual so I don't trust it 100% until someone could confirm it. I would think if it parses it, it will work correctly... ...right?

I know on other languages I could use an underscore "_" or forward slash "/" or such and I was wondering if PHP comes with any of those?

Also I figured this would be a good time to use an alternative to the if() statement. Maybe the switch/case would work better on this, but please still answer the original question.

Thanks in advance!

Temex
Reply With Quote Start a New Topic From This Comment
  #2 (permalink)  
Old Saturday, November 8th, 2008, 08:30 AM
danroth's Avatar
CMN Derivative
Become a CMN Professional Member!

 
 Join Date: Sep 2003 
 Last Online: Today 
Presuming you just need to do one test on four variables, I don't think there's a better way to do it than you are.

One coding standard note - while the "if" statement works just fine the way you coded it, it's often considered better coding to put a space after the word "if" and before the first parenthesis, in order to distinguish a control statement from a function statement.

"if (a=1)" rather than "if(a=1)"
Reply With Quote Start a New Topic From This Comment
  #3 (permalink)  
Old Saturday, November 8th, 2008, 10:07 AM
greg4god's Avatar
Church Media Regular

 
 Join Date: Jun 2007 
 Last Online: Today 
Argh!!!!! I just wrote a really nice long message about this and some other things for you, and the focus went out of this input area and I hit backspace, which took me to the previous page so I lost it all!

Short answer now, will elaborate more later:

To PHP, both examples you have look the exact same, all whitespace (spaces, tabs, newlines), get treated the exact same outside of quoting.

Also something i elaborated on more in the "lost" message was a note that when you do multiple conditions, PHP will stop evaluating as soon as it knows the outcome. Basic logic rules, FALSE && (and) anything else will always be false, so as soon as it gets a false condition AND'd with anything else, it stop evaluating. Same for OR statements, TRUE || (or) anything else will always be true, so once it gets to that it will stop.

Later I'll explain in an article the uses and benefits of knowing this.

Have a great day!

-Greg
Reply With Quote Start a New Topic From This Comment
  #4 (permalink)  
Old Saturday, November 8th, 2008, 12:05 PM
New Church Media Member

 
 Join Date: Sep 2008 
 Last Online: Thursday, December 11th, 2008 
Hehe..Greg, I'm posting this second time as well, but the funny thing is my original message included the PS for you (see below). So here it comes.. only 2 seconds later


Thanks Danroth and Greg,

Whoops, that was funny you pointed out the space between the function and the condition, I had to go to check my scripts and yes, I do have spaces there so I'm allright

Also, I have no problems with boolean and such, but just wanted to double check how breaking single line in PHP would result on the statement as far as parsing goes. I must use it on script dealing with security and definitely want to avoid any nasty surprices.

In my case more practical could be to use switch/case, but the whole hassle is so long and complicated that I must rewrite it on better time.

Thanks for your both and I hope this helps someone else too Googling to this page. Some answers are just hard to Google due to search words it involves such as PHP, another line, source code, multiline,... try it for yourself if you're boored enough

Temex

PS: Greg: After filling textbox like this one, I 98% of time just hit CTRL+A and then CTRL+C ...takes about 0.2 seconds but could save fortune! (And CTRL+Z in case something wen't wrong already.) Hope that saves your next mess
Reply With Quote Start a New Topic From This Comment
Reply

  The Church Media Community > Website Design > Website Coding Discussions

Tags
another line, enter, multiline, php code, screen

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:



Add to Google


Register Now for FREE!
Our records show you have not yet registered to our community. To sign up for your FREE account INSTANTLY fill out the form below!

Username: Password: Confirm Password: E-Mail: Confirm E-Mail:
Agree to forum rules 


All times are GMT -6. The time now is 10:09 PM.

   
 
© 1995-2008, ChurchMedia™, ChurchMedia LLC

SEO by vBSEO 3.1.0