Constantly Remaining Constant
Constants are special types of variables that recieve a value and from then on can no longer be changed. this type of variable has a special place in PHP. It is normailly used when you have something that needs to be contained in a variable that iwll never change in your script, or has the potential of bing overwritten accidentially, and you wish to prevent aht from happening.
Many of the programmers in the PHP community borrow a syntax from the "C" programming language when defining a constant. That syntax is to name the constant variable in all capital letters like so.
- A constant with a string value:
- A constant with a number value:
<?php define('YOURCONSTANT', 'your_string_value') ?>
<?php define('YOURNUMBERCONSTANT', 100000) ?>
Naming your constants with this naming convention will help you and other programmers distinguish your constants from other variables and text. When you call your constant, like in an echo statement you do not call it with a dollar sign in front of it like you do with other variable types. This is another significant difference between constants and other types of variables within PHP.
<?php echo YOURCONSTANT ?>
The output for this echo statement will be, "your_string_value" depending on what you placed as your value. Remember that strings need to be within quotes, and numbers need not be.
Calling all PHP “SUPERGLOBALS”
In PHP there are a set of variables called "SuperGlobals". These variables are of a special type and they carry along with them their own set of rules that define when and where they are available, called their scope.
The only difference between SuperGlobals and normal variables is; they can be seen everywhere in your scripts.
A list of these are as follows :
- $GLOBALS
- Holds an array of all global variables.
- $_SERVER
- Holds an array of all server enviroment variables.
- $_GET
- Holds an array of all the varialbes passed to the script through the GET method.
- $_POST
- Holds an array of all the varialbes passed to the script through the POST method.
- $_REQUEST
- Holds an array of all the varialbes passed to the script through the GET and POST methods, otherwise called user input variables.
- $_COOKIE
- Holds an array of all the cookie variables.
- $SESSION
- Holds an array of all the session variables.
- $_FILES
- Holds an array of all the variables associated to file uploads.
- $_ENV
- Holds an array of all the enviroment variables.
Balancing Parenthesis and Brackets (Equally Paired)
How to Balance Braces and Parenthesis
Making sure your braces and Parenthesis are always paired with one another properly, you can use a simple counting technique.
Starting at the initial parenthesis OR brace in the series, "starting from ZERO" for every"("OR "{" add one to the initial count of ZERO. For every "(" OR "}" subtract ONE from your ongoing count.
Do as follows:
Open: "(" OR "{" = plus 1 OR = +1 such as starting at ZERO 0+1 = 1")"
OR
Close: ")"OR "}" = minus 1 OR = -1 such as starting at ONE 1-1= 0
Start at zero = (some code 1)+(some( code)) || ((some)( code3)) = 0
Brace 1 is#1 = +1.............-1..+1...+1....-1-1...+1+1..-1+1...-1-1 = 0
Actual count = 1...............0....1.....2.......1.0....1.2.....1.2.....1..0 = 0
Remember we started at zero prior to counting anything and added and subtracted for every parenthisis character, starting with the initial one. Our final count ended up being ZERO if our brackets are balanced.
Dreamweaver CS5 now offers the ability to count the braces or parenthesis for you, and notify you of your mistake.
PHP Code Survival Guide “Rules to Live by”
Throughout my learning of PHP I have gained a few outstanding ideas as to how to make your PHP code survive the test of time. First of all read as many books and articles as time permits. If you truly want to be a proficient programmer of any type, you will never stop reading. Nor will you say, "I am the expert of all experts" no matter what stage your career is in.
Words to live by: One thing I have learned, that is now engrained into my soul is, "Every day I do not learn PHP. I learn how much PHP I do not know."
"Rules to Live by" while programming PHP:
- Never assume you know anything. If you have a doubt about the interaction of any function no matter if you created it or not, or whether it is a built in PHP function. Assume you know nothing about it, and research it to the best of your ability prior to using it for the first time, or the one hundredth time.
- Always use the XML style PHP open and closing tags:
- Or use the Script style tags, to defeat hiccups with older style tags:
- I suggest you never use the short stye PHP tags, as it will make all your code far less portable.
- One other major concern when using scripts created during the rein of PHP 4 I see far too often is the use of the PHP environment configuration setting of register_globals to be turned on. This prior to PHP 5 was nativly turned on, in PHP 5+ is is nativly off. The environment variable register_globals can cause your scripts to be breached due to security holes. The setting register_globals when on creates a variable automatically for any form field using the name of the field itself as a variable name. For instance if we had a form field as follows:
- The PHP register_globals would automatically create a variable using the form field name of "fieldOne". That would look like "$fieldOne" in your PHP code. The ability to use this might seem advantageous, however it could allow your users or a hacker to inject their own variables into your script at will unless you take the necessary steps to prevent this from happening. The easiest way to ensure that your scripts are not vulnerable through this type of attack is to turn register_globals off when using a PHP version below version 5, or just use the default setting that come with PHP version 5 or higher.
<?php /*your code*/ ?>
<?script language='php'> /*your code*/ </script>
<? /*your code*/ ?>
<input type="text" name="fieldOne" />