开发者

Simple php if query to double check

开发者 https://www.devze.com 2022-12-13 17:54 出处:网络
Is anything wrong with this code? <?php $variable = ; if (isset($variable)) { echo $variable ; echo \"also this\" ;

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


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
}


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


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!


$variable = ;

is invalid. it should be

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

if you really need to include the variable definition at all

0

精彩评论

暂无评论...
验证码 换一张
取 消