Forum Moderators: coopster
Quick Example:
if ($number == "1")
echo "comment"
else
echo "comments"
then later on I want to call that if statement. But rather than using a function, is there a way I can set a variable to store the result of that?
or if you mean reusing the result
if ($number == "1")
$myresult = "comment";
else
$myresult = "comments";
echo $myresult;
then you could use the result of the statement wherever you want
Parse error: syntax error, unexpected '=' in /home/tom/public_html/login/inc/shows.inc.php on line 316
My code looks like this:
315: if(countComments($news_arr[0], $archive)=="1")
316: { $comment-hack = "comment"; }
317: else
318: { $comment-hack = "comments";
There shouldn't be a problem, should there?
$comment-hack
you will need to take the - out of there, they aren't allowed in variable names
from
[php.net...]
Variable names follow the same rules as other labels in PHP. A valid variable name starts with a letter or underscore, followed by any number of letters, numbers, or underscores.
more often than not when you get an error reported on the last line of the file it has to do with a mismatch problem
( but no )
{ but no }
etc
like the missing brace after your else ;)
315: if(countComments($news_arr[0], $archive)=="1")
316: { $comment-hack = "comment"; }
317: else
318: { $comment-hack = "comments"; }
plus, if that 1 is supposed to be an integer you could write all that like so (also you don't need braces for single lines)
if(countComments($news_arr[0], $archive)==1) $comment-hack = "comment";
else $comment-hack = "comments";
Now there's only one more problem I face. It's not working, but I am getting no error.
Basically, it's always displaying the word "comments".
Here's the code that displays it later on:
$output = str_replace("{hack-comments}", $commenthack, $output); /// ---------- Hack here too
The problem is quite unusual now. When I make a new php file and write the following:
<?php
$individual_comments = "1";
if($individual_comments==1)
{$commenthack = "comment";}
else
{$commenthack = "comments";}
echo $commenthack;
?>
It works, no matter what the number is. But in the shows.inc.php file it doesn't work. I thought perhaps I was calling the number from the wrong place, but I'm not. >.<
// ---- Tom's Comment Hack ---- //
if(countComments($news_arr[0], $archive)==1)
{$output = str_replace("{hack-comments}", "comment", $output);}
else
{$output = str_replace("{hack-comments}", "comments", $output);}
// ---- Tom's Comment Hack ---- //
rather than calling the variable, I just wrote the if statement around it.
$individual_comments = "1";if($individual_comments==1)
Although you're happy already, I'm not sure what happens when you assign a string value of "1" to a variable and then check if it's the numerical value 1. I know, php doesn't do much type checking - but if you run into troubles again, only use " around strings, not numbers.
Good luck!