Tag Archives: php

PHP – Array dereferencing

As of PHP 5.4 it is possible to array dereference the result of a function or method call directly.
Before it was only possible using a temporary variable.

As of PHP 5.5 it is possible to array dereference an array literal.

<?php
function getArray() {
    return array(1, 2, 3, 4);
}

// on PHP 5.4
$secondElement = getArray()[1];

// earlier
$temp = getArray();
$secondElement = $temp[1];
?>

Simple email function to send html content

Use the the below send_mail() function to send the text or html mail to specific recipients

<?php
function send_mail($to, $from, $subject, $body, $cc='', $bcc=''){
    // To send HTML mail, you can set the Content-type header.
    $headers = "From: $from\r\n";
    $headers .= "Content-type: text/html\r\n";
    //add additional headers like, reply-to, etc if you need
    if(!empty($cc)){
        $headers .= "Cc: $cc\r\n";
    }
    if(!empty($bcc)){
    $headers .= "Bcc: $bcc\r\n";
    }
    // and now native mail function
    mail($to,$subject, $body, $headers);
   //you can use the return type for success message or error message
}
?>

You can use the above function by calling the function with parameters as below

<?php
    send_mail('recipient@domain.com', 'frommail@fromdomainname.com', 'Hello subject', 'Content');
?>