djtozz Posted October 22, 2009 Share Posted October 22, 2009 Hi, I'm having problems when trying following: I have: createBar("Download: <b>$kwd</b><Br />", "100%") and I'm trying to display following after $kwd: if(count($info['results'])) print '<div align="center" class="info"><b>'.$info['total_results_count'].'</b> results found, page '.$info['curpage'].' from '.$info['total_pages'].'</div>'; I replaced all double " by /" but this doesn't seems to work. createBar("Download: <b>$kwd</b> if(count($info['results'])) print '<div align=\"center\" class=\"info\"><b>'.$info['total_results_count'].'</b> results found, page '.$info['curpage'].' from '.$info['total_pages'].'</div>'<Br />", "100%") I'm getting following error: Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted October 22, 2009 Share Posted October 22, 2009 You can't do it like that. I would just generate the text for the first parameter, assign it to a variable and then pass it to the function. $text = "Download: <b>$kwd</b>"; if (count($info['results'])) { $text .= '<div align="center" class="info"><b>'.$info['total_results_count'].'</b> results found, page '.$info['curpage'].' from '.$info['total_pages'].'</div>'; } $text .= '<br />'; createBar($text, '100%'); Quote Link to comment Share on other sites More sharing options...
purpleshadez Posted October 22, 2009 Share Posted October 22, 2009 if(count($info['results'])) if what? is $info['results'] meant to equal a certain amount, be less than or more than a certain amount? Also use curly brackets with if statements it makes the code easier to read print '<div align="center" class="info"><b>'.$info['total_results_count'].'</b> results found, page '.$info['curpage'].' from '.$info['total_pages'].'</div>'; This seems fine to me but what is the rest of the code doing? what line is the error on? Look at the code from the error UP as the fault will most likely be on the line(s) above it. Have you checked for a missing semi colon? Seeing the full script would help. What is createBar() doing? Quote Link to comment Share on other sites More sharing options...
trq Posted October 22, 2009 Share Posted October 22, 2009 if what? is $info['results'] meant to equal a certain amount, be less than or more than a certain amount? if(count($info['results'])) Checks to see if the result of the call to count() returns true or false. It is perfectly valid code. Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted October 22, 2009 Share Posted October 22, 2009 if(count($info['results'])) if what? is $info['results'] meant to equal a certain amount, be less than or more than a certain amount? 0 evaluates to false in a boolean context, any other integer evaluate to true. PHP is weakly typed language. Quote Link to comment Share on other sites More sharing options...
purpleshadez Posted October 22, 2009 Share Posted October 22, 2009 if(count($info['results'])) if what? is $info['results'] meant to equal a certain amount, be less than or more than a certain amount? 0 evaluates to false in a boolean context, any other integer evaluate to true. PHP is weakly typed language. Good point, my bad. IMO its a bit lazy though Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted October 22, 2009 Share Posted October 22, 2009 Really? So you explicitly convert between all types always? $var1 = 1; $var2 = 1.6; echo (string) ((float) $var1 + $var2); Or do you just rely on PHP's automatic type inference and conversion? echo $var1 + $var2; Quote Link to comment Share on other sites More sharing options...
JAY6390 Posted October 22, 2009 Share Posted October 22, 2009 lmao nice example Quote Link to comment Share on other sites More sharing options...
purpleshadez Posted October 22, 2009 Share Posted October 22, 2009 Really? So you explicitly convert between all types always? $var1 = 1; $var2 = 1.6; echo (string) ((float) $var1 + $var2); Or do you just rely on PHP's automatic type inference and conversion? echo $var1 + $var2; No I don't explicitly convert each type always I only do it when required. But in the context of the if statement (which was what I was commenting on) I would always write it as if(count($num) > 0) { // do someting } just as I would also write if(isset($var)) { // do something } instead of if($var) { // so something } My apologies if my light hearted and perhaps vague comment has caused offence as that wasn't my intention. Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted October 22, 2009 Share Posted October 22, 2009 The statement count($num) evaluates to true if and only if count($num) > 0 evaluates to true, so the two statements are equivalent. In a boolean context, the statements isset($var) and $var are not equivalent. Specifically, if you set $var = false; then the first will evaluate to true, but the second one to false. The same thing will happen if you set $var = 0;. If the variable $var is not even set, then the first one will evaluate to false and so will the second one, but the second one will raise an error of level E_NOTICE. Quote Link to comment Share on other sites More sharing options...
djtozz Posted October 22, 2009 Author Share Posted October 22, 2009 Thanks for the excellent info guy's! Once again, I learned a lot from this post! Keep up the good work Quote Link to comment Share on other sites More sharing options...
purpleshadez Posted October 22, 2009 Share Posted October 22, 2009 The statement count($num) evaluates to true if and only if count($num) > 0 evaluates to true, so the two statements are equivalent. In a boolean context, the statements isset($var) and $var are not equivalent. Specifically, if you set $var = false; then the first will evaluate to true, but the second one to false. The same thing will happen if you set $var = 0;. If the variable $var is not even set, then the first one will evaluate to false and so will the second one, but the second one will raise an error of level E_NOTICE. Yes. Another good point! I'm not sure what I had in my mind but no, I retract my lazy statement. *Considers himself duely corrected* Quote Link to comment 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.