php-general Digest 27 Aug 2006 13:36:15 -0000 Issue 4315

Topics (messages 241128 through 241141):

Re: How to deal with errors in forms
        241128 by: Rene Brehmer

Re: Brain Death - [PHP] functions classes
        241129 by: Robert Cummings
        241130 by: Robert Cummings
        241131 by: Alex Turner
        241132 by: Alex Turner
        241133 by: tedd
        241134 by: Robert Cummings
        241135 by: tedd
        241136 by: Robert Cummings
        241137 by: Alex Turner
        241138 by: Robert Cummings
        241140 by: tedd

Re: Recommendations for PHP debuggers?
        241139 by: Larry Garfield

Email with pregmatch
        241141 by: Peter Lauri

Administrivia:

To subscribe to the digest, e-mail:
        [EMAIL PROTECTED]

To unsubscribe from the digest, e-mail:
        [EMAIL PROTECTED]

To post to the list, e-mail:
        [email protected]


----------------------------------------------------------------------
--- Begin Message ---
Documented research indicate that on Fri, 25 Aug 2006 13:18:25 +0200, "Ivo
F.A.C. Fokkema" wrote:

> 
> You might also try to process the results from the form first, and then,
> if errors are found, display the form again and put the data in there
> yourself. No need to send the user back and forth. But you may need to
> restructure your code a little. I personally always use this method.
> 
> 1) Check if form is sent.
> 1a) True? Check form contents. Put errors in a variable. If there are no
> errors, do whatever you need to do.
> 1b) False? Set all form fields to the default values (probably empty strings).
> 
> 2) Check if error variable exists.
> 2a) True? Print error variable on the screen.
> 
> 3) Print form, and load values in them.

The above method is basically what I use with great success. I've simply
added my own alerthandler functions, meaning the form checking part sets a
variable called $alert_level to a value between 0 and 3, 0 = no errors, 1 =
notice, 2 = warning, and 3 = error, and then the handler itself checks what
kind of alert to put out and displays a box coloured to match the alert
level. The main reason for doing it like that is because I wanted something
simple that I could reuse across all my pages, so alert messages look
similar - people pay more attention to them if all your pages use the same
method of notifying them of problems and errors.
The alert_handler uses a second variable, $alert_message, that the form
checker uses to tell exactly where the problem is, to avoid one of the
"There was an error in your input" situations.

On top of that, I'd like to suggest (or actually recommend) using POST
instead of GET, especially when you use 2000 char fields. PHP doesn't care
either way, but if you use POST you won't have the problem of field
contents being cut off because they won't fit in the URL. And you can keep
all your variable names the same, or even make them longer and easier to
remember perhaps.


Rene

--- End Message ---
--- Begin Message ---
On Sat, 2006-08-26 at 12:49 +0100, Alex Turner wrote:
> I don't know what I was on when I wrote the previous post!
> 
> In php you cannot create static class variables in this way (doh) or at 
> least I never have managed.  So when faced the this problem I replace 
> what in C++ would be a class variable with a class function
> 
> comme ca:
> 
> class MyClass
> {
>      function MyVar($val = null)
>      {
>       static $datum;
>       if(is_null($val))
>       {
>               return $datum;
>       }
>       $dataum=$val;
>      }
> 
> }
> 
> I definitely need more coffee!

Talking about coffee... your above code could use some. Try this:

<?php

class MyClass
{
    function MyVar( $val=null )
    {
        static $datum;

        if( $val === null )
        {
            return $datum;
        }

        $datum = $val;
     }
}

?>

But also I'd recommend fixing the the problem whereby you
can't set $datum to the null value, otherwise you may run
into unexpected issues down the road.

<?php

class MyClass
{
    function MyVar( $val=null, $set=false )
    {
        static $datum;

        if( $set === false )
        {
            return $datum;
        }

        $datum = $val;
     }
}

?>

Cheers,
Rob.
-- 
.------------------------------------------------------------.
| InterJinn Application Framework - http://www.interjinn.com |
:------------------------------------------------------------:
| An application and templating framework for PHP. Boasting  |
| a powerful, scalable system for accessing system services  |
| such as forms, properties, sessions, and caches. InterJinn |
| also provides an extremely flexible architecture for       |
| creating re-usable components quickly and easily.          |
`------------------------------------------------------------'

--- End Message ---
--- Begin Message ---
On Sat, 2006-08-26 at 16:51 +0100, Alex Turner wrote:
> Rob,
> 
> I'd go along with the setting a var to null issue (in the cases
> I have worked so far on, there has not been a need to set variables
> to null).


Maybe so, but if a variable ever happens to contain null and you're not
aware of it, the value won't get updated.

> However, what is wrong with is_null()?

As a function call it's an order of magnitude slower than === null since
it incurs the overhead for a function call. There's nothing wrong with
your use of the is_null() function, but === null is just as clear, and
much faster so I thought I'd throw at you in line with your coffee
comment :)

Cheers,
Rob.
-- 
.------------------------------------------------------------.
| InterJinn Application Framework - http://www.interjinn.com |
:------------------------------------------------------------:
| An application and templating framework for PHP. Boasting  |
| a powerful, scalable system for accessing system services  |
| such as forms, properties, sessions, and caches. InterJinn |
| also provides an extremely flexible architecture for       |
| creating re-usable components quickly and easily.          |
`------------------------------------------------------------'

--- End Message ---
--- Begin Message ---
Cool!  That is a very good point - I'll remember to use '=== null' in future.  
Then at least my code will have enough coffee even if I don't!

Thanks - AJ

Alexander J Turner Ph.D.
www.deployview.com
www.nerds-central.blogspot.com
www.project-network.com

-----Original Message-----
From: Robert Cummings [mailto:[EMAIL PROTECTED] 
Sent: 26 August 2006 17:25
To: Alex Turner
Cc: [email protected]
Subject: RE: [PHP] Brain Death - [PHP] functions classes

On Sat, 2006-08-26 at 16:51 +0100, Alex Turner wrote:
> Rob,
> 
> I'd go along with the setting a var to null issue (in the cases
> I have worked so far on, there has not been a need to set variables
> to null).


Maybe so, but if a variable ever happens to contain null and you're not
aware of it, the value won't get updated.

> However, what is wrong with is_null()?

As a function call it's an order of magnitude slower than === null since
it incurs the overhead for a function call. There's nothing wrong with
your use of the is_null() function, but === null is just as clear, and
much faster so I thought I'd throw at you in line with your coffee
comment :)

