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


if() and else(): Help needed!





Ben
I've recently coded a members system script and have come across a small problem that I need help with. It's to do with if and else statements.

Code:
<?php
if (a cookie is set) {
  get the relevant password for the id in the cookie;
if (the passwords match) {
  the user is logged in;
} else {
  the cookie is set but the user is not logged in;
} } else {
  no cookie is set;
}
?>


That is the general idea of my code.

In the section 'the cookie is set but the user is not logged in', I have about 30 lines of code and in the section 'no cookie is set' I have exactly the same 30 lines of code.

At the moment I have got the 30 lines written out twice, but is there any way of shortening it down to only write it out once?

Many thanks,
Ben
DoctorBeaver
Why not create a function that sets the cookie? That way you have the 30-odd lines in the function so you only need the function call in the if{} else{} conditionals.
yjwong
More information about user-defined functions can be found here: http://my.php.net/functions
kv
A function will do, but out of curiosity, I tried rewriting this code without writing a function or repeating that 30 lines. Here it is.

Code:

<?php
if (a cookie is set && the passwords match)
{
   get the relevant password for the id in the cookie;
   the user is logged in;
}
else
{
   if(the passwords don't match)
   {
      the cookie is set but the user is not logged in;
   }
   else
   {
      no cookie is set;
   }
   
   //common 30 lines here
}
?>


Hope this helps.
BlackSkad
kv wrote:
A function will do, but out of curiosity, I tried rewriting this code without writing a function or repeating that 30 lines. Here it is.


There's a huge logical error in your code. You first check if the passwords match, and then you get the relevant password. It should be the other way round. What you eventually could do is this:
Code:
get relevant password
if (cookie set && passwords match) {
    logged in
} else {
    not logged in
}


But to get the relevant password, you need the cookie. And as the 'get password' part comes before 'check cookie' part, the cookie might not have been set, which would result in an error.

So I guess the only possibility is indeed using a function.

BlackSkad
Ben
Thanks for all your help. I could not use a function as the 30 lines of code has functions already in them.

I have now solved the problem with the following:

Code:
<?php
if (a cookie is set) {
  get the relevant password for the id in the cookie;
if (the passwords match) {
  the user is logged in;
} else {
$notloggedin = 'Yes';
} } else {
$notloggedin = 'Yes';
}

if ($notloggedin == 'Yes') {
//30 lines of code;
}
?>


Thats the only way I could think of
BlackSkad
Ben wrote:
I could not use a function as the 30 lines of code has functions already in them.


Do you mean you declare functions in those 30 lines, or do you use them? Calling those functions in another function, wouldn't be a problem. Declaring a new function inside a function might give problems though.

Code:
function f_a () {echo 'function a';}

function f_b () {
    echo 'function b';
    f_a();
    echo 'function  b again';
}


The code above is usable, it won't result in an error (as long as you don't break anything else, offcourse). The code is usable too, but I would not recommend it.

Code:
<?php
/** calling foo will declare the function bar */
function foo()
{
  function bar()
  {
   echo "I don't exist until foo() is called.\n";
  }
}

/* We can't call bar() yet
   since it doesn't exist. */

foo();

/* Now we can call bar(),
   foo()'s processesing has
   made it accessible. */

bar();


So I suppose, using a function would still be possible in your case. But your solution is good too Smile
Related topics
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

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