0

I'm having little bit of confusion as to why we use @ in if condition in PHP

Example

foreach ($date as $value) {
        if(@$data[$value] != ""){
            $data[$value] = date("Y-m-d", strtotime($data[$value]));
        }
    }
Sam Bellerose
  • 1,782
  • 2
  • 18
  • 43
MaNsHa QaRiB
  • 37
  • 1
  • 7
  • 3
    We don't, if we're writing safe code. It suppresses errors, and should almost never be used. – ceejayoz Dec 20 '18 at 22:33
  • 1
    In this case they are using `if(@$data[$value] != "")` where the `@` likly suppresses undefined index errors. A better use would be `if(!empty($data[$value]))`. Also to note the difference between `$date` and `$data` can cause some readability issues. It's very easy to confuse one for the other (well for my dyslexic self that is) – ArtisticPhoenix Dec 20 '18 at 22:45
  • In this specific case, is the "old way" to emulate something like ?? as this is not per-se an error in php to access an undefined cell in an array. If the cell is not defined and u do this if, u get a warning about an undefined ..., If u want to use proper code, do isset($data['value] && $data['value'] !='' – Itay Moav -Malimovka Dec 20 '18 at 22:56
  • thanks to all...u guided me very-well – MaNsHa QaRiB Dec 23 '18 at 16:49

0 Answers0