Cheers,
Rob.
-- 
.------------------------------------------------------------.
| InterJinn Application Framework - http://www.interjinn.com |
:------------------------------------------------------------:
| An application and templating framework for PHP. Boasting  |
| a powerful, scalable system for accessing system services  |
| such as forms, properties, sessions, and caches. InterJinn |
| also provides an extremely flexible architecture for       |
| creating re-usable components quickly and easily.          |
`------------------------------------------------------------'


-- 
No virus found in this incoming message.
Checked by AVG Free Edition.
Version: 7.1.405 / Virus Database: 268.11.6/427 - Release Date: 24/08/2006
 

-- 
No virus found in this outgoing message.
Checked by AVG Free Edition.
Version: 7.1.405 / Virus Database: 268.11.6/427 - Release Date: 24/08/2006
 

--- End Message ---
--- Begin Message ---
Rob,

I'd go along with the setting a var to null issue (in the cases I have worked 
so far on, there has not been a need to set variables to null).  However, what 
is wrong with is_null()?

>From the php manual chm:
>>>>>
(PHP 4 >= 4.0.4, PHP 5)

is_null --  Finds whether a variable is NULL 
Description
bool is_null ( mixed var )

Finds whether the given variable is NULL. 

Parameters

var
The variable being evaluated. 

Return Values
Returns TRUE if var is null, FALSE otherwise.
<<<<<

And to be very pedantic - as null does not have a type then actually 'x === 
null' should evaluate to absurdity, but PHP is more pragmatic than that ;-)

Cheers - AJ

Alexander J Turner Ph.D.
www.deployview.com
www.nerds-central.blogspot.com
www.project-network.com

-----Original Message-----
From: Robert Cummings [mailto:[EMAIL PROTECTED] 
Sent: 26 August 2006 16:42
To: Alex Turner
Cc: [email protected]
Subject: Re: [PHP] Brain Death - [PHP] functions classes

On Sat, 2006-08-26 at 12:49 +0100, Alex Turner wrote:
> I don't know what I was on when I wrote the previous post!
> 
> In php you cannot create static class variables in this way (doh) or at 
> least I never have managed.  So when faced the this problem I replace 
> what in C++ would be a class variable with a class function
> 
> comme ca:
> 
> class MyClass
> {
>      function MyVar($val = null)
>      {
>       static $datum;
>       if(is_null($val))
>       {
>               return $datum;
>       }
>       $dataum=$val;
>      }
> 
> }
> 
> I definitely need more coffee!

Talking about coffee... your above code could use some. Try this:

<?php

class MyClass
{
    function MyVar( $val=null )
    {
        static $datum;

        if( $val === null )
        {
            return $datum;
        }

        $datum = $val;
     }
}

?>

But also I'd recommend fixing the the problem whereby you
can't set $datum to the null value, otherwise you may run
into unexpected issues down the road.

<?php

class MyClass
{
    function MyVar( $val=null, $set=false )
    {
        static $datum;

        if( $set === false )
        {
            return $datum;
        }

        $datum = $val;
     }
}

?>

Cheers,
Rob.
-- 
.------------------------------------------------------------.
| InterJinn Application Framework - http://www.interjinn.com |
:------------------------------------------------------------:
| An application and templating framework for PHP. Boasting  |
| a powerful, scalable system for accessing system services  |
| such as forms, properties, sessions, and caches. InterJinn |
| also provides an extremely flexible architecture for       |
| creating re-usable components quickly and easily.          |
`------------------------------------------------------------'


-- 
No virus found in this incoming message.
Checked by AVG Free Edition.
Version: 7.1.405 / Virus Database: 268.11.6/427 - Release Date: 24/08/2006
 

-- 
No virus found in this outgoing message.
Checked by AVG Free Edition.
Version: 7.1.405 / Virus Database: 268.11.6/427 - Release Date: 24/08/2006
 

--- End Message ---
--- Begin Message ---
At 4:51 PM +0100 8/26/06, Alex Turner wrote:
And to be very pedantic - as null does not have a type then actually 'x === null' should evaluate to absurdity, but PHP is more pragmatic than that ;-)

That's a very good point.

While one NULL variable in php can be compared to another NULL variable and produce true, it's not so in MySQL.

In MySQL NULL does not equal NULL -- such comparisons produces NULL and not true. Instead you have to use IS_NULL or NOT NULL to check for NULL.

So, it's probably best to get into the habit of using is_null.

tedd

--
-------
http://sperling.com  http://ancientstones.com  http://earthstones.com

--- End Message ---
--- Begin Message ---
On Sat, 2006-08-26 at 12:41 -0400, tedd wrote:
> At 4:51 PM +0100 8/26/06, Alex Turner wrote:
> >And to be very pedantic - as null does not have a type then actually 
> >'x === null' should evaluate to absurdity, but PHP is more pragmatic 
> >than that ;-)
> 
> That's a very good point.
> 
> While one NULL variable in php can be compared to another NULL 
> variable and produce true, it's not so in MySQL.
> 
> In MySQL NULL does not equal NULL -- such comparisons produces NULL 
> and not true. Instead you have to use IS_NULL or NOT NULL to check 
> for NULL.
> 
> So, it's probably best to get into the habit of using is_null.

I strongly disagree... If I'm writing SQL then I adhere to the language
constructs of SQL, if I'm coding in PHP then I adhere to the language
constructs of PHP. There is no reason why one should forgo better
constructs just because they don't appear in other languages. When in
Rome... Oh btw, PHP draws strongly from C, and in C NULL == NULL, and
last I checked C predates SQL.

