2 Obscure PHP Functions Blogpost Cover

2 Obscure PHP Functions

Do note that these 2 functions below are built-in PHP functions supported in 5.3=<

GOTO

There are lots of jokes about the GOTO function in other languages, but in PHP, it can be a really useful exception catching mechanism.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<?php
restart:
if (error_condition1) {
goto error;
}
if (error_condition2) {
goto restart;
}
error:
report_error();
exit;
?>
<?php restart: if (error_condition1) { goto error; } if (error_condition2) { goto restart; } error: report_error(); exit; ?>
<?php

restart:
    if (error_condition1) {
        goto error;
    }
    if (error_condition2) {
        goto restart;
    }

error:
    report_error();
    exit;

?>  

Date Parsing

12-03-11 could mean 3 different dates:

  1. December 3, 2011
  2. March 12, 2011
  3. March 11, 2012

It is better to be really specific when working with date and time.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<?php
$date = date_create_from_format('d-m-y', '16-03-11');
var_dump(date_format($date, 'F d, Y'));
//Outputs string(14) "March 16, 2011"
?>
<?php $date = date_create_from_format('d-m-y', '16-03-11'); var_dump(date_format($date, 'F d, Y')); //Outputs string(14) "March 16, 2011" ?>
<?php

$date = date_create_from_format('d-m-y', '16-03-11');
var_dump(date_format($date, 'F d, Y'));
//Outputs string(14) "March 16, 2011"

?>  

Leave a comment: