Perl Quotes and Escape Sequences

Perl string literals are enclosed in quotation marks. You can use either single quotes ('') or double quotes ("").

print "hello";
print 'hello'; 

Now suppose you need to print a string which contains a single or double quote such as following strings:

This is bob's watch.
Alice replied, "It was a birthday gift."

If I do the following,

Pushed changes to GIT but don't see it in repository

Problem

You pushed your changes to GIT but you don't see it in the repository.

Diagnosis

First thing is to compare status with log

git log

will give you a list of the commits. You will not see your changes here.

git status

You will see your files here. Above the list of files, you will see

Changes to be committed

Cause

The reason you don't see your changes in repository is because they were not committed.

Converting to and from epoch time

Epoch time is Unix timestamp which counts the number of seconds since January 1 1970 00:00:00 UTC. Without getting into long discussion, follow is how your convert between human readable date and epoch time.

Convert Human Readable Date to Epoch Time

<?php
  $now = strtotime('2010-04-25');
  print $now;
?>

Convert Epoch Time to Human Readable Time

<?php
  $human_date=date("Y-m-d", 1251627495);
  print human_date;
?>

Validating Email in PHP

Validating email is more difficult than one might think. Recently I found this this code at PHP.net and I feel a need to republish this to emphasize its effectiveness and simplicity.

$email = "user@email.com";
if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
  echo "E-mail is not valid";
} else {
  echo "E-mail is valid";
}

PHP Array functions

Passing arrays by value and reference

The difference between passing by value and passing by reference is that you when pass by value, you are passing a copy of the array. When passing by reference, you are passing a pointer i.e any changes made in the function you pass to can modify the original.

function foo($var) {
  $var[1]++;
}
function bar(&$var) {
  $var[1]++;
}

$a=array(5,6,7);
foo($a);
print "Passed by value:\n";
print_r($a);
bar($a);
print "Passed by reference:\n";
print_r($a);

output

Pages