Cheers,
Rob.
-- 
.------------------------------------------------------------.
| InterJinn Application Framework - http://www.interjinn.com |
:------------------------------------------------------------:
| An application and templating framework for PHP. Boasting  |
| a powerful, scalable system for accessing system services  |
| such as forms, properties, sessions, and caches. InterJinn |
| also provides an extremely flexible architecture for       |
| creating re-usable components quickly and easily.          |
`------------------------------------------------------------'

--- End Message ---
--- Begin Message ---
At 2:30 PM -0400 8/26/06, Robert Cummings wrote:
On Sat, 2006-08-26 at 12:41 -0400, tedd wrote:
 > At 4:51 PM +0100 8/26/06, Alex Turner wrote:
 >And to be very pedantic - as null does not have a type then actually
 > >'x === null' should evaluate to absurdity, but PHP is more pragmatic
 >than that ;-)

 That's a very good point.

 While one NULL variable in php can be compared to another NULL
 variable and produce true, it's not so in MySQL.

 In MySQL NULL does not equal NULL -- such comparisons produces NULL
 and not true. Instead you have to use IS_NULL or NOT NULL to check
 for NULL.

 So, it's probably best to get into the habit of using is_null.

I strongly disagree... If I'm writing SQL then I adhere to the language
constructs of SQL, if I'm coding in PHP then I adhere to the language
constructs of PHP. There is no reason why one should forgo better
constructs just because they don't appear in other languages. When in
Rome... Oh btw, PHP draws strongly from C, and in C NULL == NULL, and
last I checked C predates SQL.

Cheers,
Rob.

Rob:

As the old woman who kissed the cow said "To each their own."

My reasoning is simple and I don't strongly agree, or disagree, with other methodologies. I write in several languages, such as php, js, mySQL, css, and others -- and each have their own constructs. As such, I try to use similar constructs where ever possible. My memory isn't what it used to be.

Besides, what Alex Turner said about NULL is correct -- it's absurd to have NULL evaluate to anything. Just because one language allows absurdity doesn't mean you have to practice it.

Your mileage may differ and that's Okay.  :-)

tedd

--
-------
http://sperling.com  http://ancientstones.com  http://earthstones.com

--- End Message ---
--- Begin Message ---
On Sat, 2006-08-26 at 14:23 -0400, tedd wrote:
> At 2:30 PM -0400 8/26/06, Robert Cummings wrote:
> >On Sat, 2006-08-26 at 12:41 -0400, tedd wrote:
> >  > At 4:51 PM +0100 8/26/06, Alex Turner wrote:
> >>  >And to be very pedantic - as null does not have a type then actually
> >  > >'x === null' should evaluate to absurdity, but PHP is more pragmatic
> >>  >than that ;-)
> >>
> >>  That's a very good point.
> >>
> >>  While one NULL variable in php can be compared to another NULL
> >>  variable and produce true, it's not so in MySQL.
> >>
> >>  In MySQL NULL does not equal NULL -- such comparisons produces NULL
> >>  and not true. Instead you have to use IS_NULL or NOT NULL to check
> >>  for NULL.
> >>
> >>  So, it's probably best to get into the habit of using is_null.
> >
> >I strongly disagree... If I'm writing SQL then I adhere to the language
> >constructs of SQL, if I'm coding in PHP then I adhere to the language
> >constructs of PHP. There is no reason why one should forgo better
> >constructs just because they don't appear in other languages. When in
> >Rome... Oh btw, PHP draws strongly from C, and in C NULL == NULL, and
> >last I checked C predates SQL.
> >
> >Cheers,
> >Rob.
> 
> Rob:
> 
> As the old woman who kissed the cow said "To each their own."
> 
> My reasoning is simple and I don't strongly agree, or disagree, with 
> other methodologies. I write in several languages, such as php, js, 
> mySQL, css, and others -- and each have their own constructs. As 
> such, I try to use similar constructs where ever possible. My memory 
> isn't what it used to be.
> 
> Besides, what Alex Turner said about NULL is correct -- it's absurd 
> to have NULL evaluate to anything. Just because one language allows 
> absurdity doesn't mean you have to practice it.

One language? I know for certain that PHP, C, PERL, and JavaScript all
return true for comparisons of null to itself. I know quite a few
languages too... though I'm not sure why you threw CSS in there since
the concept of null doesn't apply.

> Your mileage may differ and that's Okay.  :-)

As always :)

Cheers,
Rob.
-- 
.------------------------------------------------------------.
| InterJinn Application Framework - http://www.interjinn.com |
:------------------------------------------------------------:
| An application and templating framework for PHP. Boasting  |
| a powerful, scalable system for accessing system services  |
| such as forms, properties, sessions, and caches. InterJinn |
| also provides an extremely flexible architecture for       |
| creating re-usable components quickly and easily.          |
`------------------------------------------------------------'

