-2

What's wrong with this program? It never returns done and the mail never goes out. if you remove getOrDefault function it works fine. It doesn't throw an error or an exception on php 5.3.13

<?php

    function getOrDefault(&$var, $default = null) {
        // because I'm tired of typing $x = isset($_POST['x'])? $_POST['x']:null;
        if (isset($var)) {
            return $var;
        } else {
            return $default;
        }
    }

    $HTTP_CLIENT_IP = getOrDefault($_SERVER['HTTP_CLIENT_IP']);

    mail('test@gmail.com', 'test', $HTTP_CLIENT_IP);
    echo 'done';
?>
nullException
  • 1,112
  • 4
  • 17
  • 29
  • Using `getOrDefault` in this snippet is meaningless. To see the whole picture, add `error_reporting(-1)` line to the beginning of the code. – raina77ow Oct 13 '13 at 05:59
  • Why are your referencing it? You can just put `getOrDefault($var, $default = null)` as you don't (and won't) modify the `$_SERVER['HTTP_CLIENT_IP']`. – Willy Pt Oct 13 '13 at 06:38
  • and the `isset` is kind of dangerous in your case. Might as well put it with `$var != ""` – Willy Pt Oct 13 '13 at 06:39
  • @WillyPt This is wrong. Its not dangerous in that case, since `$var` is passed by reference. By doing `$var != ""` you'll get a `E_NOTICE` in case a variable isn't set. – Yang Oct 13 '13 at 07:22
  • @raina77ow What is the difference between `error_reporting(E_ALL)` and `error_reporting(-1)`? – Yang Oct 13 '13 at 07:26
  • Quoting the [doc](http://php.net/manual/en/function.error-reporting.php): "Passing in the value -1 will show every possible error, even when new levels and constants are added in future PHP versions. The E_ALL constant also behaves this way as of PHP 5.4." – raina77ow Oct 13 '13 at 07:31

1 Answers1

1

You don't need this

$HTTP_CLIENT_IP = getOrDefault($_SERVER['HTTP_CLIENT_IP']);

because you can use this

$HTTP_CLIENT_IP = isset($_SERVER['HTTP_CLIENT_IP'])?$_SERVER['HTTP_CLIENT_IP']:$default;
crafter
  • 6,246
  • 1
  • 34
  • 46