php programming

Most programmers make use of only perhaps 20% of their chosen language overall. This is done through using the same code and methods over and over again. It makes perfect logical sense that when a programmer finds a method to provide a solution to a particular programming problem, that method is always used.

While logically sound, this methodology has a hidden cost in not allowing the programmer to making use of any new developments or shortcuts in the language. Occasionally the programming language of choice should be re-investigated by reading updated textbooks and then adopting some of the new functionalities found there. By doing this the programmer will break the barrier on the remaining 80% and greatly enhance their coding power and problem solving abilities.

PHP Shortcuts Using Replacement

The PHP language has several built-in time saving features for the programmer. One of these features provides shorthand notation for typing in command strings. PHP has built-in assignment with operation, therefore the programmer can apply both a math and an assignment operator at the same time. Instead of these longhand operations:

$str1 = $str1 + $str2;
$str1 = $str1 - $str2;
$str1 = $str1 * $str2;
$str1 = $str1 / $str2;

The PHP programmer can now consolidate most addition, multiplication, subtraction and division operations:

$str1 += $str2;
$str1 -= $str2;
$str1 *= $str2;
$str1 /= $str2;

PHP Mathematical statements of addition and subtraction of values of 1 can also be condensed using their corresponding double-plus or double-minus operators:

$i = $i + 1;
$i = $i - 1;

can be expressed as:

$i++;
$i--;

It is possible to print strings to the response object using a shorter form of PHP notation used in PHP embedded in HTML:

<?PHP echo $myvar ?>

can be expressed as:

<?=$myvar?>

But beware, shortcuts like this can break some website templates in Drupal and possibly others like Mambo.

PHP Shortcuts Through Dropping Elements

Since the PHP compiler has intelligence, several conventions can be dropped and are understood by the system. This can speed programming to a small degree. The programmer can drop brackets in a simple if/then statement:

if($a==$b)
$c += $d;

Making use of else/if when possible can greatly cut down on if/then statements, especially when combined with dropping brackets:

if($a)
echo 'A';
elseif($b)
echo 'B';
else
echo 'C';

PHP Shortcuts Through Learning New Techniques

Some programmers have utilized most of their programming language however the majority have not.

If the manuals are perused usually a new trick or shortcut can be gleaned and a better method of doing a common task found. Some of the lessor used methods are:

Learning to use the ternary operator can save the programmer a lot of typing in if/then statements:

$variable = (expression)?'true value':'false value';
$myvar3 = (isset($myvar1)) ? $myvar1 : $myvar2;

Making use of Regex Expressions can certainly cut down on unnecessary coding if/then statements:

preg_match ( pattern, subject [, matches [, flags [, offset]]])
if (preg_match("/o/", "Eldorado!", $matches))
{
$nbsp;$nbsp;echo "o was Found ";
$nbsp;$nbsp;echo $matches[0];
}

There are many other techniques to be found in the PHP Manual.

PHP also has some specialized shortcuts created expressly for Session and Cookie handling.

A little research and applying what is found always pays off in programming.

PHP shortcuts help to make the programmer’s code as short and concise as possible while also being created with as few keystrokes as possible. In this respect Less=More as more work will be accomplished with less effort and in less time.

Leave a Reply

Your email address will not be published.