Forum Moderators: coopster

Message Too Old, No Replies

Script PHP

A shortest way to write a lot of " if "

         

Maleville

9:21 pm on Jul 15, 2003 (gmt 0)

10+ Year Member



Good afternoon, morning or night, depends on which part of the world you are!

I have a script which is like this:

<?
$result=1;
if (preg_match("'DigExt'",$HTTP_USER_AGENT))
{
$result=0;
}
if (preg_match("'smi'",$SCRIPT_URI))
{
$result=0;
}
if (preg_match("'cltreq.asp'",$SCRIPT_URI))
{
$result=0;
}
//
//and so on............ with a lot of "if"
//

if ($result==1){
//
//active script:send a PHP mail
//
}
?>
// if "no", write only HTML page
<html>
<head>
<title>erreur404</title>
</head>
<body>
</body>
</html>

Is there a shortest and easy way to write the "if" sequence?

Thank's for your cooperation.

vincevincevince

9:28 pm on Jul 15, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




switch ($result)
{
case "1" :
{
//do stuff
break;
}
case "0" :
{
//do stuff
break;
}
default :
{
//do stuff
break;
}
}

that suit you?

Shadi

9:59 pm on Jul 15, 2003 (gmt 0)

10+ Year Member



The Famous Switch is a grate alternative to the if statment, and also processed faster :) Good answer vincevincevince.

Maleville

10:35 pm on Jul 15, 2003 (gmt 0)

10+ Year Member



Thank you for your quick answer.

I don't know a lot about PHP but the way I would want the script works is:

case "1" : mail() to webmaster to tell him content (URL, USER_AGENT...) of error404 AND write a HTML 404 page to surfer for information and valid links.

case "0" : no mail() to webmaster BUT the same HTML page to the surfer.

In fact there is no "default" case.

How do you write the script in this case?

redshift

10:42 pm on Jul 15, 2003 (gmt 0)

10+ Year Member



Just

default: {}

instead of

default: {
// do stuff
break;
}

That means nothing is done per default. But you should be sure to catch every possible case with "case x:".
And if so, default{} will never be used.
So there's no reason for default{} at all and the final answer is just to write your cases and no default{}.

Shadi

10:52 pm on Jul 15, 2003 (gmt 0)

10+ Year Member



i woul recommend that you always write a test case just for user friendlyness and *just incase* even if it takes you back to the place where the case is chosen, just a means of error catching and you have nothing to lose with it, the script will still function at exactly the same speed. (cus if a case is used it will stop there and break) so it will only use it if it needs to.

jatar_k

11:07 pm on Jul 15, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



keep in mind switch is testing alternate values for the same variable and Maleville is using preg_match against various vars from the look of it.

Each preg_match will still have to be done in the if statements, which is what Maleville is trying to correct. The switch only replaces the simple if ... else at the end.

A thought that comes to mind is using arrays and loops to cut down on code. You could put all of your patterns in an array and then loop through them.


$patternarr[0] = array('HTTP_USER_AGENT','DigExt');
$patternarr[1] = array('SCRIPT_URI','smi','cltreq.asp');
$counter1 = 0;
while (is_array($patternarr[$counter1])) {
$counter2 = 1;
while (isset($patternarr[$counter1][$counter2])) {
if (preg_match($patternarr[$counter1][$counter2],${$patternarr[$counter1][0]})) {
$result=0;
}
$counter2++;
}
$counter1++;
}

You could add any other patterns or env vars into the arrays.

Seeing you responded while I was cooking this it seems you only wanted to get rid of the little if .. else. I haven't tested the above so it may need a little tweak but in theory it should work.

vincevincevince

11:27 pm on Jul 15, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



and one final thing....

the { } braces are only needed if you have more than one statement...

so eg:


if (preg_match("'DigExt'",$HTTP_USER_AGENT))
{
$result=0;
}

can be written just as well (better) as:


if (preg_match("'DigExt'",$HTTP_USER_AGENT)) $result=0;

and SECONDLY...

if you are just looking for whether a substring exists within a longer string - don't use preg_match... it's much much faster to use strpos(), which works much the same way, [but doesn't use regular expressions (which you don't need)]

MTKilpatrick

11:38 pm on Jul 15, 2003 (gmt 0)

10+ Year Member



It seems to me that all you wanted to do was set a flag to zero if a whole load of conditions were set. Since preg_match returns 1 or 0, you can just do a logical AND for each successive item. You avoid all those if statements and brackets, and you have some code that is very easy to read.
You don't even need to set it to 1 before you start:

$result = preg_match($blah1, $blah2);
$result &= preg_match($blah3, $blah4);
$result &= preg_match($blah5, $blah6);
$result &= preg_match($blah7, $blah8);
$result &= preg_match($blah9, $blah10);
$result &= preg_match($blah11, $blah12);
$result &= preg_match($blah13, $blah14);

Or just stick them all in one long, rather horrible line:

$result = preg_match($blah1, $blah2) && preg_match($blah3, $blah4) && preg_match($blah5, $blah6) && preg_match($blah7, $blah8) && .....etc.....

toodle-pip,

Michael

Maleville

4:31 pm on Jul 17, 2003 (gmt 0)

10+ Year Member



Thank you for and to all.