Eyes, JAPAN Blog > Some tips of coding in PHP

Some tips of coding in PHP

Tang

この記事は1年以上前に書かれたもので、内容が古い可能性がありますのでご注意ください。

Use FALSE as error code, use NULL as not exist.

For the function of the operation, the failure returns FALSE, indicating “the operation failed”, and for the function of the query, if the desired value cannot be found, it should return NULL, indicating “cannot find the value”.

 

NULL=== could be better than is_null()

Both of them could give the same result, but is_null could call one more function call.

 

Use $_SERVER['REQUEST_TIME'] instead of time() to get the current time.

The function, time(), will cause a function call. If you do not need precise time value, $_SERVER[‘REQUEST_TIME’] will become a better choice, which is much faster.

 

List() could help you to get the specific value of an exploded array.

    $string = "Hello World; Hello there; Hello little world";
    list( ,  $mid) = explode(';', $string);
    echo $mid;

Output: Hello there

 

=== is better than ==

PHP has two sets of equality comparison operators ===/!== and ==/!===/!= will have implicit type conversion, and ===/!== will strictly compare whether the type of two operations are the same and the values are equal.

We should try to use === instead of ==, not only because the conversion rules are harder to remember, but also because if you use ===, it will be more comfortable for future maintenance or people reading your code:

“At this moment , this line of statement, this variable is of this type!”.

 

Avoid using continue

Continue means “go back to the top of the loop”. However, the end of current loop is going to the start of the next loop. We can use proper construction to avoid continue.

 

Caution about the loose comparison, such as switch and in_array

Let’s see one example.

    $x = "1";
    switch ($x) {
        case 1:
            echo "int 1";
            break;
        case "1":
            echo "string 1";
            break;
    }

Output: int 1

Both switch and in_array use loose comparison, so when the types of variables to be compared are different, it is easy to make mistakes.

You could use if...else... to avoid this problem.:

$x = "1";
    if ($x === "1") {
        echo "string 1";
    } else {
        echo "int 1";
    }


Switch also has its advantages:

switch is not just for discriminating variables.

 

if($a) {
} else if ($b) {
} else if ($c || $d) {
}

You could write it more clearly by switch

switch (TRUE) {
        case $a:
            break;
        case $b:
            break;
        case $c:
        case $d:
            break;
}

 

Faster than floor()

Using ~~ instead of floor() will faster 3 times than floor().

    echo  ~~4.9;
    echo " ";
    echo  floor(4.9);

Output: 4 4

Tips: For big number, ~~ may cause problem in early PHP version.

    echo ~~99999999999999.99; 
    echo " ";
    echo floor(99999999999999.99); 

Output: 276447231 99999999999999

Tips about do{}while(0)

Same as in C++do{}while(0) could do something interesting, such as error check.

do {
  // do something
  if (error) {
    break;
  }
  // do something else
  if (error) {
    break;
  }
  // etc..
} while (0);

 

Try to avoid doing calculation in the loop

This is the bad example, because the program will call the function strlen() every time:

for($i=0; $i<strlen($str); $i++) {
    }

Good example:

    for($i=0, $j=strlen($str); $i<$j; $i++) {
        $char = $str[$i];
    }

 

Comments are closed.