0

Possible Duplicate:
What is the PHP shorthand for: print var if var exist

In order to print a variable, I'm doing this:

if(isset($var)) print $var;

But Im repeating $var 2 times here. Is there a way to make it look like:

If $var exists then print it

instead of

If $var exists then print $var
Community
  • 1
  • 1
Alucard
  • 1,814
  • 1
  • 21
  • 33
  • 6
    what's the problem if you call the variable again? – John Woo Dec 26 '12 at 09:13
  • There is no problem, i'm just looking for a way to improve my code (if there is). It follow the same thinking as when for an example I learned that I can do: = $var ?> instead of .. – Alucard Dec 26 '12 at 09:15
  • 3
    There's no need to shorten that, shorter is'nt always better. As a sidenote, shorttags are not always a good idea either, as they are disabled by default in some versions of PHP. – adeneo Dec 26 '12 at 09:18
  • 1
    echo ($var) ? $var : ""; – Suresh Kamrushi Dec 26 '12 at 09:18
  • 1
    @SureshKamrushi : that will not make an isset on the variable. if variable doesn't exist , will throw error. – Mithun Satheesh Dec 26 '12 at 09:19
  • Yes @Zaffy, I could use a custom function, but it wouldn't be better than the if statement Im avoiding.. Well, Im not desperate on this, if there isn't a way then there isn't a way, I'm just looking out of curiosity – Alucard Dec 26 '12 at 09:24

5 Answers5

1

You can use print (isset($var) ? $var : '');

?: called ternary operator

Artem L
  • 10,123
  • 1
  • 20
  • 15
  • 1
    here also $var repeating; – Mohammed H Dec 26 '12 at 09:20
  • yeah, but I would access $var 2 times. What I'm looking for is like a function or a way to write: `If_this_variable_exists_then_print_it($var);` – Alucard Dec 26 '12 at 09:21
  • 1
    @vladimire There is no way of doing that, short of writing such as function yourself. That is just how the language works; it is much easier to just accept the language for what it is. – Sverri M. Olsen Dec 26 '12 at 09:39
  • Actually there are no way to do anything with undefined variable in a function like in comment. It will print a notice anyway. – Artem L Dec 26 '12 at 10:47
1

Initialize $var to the empty string so you will not need the if-check.

Assuming that you do $var = $_REQUEST['var'];, you'll be out of luck.

Imho it is a micro-optimisation, not really important.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
0

Use the help of Error Control Operator "@" http://php.net/manual/en/language.operators.errorcontrol.php

echo @$var;

I don't think to use "If $var exists then print it" is faster than "If $var exists then print $var". Internally the script-engine to check the variable is set or not. May be it is micro-optimization.

This type questions may be asked in IT-Quizzes. I remember one question "how to write a program which print a string. The condition is that don't use semicolon". Answer is given.

<?php if(print("Hello World")){}
Mohammed H
  • 6,880
  • 16
  • 81
  • 127
-1

Unfortunately there is no short and PHP built-in solution for this.

In our framework we use our own function:

function isset_(&$aVariable, $aDefaultValue = null)
{
    if (isset($aVariable)) return $aVariable;
    return $aDefaultValue;
}

Then you can do something like:

echo isset_($var);
-2

You could do <?php echo @$var; ?>. Or <?= @$var ?>.

zafus_coder
  • 4,451
  • 2
  • 12
  • 13