PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 14th, 2013, 10:28 PM
gayathri.G gayathri.G is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 24 gayathri.G User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 29 m 22 sec
Reputation Power: 0
Loop problem

PHP Code:
for($i=0$i<$count;$i++){
if(
$var[$i]==$str)
{
echo 
"$str".$var[$i];
$strfirstlettercaps strtolower($str);
$var1=$strfirstlettercaps.".php";
require_once 
$var1;  
}
else 
{
$strfirstlettercaps="";
$var1=fopen("$strfirstlettercaps".".php","x+");
echo 
"<img src='../uc1.jpg'>";


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.

Reply With Quote
  #2  
Old February 15th, 2013, 12:54 AM
Jacques1's Avatar
Jacques1 Jacques1 is offline
pollyanna
Click here for more information.
 
Join Date: Jul 2012
Location: Germany
Posts: 1,847 Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 1 Day 10 h 36 m 10 sec
Reputation Power: 813
Hi,

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 01:00 AM.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPHP Development > Loop problem

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap