News: PHP coding tips to write less code (If then else and for loop)

Home News Photos News Videos News Cartoons News Blogs RSS

Home > PHP Scripts Examples and Tutorials > PHP coding tips to write less code (If then else and for loop)

PHP coding tips to write less code (If then else and for loop)

PHP is a good language, but there are always surprises. This tip is useful to "lazy" developers who do not even think about variable names. They may prefer magic names like ${0} and 0 is good enough variable name, why not... So here are a few tips that can make your code shorter and harder to read :-)

Thursday, Jan 31, 2008 | 2093 Views | Comments [View/Post]

1. Use || (or) and && (and) operations instead of if.

// A lot of code
$status = fwrite($h, 'some text');
if (!$status) {
    log('Writing failed');
}

// Less code
${0} = fwrite($h, 'some text');
if (!${0}) log('Writing failed');

// Even less code
fwrite($h, 'some text') or log('Writing failed');

2. Use ternary operator.

// A lot of code
if ($age < 16) {
    $message = 'Welcome!';
} else {
  $message = 'You are too old!';
}

// Less code
$message = 'You are too old!';
if ($age < 16) {
    $message = 'Welcome!';
}

// Even less code
$message = ($age < 16) ? 'Welcome!' : 'You are too old!';

3. Use for instead of while.

// A lot of code
$i = 0;
while ($i < 100) {
  $source[] = $target[$i];
  $i += 2;
}

// less code
for ($i = 0; $i < 100; $source[] = $target[$i+=2]);

4. In some cases PHP requires you to create a variable. Some examples you can find in PHP fluent API tips article. Another example is getting array element when array is returned by the function.

$ext = pathinfo('file.png')['extension'];
// result: Parse error: syntax error, unexpected '[' in ... on line ...

To handle all these situation you can create a set of small functions which shortcuts frequently used operations.

// returns reference to the created object
function &r($v) { return $v; }
// returns array offset
function &a(&$a, $i) { return $a[$i]; }

5. Explore the language you use. PHP is very powerful and has a lot of functions and interesting aspects of the language which can make your code more efficient and short.

6. When it is better to write more and then read the code easily, do not be lazy. Spend a few seconds and write a comment and more readable construction. This is the only tip in this list that really can save hours, not minutes.



http://www.alexatnet.com

Comment on this article
Guidelines: You must register with a social media account such as Facebook, Twitter, Yahoo, etc. to comment on this story. Click on the "Login" button below to choose your login account of choice. We welcome your thoughts, but this is not an open forum. For the sake of all readers, please refrain from the use of obscenities, personal attacks or racial slurs. All comments must remain on topic and cyber bullying will not be tolerated. All comments are subject to our terms of service. Comments that do not comply may be removed. Repeat offenders will lose commenting privileges.
News Other Language
World News
Movie News
Information
Travel News