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


"if" statements





ChrisCh
Hey, I'm wondering how do to the following IF statement in PHP!

Code:
if($variable (begins with) Mozilla/5.0){
      print 'Hi!';
}

if($variable (contains) Mozilla/4.0){
      print 'Hello!';
}

else{
      print 'Boo!';
}

(examples only)


As you can see, I am wondering what the PHP equivalent for (begins with) and (contains) is. If it helps, the $variable is the user's client information (eg. Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; Q312461; .NET CLR 1.1.4322) because I want to show 1 thing to Mozilla/5.0 browsers such as Firefox 1.5.0.4 and another for Mozilla/4.0 browsers such as Internet Explorer 6. (I have worked out how to assign client information a variable, so i only need help with the if statements!) Smile

Anyone that knows how to do this, or has a better way of going about it, please reply! Smile
hexkid
Use regular expressions, namely with the preg_match() function:

Code:
<?php
$variable = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; Q312461; .NET CLR 1.1.4322';
if (preg_match('/^Mozilla\/5\.0/', $variable)) {
  echo 'Hi!';
}
if (preg_match('/Mozilla\/4\.0/', $variable)) {
  echo 'Hello!';
} else {
  echo 'Boo!';
}
?>
hexkid
strpos() might be a better option for this particular need of yours.

Code:
<?php
if (strpos($variable, 'Mozilla/5.0') === 0) {
  echo 'Hi!';
}
if (strpos($variable, 'Mozilla/4.0') !== false) {
  echo 'Hello!';
} else {
  echo 'Boo!';
}
?>
ChrisCh
Ok Smile So I understand exactly what it does, not just the code, would someone tell me if I'm right about this:

Code:
<?php
if (strpos($variable, 'Mozilla/5.0') === 0) {
  echo 'Hi!';
}
if (strpos($variable, 'Mozilla/4.0') !== false) {
  echo 'Hello!';
} else {
  echo 'Boo!';
}
?>


So the 2nd line states that if Mozilla/5.0's string position is 0 (beginning of the string) it will print Hi! to the screen?

I don't understand the next part (the
Code:
if (strpos($variable, 'Mozilla/4.0') !== false)
part... Is that just a different way of writing the code when compared to what you wrote for the Mozilla/5.0 part?

And I understand the else section obviously Smile

Thanks for your help! Smile
hexkid
ChrisCh wrote:
I don't understand the next part (the
Code:
if (strpos($variable, 'Mozilla/4.0') !== false)
part... Is that just a different way of writing the code when compared to what you wrote for the Mozilla/5.0 part?


It's because of what strpos() can return.
If the string is found within the variable, strpos() returns the position of the string; otherwise it returns false.
But, in PHP, false == 0 (zero), so we need to differentiate between them
Code:
<?php
$p1 = strpos('this is a test for php', 'test'); // $p1 = 10
$p2 = strpos('this is a test for php', 'this'); // $p2 = 0
$p3 = strpos('this is a test for php', 'that'); // $p3 = false

echo 'simple condition:<br>';
if ($p1) echo "\$p1 found<br>\n";
if ($p2) echo "\$p2 found<br>\n";
if ($p3) echo "\$p3 found<br>\n";

echo '<br>strpos() >= 0:<br>';
if ($p1 >= 0) echo "\$p1 found<br>\n";
if ($p2 >= 0) echo "\$p2 found<br>\n";
if ($p3 >= 0) echo "\$p3 found<br>\n";

echo '<br>strpos() == 0:<br>';
if ($p1 == 0) echo "\$p1 found at start<br>\n";
if ($p2 == 0) echo "\$p2 found at start<br>\n";
if ($p3 == 0) echo "\$p3 found at start<br>\n";

echo '<br>strpos() === 0:<br>';
if ($p1 === 0) echo "\$p1 found at start<br>\n";
if ($p2 === 0) echo "\$p2 found at start<br>\n";
if ($p3 === 0) echo "\$p3 found at start<br>\n";

echo '<br>!strpos():<br>';
if (!$p1) echo "\$p1 not found<br>\n";
if (!$p2) echo "\$p2 not found<br>\n";
if (!$p3) echo "\$p3 not found<br>\n";

echo '<br>strpos() !== false:<br>';
if ($p1 !== false) echo "\$p1 found<br>\n";
if ($p2 !== false) echo "\$p2 found<br>\n";
if ($p3 !== false) echo "\$p3 found<br>\n";
?>
ChrisCh
Ohhhhh ok Smile I get it now! Thanks Smile

Well I have just a couple more things to ask - being new to using PHP Very Happy

1) print 'hi'; and echo "hi"; seem to do the same thing. How are these tags actually different Question

2) Ok I am also wondering how to change these from JavaScript into their PHP equivilent so that the validation is done server-side:
Code:
  if (theForm.user.value.length < 2)
  {
    alert("Your username is invalid, as it must be at least 2 characters.");
    theForm.user.focus();
    return (false);
  }


I was thinking it might look a little like this, but I'm not sure:

Code:
if($user_name < 2){
print 'Your username is invalid, as it must be at least 2 characters.';
}


(the variable that the submitted username is stored in is $user_name). But yea I just want it to check and make sure that the length of the username they entered is at least 2 characters.

Anyone with any idea how to do this? Your help would be greatly appreciated Smile
hexkid
ChrisCh wrote:
1) print 'hi'; and echo "hi"; seem to do the same thing. How are these tags actually different Question

FAQTs - Knowledge Base - View Entry - What is the difference between echo and print?
Basically, just pretend they're the same and use the one you prefer Smile


ChrisCh wrote:
2) Ok I am also wondering how to change these from JavaScript into their PHP equivilent so that the validation is done server-side:
Code:
  if (theForm.user.value.length < 2)
  {
    alert("Your username is invalid, as it must be at least 2 characters.");
    theForm.user.focus();
    return (false);
  }


I was thinking it might look a little like this, but I'm not sure:

Code:
if($user_name < 2){
print 'Your username is invalid, as it must be at least 2 characters.';
}


(the variable that the submitted username is stored in is $user_name). But yea I just want it to check and make sure that the length of the username they entered is at least 2 characters.

You need the strlen() function.
Code:
<?php
if (strlen($user_name) < 2) {
  print 'invalid username';
}
?>
Related topics
Accident insurance claims
htaccess Password Protection
The Unofficial Jokes Thread
Harry Potter 6 - The Half-Blood Prince *(ENDING REVEALED)*
Chinese Gov't: Smoking is great for Health
How To : Improve Your PHP Programming
How To : Secure Your PHP Website
php dropdown lists with if statements or switch
Problem with PHP if else statements.
Fallicious Statements On the Existence of a God
Validating sql statements
Obama orders review of Bush's signing statements
Knowledge and Belief
Why do people hate goto statements so much?
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.