tags:

views:

88

answers:

6

what am I doing wrong here guys?

 $string = "string How Long is a Piece of String?";

if $string = <5;
{
echo "string is less than 5";
}

else
{
echo "string is more than 5";
}
+6  A: 

1st, condition are in parenthesis.

2nd, you don't need a ; after a condition.

3rd, less than is simply < not <= unless you want to echo "string is less or equals than 5"

$string = "string How Long is a Piece of String?";

if (strlen($string) < 5)
{
   echo "string is less than 5";
}
else
{
   echo "string is more than 5";
}
HoLyVieR
=< is nothing, <= is "less than or equal to"
Marius
indeed, it's corrected now.
HoLyVieR
Don't forget 4th, you need strlen. Shown in code, but not stated.
Timothy
A: 

missing parentheses around if statement and no need for semi-colon? also less than or equal operator in wrong order. should be like this:

if ($string <=5) { echo "string is less than 5"; }

nathan gonzalez
+2  A: 

Others pointed out the syntax errors, to actually compare to the length of the string you need to use the strlen function:

$string = "string How Long is a Piece of String?";

if (strlen($string) < 5)
{
   echo "string is less than 5";
}
else
{
   echo "string is more than 5";
}
Tatu Ulmanen
lmao, I forgot the obvious strlen. Sometimes when the answer is so easy you forget the obvious.
HoLyVieR
+1  A: 

may be you're looking for the strlen() function?

Col. Shrapnel
+2  A: 

Type juggling it is called:
http://nl2.php.net/manual/en/language.types.type-juggling.php

$string = "string How Long is a Piece of String?";
if ($string < 5)

string is cast to int, becomes 0

if (0 < 5)

true!

strlen / mbstrlen are possible candidates you're loking for

But that wasn't the question, there are obivously more things wrong with the code :)

Wrikken
A: 

Also note that if that string has multi byte chars it will return a wrong char count, but a byte count.

You'll probably need to know this down the track :) For now, get on top of your syntax.

alex