Forum Moderators: coopster

Message Too Old, No Replies

if == 'anything else'

         

tonynoriega

3:31 pm on Dec 11, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



i have an if statement that says

if == 'a' {
do this
}

if == 'b' {
do this
}

//how do i say

if == 'any other letter in the alphabet'{
do this
}

gergoe

3:39 pm on Dec 11, 2007 (gmt 0)

10+ Year Member



Use else and elseif:

if ($var == 'a') { 
// $var is a
} elseif ($var == 'b') {
// $var is b
} else {
// $var is anything else
}

But I suggest you to look into the use of switch() [php.net] statement, as that is much more suitable for similar 'problems'.

phranque

3:47 pm on Dec 11, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



depending on what you are trying to do the answer is either this:
if ($foo == "a") {
echo "do a";
} elseif ($foo == "b") {
echo "do b";
} else {
echo "do neither a nor b";
}

or this:

if (($foo == "a") ¦¦ ($foo == "b")) {
echo "do a or b";
} else {
echo "do neither a nor b";
}

or this:

if ($foo == "a") {
echo "do a";
}
if ($foo == "b") {
echo "do b";
}
if (!($foo == "a") &&!($foo == "b")) {
echo "do neither a nor b";
}

PHP_Chimp

4:19 pm on Dec 11, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you need an else clause then switch statements with a default are often quicker. However if you dont have a default/else clause then the if/elseif are quite often quicker.
So if you are looking for speed of code and need a default option then switch is what you want. I found switch about a 1/5 faster than elseif loops when you want a default of else clause. When you remove the default/else and are just looking for if/elseif then it worked the other way around and the if/elseif loop was about a fifth faster.

Over an array with 20000 values the difference with a default was about 0.25 seconds with my set up.

[edited by: PHP_Chimp at 4:25 pm (utc) on Dec. 11, 2007]

Noddegamra

11:02 am on Dec 12, 2007 (gmt 0)

10+ Year Member



Id also use case.

Here is an example though to help your understanding:


<?php

// $var = the value/letter chosen (example: a,b,c,d,e..)

switch ($var):
case a:
// call function / do something here
break;
case b:
// call alternative function / do something else here
break;
default:
// default action / call default function action
endswitch;

?>

I hope this helps you :)

PHP_Chimp

9:00 pm on Dec 12, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Although of course the example above without the typo is -

switch ($var):
case [b]'[/b]a[b]'[/b]:
// call function / do something here
break;
case [b]'[/b]b[b]'[/b]:
// call alternative function / do something else here
break;
default:
// default action / call default function action

As a and 'a' are not the same thing. Numeric cases dont need the 's but strings need there ' or " around them.
Just have a look at the switch [uk2.php.net] part of the manual.

I love switch statements, they just look more ordered than if/elseif/else, but if speed is an issue then use the fastest version as they give the same results.

Demaestro

9:52 pm on Dec 12, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The problem with all the suggestions is they assume that the input is a single letter.

what if it is a number or more the one letter?

If this isn't possible because of form validation or just doesn't matter then don't worry about it.

But the post asked how to see if it is any other single letter...

which would require somthing like (sorry for bad php but I don't know it well)

var_is_good = True

if ($var.length() > 1){
// $var is not a single letter
// var_is_good = False
}

if ($var.isAlpha() == False){
// $var is not a letter at all.
// var_is_good = False
}

if (var_is_good!= False){
if ($var == 'a') {
// $var is a
} elseif ($var == 'b') {
// $var is b
} else {
// Now $var is any other SINGLE letter.
}
}

That will make sure the input is a single letter before determining what letter it is.

[edited by: Demaestro at 9:53 pm (utc) on Dec. 12, 2007]

phranque

1:51 am on Dec 13, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



But the post asked how to see if it is any other single letter...

nice catch!
the winner is the one who reads the whole question...