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";
}
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";
}
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";
}
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"; }
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";
}
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 :)
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.