...

View Full Version : Cant search Google for PHP OR examples! Argh, please can you help?



b99
06-20-2010, 03:47 PM
Hi Guys,

Great forum by the way!

I am having a little problem and wondered if any of you nice people would be kind enough to help me out?

I am having problems with a php if else statement.

Basically I am trying to tell php to echo something if the 1st letter equals 'a' or 'b'.

for example, this works fine and echos 'test' if the word begins with 'a':



if ($example == NULL & (substr(strip_tags($_GET['ps']),0,1) == 'a'))
{
echo 'test';
} else {
echo '';
}


however the following echos 'test' no matter what the word begins with:



if ($example == NULL & (substr(strip_tags($_GET['ps']),0,1) == 'a' || 'b'))
{
echo 'test';
} else {
echo '';
}


Am i missing something?

I cant search google because it ignores the word 'or'!!!!!


Any help would be much appreciated,

Many thanks

Mike

ps: i have also tried the following with no luck;




if ($example == NULL & (substr(strip_tags($_GET['ps']),0,1) == 'a' || (substr(strip_tags($_GET['ps']),0,1) == 'b'))
{
echo 'test';
} else {
echo '';
}

DJCMBear
06-20-2010, 03:58 PM
Try this


<?php
if ($example == NULL && substr(strip_tags($_GET['ps']),0,1) == 'a' ||
$example == NULL && substr(strip_tags($_GET['ps']),0,1) == 'b')
{
echo 'test';
}
else
{
echo '';
}
?>

Keleth
06-20-2010, 04:04 PM
You made a typo in your last example, with too many parenthesis :-P


if ($example == NULL & (substr(strip_tags($_GET['ps']),0,1) == 'a' || substr(strip_tags($_GET['ps']),0,1) == 'b'))
{
echo 'test';
} else {
echo '';
}

You need to drop the open parenthesis before the second substr and it works fine.

In PHP, at least in any way I have found so far, you can't chain OR statements when doing a comparison. How I get around it is using an array and in_array:



$needle = substr(strip_tags($_GET['ps']),0,1);

if ($example == NULL & in_array($needle, array('a', 'b'))
{
echo 'test';
} else {
echo '';
}

DJCMBear
06-20-2010, 04:10 PM
In PHP, at least in any way I have found so far, you can't chain OR statements when doing a comparison.

You can as the code I wrote checks both 'a' and 'b' so if it doesn't equal 'a' it then checks to see if it equals 'b' if it doesn't equal 'a' or 'b' then it wont pass but if it equals one of them the it will pass.

b99
06-20-2010, 04:10 PM
thanks guys!

Very very very much appreciated, I thought there must be way to make it work and keleth you array and in_array will shorten the code massively, especially when im going to be doing all the letters of the alphabet!

Thumbs up!

Keleth
06-20-2010, 04:15 PM
You can as the code I wrote checks both 'a' and 'b' so if it doesn't equal 'a' it then checks to see if it equals 'b' if it doesn't equal 'a' or 'b' then it wont pass but if it equals one of them the it will pass.

Err... I meant in the way he first used it, trying to do something = a || b.

And anyway, his last example was fine as well, checked if it was a or b, but with less code :-P He just had an extra parenthesis.

I prefer the array method just for coder readability. When I'm checking for the same thing in many different places, I create a page with some constant variables and include it on w/e pages need it, use an in_array test to check for each one.


Very very very much appreciated, I thought there must be way to make it work and keleth you array and in_array will shorten the code massively, especially when im going to be doing all the letters of the alphabet!

Do you plan on just testing the start of the variable for certain characters? Are you going to search for strings? The method above is ok for a few characters, two or three, but after that I'd go with a regex based on what you're doin.

DJCMBear
06-20-2010, 04:17 PM
thanks guys!

Very very very much appreciated, I thought there must be way to make it work and keleth you array and in_array will shorten the code massively, especially when im going to be doing all the letters of the alphabet!

Thumbs up!

To make your life so much easier why dont you use a php function to do all the work.

Like this:


<?php
function checkFirst($str)
{
$str = substr(strip_tags($str),0,1);
$letters = array('a','b','c','e','f','g','h','i','j','k','l','m','n',
'o','p','q','r','s','t','u','v','w','x','y','z');
return in_array($str,$letters);
}

if ($example == NULL & checkFirst($_GET['ps']))
{
echo 'test';
}
else
{
echo '';
}

?>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum