Use @to ignore or if/Else?

Asked

Viewed 33 times

2

In PHP, we use methods POST and GET (besides the REQUEST) to acquire the variables passed by HTML, but we can go through inconvenient situations when redeeming these keys, such as having the display of news and alerts stating that the variables could not receive a value, as we can see in the example:

<?php
    $variavel = $_REQUEST['valor'];
    //na primeira execução da página não teremos a chave 'valor'
?>

<form action="#">
    <input type="text" name="valor"/>
    <input type="submit" value="..."/>
</form>

And we will see the following warning:

Notice: Undefined index: value in .. page.php on line 2 Call Stack

#Time Memory Function Location 1 0.0011 131632 {main}( ) .. page.php:0

To solve this we have two options:

-- Use @ before the statement, which means ignoring the news and warnings;

<?php
@$variavel = $_REQUEST['valor'];
?>
<form action="#">
    <input type="text" name="valor"/>
    <input type="submit" value="..."/>
</form>

-- Make conditions to handle variable value;

<?php
if(isset($_REQUEST['valor']))
    $variavel = $_REQUEST['valor'];
else
    $variavel = NULL;
?>
<form action="#">
    <input type="text" name="valor"/>
    <input type="submit" value="..."/>
</form>

I want to know how best to treat this, I personally prefer the second, because for me the first form seems more like a 'gambiarra', but I want to see your opinion and see which method is most used.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.