Jump to content

[SOLVED] insert PHP code into function


djtozz

Recommended Posts

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

 

 

 

Link to comment
Share on other sites

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%');

Link to comment
Share on other sites

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?

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

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.  :keep_quiet:

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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* :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.