News:

Facebook killed the radio star. And by radio star, I mean the premise of distributed forums around the internet. And that got got by Instagram/SnapChat. And that got got by TikTok. Where the fuck is the internet we once knew?

Main Menu

ospap - Alpha1

Started by iago, September 14, 2005, 06:46:00 PM

Previous topic - Next topic

0 Members and 2 Guests are viewing this topic.

rabbit

Quote from: iago on October 14, 2005, 11:35:38 PM
Is there some way to make it show an error if you don't declare variables like that?

On Perl, it's "use strict;"
Use error_reporting(E_ALL); at the beginning of your core file (IE: the one that's always there, like the index.php or something).  It will throw a warning when you use a variable that isn't $_GET[], $_POST[], $_SESSION[], or my.

Sidoh

There's also this entry in the php.ini file which usually (by default, on my box anyway) this:

error_reporting  = E_COMPILE_ERROR|E_ERROR|E_CORE_ERROR

Obviously, just change it to:

error_reporting = E_ALL

However, that's not going to stop your code from running, it will just display a Notice.

rabbit

Just a note: using the error_reporting() function is easier than changing the PHP hard configuration.

Sidoh

Quote from: rabbit on October 15, 2005, 11:51:55 AM
Just a note: using the error_reporting() function is easier than changing the PHP hard configuration.
Not if you want to make a global change on your server.  :P

It's easier to develop with E_ALL on in PHP.ini, then you don't have to worry about removing it when you deploy it (because people don't like error reporting in E_ALL, especially on scripts that are included as an addon or something to that affect).

Warrior

Install a custom error handlers in your PHP code and treat the notices as fatal exceptions and exit your code.
One must ask oneself: "do I will trolling to become a universal law?" And then when one realizes "yes, I do will it to be such," one feels completely justified.
-- from Groundwork for the Metaphysics of Trolling

Sidoh

Quote from: Warriorx86] link=topic=2860.msg33966#msg33966 date=1129492486]
Install a custom error handlers in your PHP code and treat the notices as fatal exceptions and exit your code.
That would be way too much work for such a silly thing.

Warrior

It's surprisingly easy but yea I think it might be overdoing it a bit. Should help for other things though too.
One must ask oneself: "do I will trolling to become a universal law?" And then when one realizes "yes, I do will it to be such," one feels completely justified.
-- from Groundwork for the Metaphysics of Trolling

Sidoh

Quote from: Warriorx86] link=topic=2860.msg34025#msg34025 date=1129502290]
It's surprisingly easy but yea I think it might be overdoing it a bit. Should help for other things though too.
I know, I've done it before.  There's other unseen issues that it would affect too though.  Doing it would be stupid.  It's like having a car that doesn't work if it doesn't make a sound when the driver's side door is open.  Quite useless.

Warrior

You can of course filter the notice messages to the one you need :p
One must ask oneself: "do I will trolling to become a universal law?" And then when one realizes "yes, I do will it to be such," one feels completely justified.
-- from Groundwork for the Metaphysics of Trolling

Sidoh

Quote from: Warriorx86] link=topic=2860.msg34073#msg34073 date=1129515392]
You can of course filter the notice messages to the one you need :p
Quote from: Sidoh on October 16, 2005, 10:08:33 PM
Quite useless.

: - P

Warrior

Don't see how it's still useless if I showed how it could be singled out into one exception and handled from there instead of making the entire thing croak becauause of every notice. (Even though IMHO it should, it enforces nice programming)

One must ask oneself: "do I will trolling to become a universal law?" And then when one realizes "yes, I do will it to be such," one feels completely justified.
-- from Groundwork for the Metaphysics of Trolling

Sidoh

Quote from: Warriorx86] link=topic=2860.msg34085#msg34085 date=1129522028]
Don't see how it's still useless if I showed how it could be singled out into one exception and handled from there instead of making the entire thing croak becauause of every notice. (Even though IMHO it should, it enforces nice programming)

I don't understand how not explicitly defining variables in a language that doesn't require it (or even imply that you should, unlike Visual Basic) is "nice programming."  PHP's variable declarations work completely well when defined implicitly and I don't see a purpose in forcing code to define them explicitly.  It's just stupid.

Having code croak on every notice would be stupid for the lack of a better word.

Warrior

Forcing all variables to be declared not only helps when debugging code, wait sometimes it eliminates the need to debug since the reason is right there. I fail to see where there is no advantage to this.
One must ask oneself: "do I will trolling to become a universal law?" And then when one realizes "yes, I do will it to be such," one feels completely justified.
-- from Groundwork for the Metaphysics of Trolling

iago

You *should* declare all of a functions's variables at the top of the function, along with comments on what they do.  It helps you keep them straight, it forces you to stop and think about what you're doing, and it *greatly* helps anybody else who is looking at your code.  Doing it that way really is *nice* code.  Maybe it's not SO important in PHP, but right now I'm doing low level microcontroller programming, and it's very important. 

Sidoh

#119
It's not important in PHP.  Debugging is a pretty negligible challenge in PHP, IMO.

Warrior, I'm not saying there's no advantage, I'm just saying its a moot effort.  It's trying to make PHP something it's not.  You're adding another unnecessary failure point.

Besides, if you use intuitive, predictable variable names, it doesn't do much for you declaring them at the beginning of a function.  Most PHP code I use is outside of a function anyway.  Most variables I handle are in a superglobal array, so moving them to a variable of lower scope is often a waste of time and resources.

Secondly, since variable types in PHP are determined completely by implicit methods, declaring variables doesn't increase the clarity of the code by that much.  Declaring a variable in PHP (obviously) would look like this:


$int_variable = 0;
$str_variable = '0';
$flt_variable = '1.0';
$bol_variable = true;


Declaring a variable's scope in PHP is obviously a different story.  I ALWAYS do this because not doing it is a complete waste of time, cleanliness of code and intelligence.  If you have something like:


$db = new mysql_db();

function get_username($user_id, $db) {
  // ...
}

get_username(1, $db);


It's pretty stupid when you can save a lot of trouble by doing:


$db = new mysql_db();

function get_username($user_id) {
   global $db;

   // ...
}

get_username(1);