6 tips to reduce the amount of your PHP code

Source: Internet
Author: User
Tags parse error readable

PHP It's a good language, and it's a lot of surprises. And today I saw an interesting way to blog in Arnold Daniels . He talked about the temporary variables in PHP . This secret is useful to " lazy " programmers, so that programmers can no longer think about what to name the variables. They can use this variable name:${0}.

I'm lazier than Arnold Daniels , I don't want to use a variable at all. Here are some tips to make your code less.

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

// Standard wording
$status = fwrite ($h, ' some text ');
if (! $status) {
log (' writing failed ');
}

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

Less code
fwrite ($h, ' some text ') or log (' writing failed ');

2. Use the ternary operator .

// Standard wording
if ($age < 16) {
$message = ' welcome! ';
} else {
$message = ' You are too old! ';
}

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

Less code
$message = ($age < 16)? ' welcome! ': ' You are too old! ';

3. Replace the while with a for .

// Standard wording
$i = 0;
while ($i < 100) {
$source [] = $target [$i];
$i + 2;
}

Less code
For ($i = 0; $i < $source [] = $target [$i +=2]);

4. Many places are required to write variables. For example: PHP fluent API tips . For example, a function call gets an array and then uses the array element directly.

// The following example will have an error, because the function call, the returned array does not first assign value to a variable, but directly using [' extension '].

$ext = PathInfo (' file.png ') [' extension '];
Result:parse error:syntax error, unexpected ' ['] in ... on line ...

You can create a function to solve this problem, as follows:( pretty good way, look a little awkward ...)

Returns reference to the Created object
function &r ($v) {return $v;}
Returns array offset
Function &a (& $a, $i) {return $a [$i];}

5. spend more time studying PHP 's own function methods,PHP has a lot of interesting ways to make your code shorter.

6. When writing more code to make the program clearer, don't be lazy. Spend more time writing notes and writing readable code as much as possible. This is the real time saving skills. ( write more comments and easy to read code, will save time when you modify and debug later )

PHP is a good language, but there are always surprises. And today I ' ve seen a interesting approach in Arnold Daniels ' s blog. He talks about temporary variables in PHP. This tip are useful to "lazy" developers The WHO does not even about variable names. They may prefer magic names like ${0} and 0 are good enough variable name, why not ...

But I ' m even more lazy then Arnold and sure this when there are no variable, then there is no prob Lem. So this is are a few tips that can make your code shorter and harder to read:-)

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 the while.

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

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

4. In some cases PHP requires, you have to create a variable. For example, Ech the PHP fluent API tips article. Another example is getting array element ' is returned by ' function.

$ext = PathInfo (' file.png ') [' extension '];
Result:parse error:syntax error, unexpected ' ['] in ... on line ...

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

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. PHP is very powerful and has a lot of functions and interesting aspects of the language-can make which code more your Cient and short.

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.