homepage Welcome to WebmasterWorld Guest from 54.204.94.228
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Making an if statement a variable
Programmers

5+ Year Member



 
Msg#: 3008251 posted 10:16 pm on Jul 14, 2006 (gmt 0)

I would like to delcared an if statement as a variable. So I can just call the variable after the if statement has been performed.

Can this be done, or do I have to use a function.

 

eeek

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 3008251 posted 10:39 pm on Jul 14, 2006 (gmt 0)

How can a statement be a variable? I don't get it.

spander

10+ Year Member



 
Msg#: 3008251 posted 10:42 pm on Jul 14, 2006 (gmt 0)

I think you need to explain a little better. Maybe an example...

Programmers

5+ Year Member



 
Msg#: 3008251 posted 10:46 pm on Jul 14, 2006 (gmt 0)

Okay, sorry. What I mean is I want to call a variable that is the result of an if statement.

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?

eeek

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 3008251 posted 10:48 pm on Jul 14, 2006 (gmt 0)

Something like this?

if ($something=='stuff')
$result=1;

jatar_k

WebmasterWorld Administrator jatar_k us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 3008251 posted 10:50 pm on Jul 14, 2006 (gmt 0)

or if you mean reusing the same code (the if statement) over again later on then you should put it into a fucntion then and call the function wherever you need it

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

Programmers

5+ Year Member



 
Msg#: 3008251 posted 11:01 pm on Jul 14, 2006 (gmt 0)

jatar_k, that's a brilliant soloution. Thank you. However, I'm getting an error now for something that shouldn't really be an error:

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?

jatar_k

WebmasterWorld Administrator jatar_k us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 3008251 posted 11:11 pm on Jul 14, 2006 (gmt 0)

if this is your variable name

$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.

Programmers

5+ Year Member



 
Msg#: 3008251 posted 11:18 pm on Jul 14, 2006 (gmt 0)

Excellent stuff, thanks for pointing that out. Now I have this error!

Parse error: syntax error, unexpected $end in /home/tom/public_html/login/inc/shows.inc.php on line 778

*sigh*

778:?>

jatar_k

WebmasterWorld Administrator jatar_k us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 3008251 posted 11:21 pm on Jul 14, 2006 (gmt 0)

those are tricky

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";

Programmers

5+ Year Member



 
Msg#: 3008251 posted 11:38 pm on Jul 14, 2006 (gmt 0)

Okay, that's that sorted. This is all going really well (thanks for your patience and help so far).

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

jatar_k

WebmasterWorld Administrator jatar_k us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 3008251 posted 11:56 pm on Jul 14, 2006 (gmt 0)

maybe try single instead of double quotes around it in str_replace

that is a WAG, not sure

Programmers

5+ Year Member



 
Msg#: 3008251 posted 12:06 am on Jul 15, 2006 (gmt 0)

It never worked... =\ Also, what's a WAG? if it's something in the code I posted, I don't know because I didn't write it. I am writing a "hack", or making an ammendment to someones content management system.

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

Programmers

5+ Year Member



 
Msg#: 3008251 posted 12:34 am on Jul 15, 2006 (gmt 0)

I DID IT - Booyahh! Thanks for the help everyone. The soloution to my problem was the following:

// ---- 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.

jatar_k

WebmasterWorld Administrator jatar_k us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 3008251 posted 12:50 am on Jul 15, 2006 (gmt 0)

nice work

WAG - wild ass guess ;)

Programmers

5+ Year Member



 
Msg#: 3008251 posted 1:05 am on Jul 15, 2006 (gmt 0)

Thank you. =)

the_nerd

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 3008251 posted 8:09 pm on Jul 15, 2006 (gmt 0)

$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.

Programmers

5+ Year Member



 
Msg#: 3008251 posted 10:40 pm on Jul 15, 2006 (gmt 0)

Okay, thanks mate. =)

eeek

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 3008251 posted 7:00 am on Jul 16, 2006 (gmt 0)

only use " around strings

And use ' around strings when you don't want variable substitution or \ escapes.

Programmers

5+ Year Member



 
Msg#: 3008251 posted 7:20 am on Jul 16, 2006 (gmt 0)

What do you mean?

eelixduppy

WebmasterWorld Senior Member eelixduppy us a WebmasterWorld Top Contributor of All Time 5+ Year Member



 
Msg#: 3008251 posted 1:23 pm on Jul 16, 2006 (gmt 0)

eeek is talking about different syntax for strings [php.net]. Read up on it because it may be something you will need to know in the future.

Good luck!

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved