Further shortening of your code (Part 1)

May 13th, 2008

Many people are using PHP for development and most have truly mastered the art of programming the easy way round. Sadly, many older web sites that used these older styles of programming are not only long but reminiscent of reading something similar to alien writing that is long and incomprehensible to the layman. But good for all of us, there are some people out there who are willing to share their learned ways to those who are just beginning to master the strengths and powers of PHP. The traditional PHP way of checking for Boolean expressions was long and filled with those double equal signs (==)and others that were akin to some other mathematically adept languages but there are simpler ways of doing things as there always is with any language. In the next post we would give and example of the concept.

Control Structures

May 9th, 2008

There used to be an old saying in PHP that a control structure needs a single constant to locate it and a set of brackets to clear it all up. Well, that might all be true but using too many of these curly brackets when it isn’t needed at all doesn’t help you any and it even makes your code a tad bit harder to understand. Let’s show this with the following example:

if ($superman == ‘alien’) {
$strength — ;
}

Saying that, it would be simpler to code it in this simpler more readable manner:

if ($superman == ‘alien’) $strength — ;

The fewer brackets that muddle up the code the better for it makes it all the readable thus easier to understand and fix in case something goes wrong.

PHP - Single vs. Double Quotes (Part 2)

May 5th, 2008

Single quotes are used to define a set of characters and double quotes for variables, and as said the two are not inter-changeable and PHP will not check for a variable within single quotes. Below is a faster way of doing the same script with the simple substitution of the double for the single quotes.

Example:
$myword = ‘PHP is Free’;
if ($myword == ‘PHP is Free’)
{
echo ‘Learn it Easy’;}

The changes might be very minimal but for a serious scripting author, speed is of the essence and slow web pages are quite annoying.  As a web user like you and me should know, the faster a page opens, the better and the more we like it.

PHP - Single vs. Double Quotes (Part 1)

May 1st, 2008

PHP may be an easy language to use and learn but there are simple steps most budding programmers forget when it comes to coding in the scripting language of choice for web pages. The quotes, which come in single and double forms. They may seem inter-changeable but they have two distinct uses; the double quote is used for variables and coding without proper knowledge of this wastes computing time for PHP is forced to look for a non-existent variable wasting precious computing time thus resulting in longer processing.

Example:
$myword = “PHP is Free”;
if ($myword == “PHP is Free”)
{
echo “Learn it Easy”;}

PHP Programming.

March 25th, 2008

PHP is free! (Most Web hosts provide PHP in even their most basic plans). PHP or the PHP Hypertext Pre-processor is a server-side programming language that gives Web developers it has the ability to build virtually any type of application, from simple contact forms to shopping carts, dynamic job sites, and complex content management systems.

PHP Programming Tutorials & Tips

March 20th, 2008

One of the more popular technologies for web programming in the recent years owing to its open source technology is the PHP. And it only means that it’s free to use and also compatible with almost any server, say Windows, Linux/Unix and Apache. This technology has many advanced features included in it though its open source.

PHP programming jobs still in demand

March 15th, 2008

Technology Driven Marketing Solutions is an industry leading developer of PHP solutions who will become part of a dynamic and fast growing team. You may experience to make codes for a range of web applications by applying your PHP and OOD programming. Success in this field will lead to further career opportunities within the organization.

Tips in safe Mode

March 10th, 2008

Apache module is a PHP installed Apache configuration files (.httpd) and .htaccess files help its settings to be changed. It also enable or disable PHP safe mode for the entire web server you can use the php.ini file and also allows the file to modify many aspects of PHP configuration.

Array_diff_uassoc Function

March 6th, 2008

Next in line is array_diff_uassoc() function which compares two or more arrays while checking for differences before comparing the keys with a user-defined location. It then returns an array withthe keys and values from the first array(to which all the values were comapred against) it the function allows it. Syntax is as follows : array_diff_uassoc(array1,array2,array3….,function). with a sample below of how it is used.

function userdefined($v1,$v2)
{
if ($v1 === $v2)
{
return 0;
}
if ($v1 > $v2)
{
return 1;
}
else
{
return -1;
}
}
$a1=array(0=>”Dog”,1=>”Cat”,2=>”Horse”)
$a2=array(3=>”Dog”,1=>”Cat”,5=>”Horse”)
print_r(array_diff_uassoc($a1,$a2,”userdefined”));
?>

which results in the following output : Array( [0] => Dog [2] => Horse). For an example of the same function with two or more assigned arrays to the function:

function userdefined($v1,$v2)
{
if ($v1 === $v2)
{
return 0;
}
if ($v1 > $v2)
{
return 1;
}
else
{
return -1;
}
}
$a1=array(0=>”Dog”,1=>”Cat”,2=>”Horse”)
$a2=array(3=>”Dog”,1=>”Cat”,5=>”Horse”)
$a3=array(6=>”Onyx”,0=>”Dog”,5=>”Horse”)
print_r(array_diff_uassoc($a1,$a2,$a3,”userdefined”));
?>

Which in turn, gives you : Array ([2] => Horse )

So we see the different array_diff function variants and the diffeerent ways they are used to compare the values of one or more arrays with one another.

Array_diff_assoc and array_diff_key Functions

March 1st, 2008

The next array comparison functions is the array_diff_assoc(array1,array2,array3,array3…..), usage is similar with all of these array_diff functions varying only in the way the comparisons are done. Below is sample code for array_diff_assoc:

$a1=array(0=>“Mouse”,1=>”Cat”,2=>”Dog”);
$a2=array(0=>”Lizard”,1=>”Dog”,2=>”Cat”);
$a3=array(0=>”Dog”,1=>”Cat”,2=>”Mouse”);
print_r(array_diff_assoc($a1,$a2,$a3))
?>

Giving you : Array ([0] => Mouse [2] => Dog).

Next we have the array_diff_key() function compares two or more arrays and returns an array with the keys and values from the first array only if the key is not present in the other arrays. Syntax is array_diff_key(array1,array2,array3……)which is similar to the other array_diff functions.

Sample usage:
$a1=array(0=>“Mouse”,1=>”Cat”,2=>”Dog”);
$a2=array(2=>”Fish”,3=>”Rat”,4=>”Bee”);
$a3=array(5=>”Dog”,6=>”Cat”,7=>”Fish”)
print_r(array_diff_key($a1,$a2,$a3));
?>

Giving you : Array([0] => Mouse [0] => Cat)