elkebirmed
elkebirmed

Reputation: 2357

PHP equal operator precedence

What's the purpose of checking the value of $_post array from two text input fields like so:

$start = $end = ''; // i need to understand this line

if (isset($_POST['start'])) $start = $_POST['start'];
if (isset($_POST['end'])) $end= $_POST['end'];

Upvotes: 0

Views: 49

Answers (5)

AbraCadaver
AbraCadaver

Reputation: 78994

This just sets $start and $end to '' in one statement.

Then the following ifs check to see if something isset() before referencing them so that you don't get undefined variable notices.

So the first line is setting the default value in case the isset()s fail.

This accomplishes the same (and I prefer):

$start = isset($_POST['start']) ? $_POST['start'] : '';
$end   = isset($_POST['end']) ? $_POST['end'] : '';

Upvotes: 1

Frederic Nault
Frederic Nault

Reputation: 996

$start = '';
$end   = '';

is same as:

$start = $end = ''; // i need to understand this line

That assign the value '' to $start and $end. All variable will have the last value of the assignation chain.

the purpose of: if (isset($_post['start'])) $start = $_post['start'];

if we look what that does :

[pseudo code]
    IF (I HAVE SOMETHING IN $_post['start'])
        THEN
            I CAN ASSIGN THIS VALUE TO $start

We do not want to assign an non-existing value to $start.

voila!

Upvotes: 1

Frederik Spang
Frederik Spang

Reputation: 3454

You could say that $start equals end that equals ''. Therefore $start is '' too.

Upvotes: 1

Mooseman
Mooseman

Reputation: 18891

$start = $end = ''; sets both variables to '': an empty string.

Upvotes: 1

sunshinejr
sunshinejr

Reputation: 4854

Because you need to find out if the variable has been defined. If it hasn't been defined, you are storing undefined variable and you will get an error (notice). That's why the check with isset() and storing empty string instead. Also $_post should capitalized ($_POST).

Upvotes: 1

Related Questions