$x=$y or if($x==$y)

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
Hey, first time in the group.

I wonder, is it better to fill a variable at the top of a page and then
re-fill it later when needed, or do an if() at the later spot?  For example:

Line 3    $reports = $sql_row[reports];
Line 60    $reports = $_POST[reports];


Line 60    if($sql_row[reports]) {
      $reports = $sql_row[reports];
    else {
      $reports = $POST[reports];

Thank you,

Re: $x=$y or if($x==$y)

jdoerr schreef:
Quoted text here. Click to load it

i should stop worrying about these details until your application is
1) not performing
2) not working
3) not doing what it is supposed to do
4) not doing whay YOU want it to do.
5) ....?


Re: $x=$y or if($x==$y)

Quoted text here. Click to load it

Personally I do the former in the cause of simplicity and readability.

In any case I don't know what:

  if ($sql_row[reports]) { ...



"That excessive bail ought not to be required, nor excessive fines imposed,
nor cruel and unusual punishments inflicted"  --  Bill of Rights 1689

Re: $x=$y or if($x==$y)

On Thu, 14 May 2009 18:42:32 +0000, jdoerr wrote:

Quoted text here. Click to load it

In my coding, it depends upon the nature of what I'm checking.

Let me give you two examples.

//Where I set the 'default' at the top.

$data_valid = true;  // Assume the data is good, and if
                     // find something wrong, we'll flag it.

if ($age < 0) {
  $data_valid = false;

if ($first_name === "") {
  $data_valid = false;

// No real "default" here, so I set in an "else" condition.

// Are we printing the names one-per-line, or are we printing them all in
// a row?

if ($print_mode === "line") {
  $item_tag = 'li';
  $item_separator = "";
} else {
  $item_tag = 'span';
  $item_separator = ", ";

Re: $x=$y or if($x==$y)

Jeremy J Starcher wrote:

Quoted text here. Click to load it

Always assume data is bad. Then validate it and flag it as OK. It makes
your code more scalable en maintainable.

<snip code blocks>

Nothing wrong with these. You could also use exception handler.


Re: $x=$y or if($x==$y)

On Fri, 15 May 2009 08:12:55 +0200, Bart Friederichs wrote:

Quoted text here. Click to load it

Sometimes it is easier to express in one form or another, but yes, I
understand what you are saying.

(dropping to p-code)
// I find this the easiest way to code:
  dataValid = true;
  for (i = 0; i < list.length; i++) {
    if (!isValid(list[i])) {
      dataValid = false;

// Over something like this:
  pointsOfValidData = 0;
  for (i = 0; i < list.length; i++) {
    if (isValid(list[i])) {
  dataValid = (pointsOfValidData === list.length)

Re: $x=$y or if($x==$y)

jdoerr escribió:
Quoted text here. Click to load it

A slightly unrelated note: strings must be quoted in PHP. E.g.:


-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://borrame.com
-- Mi web de humor satinado: http://www.demogracia.com

Site Timeline