Jump to content

Setting A Variable, Ternary Operator Vs. Error Control Operator


jmartinsen

Recommended Posts

Hi all.

 

What's the best practice with using the Ternary Operator vs. the Error Control Operator when setting a simple varaible? I doubt there is any performance issues?

 

<?php
$var = isset($_GET["var"]) ? $_GET["var"] : "";
$var = @$_GET["var"];
?>

Oh, you're absolutely right. I first thought that the @ ignored the error handler altogether, but it doesnt. It calls it, but just doesnt do anything with it. I did a supersimple test and it shows that the ternary operator was 4 times faster.

 

With performance out of the picture any thoughts on using one over the other?

the suppression way gives a different result than the ternary

 

This is the important point, more so than any performance concerns of the @-operator.  The ternary example allows you to define the value when the variable is not set, whereas the @'d variable will give you NULL (so you'll later go check for NULL and assign a value, right).

  • 4 weeks later...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.