--- End Message ---
--- Begin Message ---
Right or wrong - C does not actually have NULL :-)  NULL in C is simply mapped 
to zero.  It is useful in logic to sometimes use mapping of logical sentence 
letters to numbers and use mathematics to perform proof, but that is a special 
case.  

In languages like C, there is no special method of marking a variable as true 
NULL.  Thus, the language has no way of processing three state logic.  In SQL, 
there is a special flag to note that variables are truly null and so it can 
process three state logic.

PHP is odd because it does have a special NULL state but it does not fully 
support three state logic.  But hey - it is a web scripting language - it would 
be a shame if PHP got all theoretical on us!

BTW:
>Rome... Oh btw, PHP draws strongly from C, and in C NULL == NULL, and

NULL == NULL === TRUE

It is

NULL === NULL == absurd.

So - my approach:

I may well use is_null() in low traffic areas on PHP, but then when in an inner 
loop etc, use === null and maybe comment what it means.

Cheers to all

AJ


Alexander J Turner Ph.D.
www.deployview.com
www.nerds-central.blogspot.com
www.project-network.com

-----Original Message-----
From: tedd [mailto:[EMAIL PROTECTED] 
Sent: 26 August 2006 19:24
To: Robert Cummings; tedd
Cc: Alex Turner; [email protected]
Subject: RE: [PHP] Brain Death - [PHP] functions classes

At 2:30 PM -0400 8/26/06, Robert Cummings wrote:
>On Sat, 2006-08-26 at 12:41 -0400, tedd wrote:
>  > At 4:51 PM +0100 8/26/06, Alex Turner wrote:
>>  >And to be very pedantic - as null does not have a type then actually
>  > >'x === null' should evaluate to absurdity, but PHP is more pragmatic
>>  >than that ;-)
>>
>>  That's a very good point.
>>
>>  While one NULL variable in php can be compared to another NULL
>>  variable and produce true, it's not so in MySQL.
>>
>>  In MySQL NULL does not equal NULL -- such comparisons produces NULL
>>  and not true. Instead you have to use IS_NULL or NOT NULL to check
>>  for NULL.
>>
>>  So, it's probably best to get into the habit of using is_null.
>
>I strongly disagree... If I'm writing SQL then I adhere to the language
>constructs of SQL, if I'm coding in PHP then I adhere to the language
>constructs of PHP. There is no reason why one should forgo better
>constructs just because they don't appear in other languages. When in
>Rome... Oh btw, PHP draws strongly from C, and in C NULL == NULL, and
>last I checked C predates SQL.
>
>Cheers,
>Rob.

Rob:

As the old woman who kissed the cow said "To each their own."

My reasoning is simple and I don't strongly agree, or disagree, with 
other methodologies. I write in several languages, such as php, js, 
mySQL, css, and others -- and each have their own constructs. As 
such, I try to use similar constructs where ever possible. My memory 
isn't what it used to be.

Besides, what Alex Turner said about NULL is correct -- it's absurd 
to have NULL evaluate to anything. Just because one language allows 
absurdity doesn't mean you have to practice it.

Your mileage may differ and that's Okay.  :-)

tedd

-- 
-------
http://sperling.com  http://ancientstones.com  http://earthstones.com

-- 
No virus found in this incoming message.
Checked by AVG Free Edition.
Version: 7.1.405 / Virus Database: 268.11.6/427 - Release Date: 24/08/2006
 

-- 
No virus found in this outgoing message.
Checked by AVG Free Edition.
Version: 7.1.405 / Virus Database: 268.11.6/427 - Release Date: 24/08/2006
 

