NYCPHP Meetup

NYPHP.org

Position of variable definitions [was: Re: [nycphp-talk] php script]

David Krings ramons at gmx.net
Fri Feb 22 19:49:45 EST 2008


Allen Shaw wrote:
> That depends on the structure of your script.  I would recommend that 
> you define that variable just before it's used, not necessarily at the 
> top of your script.  Again, it totally depends on the logical flow of 
> your scripts.

This is from a different thread and it caught my attention. I spend some 
considerable time in programming courses (not that it really helped, but that 
is a different story) and each and every time the instructors or authors drove 
home that one has to define and initialize variables and that this has to be 
done before doing anything else.

When I started with PHP I was reminded at my Commodore BASIC V2 programming. 
Need a variable? Go ahead and take one whenever needed. Until the scripts got 
a bit longer and the number of variables got a bit bigger that was OK. I 
forgot what exactly happened, but it probably was something abot 0, NULL, 
empty string, and FALSE not being the same. Anyhow, I got convinced the hard 
way that declaring (initializing? defining?) variables with smartly selected 
values (values that either make things work in a default manner or that cause 
the script to crash if not set correctly later) is a must.

The script doesn't care where that happens, but since I was always made 
believe that it had to be at the top (although I found that to be troublesome 
at times with session_start() not working right if it isn't the first thing 
done). My scripts usually (OK, I sometimes get lazy, but I try to stick to it) 
start off like that:

<?php
// ======================================
// This script gets called from
// - welcome.php
// - start.php
// and expects firts name and last name
// as strings via $_POST
// This script checks the input and if
// passed writes the strings to the db
// This script calls
// - connect2db
// This script returns / passes
// - success/failure string of db insert
// ======================================

// Start session
session_start();

// ======================================
// Define variables
// ======================================

// From $_POST
$firstname = "";			// Text string for first name, limited to 50 chars
$lastname = "";				// Text string for last name, limited to 50 chars

// For script
$errflag = true;			// Flag used for evalutation of input, true = error exists
$dblink = "";				// DB link
$insertquerysuccess = "Failed!";	// Return string
......


I guess you get the concept. It is a pain in the rear to keep up with it and 
maintain it, but even after months not looking at a script I know what the 
script does, what goes into it, what comes out of it, what calls it, and what 
gets called. So besides the blahblah at the top the first few dozen lines 
contain all the variable definitions. Allen suggests to "define that variable 
just before it's used, not necessarily at the top of your script". Allen or 
someone else, what is the benefit of doing it that way? What happens when you 
need to know which variables are used in a script? When you document the 
script and have to note which variables are input and which ones are output? 
Or if you know you used a variable, but do not know the name, how would you 
find it?
Allen also wrote "Again, it totally depends on the logical flow of your 
scripts". Why/when would the location of variable definitions depend on the 
logical flow of the script?

Am I missing something? Do I make my life more difficult than necessary? Is 
all the stuff the instructors said just BS? Why do I ask all these questions?

Any answers/discussions are greatly appreciated. Just to point out, I do not 
claim that my way is better. Which is why I ask if there is something better.

David



More information about the talk mailing list