Thursday, January 12, 2012

Equal Operator Common Mistake


 Logic errors are the hardest to find and fix on every programming languages.
These are errors that make your program do what is not intended to do.

One of the common mistakes that you may encounter many times on your program is the use of equal assignment operator for the purpose of using equal comparison operator.

This is because comparison operator is used to test if two values are equal. It returns a boolean value of either true, which is equivalent to nonzero value, or false, which is equivalent to zero value in boolean. While the assignment operator produces an output value which is equal to the value of the left variable on the operation, you may commit a logic error on your conditional statement.

For example:

$a = 3;
$b = 5;

echo $a == $b; //output is false
echo $a = $b; //output is 5 which is true on boolean


So if you will use it to your conditional statements, you will produce an error that will take you time to find and fix.

Another example for conditional statement effects:

if( $skillPoints == 100 ){
//give your employees a salary raise
}

and if you use this:

if( $skillPoints = 100 ){
//give your employees a salary raise
}

Cool! You definitely got a salary raise by your mistake. =D

__________________________________________________
Web Programming Tutorial by Gerlie Mendoza | Web Developer
http://on.fb.me/Ay7OPZ

Configuration File For your Web Applications or Websites

Creating a configuration file for your web applications is very important. 
This is where you store all your constant variables that you can use for your entire project.

There are a lot of ways that you can store these information:
1. Include a Php File that contains the variables
2. Store information to a database and retrieve
3. Create an ini file

Creating an ini file is my favorite way of storing my variables. :)


Here is how to use it:


1. Create the ini file and name it as sample.ini and put your variables on it like below:
;Note how comments are made with semi colons?
[db_info]
db_name = MyDatabase
rootdir = http://localhost/mywebpage/

2. Create a function that reads the ini file and include it on your web page.
Example:


function getIniValue( $name ){
$ini = parse_ini_file( "sample.ini" );

$name_var = $ini[$name];

return $name_var;
}

Note: You could also do a single call to parse_ini_file() function that returns an associative array of all the variables declared on your ini file if you need all of those variables on one page. 
It depends on your approach on how you could optimize the processes on your page.

3. On your web page, whenever you need a constant value, call your function and assign it to a variable.
Example:
$dbname = getIniValue( "db_name" ); // Now $dbname = "MyDatabase"

One thing that we just need to remember when we use this is to restrict the access to this file because it can be opened and appear as plain text on your browser. 
Another thing is, as much as posible, do not put any login information specially database constants like database host, root user and root password.

__________________________________________________
Web Programming Tutorial by Gerlie Mendoza | Web Developer
http://on.fb.me/Ay7OPZ