views:

167

answers:

4

Is anything wrong with this code?

<?php

$variable = ;

if (isset($variable))

{

echo $variable ;
echo "also this" ;

}

else

echo "The variable is not set" ;

?>

also, the other potential value of the variable is :

$variable = <a href="http://www.mysite.com/article"&gt;This Article</a>;

To clarify, I have a variable that may hold one of two possible values : an a href tag with it's url, or notihng at all. I need to have two different printouts for each of these cases, maybe I'm not doing it the right way though!

A: 

should be:

<?php


if (isset($variable))
{

    echo $variable ;
    echo "also this" ;

}

else
{
    echo "The variable is not set" ;
}    

?>

Or more concisely:

<?php echo isset($variable) ? $variable."\nalso this" : null; ?>

prodigitalson
+1  A: 

In PHP you do not need to initialize a variable to check if it is set. The first line of your code is not only invalid, but also unnecessary.

Edit: Okay per your clarification in comments, the variable is always set, however it sometimes contains text and sometimes contains an empty string. In this case, I would do follow the advise by @prodigitalson in the comments:

if (isset($variable) && !empty($variable))
{
    // do set stuff here
}
else
{
    // not set, do blank stuff here
}
Scott Anderson
But the variably might be set. To clarify, I have a variable that may hold one of two possible values : an <a href> tag with it's url, or notihng at all. I need to have two different printouts for each of these cases, maybe I'm not doing it the right way though!
skarama
Either way, you don't need to set it to nothing. Your if statement is fine by itself. If you want to unset it manually, you can call unset($variable);
Scott Anderson
I don't understand how this would work in my case. This code is loaded as a template at the bottom of user-posted content. If the user has submitted his content using form A, the variable will be empty thus return the else segment. If the user has used the form B to submit his content, then variable is automatically set (as a hidden field in that form) and I would need the if segment to be returned.
skarama
prodigitalson
isset() will always return true as long as the variable is set to something other than NULL, otherwise it returns false. This is the only check you need.
Scott Anderson
Thanks prodigitalson, I think this will do the trick! I never know in advance wether or not the variable will have a value, but I guess it is always set, with an empty value, or an url. This should make it work, but I'll confirm in a min!
skarama
Thank you all this works now!
skarama
A: 

line one states

$variable = ;

You can't do this... you need to set the variable to something, or unset it. E.g

$variable = '';

or

unset($variable);

It would help in future if you posted the error message you were receiving, as that would help us to help you!

Dave Rix
Thanks, I will try to do so but I don't actually get any errors, just no result whatsoever! Thanks for pointing out the missing ''!
skarama
Ok, I guess the errors will be in the log files instead :)
Dave Rix
A: 
$variable = ;

is invalid. it should be

$variable = null; // or any other empty value

if you really need to include the variable definition at all

Janek