--- End Message ---
--- Begin Message ---
On Sat, 2006-08-26 at 15:38 -0400, tedd wrote:
> At 3:04 PM -0400 8/26/06, Robert Cummings wrote:
> >On Sat, 2006-08-26 at 14:23 -0400, tedd wrote:
> >
> >  > Besides, what Alex Turner said about NULL is correct -- it's absurd
> >>  to have NULL evaluate to anything. Just because one language allows
> >>  absurdity doesn't mean you have to practice it.
> >
> >One language? I know for certain that PHP, C, PERL, and JavaScript all
> >return true for comparisons of null to itself. I know quite a few
> >languages too...
> 
> The following said using the accent of Dr. Zoidberg.
> 
> One, 18 kazillion, what does it matter? How many wrongs does it take 
> to make it right?

*lol* But the same statement applies to the converse argument also >:)

> >though I'm not sure why you threw CSS in there since
> >the concept of null doesn't apply.
> 
> Ahh, I threw that in there was just because I use it and I'm trying 
> to impress you. :-)

Aww shucks *blushes*.

> True, css doesn't have a NULL, but that just means it's NULL in the 
> NULL department, right?  :-)

*groan* ;)

Cheers,
Rob.
-- 
.------------------------------------------------------------.
| InterJinn Application Framework - http://www.interjinn.com |
:------------------------------------------------------------:
| An application and templating framework for PHP. Boasting  |
| a powerful, scalable system for accessing system services  |
| such as forms, properties, sessions, and caches. InterJinn |
| also provides an extremely flexible architecture for       |
| creating re-usable components quickly and easily.          |
`------------------------------------------------------------'

--- End Message ---
--- Begin Message ---
At 3:04 PM -0400 8/26/06, Robert Cummings wrote:
On Sat, 2006-08-26 at 14:23 -0400, tedd wrote:

 > Besides, what Alex Turner said about NULL is correct -- it's absurd
 to have NULL evaluate to anything. Just because one language allows
 absurdity doesn't mean you have to practice it.

One language? I know for certain that PHP, C, PERL, and JavaScript all
return true for comparisons of null to itself. I know quite a few
languages too...

The following said using the accent of Dr. Zoidberg.

One, 18 kazillion, what does it matter? How many wrongs does it take to make it right?

though I'm not sure why you threw CSS in there since
the concept of null doesn't apply.

Ahh, I threw that in there was just because I use it and I'm trying to impress you. :-)

True, css doesn't have a NULL, but that just means it's NULL in the NULL department, right? :-)

Cheers,

tedd
--
-------
http://sperling.com  http://ancientstones.com  http://earthstones.com

--- End Message ---
--- Begin Message ---
On Saturday 26 August 2006 06:29, Colin Guthrie wrote:

> > So unless someone has a better idea, I'm back to Kate and a terminal
> > window. :-)
>
> That's the setup I always seem to revert to too!! Can't help but love
> Kate, if only it had some sort of code completion I'd be a happy camper...
>
> Col.

Actually it kinda sorta does.  It will code complete any string to another 
string already used in the same file.  It's not real code completion, but 
it's a halfway-decent facsimile and still better than Eclipse / PHP IDE.  

Although I'm extremely upset at projects being removed in 3.5.  Apparently 
there's now a plugin for it to add projects back in, but it's only available 
as source.  As far as I'm concerned, if it requires a compiler to install 
then it's not actually released and doesn't actually exist yet.  

-- 
Larry Garfield                  AIM: LOLG42
[EMAIL PROTECTED]               ICQ: 6817012

"If nature has made any one thing less susceptible than all others of 
exclusive property, it is the action of the thinking power called an idea, 
which an individual may exclusively possess as long as he keeps it to 
himself; but the moment it is divulged, it forces itself into the possession 
of every one, and the receiver cannot dispossess himself of it."  -- Thomas 
Jefferson

--- End Message ---
--- Begin Message ---
Hi,

I am trying to check if an email is an email or not, so I used this that I
found on the internet:

preg_match("/^([a-zA-Z0-9])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)+/",
$_POST['email']);

BUT, it returns false with the email [EMAIL PROTECTED]

And ok for [EMAIL PROTECTED]

What is the error here :)

/Peter

--- End Message ---

Reply via email to