Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proper way to declare multiple vars in PHP

Tags:

variables

php

I've been coding personal scripts for years in PHP and get used to turn off Error display. I'm about to release some of these scripts and would like to do it the proper way.

The only reason why I turn off error display is to avoid having to test every single var, prior using it, thanks to isset().

So, here is my question:

Is there a better way to declare multiple vars than this ?

<?php   // at the begining of my main file   if (!isset($foo))  ($foo = '');   if (!isset($bar))  ($bar = '');   if (!isset($ping)) ($ping = '');   if (!isset($pong)) ($pong = '');   // etc. for every single var ?> 

Something like this for instance :

<?php   var $foo, $bar, $ping, $pong; ?> 
like image 996
Tom Biakryek Avatar asked Jun 14 '12 00:06

Tom Biakryek


People also ask

How can I assign multiple values to a variable in PHP?

$IP = array('111.111. 111.111', '222.222. 222.222', '333.333. 333.333');

Can you declare multiple variables?

Do not declare more than one variable per declaration. Every declaration should be for a single variable, on its own line, with an explanatory comment about the role of the variable. Declaring multiple variables in a single declaration can cause confusion regarding the types of the variables and their initial values.


1 Answers

<?php   $foo = $bar = $ping = $pong = ''; ?> 

If it's your script and you know which variables where you use, why you want spend recourses to check if the variable was declared before?

like image 120
DaneSoul Avatar answered Oct 18 '22 08:10

DaneSoul