sdyates2001 Posted January 27, 2009 Share Posted January 27, 2009 My PHP coding has become more advanced since yesterday, but I am still thinking like perl. Instead of wasting space, I want to ask if $Questions['ch1...13'] is not equal to "", then to perform a statement block. Could you please assist me with where I am going wrong? $count = ""; while ( $count < 13 ) { if ( $Questions['ch$count'] != "" ) { $PositiveFirstCriteria += 1; echo "<p>Count: $count, $PositiveFirstCriteria</p>"; } $count += 1; } Thanks in advance to anyone who takes the time to respond! Quote Link to comment https://forums.phpfreaks.com/topic/142699-solved-being-creative-with-scalars/ Share on other sites More sharing options...
DeanWhitehouse Posted January 27, 2009 Share Posted January 27, 2009 try $Questions['ch'.$count] the dot is concatenation (i think that is how it is spelt) it is used to join two strings together. Quote Link to comment https://forums.phpfreaks.com/topic/142699-solved-being-creative-with-scalars/#findComment-747957 Share on other sites More sharing options...
bluesoul Posted January 27, 2009 Share Posted January 27, 2009 Something tells me it has to do with your use of ch$count, PHP isn't gonna know what that is. Perhaps ch.$count will work but I think you'll need to rethink your logic there. Also you can use $PositiveFirstCriteria++ instead of += 1. Same thing for $count. Quote Link to comment https://forums.phpfreaks.com/topic/142699-solved-being-creative-with-scalars/#findComment-747960 Share on other sites More sharing options...
DeanWhitehouse Posted January 27, 2009 Share Posted January 27, 2009 It will work, i have used it many times Quote Link to comment https://forums.phpfreaks.com/topic/142699-solved-being-creative-with-scalars/#findComment-747963 Share on other sites More sharing options...
DeanWhitehouse Posted January 27, 2009 Share Posted January 27, 2009 Just noticed as well, if you have 13 questions then you will need to change your < (less than) to <= (less than or equal to) otherwise it will skip number 13, although a for loop is better suited for looping through an array like you are. Quote Link to comment https://forums.phpfreaks.com/topic/142699-solved-being-creative-with-scalars/#findComment-747971 Share on other sites More sharing options...
Prismatic Posted January 27, 2009 Share Posted January 27, 2009 <?php $Questions["ch1"] = "ch1"; $Questions["ch2"] = "ch2"; $Questions["ch3"] = "ch3"; $Questions["ch4"] = "ch4"; $Questions["ch5"] = "ch5"; $Questions["ch6"] = "ch6"; $Questions["ch7"] = "ch7"; $Questions["ch8"] = "ch8"; $Questions["ch9"] = "ch9"; $Questions["ch10"] = "ch10"; $Questions["ch11"] = "ch11"; $Questions["chDontCountMe12"] = "ch12"; $Questions["ch13"] = "ch13"; $PositiveFirstCriteria = 0; for($i = 1; $i <= 13; $i++) { if($Questions["ch".$i]) { $PositiveFirstCriteria++; echo "<p>Count: ". $i .", ". $PositiveFirstCriteria ."</p>"; } } ?> Outputs Count: 1, 1 Count: 2, 2 Count: 3, 3 Count: 4, 4 Count: 5, 5 Count: 6, 6 Count: 7, 7 Count: 8, 8 Count: 9, 9 Count: 10, 10 Count: 11, 11 Count: 13, 12 Quote Link to comment https://forums.phpfreaks.com/topic/142699-solved-being-creative-with-scalars/#findComment-747976 Share on other sites More sharing options...
DeanWhitehouse Posted January 27, 2009 Share Posted January 27, 2009 <?php $Questions["ch1"] = "ch1"; $Questions["ch2"] = "ch2"; $Questions["ch3"] = "ch3"; $Questions["ch4"] = "ch4"; $Questions["ch5"] = "ch5"; $Questions["ch6"] = "ch6"; $Questions["ch7"] = "ch7"; $Questions["ch8"] = "ch8"; $Questions["ch9"] = "ch9"; $Questions["ch10"] = "ch10"; $Questions["ch11"] = "ch11"; $Questions["chDontCountMe12"] = "ch12"; $Questions["ch13"] = "ch13"; $PositiveFirstCriteria = 0; for($i = 1; $i <= 13; $i++) { if(strlen(trim($Questions["ch".$i])) > 0) { $PositiveFirstCriteria++; echo "<p>Count: ". $i .", ". $PositiveFirstCriteria ."</p>"; } } ?> Just changed one thing Quote Link to comment https://forums.phpfreaks.com/topic/142699-solved-being-creative-with-scalars/#findComment-747984 Share on other sites More sharing options...
sdyates2001 Posted January 27, 2009 Author Share Posted January 27, 2009 @All - Thanks adding the . in ch.$count was it. ++ and += both work Quote Link to comment https://forums.phpfreaks.com/topic/142699-solved-being-creative-with-scalars/#findComment-747990 Share on other sites More sharing options...
Prismatic Posted January 27, 2009 Share Posted January 27, 2009 @All - Thanks adding the . in ch.$count was it. ++ and += both work Personal preference $var++; is shorter. Quote Link to comment https://forums.phpfreaks.com/topic/142699-solved-being-creative-with-scalars/#findComment-747992 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.