RE: [PHP] help with undefined variables, please
At 21:57 14/05/2002 +0200, Luc Saint-Elie wrote: > > 4.1 => isset() returns false if the variable is not set OR if the > variable is empty typo.. wanted to say : > 4.1 isset() returns true if the variable is set ORT if the variable is empty Luc -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] help with undefined variables, please
At 12:49 14/05/2002 -0600, Matthew Walker wrote: >I prefer to use isset(), not empty(). It's more accurate, in that it >knows the difference between a variable that just isn't set, and one >that has an empty value (which may be valid). hello, You may want to do some tests. last time i did that : > 4.1 => isset() returns false if the variable is not set OR if the variable is empty So for example For php > 4.1 foo is ALWAYS set (it can be empty if no value is typed, but it exists so it is set) If you want to know is the user typed something you must use empty() Luc -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] help with undefined variables, please
I prefer to use isset(), not empty(). It's more accurate, in that it knows the difference between a variable that just isn't set, and one that has an empty value (which may be valid). Matthew Walker Senior Software Engineer ePliant Marketing -Original Message- From: Philip Olson [mailto:[EMAIL PROTECTED]] Sent: Tuesday, May 14, 2002 12:04 PM To: The Gabster Cc: [EMAIL PROTECTED] Subject: Re: [PHP] help with undefined variables, please > PHP Notice: Undefined variable: action in > C:\Apache\htdocs\easyletter2\easyletter.php on line 73 > > Why do I get an undefined variable error for $action, $pw, $disp, > $found when they do not have to be declared in the script? > if ($action=="sign"){ At this point, $action was not set. At some point in your script you may want to check if $action exists at all and if not, assign it to boolean false (just one of a million things you can do). Like, on top of the script: // Initialize various variables if they are empty $vars_to_init = array('action','pw','disp','found'); foreach ($vars_to_init as $var) { if (empty(${$var})) { ${$var} = false; } } That'll check them, and set empty vars to false. Seems kinda weird, it is ;) Undefined variables are level E_NOTICE errors in PHP. Some people find these a bit annoying and turn them off, most prefer to eliminate them. When you said variables don't have to be declared in PHP, you are right in that they are just E_NOTICE level errors, not Fatal E_ERROR. By default, error_reporting levels do not show E_NOTICE but good little programmers attend to them. A quick and dirty way to magically get rid of them is to turn down error_reporting in php.ini or at runtime. error_reporting is both a PHP directive and a PHP function. There is also '@' available to suppress an error, it works on both functions and variables. See: http://www.php.net/manual/language.operators.errorcontrol.php What is done of course depends on the situation and the person. Regards, Philip Olson -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php --- Outgoing mail is certified Virus Free. Checked by AVG anti-virus system (http://www.grisoft.com). Version: 6.0.351 / Virus Database: 197 - Release Date: 4/19/2002 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] help with undefined variables, please
> PHP Notice: Undefined variable: action in > C:\Apache\htdocs\easyletter2\easyletter.php on line 73 > > Why do I get an undefined variable error for $action, $pw, $disp, > $found when they do not have to be declared in the script? > if ($action=="sign"){ At this point, $action was not set. At some point in your script you may want to check if $action exists at all and if not, assign it to boolean false (just one of a million things you can do). Like, on top of the script: // Initialize various variables if they are empty $vars_to_init = array('action','pw','disp','found'); foreach ($vars_to_init as $var) { if (empty(${$var})) { ${$var} = false; } } That'll check them, and set empty vars to false. Seems kinda weird, it is ;) Undefined variables are level E_NOTICE errors in PHP. Some people find these a bit annoying and turn them off, most prefer to eliminate them. When you said variables don't have to be declared in PHP, you are right in that they are just E_NOTICE level errors, not Fatal E_ERROR. By default, error_reporting levels do not show E_NOTICE but good little programmers attend to them. A quick and dirty way to magically get rid of them is to turn down error_reporting in php.ini or at runtime. error_reporting is both a PHP directive and a PHP function. There is also '@' available to suppress an error, it works on both functions and variables. See: http://www.php.net/manual/language.operators.errorcontrol.php What is done of course depends on the situation and the person. Regards, Philip Olson -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] help with undefined variables, please
thanks a lot gabi "Jerome Houston" <[EMAIL PROTECTED]> wrote in message [EMAIL PROTECTED]">news:[EMAIL PROTECTED]... > hi- > > it is not REQUIRED for variables to be initialized in PHP, but if your error > reporting is on too high, it will tell you, just to make sure you know. to > turn your error reporting to a different level, you can change your php.ini: > > http://www.php.net/manual/en/configuration.php#ini.error-reporting > > or just change it for this script > > http://www.php.net/manual/en/function.error-reporting.php > > hope this helps, > > jerome > > >From: "The Gabster" <[EMAIL PROTECTED]> > >To: [EMAIL PROTECTED] > >Subject: [PHP] help with undefined variables, please > >Date: Tue, 14 May 2002 12:21:45 -0500 > > > >Hello all! > > > >I have a newsletter script that works fine but I receive some undefined > >variables errors in Apache's error log: > > > >PHP Notice: Undefined variable: action in > >C:\Apache\htdocs\easyletter2\easyletter.php on line 73 > >PHP Notice: Undefined variable: action in > >C:\Apache\htdocs\easyletter2\easyletter.php on line 100 > >PHP Notice: Undefined variable: pw in > >C:\Apache\htdocs\easyletter2\easyletter.php on line 123 > >PHP Notice: Undefined variable: pw in > >C:\Apache\htdocs\easyletter2\easyletter.php on line 159 > >PHP Notice: Undefined variable: disp in > >C:\Apache\htdocs\easyletter2\easyletter.php on line 159 > >PHP Notice: Undefined variable: found in > >C:\Apache\htdocs\easyletter2\easyletter.php on line 75 > >PHP Notice: Undefined variable: pw in > >C:\Apache\htdocs\easyletter2\easyletter.php on line 123 > >PHP Notice: Undefined variable: pw in > >C:\Apache\htdocs\easyletter2\easyletter.php on line 159 > > > >Why do I get an undefined variable error for $action, $pw, $disp, $found > >when they do not have to be declared in the script? > > > >And the script is: > > > > ># Version 1.1 Debugged, please upgrade older ones... > > > > > > > ># Global Settings... > > > > > > > ># Set the password for the eMail List editor! > > > >$pass="secret"; > > > ># Name of the datafile > > > >$filelocation="subscribers.txt"; > > > ># Title of the newsletter, will be displayed in the FROM field of the > >mailclient > > > >$lettername="Test"; > > > ># Your email, will be the reply-to mail > > > >$youremail="[EMAIL PROTECTED]"; > > > > > > > ># Displayed Messages > > > > > > > ># Welcome message displayed above the form for subscribing/unsubscribing > > > >$welcomemessage = "This is the place to subscribe and unsubscribe for the > >Onlinetools.org Newsletter. I will be sending this letter out every time > >there are some new tools or changes to the page. Your data will not be > >given > >to anyone and I send out small text mails only."; > > > ># Sorrymessage for failed subscription, will be followed by the email! > > > >$sorrysignmessage = "Sorry, there is already an entry for "; > > > ># Subscribe message, will be displayed when subscribing > > > >$subscribemessage = "Thank you for subscribing to the newsletter, a > >confirmation email is on its way!"; > > > ># Subscribemail, will be sent when someone subscribes. > > > >$subscribemail = "Thank you for subscribing to the newsletter."; > > > ># Unsubscribemessage for deletion, will be followed by the email! > > > >$unsubscribemessage = "We deleted the following entry: "; > > > ># Unsubscribemessage for failed deletion, will be followed by the email! > > > >$failedunsubscriptionmessage = "Sorry, you cannot unssubscribe as we didn't > >find an entry for "; > > > > > > > ># Let the code begin... > > > > > > > ># Checks if the file exists, if not creates a new one > > > >if (!file_exists($filelocation)) { > > > >$newfile = fopen($filelocation,"w+"); > > > >fclose($newfile); > > > >} > > > ># Open the datafile and read the content > > > >$newfile = fopen($filelocation,"r"); > > > >$content = fread($newfile, filesize($filelocation)); > > > >fclose($newfile); > > > ># Remove the slashes PHP automatically puts before special characters > > > >$content=stripslashes($content); > > > ># Reset the output of the "search result" > > > >$out=""; > > > ># Put the entries into the array lines > > > >$lines = explode("%",$content); > > > >for ($key=1;$key > > ># when the email is not in the list, add the old entries > > > >if ($lines[$key] != $email){ > > > >$out .= "%".$lines[$key]; > > > >} > > > ># when it's already in the list, set found > > > >else { > > > >$found=1; > > > >} > > > >} > > > > > > > ># Signing in > >
Re: [PHP] help with undefined variables, please
hi- it is not REQUIRED for variables to be initialized in PHP, but if your error reporting is on too high, it will tell you, just to make sure you know. to turn your error reporting to a different level, you can change your php.ini: http://www.php.net/manual/en/configuration.php#ini.error-reporting or just change it for this script http://www.php.net/manual/en/function.error-reporting.php hope this helps, jerome >From: "The Gabster" <[EMAIL PROTECTED]> >To: [EMAIL PROTECTED] >Subject: [PHP] help with undefined variables, please >Date: Tue, 14 May 2002 12:21:45 -0500 > >Hello all! > >I have a newsletter script that works fine but I receive some undefined >variables errors in Apache's error log: > >PHP Notice: Undefined variable: action in >C:\Apache\htdocs\easyletter2\easyletter.php on line 73 >PHP Notice: Undefined variable: action in >C:\Apache\htdocs\easyletter2\easyletter.php on line 100 >PHP Notice: Undefined variable: pw in >C:\Apache\htdocs\easyletter2\easyletter.php on line 123 >PHP Notice: Undefined variable: pw in >C:\Apache\htdocs\easyletter2\easyletter.php on line 159 >PHP Notice: Undefined variable: disp in >C:\Apache\htdocs\easyletter2\easyletter.php on line 159 >PHP Notice: Undefined variable: found in >C:\Apache\htdocs\easyletter2\easyletter.php on line 75 >PHP Notice: Undefined variable: pw in >C:\Apache\htdocs\easyletter2\easyletter.php on line 123 >PHP Notice: Undefined variable: pw in >C:\Apache\htdocs\easyletter2\easyletter.php on line 159 > >Why do I get an undefined variable error for $action, $pw, $disp, $found >when they do not have to be declared in the script? > >And the script is: > ># Version 1.1 Debugged, please upgrade older ones... > > > ># Global Settings... > > > ># Set the password for the eMail List editor! > >$pass="secret"; > ># Name of the datafile > >$filelocation="subscribers.txt"; > ># Title of the newsletter, will be displayed in the FROM field of the >mailclient > >$lettername="Test"; > ># Your email, will be the reply-to mail > >$youremail="[EMAIL PROTECTED]"; > > > ># Displayed Messages > > > ># Welcome message displayed above the form for subscribing/unsubscribing > >$welcomemessage = "This is the place to subscribe and unsubscribe for the >Onlinetools.org Newsletter. I will be sending this letter out every time >there are some new tools or changes to the page. Your data will not be >given >to anyone and I send out small text mails only."; > ># Sorrymessage for failed subscription, will be followed by the email! > >$sorrysignmessage = "Sorry, there is already an entry for "; > ># Subscribe message, will be displayed when subscribing > >$subscribemessage = "Thank you for subscribing to the newsletter, a >confirmation email is on its way!"; > ># Subscribemail, will be sent when someone subscribes. > >$subscribemail = "Thank you for subscribing to the newsletter."; > ># Unsubscribemessage for deletion, will be followed by the email! > >$unsubscribemessage = "We deleted the following entry: "; > ># Unsubscribemessage for failed deletion, will be followed by the email! > >$failedunsubscriptionmessage = "Sorry, you cannot unssubscribe as we didn't >find an entry for "; > > > ># Let the code begin... > > > ># Checks if the file exists, if not creates a new one > >if (!file_exists($filelocation)) { > >$newfile = fopen($filelocation,"w+"); > >fclose($newfile); > >} > ># Open the datafile and read the content > >$newfile = fopen($filelocation,"r"); > >$content = fread($newfile, filesize($filelocation)); > >fclose($newfile); > ># Remove the slashes PHP automatically puts before special characters > >$content=stripslashes($content); > ># Reset the output of the "search result" > >$out=""; > ># Put the entries into the array lines > >$lines = explode("%",$content); > >for ($key=1;$key ># when the email is not in the list, add the old entries > >if ($lines[$key] != $email){ > >$out .= "%".$lines[$key]; > >} > ># when it's already in the list, set found > >else { > >$found=1; > >} > >} > > > ># Signing in > > > >if ($action=="sign"){ > ># When there is already a subscription for this email *duh* > >if ($found==1){ > ># Display Sorrymessage > >echo "align=\"center\">".$sorrysignmessage.$email.""; > >$disp="yes"; > >} > ># otherwise, add the email to the list > >else { > >$disp="no"; > >$newfile = fopen($filelocation,"a+"); > >$add = "%".$email; > >fwrite($newfile, $add); > >fclose($ne