Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP coding practices

Tags:

php

If I get a variable and I don't know is it set or not I can write

if (isset($a) && $a > 2)

or I can write

if (@ $a > 2)

which is shorter. Is the second syntax good or not?

like image 541
Dan Avatar asked Aug 31 '10 12:08

Dan


People also ask

What are PHP coding standards?

Overview. Code MUST follow a "coding style guide" PSR [PSR-1]. Code MUST use 4 spaces for indenting, not tabs. There MUST NOT be a hard limit on line length; the soft limit MUST be 120 characters; lines SHOULD be 80 characters or less.

Why is PHP still so popular?

It's versatile: One of the major benefits of PHP is that it is platform independent, meaning it can be used on Mac OS, Windows, Linux and supports most web browsers. It also supports all the major web servers, making it easy to deploy on different systems and platforms at minimal additional cost.


2 Answers

The expression if(@$a) does not check whether the variable is set or not. The @ symbol just surpresses any warnings which is a bad coding style.

like image 90
Nick Avatar answered Oct 08 '22 18:10

Nick


I totally sympathize, but suppressing the error using @ is bad practice.

The error still occurs, it just gets suppressed. It costs microscopical amounts of time that can however accumulate to a lot if done in loops.

Also, you take away the possibility to use "undefined variable" notices to your advantage: As a mechanism to avoid typos.

like image 34
Pekka Avatar answered Oct 08 '22 20:10

Pekka