PHP syntactic sugar code example

According to Wikipedia, Syntactic sugar is syntax within a programming language that is designed to make things easier to read or to express.

Though PHP doesn’t necessarily have much concept of sugaring, I across this one example sometime back. The idea here is to add a given set of values to an array but at the alternating ends.


For a given set of values, 10, 12, 124, 349, 43, 0, 493, 3, 32. The output should have values pushed to each end alternatively. So, the first value into the array would be 10, 2nd would be 12, pushed to the end (or at the start), 3rd pushed other and so on.

#1    #2    #3    #4    #5    #6    #7    #8    #9
                                    493   493   493
                        43    43    43    43    43
            124   124   124   124   124   124   124
10    10    10    10    10    10    10    10    10
      12    12    12    12    12    12    12    12
                  349   349   349   349   349   349
                              0     0     0     0
                                          3     3

The function here loops over an array pushes values in array $e alternatively.

$x = [10, 12, 124, 349, 43, 0, 493, 3, 32];
$i = 0;
foreach ($e as $v) {

It’s an array with the two function names ['array_push','array_unshift'] with [++$i%2] as the index of the array alternating between a 0 or 1 so will evaluate to the other function each time. PHP’s “variable functions” let you assign a variable to a function and execute by calling with parenthesis (ex: $f='array_push'; $f($e,$d); == array_push($e,$d)) so the ($e,$d) is then calling the evaluated element of the array.

Just a shorter way to do

if (++$i%2)

Laravel Custom Exception Handlers


Laravel throws a large number of exceptions and is very easy to customize them to suit our needs as required.

Note: These exceptions are most helpful if you’re building an API.

The code for common exception handler lies in the {project_root}/app/Exceptions/ folder, named Handler.php. The default file looks like this in Laravel 5.7.


namespace App\Exceptions;

use Exception;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;

class Handler extends ExceptionHandler
     * A list of the exception types that are not reported.
     * @var array
    protected $dontReport = [

     * A list of the inputs that are never flashed for validation exceptions.
     * @var array
    protected $dontFlash = [

     * Report or log an exception.
     * @param  \Exception  $exception
     * @return void
    public function report(Exception $exception)

     * Render an exception into an HTTP response.
     * @param  \Illuminate\Http\Request  $request
     * @param  \Exception  $exception
     * @return \Illuminate\Http\Response
    public function render($request, Exception $exception)
        return parent::render($request, $exception);

Some of the common exceptions that we’re going to handle in this tutorial are AuthenticationException, MethodNotAllowedHttpException, ModelNotFoundException, NotAcceptableHttpException, NotFoundHttpException, PostTooLargeException, ValidationException.

Below is the explanation for each variable, functions and handler implemented.

protected $dontReport = [] is the array of exceptions that Laravel wouldn’t report. Here we’re going to mention the exceptions that we don’t want laravel to report.

Continue reading “Laravel Custom Exception Handlers”

Customizing Laravel validation JSON message format

laravel request validation customize error message format

By default laravel gives a good enough JSON format for validation errors but what if you want to customize it?

#Original JSON format
    "message": "The given data was invalid.",
    "errors": {
        "email": [
            "Please enter email address."
        "password": [
            "Please enter password."

But for this particular project I decided to change the format to this.

    "success": false,
    "success_code": 400,
    "message": "Please enter email address.",
    "errors": [
            "field": "email",
            "message": "Please enter email address."
            "field": "password",
            "message": "Please enter password."

To achieve this I created a ValidationException handler function in app/Exceptions/Handler.php which helped me catch all the exceptions raised as a result of failed validations.

Continue reading “Customizing Laravel validation JSON message format”

AngularJS: Prevent form submission or stop page refresh on submit

I have recently started learning AngularJS and came across form that would refresh every time I click the submit button. So, here are a couple of ways to prevent that from happening.


<div ng-app="my-app">
  <div ng-controller="myFormController">
    <form action="test_submit.php" method="post" accept-charset="utf-8" name="myTestForm" ng-submit="myTestForm.$valid && submit()" novalidate>
        <label for="fname">First Name</label>
        <input type="text" ng-model="dataForm.fname" name="fname" id="fname" required>

        <label for="lname">Last Name</label>
        <input type="text" ng-model="dataForm.lname" name="lname" id="lname" required>

        <label for="email">Email</label>
        <input type="text" ng-model="" name="email" id="email" required>

        <button name="submit" ng-disabled="myTestForm.$invalid" type="submit">Submit</button>

Angular JS

var myApp = angular.module('my-app', []);

myApp.controller('myFormController', function($scope, $http) {
  $scope.dataForm = {};

  $scope.submit = function() {
    // Ajax

Continue reading “AngularJS: Prevent form submission or stop page refresh on submit”

Gauss Elimination method in C language using Lower triangular matrix

This is a simple C language program that calculates solution of n-linear equations using Non-pivotal Gauss Elimination method. It uses lower triangular matrix to do so, for upper triangular matrix visit here.

float matrix[10][10], m, temp[10];
int i, j, k, n;
void lower_traingularisation() {
    for (i=n-1; i>0; i--)
        for (j=i-1; j>=0; j--) {
            m	= matrix[j][i]/matrix[i][i];
            for (k=0; k<n+1; k++) {
                matrix[j][k]	= matrix[j][k]-(m*matrix[i][k]);
} //lower_traingularisation
void back_subsitution() {
    for (i=0; i<n; i++) {
        m	= matrix[i][n];
        for (j=0; j<i; j++)
            m	= m - temp[j] * matrix[i][j];
        temp[i]	= m/matrix[i][i];
        printf("\n x%d => %f", i+1, temp[i]);
} // back_subsitution
void main() {
    printf("Enter number. of variables :: ");
    scanf("%d", &n);
    printf("Enter the augmented matrix: \n");
    for (i=0; i<n; i++)
        for (j=0; j<n+1; j++)
            scanf("%f", &matrix[i][j]);
    printf("The lower traingular matrix is : \n");
    for (i=0; i<n; i++) {
        for (j=0; j<n+1; j++)
            printf("%f \t", matrix[i][j]);
    printf("The required result is : \n");
} // main