3

Is there any way to shorten this code ?

<?= isset($email)?$email:''; ?>

I feel it's kind of stupid to repeat $email. I've tried

<?= isset($email)?:''; ?>

But it echoes the boolean coming from isset instead.

7
  • 2
    It's probably terrible practice, but you could use @$email to say "this might not be set but I don't care." Commented Oct 22, 2013 at 21:01
  • I don't think there is but would be interested in hearing different. Commented Oct 22, 2013 at 21:02
  • 1
    Yes, it can be the solution <?=@$mail;?> Commented Oct 22, 2013 at 21:02
  • Why would this be "bad practise"? Commented Oct 22, 2013 at 21:04
  • 1
    @Gavin normally considered bad practice to suppress notices/warnings which is what the @ does. Commented Oct 22, 2013 at 21:11

3 Answers 3

2
<?= isset($email)?$email:''; ?> // is the shortest way to do it.
Sign up to request clarification or add additional context in comments.

1 Comment

If short tags is off then you must use <?php echo isset($email)?$email:'' ?> this is the version of the code that will always work.
1

You could write a custom function:

function safeEcho(&$var) {
    if (isset($var))
        return $var;

    return null;
}

and call this function:

<?= safeEcho($var) ?>

4 Comments

This is wrong in two ways : 1) A function that returns a value should NOT be called safeEcho. 2) This isn't as "safe" as you think - it will issue E_NOTICE if a $email isn't set
Edited (variable pass by reference) as per @Dave Just
@DaveJust you're right, at first, I had a wrong concept in mind when writing the function. Surely, there are better names possible.
@TheWolf Which version of PHP are you on? All variables are passed by reference since version 5.3.0 and use of & is deprecated. Removed from 5.4.0 onwards. php.net//manual/en/language.references.pass.php
1

The only way of "shorting" that, is a custom function.

function get(&$email) { // <- Note, there must be a reference!!!
  return isset($email) ? $email : '';
} 

<?= get($email); ?>

If you pass $email without a reference, then isset() will issue a E_NOTICE if a variable isn't set. This is because you pass to isset() a copy of undefined variable, not a variable itself.

5 Comments

it depends on what php version you are talking, starting from some version everything is passed by reference.
@MehdiKaramosly citation needed
php.net/manual/en/language.references.pass.php Note: There is no reference sign on a function call - only on function definitions. Function definitions alone are enough to correctly pass the argument by reference. As of PHP 5.3.0, you will get a warning saying that "call-time pass-by-reference" is deprecated when you use & in foo(&$a);. And as of PHP 5.4.0, call-time pass-by-reference was removed, so using it will raise a fatal error.
This is exactly what is happening here: Reference sign on function definition, no reference sign on function call.
You don't need the reference sign at all unless you are using PHP < 5.3.0 in which case you need to upgrade.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.