Re: [PHP] Warning: Undefined variable
Good to hear from you again David, On Wednesday, April 3, 2002, at 06:28 AM, DL Neil wrote: > If they are one and the same machine/server, then use 'debug' code to > reset options at the beginning of dev code (and remove it when the > routine is 'promoted' to prod). Excellent idea. E_ALL for me from now on in development, ERROR_REPORTING: 0 in the php.ini. Whoah -- dozens of errors at the top of my scripts now. I'm going to have to do some research just to find out what all these undefined indexes and variables are all about. > In strongly typed languages, I think there are (?have been historically) > two objectives in defining variables prior to their use: > 1 to set aside the storage space, and > 2 to define the variable type > > 1 in the case of a scalar variable this seems trivial, eg > INTEGER J > Dim intJ As Integer > > In the case of an array or var/set length string, then replication needs > to be added into the equation for calculating both an individual > component's address and working out how much storage to set aside, eg > INTEGER K(10) > > 2 Somewhat obviously a double-precision floating point number will > require more space/bytes to store its value than a simple integer. > However a common error made when programming is that you think a > variable holds data of a certain type, when in fact you earlier used it > in some other (very similar, but not quite the same) manner. eg > > //don't use this code > $User = "Fred"; > later on we do (say) a db call and pull out a row of personal data: > $query = "SELECT * FROM persTbl WHERE persId = '$User' "; > //I'm assuming that persId is likely some sort of personnel/membership > number > > PHP is a 'little ripper' when it comes to quick and dirty usage, because > you don't have to type a declaration line before using a > variable/assigning storage (per part (1)) - PHP works it out for > you/from context, so it can 'very fast' (from a coder's point of view). > However such does leave one open to the problems described in part (2) - > which a strongly typed language would flag as an error! To this end, > some people decide to describe the usage of each variable as part of the > name. For example I was reading some Microsoft Excel macro/VBA stuff and > there's talk of intCounter and txtNameEntered - for an integer value and > the contents of a data-entry form's text box (resp). This seems like good advice, and I also try to be as descriptive as possible. Most of my variable names are at least ten characters long -- not necessarily quick to type, but then I type reasonably quickly and it's better for me to have $input_number_invalid = "Bad input" than $error (even though you rarely see a variable name that's longer than the string it represents!) > You have gone on to describe how the PHP attitude to typing enables the > creation of dynamic variables/variable names. This is a really powerful > feature, and has an important place in certain functions. However it is > not something I would put into an intro tutorial, because with such > power comes the potential to greatly expand the size of feet before > bringing them into close proximity with one's mouth! I agree wholeheartedly. And as another person on this list pointed out, arrays often will do the job much more efficiently (as there are plenty of functions that can let you do various tricks upon arrays). In this case, I needed to generate unique names for HTML form fields, so I had to use this hackish method -- I ended up with names like person1, person2, address1, address2, etc. > One of your correspondents pointed out that a really good compromise is > the associative array - the variable name (array name) stays 'constant', > but the array argument/pointer can be almost anything as a 'label'. As > another aside, it is also an 'advance' to know that the various contents > of an array in PHP do not have to be all the same datatype (as they > do/did in other languages)! Thus: > > $aPersRow[ "persId" ] = 12345; //integer > $aPersRow[ "persNm" ] = "Fred"; //string > $aPersRow[ "persDoB" ] = "1970-01-01"; //date And, having just gotten comfortable with classes and objects, I'm wondering if arrays can contain object references as well -- but I have already asked this on this list. Steve Cayford pointed out that objects work as expected for the most part with the exception of chained method calls and to be careful with passing by reference vs copy, which is useful information. > In PHP I have found it helpful to do something similar to the M$ > conventions mentioned earlier. I have set myself a set of 'naming > conventions' for PHP to help accommodate the power of dynamic typing, > whilst avoiding the potential traps. Here is a range I've grabbed from > some code I've just been working on: > > $iFrequency = 123; //is an integer > $aCounter = array( > "RowsAdded" => 0, ... ); //is an assoc array (of counters, > as described earlier) > $dBirth = "
Re: [PHP] Warning: Undefined variable
> I got back quite a few responses on this list on the subject of having > variables that are not defined before they are used. That's > interesting -- it's nice to know that I can take shortcuts and conjure > them up on the fly, but I didn't realize that it was good practice to > declare the variable before you actually use it in a script. (I never > studied programming in any formal sense, as anyone who has seen my > source code can attest :). You don't need to declare variables, simply initialize them before using them in a way that wouldn't auto-initialize. For example: $foo = $i++; In this case $foo is auto-initialized, but you are incrementing $i without explicitly initializing it to 0. This code should be: $i = 0; $foo = $i++; It is common sense really. -Rasmus -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Warning: Undefined variable
Erik, My two cents' (with allowance for inflation...) > > If you're distributing code in cyberspace it's a good > > idea to make it error free too, E_NOTICE or otherwise. To get the maximum help when coding, and to ensure the minimum of confusion for your users, I recommend: 1 set the dev server's PHP error setting to catch every single error/warning msg, and code well (?code Shell) 2 set the prod server to not report any errors except a complete disaster to the user (?catastrophe theory) If they are one and the same machine/server, then use 'debug' code to reset options at the beginning of dev code (and remove it when the routine is 'promoted' to prod). > I got back quite a few responses on this list on the subject of having > variables that are not defined before they are used. That's > interesting -- it's nice to know that I can take shortcuts and conjure > them up on the fly, but I didn't realize that it was good practice to > declare the variable before you actually use it in a script. (I never > studied programming in any formal sense, as anyone who has seen my > source code can attest :). > > Sometimes I create variables dynamically, such that I could never > declare/initialize them in any realistic sense -- for instance, I might > have a while loop that executes a number of times equal to the number of > rows pulled from a database query. And in this while loop, I might > generate a new variable (assigning it a name like $variable$i, using the > old $i++ trick to give it a unique numeric suffix). > > If I use this trick, does this doom me to never having fully-error-free > code? Or is there something that I'm not getting here... The short of > it is that, as a novice programmer, I'd like to make sure I'm writing > the most legitimate code that I can. Thanks for any input on this > thread, past or future. In strongly typed languages, I think there are (?have been historically) two objectives in defining variables prior to their use: 1 to set aside the storage space, and 2 to define the variable type 1 in the case of a scalar variable this seems trivial, eg INTEGER J Dim intJ As Integer In the case of an array or var/set length string, then replication needs to be added into the equation for calculating both an individual component's address and working out how much storage to set aside, eg INTEGER K(10) 2 Somewhat obviously a double-precision floating point number will require more space/bytes to store its value than a simple integer. However a common error made when programming is that you think a variable holds data of a certain type, when in fact you earlier used it in some other (very similar, but not quite the same) manner. eg //don't use this code $User = "Fred"; later on we do (say) a db call and pull out a row of personal data: $query = "SELECT * FROM persTbl WHERE persId = '$User' "; //I'm assuming that persId is likely some sort of personnel/membership number PHP is a 'little ripper' when it comes to quick and dirty usage, because you don't have to type a declaration line before using a variable/assigning storage (per part (1)) - PHP works it out for you/from context, so it can 'very fast' (from a coder's point of view). However such does leave one open to the problems described in part (2) - which a strongly typed language would flag as an error! To this end, some people decide to describe the usage of each variable as part of the name. For example I was reading some Microsoft Excel macro/VBA stuff and there's talk of intCounter and txtNameEntered - for an integer value and the contents of a data-entry form's text box (resp). You have gone on to describe how the PHP attitude to typing enables the creation of dynamic variables/variable names. This is a really powerful feature, and has an important place in certain functions. However it is not something I would put into an intro tutorial, because with such power comes the potential to greatly expand the size of feet before bringing them into close proximity with one's mouth! One of your correspondents pointed out that a really good compromise is the associative array - the variable name (array name) stays 'constant', but the array argument/pointer can be almost anything as a 'label'. As another aside, it is also an 'advance' to know that the various contents of an array in PHP do not have to be all the same datatype (as they do/did in other languages)! Thus: $aPersRow[ "persId" ] = 12345; //integer $aPersRow[ "persNm" ] = "Fred"; //string $aPersRow[ "persDoB" ] = "1970-01-01"; //date cf INTEGER K(10) setting aside space only for a collection of integers! One way that I have utilised this technique is to keep a set of counters for system/logging/reporting purposes in an associative array. The array element/label is self-documenting, and the ability to pass them all into a reporting sub-procedure as a single parameter/array name provides an easy cohesion/grouping! In PHP I have found it helpful to do something similar t
Re: [PHP] Warning: Undefined variable
We are confusing one another :) I'm not talking about declaring variables per se, but rather making sure the variable exists before evaluating it. So: $var = 'foobar'; if ($var == 'foobar') That's fine, no need to do declare $var before this in PHP. My words here simply mean to make sure it exists before comparing it. (let's not talk about oop) Your example of $var$i is fine, and will work without any errors (if I'm following). Btw, you should use arrays versus this method, arrays kick ass. Sure doing ${'foo'.$i} or similiar has its purpose but usually an array will do (please!). When in doubt, put error_reporting(E_ALL) on top of your script and go to town. Regards, Philip Olson On Tue, 2 Apr 2002, Erik Price wrote: > > On Tuesday, April 2, 2002, at 03:27 PM, Philip Olson wrote: > > > If you're distributing code in cyberspace it's a good > > idea to make it error free too, E_NOTICE or otherwise. > > I got back quite a few responses on this list on the subject of having > variables that are not defined before they are used. That's > interesting -- it's nice to know that I can take shortcuts and conjure > them up on the fly, but I didn't realize that it was good practice to > declare the variable before you actually use it in a script. (I never > studied programming in any formal sense, as anyone who has seen my > source code can attest :). > > Sometimes I create variables dynamically, such that I could never > declare/initialize them in any realistic sense -- for instance, I might > have a while loop that executes a number of times equal to the number of > rows pulled from a database query. And in this while loop, I might > generate a new variable (assigning it a name like $variable$i, using the > old $i++ trick to give it a unique numeric suffix). > > If I use this trick, does this doom me to never having fully-error-free > code? Or is there something that I'm not getting here... The short of > it is that, as a novice programmer, I'd like to make sure I'm writing > the most legitimate code that I can. Thanks for any input on this > thread, past or future. > > > Erik > > > > > > Erik Price > Web Developer Temp > Media Lab, H.H. Brown > [EMAIL PROTECTED] > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Warning: Undefined variable
On Tuesday, April 2, 2002, at 03:27 PM, Philip Olson wrote: > If you're distributing code in cyberspace it's a good > idea to make it error free too, E_NOTICE or otherwise. I got back quite a few responses on this list on the subject of having variables that are not defined before they are used. That's interesting -- it's nice to know that I can take shortcuts and conjure them up on the fly, but I didn't realize that it was good practice to declare the variable before you actually use it in a script. (I never studied programming in any formal sense, as anyone who has seen my source code can attest :). Sometimes I create variables dynamically, such that I could never declare/initialize them in any realistic sense -- for instance, I might have a while loop that executes a number of times equal to the number of rows pulled from a database query. And in this while loop, I might generate a new variable (assigning it a name like $variable$i, using the old $i++ trick to give it a unique numeric suffix). If I use this trick, does this doom me to never having fully-error-free code? Or is there something that I'm not getting here... The short of it is that, as a novice programmer, I'd like to make sure I'm writing the most legitimate code that I can. Thanks for any input on this thread, past or future. Erik Erik Price Web Developer Temp Media Lab, H.H. Brown [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Warning: Undefined variable
> > Good little programmers define variables before > > using them, or at least before evaluating them. > > Really? I'm not arguing with you, I'm curious: I thought that it was a > valued feature of newer scripting languages that they do not require > declaration of variables. At least that's what people say when they > praise PHP or Python or whichever language allows this. > > Is this "feature" not as desirable as what I had first heard? If you turn down error reporting then you don't have to worry about it, so it depends on you. So you're correct, it is not "required" but still it's nice. For example, if you're debugging a script you'll instantly know that you're using a undefined variable somewhere, perhaps a typo? E_NOTICE also applies to non-existent array keys, and other things. If you're distributing code in cyberspace it's a good idea to make it error free too, E_NOTICE or otherwise. Regards, Philip Olson -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: Re: [PHP] Warning: Undefined variable
PHP does give warnings about undefined array positions if you don't use quotes around them like $array1[var1] will warning (if warnings are on), but $array1["var1"] won't. I have never seen (even with error reporting turned to the maximum setting) PHP throw an error about doing something like: $robot = findRobot(); or whatever without defining "var $robot;" above this, so I'm pretty sure it's ok not to define them, and other then organizationally, it doesn't really serve a purpose since you can't define datatypes (strings, int's, or any other type can go directly into the same $robot). Adam Voigt [EMAIL PROTECTED] On Tue, 2 Apr 2002 08:30:59 -0600 , Rick Emery <[EMAIL PROTECTED]> wrote: > I've used undefined variables for over 30 years...which has caused many > late-night debugging sessions and much pain. > > Defining and initializing variables is a good thing. I believe PHP has a > flag set to warn of use of un-initialized data. > > I do prefer strongly-typed languages, such as C++. That said, I love the > power that PHP provides as far as functionality is concerned. > > just my 2 pfennigs... > > -Original Message- > From: Erik Price [mailto:[EMAIL PROTECTED]] > Sent: Tuesday, April 02, 2002 8:26 AM > To: Philip Olson > Cc: kip; [EMAIL PROTECTED] > Subject: Re: [PHP] Warning: Undefined variable > > > > On Monday, April 1, 2002, at 11:15 PM, Philip Olson wrote: > > > Good little programmers define variables before > > using them, or at least before evaluating them. > > Really? I'm not arguing with you, I'm curious: I thought that it was a > valued feature of newer scripting languages that they do not require > declaration of variables. At least that's what people say when they > praise PHP or Python or whichever language allows this. > > Is this "feature" not as desirable as what I had first heard? > > > Erik > > > > > > > > Erik Price > Web Developer Temp > Media Lab, H.H. Brown > [EMAIL PROTECTED] > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Warning: Undefined variable
I've used undefined variables for over 30 years...which has caused many late-night debugging sessions and much pain. Defining and initializing variables is a good thing. I believe PHP has a flag set to warn of use of un-initialized data. I do prefer strongly-typed languages, such as C++. That said, I love the power that PHP provides as far as functionality is concerned. just my 2 pfennigs... -Original Message- From: Erik Price [mailto:[EMAIL PROTECTED]] Sent: Tuesday, April 02, 2002 8:26 AM To: Philip Olson Cc: kip; [EMAIL PROTECTED] Subject: Re: [PHP] Warning: Undefined variable On Monday, April 1, 2002, at 11:15 PM, Philip Olson wrote: > Good little programmers define variables before > using them, or at least before evaluating them. Really? I'm not arguing with you, I'm curious: I thought that it was a valued feature of newer scripting languages that they do not require declaration of variables. At least that's what people say when they praise PHP or Python or whichever language allows this. Is this "feature" not as desirable as what I had first heard? Erik Erik Price Web Developer Temp Media Lab, H.H. Brown [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Warning: Undefined variable
On Monday, April 1, 2002, at 11:15 PM, Philip Olson wrote: > Good little programmers define variables before > using them, or at least before evaluating them. Really? I'm not arguing with you, I'm curious: I thought that it was a valued feature of newer scripting languages that they do not require declaration of variables. At least that's what people say when they praise PHP or Python or whichever language allows this. Is this "feature" not as desirable as what I had first heard? Erik Erik Price Web Developer Temp Media Lab, H.H. Brown [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Warning: Undefined variable
Good little programmers define variables before using them, or at least before evaluating them. print $undefined; // E_NOTICE error The most common use of this is: if ($submit) { To check if a form submit button named submit has been submitted. If not, this does indeed evaluate to false BUT also an error of level E_NOTICE is created. So, one might do the following, which will never give E_NOTICE: if (isset($submit)) { At any rate, the reason is your error levels are different. Look up the error_reporting directive: http://www.php.net/manual/en/configuration.php#ini.error-reporting A function also controls this behavior within scripts, it's appropriatly named error_reporting() http://www.php.net/error_reporting So, start programming your code with error_reporting(E_ALL) and have fun! Regards, Philip Olson p.s. Although it's fairly common to create E_NOTICE all over the place, it's not a good idea. More and more people are learning to not do that. On Tue, 2 Apr 2002, kip wrote: > Hi, > > As i know we don't need to define a variable before in PHP. But i have a > very strange case. In my server, i hosted a PHP website. When i update the > server from PHP3 to PHP4, problems came out. Most of the php pages didn't > work and display many warnings like that : > "Warning: Undefined variable: variable_name". In fact, nobody has changed > the code and all of the pages are working property in PHP3 server. So, does > anyone know what happen? I also contact the hosting company, but they said > that everything is working fine and they don't know what problem is it. > Thats why i have no idea how to correct it. > > Thanks. > Kenny > > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Warning: Undefined variable
maybe the reporting of warning is turn off for the php3 installation, but turned on for the php4 installation. -Original Message- From: kip [mailto:[EMAIL PROTECTED]] Sent: Tuesday, April 02, 2002 12:21 PM To: [EMAIL PROTECTED] Subject: [PHP] Warning: Undefined variable Hi, As i know we don't need to define a variable before in PHP. But i have a very strange case. In my server, i hosted a PHP website. When i update the server from PHP3 to PHP4, problems came out. Most of the php pages didn't work and display many warnings like that : "Warning: Undefined variable: variable_name". In fact, nobody has changed the code and all of the pages are working property in PHP3 server. So, does anyone know what happen? I also contact the hosting company, but they said that everything is working fine and they don't know what problem is it. Thats why i have no idea how to correct it. Thanks. Kenny -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] "Warning: Undefined variable" when used locally, but fineon remote server
This has to do with error_reporting level setting set in php.ini echo $iamnotset; when E_NOTICE level is on, that will produce a Warning. Otherwise, it will not. I posted something similiar recently, see it here: http://marc.theaimsgroup.com/?l=php-general&m=100083224311516 On uklinux, either "fix" the code (yeah!) or use the error_reporting() function to suppress the warnings. Strange that a free host would have it on, actually, it's kinda cool :) regards, Philip Olson On Mon, 24 Sep 2001, Justin Colson wrote: > I have recently installed Apache 1.3.2 and PHP 4.0.6 using the instructions > at http://hotwired.lycos.com/webmonkey/00/44/index4a.html?tw=programming on > a Win98 machine which I will later use as an intranet server (NT workstation > refuses to detect NT server, and the machine is too slow for Win2k). When I > run scripts on this instalation that work fine on my free webspace at > uklinux.net, they return "Warning: Undefined variable: sub" at every > occurance of my menu system which uses sub as it its main vairable, if i > specify the variable in the URL that partiular page will work, but only of > the links > > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > To contact the list administrators, e-mail: [EMAIL PROTECTED] > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]