Jump to content

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...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.