开发者

Testing variables in PHP

开发者 https://www.devze.com 2023-04-06 05:25 出处:网络
Earlier today I tried to do this: Example 1: <?php echo $myVar || \"rawr\"; ?> I thought this might print out $myVar if it was set, and print \"rawr\" if not.

Earlier today I tried to do this:

Example 1:

<?php 

  echo $myVar || "rawr";

?>

I thought this might print out $myVar if it was set, and print "rawr" if not. It printed a 1, I assume this is the result of the OR test.

What I then tried was this:

Example 2:

<?php
  if ($myVar)
  {
    echo $myVar;
  }
  else
  {
    echo "rawr";
  }
?>

Which is what I was trying to accomplish.

I think I understand why the first prints the results of the O开发者_StackOverflow社区R test rather than one of the variables, and also why I tried it - been spending some time on the bash shell recently :)

Can anyone tell me if there is a way to perform the text in example #2 but in similar syntax to example #1?


PHP 5.3:

 echo $myVar ?: 'rawr';

Pre 5.3:

 echo $myVar ? $myVar : 'rawr';

If $myVar may not be set you'd have to use isset($myVar) and $myVar as the condition (which then wouldn't work with the shorthand ?: syntax, as this would echo 1 if it was set, rather than the value).

 echo (isset($myVar) and $myVar) ? $myVar : 'rawr';


PHP is notoriously inelegant in this department, and there really is no good way of making this test.

As a general solution, you 'd need to use the ternary operator as in empty($var) ? "rawr" : $var. However, in practice what happens is that you have one of two scenarios:

1. Your own variable

In this case where you define the variable yourself, the best solution is to just give it a known default value at the place you define it (possibly with the ternary operator).

2. Inside an array

If the array is one that should not be touched like one of the superglobals, then you can wrap the test inside a function (pretty much that's what everyone does).

If the array is one under your jurisdiction but it comes from an external source, you can use the "add the defaults" trick:

$incoming = array(...);
$defaults = array("foo" => "bar");

// Inject the defaults into $incoming without overwriting existing values
$incoming += $defaults;

At this point you know for a fact that every key inside $defaults also exists inside $incoming.


In PHP 5.3+ it is possible to leave out the second argument:

echo $myVar ?: 'rawr';

This is probably closest to what you want.


Try this:

echo $myVar = ($myVar) ? $myVar : 'rawr';

or

echo $myVar ? $myVar : 'rawr';


I think you want to use something like var_dump($varname) or isset($varname) ?


it's matter of operator precedence. you can't do that.

two points as a food for thought

  1. if $myVar is not set, you'll get an error message, not "wawr";

  2. by the time you're going to echo some variables, every one of them should be defined and have value. That will make your template clean and readable.


Try echo ($myVar != null ? $myVar : "Rawr!");

0

精彩评论

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