Thursday, August 29, 2013

Strange and Interesting facts about PHP

Comparison:
In PHP the boolean values TRUE and FALSE are case in-sensitive.

Example:
$a=TrUe;
if($a)
 echo 'Entered';     //will come
if($a==TRUE)
 echo 'Entered';    //will come
if($a===TRUE)
 echo 'Entered';   //will come

And TRUE = Something other than FALSE or 0 or '0'.

Example:
$a='test';
$b='0';
if($a==TRUE)
    echo 'Entered';   //will come
if($b==TRUE)
    echo 'Entered';   //will not come

Increment Operator:
If we use an increment operator along with a string, it will increment last letter of the string(When the last letter of the string is alpha numeric, that is other than special characters).

Example:
$a='apple3';
$b='apple';
$c='apple?';

echo ++$a;    // output: apple4
echo ++$b;   // output: applf
echo ++$c;   // output: apple?

Case Sensitive:
In PHP variables are case sensitive but functions are not.

Example:
$test='red';
echo $tesT;   // will not work
sample();      // will work

function Sample()
{
echo 'inside function';
}


Please go throw the following links for more details:
http://www.sitepoint.com/3-strange-php-facts-you-may-not-know/
http://php.net/manual/en/types.comparisons.php

Thursday, August 22, 2013

Javascript Local Storage

Reference: http://www.htmldog.com/guides/javascript/advanced/localstorage/

When building more complex JavaScript applications, it’s very useful to be able to store information in the browser, so that the information can be shared across different pages.

Unlike cookies, local storage can only be read client-side - that is, by the browser and your JavaScript. If you want to share some data with a server, cookies may be a better option.

Set Item: localStorage.setItem('name', 'tom');

Get Item: var name = localStorage.getItem('name');


Example:

<html>
      <head> 
              <title>Javascript Local Storage</title>
      </head>
      <body>
              Give some input to store on local: <input type="text" id="input_msg" />&nbsp&nbsp              <input type="button" value="Submit" id="submit" onclick="saveOnLocal();" />
              <br /><br />
              Data from local: <span id="local_data"></span>
      </body>
</html>

<script type="text/javascript">
function saveOnLocal()
{
var data=document.getElementById('input_msg').value;
localStorage.setItem('input_msg',data); //Set local storage
document.getElementById('local_data').innerHTML=localStorage.getItem('input_msg'); //Get local storage
 localStorage.removeItem('input_msg'); //Clear specific local storage item
 //localStorage.clear(); //Clear all local storage item

}
</script>

You can only save string using javascript local storage, use JSON for save array.

Example:
<script type="text/javascript">
localStorage.setItem('name', JSON.stringify(your_array));
var user = JSON.parse(localStorage.getItem('name'));
</script>


Browser support for local storage is not 100% but it’s pretty good - you can expect it to work in all modern browsers, IE 8 and above, and in most mobile browsers too. And the storage size depends on the browser.




Thursday, August 8, 2013

Get file size using Java Script only

HTML:
<input type="file" name="photo" id="photo" />

Java Script:
$('#photo').live('change', function(){
      var file_size=this.files[0].size;
      alert(file_size);
});

Notes:
1. This is a HTML5 feature. So I think its only works on HTML5 supported browsers.
2. Similarly we can get some attribute of file.

Source:
http://stackoverflow.com/questions/8192516/html5-file-browse-tag
http://forum.jquery.com/topic/how-to-validate-file-size-function-before-submitting-form

Learn JavaScript - String and its methods - 16

<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>String and it's methods - JS&l...