February 14th, 2013, 11:28 PM
please anyone tell me wat i'm doing wrog here. The loop is entering in both if and else condition. BUt what i need to do is if the condition satisfies it should enter the if, and if it not satisfied then it should enter the else condition. Any ideas please tell me.
$strfirstlettercaps = strtolower($str);
echo "<img src='../uc1.jpg'>";
February 15th, 2013, 01:54 AM
that looks like a logical error. Obviously your loop will check the condition on each run, so every time either the "if" part or the "else" part will be executed.
But I guess you want something like:
If any element of $var equals $str, then run the first part, otherwise run the second part.
And that's what in_array is for.
Your programming style generally is a bit weird. You might wanna improve the following things:
- Use sensible variable names. Variables like "$var", "$var1" and "$str" tell you nothing about what they actually contain and what they're for. This makes the code very difficult to read. And $strfirstlettercaps for a lowercase(!) string is just weird.
- Get rid of this low level programming style. Use "foreach" instead of "for" when you want to iterate through an array. Use built-in arrays functions instead of reinventing the wheel.
- Get rid of this "$variable" stuff (a string with a variable as its only contant). PHP does automatic type conversions, so this is completely useless 99% of the time. In the rare case you actually need an explicit type cast, use (string) $var.
- Try to structure your code in a logical and intuitive way. Things like "If x, then require a file, otherwise output an image" don't really make sense. It's also not a good idea to use the same variable for two completely different types. In your case, $var1 can either hold a file name or a resource. How is that supposed to work in your later code?
- Indent your code!
Last edited by Jacques1; February 15th, 2013 at 02:00 AM.