Page 3 of 4

Posted: Thu Jun 29, 2006 5:48 am
by Jenk
bokehman wrote:
Jenk wrote:Gripe time..

Code: Select all

if ($_POST) {
Is an improper challenge.
Why? $_POST is always set whatever the request type and if($_POST ) returns false if it is an empty array.
No it doesn't.

It generates an error of E_WARNING level when using an undefined variable.

Code: Select all

if ($_POST) {
does not check if the variable is set, it attempts to evaluate the VALUE of the variable given.

If the value of the argument equates to TRUE, then the block within the if () {} will be executed, if the argument evaluates to FALSE, it will not.

Even in pseudo code it is improper.

Pseudo for if's:

Code: Select all

If The Result of the Arguments result in TRUE Execute this code:
[stuff]
Otherwise skip it and continue from here:
[more tuff]
What would happen if you assign FALSE to $_POST? The variable is set, but the value is FALSE, there for the IF would 'fail' ..

It's lazy programming, and is improper programming.

:)

Posted: Thu Jun 29, 2006 6:03 am
by bokehman
Jenk wrote:It generates an error of E_WARNING level when using an undefined variable.
No it does not as is prooved by the following!

Code: Select all

<?php

ini_set('display_errors', 'On');
error_reporting(E_ALL);

if($_POST)
{
	echo '$_POST not empty';
}
else
{
	echo '$_POST empty';
}

?>
If an array is empty it evalutes to false. No notice or error will be raised. Notices only occur when variables are not set. Since the $_POST array is always set this is not the case.

Re: I had a revelation today.

Posted: Thu Jun 29, 2006 6:18 am
by freefall
What you have stumbled across is the CONTROLLER part of the MVC model (go google it). Its a crucial model and I would wager 95% of enterprise applications will be based upon it. How does it help you? Well I'll let you figure that out with some background reading. Have fun with it :)
Daedalus- wrote:EDIT: I do not have a good title for this topic and if you have a suggestion, post it. Better yet, if you are an admin, change it.

Forever and ever, since I have been coding websites, I have had one problem that drives me insanely crazy.

The URLs of my website look like this:

http://www.camicus.net/?q=home
http://www.camicus.net/?q=weblog&a=search
http://www.camicus.net/?q=weblog&a=details&id=1

Stop! You are thinking, don't. Just listen.

Forever and ever, whenever I would be writing admin pages, I would POST the form. Now, you can't post to something sexy like a querystring.

You have to post to form_handler.php and then redirect back to a sexy querystring.

I hate that! I hate, hate, hate that! I hate it.

I figured it out today. It was beautiful. PHP Buddha came to me and told me how to right this horrible, horrible wrong.

I have a page, index.php

index.php inits a class named PageBuilder

every form posts to index.php?q=weblog&a=addentry or something other such thing

PageBuilder checks for POST data to index php and then checks the querystring and decideds what functions it needs to call.

Code: Select all

// Gangster pseudo-code

$pagebuilder->buildpage

// buildpage:

function buildpage
{
    if ($_POST)
    {
        // handle the form data
    }
    // call functions to output page
}
Now, I don't know if anyone has ever cared about this, much less this vehemently but I solved it! I frickin' figured it out and today, I love myself.

I hope this helps someone as stupid as me.

I'll never use header() or meta redirects again.

Posted: Thu Jun 29, 2006 6:19 am
by Jenk
EDIT: This is of course a reply to bokehman

Sorry, you are incorrect.

Code: Select all

if ($_POST) {
is NOT the correct way to determine if the $_POST variable contains any data.

Using an undefined variable in anysituation, other than as an argument for empty() or isset() will result in an error.

If your example does not produce an error, when the $_POST array has not been defined, you have something wrong with your installation of PHP - something quite seriously wrong at that.

Try your example again with $random in place of $_POST and see what happens.

Then try it again with the line

Code: Select all

$random = FALSE;
inserted before the if() statement.

If you are checking if the $_POST array contains any data, you should be using empty() like so:

Code: Select all

<?php

if (empty($_POST)) {
    echo '$_POST is empty.';
} else {
    echo '$_POST is not empty.';
}

?>
and as far as I am aware, $_POST is only set when there is some data to be defined within it. If there is absolutely no POST data transmitted upon request, PHP will not define the $_POST array.

The other 'lazy' point to raise is that when using arrays such as $_POST, you should be explicity checking each and every $_POST indice that you will be using in your code.

Posted: Thu Jun 29, 2006 6:48 am
by JayBird
Jenk wrote:and as far as I am aware, $_POST is only set when there is some data to be defined within it. If there is absolutely no POST data transmitted upon request, PHP will not define the $_POST array.
$_POST, $_GET, etc. are all superglobal arrays, which are always defined.

...but, it is still bad practice IMO as Jenk said to do

Code: Select all

if($_POST)
   // do something

Posted: Thu Jun 29, 2006 7:16 am
by bokehman
Oh dear!
  • false
  • null
  • empty array
  • empty string
  • zero
All of the above produce a negative result when tested in an if statement. If you are not concerned about typecasting there is nothing wrong with this construction whatsoever. $_POST is always an array, either empty or with content. It is always present and as such will never raise a notice. There are times when type testing is necessary but this certainly is not one of them.

Posted: Thu Jun 29, 2006 8:05 am
by Jenk
Now you are just further proving you do not know what you are talking about..

Posted: Thu Jun 29, 2006 8:55 am
by bokehman
Jenk wrote:Now you are just further proving you do not know what you are talking about..
Please keep this above a personal level and support your arguments with fact. You are the one that keeps bashing on about the $_POST array being undefined variable but this is not true and I even posted code that prooves this. If this construction was so bad PHP would not allow it. If you believe I have missed something from the list of things that produce a negative result point it out, but if I have not all other things evaluate to positive.

The only time it is necessary to check if a variable is set is when there is a possibility that it may not be set. In this particular case though there will never be any difference in the result produced by

Code: Select all

if($_POST)
or

Code: Select all

if(!empty($_POST))
If you believe otherwise post some code that backs up your arguments.

Posted: Thu Jun 29, 2006 10:37 am
by Jenk
Nice of you to try and sidetrack with the personal bit.. I'll leave it at that incase you get uppity about it again.

-

if() does not check for data. if() checks for boolean FALSE as the result of it's argument. That is it. Upon finding false, it skips to the closing brace, or to a secondary condition (elseif) or opposing statement (else.)

The if() statement typecasts the output of it's criteria to boolean. It is PHP's management of types that cause 0 (integer zero), '0', '', 'false' (string) to boolean FALSE.

Try it.

Code: Select all

<?php

$var = (bool) 0;

var_dump($var);

?>
It DOES NOT check for content, it DOES NOT look at what you pass it and decided upon what type of variable it is, it does NOTHING more than evaluate the result of the condition inbetween the accompanying parenthesis.

I have given you the facts, you chose to ignore them.

Pimptastic has agreed it is bad practice, but you STILL persist in claiming you are right, you are NOT.

I said twice that $_POST is undefined, as a sideline to what the main point is - that main point being, incase you chose to ignore it yet again, that

Code: Select all

if($_POST)
is the incorrect and improper way to check that the $_POST array is empty.

This is exactly what empty() and isset() are for.

Just think about it..

Code: Select all

<?php

if (1 > 0) {
  //blah
}

?>
is 1 > 0? boolean TRUE..

Code: Select all

<?php

if ('string' == 'different string') {
  //blah
}

?>
boolean TRUE.

if's are not used with any other type of condition, it is always 'does the result of this condition equate to TRUE, or FALSE?' There isn't a sub-section to if() that says 'does this array contain any data?'

Posted: Thu Jun 29, 2006 10:54 am
by bokehman
$_POST is always defined. In its native form it is never false, null, empty string, or integer zero, it is always an array. Since we already know the variable type is array and we are just testing to see if the array has indeces

Code: Select all

if($_POST)
produces an identical effect to

Code: Select all

if(!empty($_POST))
Your examples are not related to this instance. In this particular instance

Code: Select all

if(!empty($_POST))
has no advantage whatsoever.

Posted: Thu Jun 29, 2006 11:30 am
by RobertGonzalez
Jenk, I got nothing but love for you man, and I totally agree with you that evaluating $_POST is a bit lazy when it comes to coding, but I just ran the following PHP script:

Code: Select all

<?php
ini_set('error_reporting', E_ALL);
ini_set('display_errors', 'On');
print_r($_POST);

if ($_POST)
{
	echo "\n<p>POST evals to true!</p>\n";
}
else
{
	echo "\n<p>POST evals to false!</p>\n";
}
?>
And I got the following results:

Code: Select all

Array ( ) 

POST evals to false!
Sorry Jenk, but it looks like bokehman is right about what he was saying.

Posted: Thu Jun 29, 2006 11:34 am
by JayBird
Jenk wrote:
I said twice that $_POST is undefined, as a sideline to what the main point is - that main point being, incase you chose to ignore it yet again, that

Code: Select all

if($_POST)
is the incorrect and improper way to check that the $_POST array is empty.
$_POST is ALWAYS defined...ALWAYS, ALWAYS, ALWAYS.

I'm not sure which is the proper way of doing it.

Sure, doing if($_POST) works, but putting cooking oil in you car make your car "work" but it just aint the thing you do.

Hmmm....

Im on the fence, but from personal preference, i would check a specific indicie or use empty for tidyness

Posted: Thu Jun 29, 2006 11:34 am
by daedalus__
This thread went totally off-topic.

Post the "right" way to check for $_POST variables or gtfo out of this thread please.

Start your own.

Posted: Thu Jun 29, 2006 11:43 am
by RobertGonzalez

Code: Select all

<?php
if (isset($_POST['knownindextocheckagainst']))
{
    // do something
}
?>

Posted: Thu Jun 29, 2006 12:13 pm
by daedalus__
ty :)