Monday, April 22, 2013

Redirect non-www domain to www using .htaccess file

Hello friends,

Today in Developer's Blog, I am going to tell you the way to Redirect your domain from non-www to www using .htaccess file.

Suppose you have domain named www.example.com and if users browse the url by example.com and you want the www in the url, so you can write the below code to redirect to non-www to www using .htaccess file.

Here's the code you will have to write in .htaccess file : 

RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]

So by this you will be able to Redirect to www from non-www using .htaccess file.

I hope this will help you and you will like it.

Don't forget to leave your comments.

Thank you
Ravinder

Friday, April 12, 2013

Format Numbers Using JavaScript

Hello friends,

Today in Developer's Blog, I will tell you the way to Format Numbers Using JavaScript.

We already have a function to format numbers in PHP, which is number_format(number,decimal_places).

So we also need a function which can format numbers in JavaScript, because a lot of times we need to calculate the cost or something and show it formatted using JavaScript.

So here's the function which will format your numbers using JavaScript.


function number_format(str) {
    var amount = new String(str);
    amount = amount.split("").reverse();

    var output = "";
    for ( var i = 0; i <= amount.length-1; i++ ){
        output = amount[i] + output;
        if ((i+1) % 3 == 0 && (amount.length-1) !== i)output = ',' + output;
    }
    return output;
}


So by this you will be able to Format Numbers Using JavaScript.

I hope this will help you and you will like it.

Don't forget to leave your comments.

Thank you
Ravinder

Monday, April 8, 2013

PHP Interview Questions for 0 - 1.5 years Experienced Developer

Hello friends,

Today In Developer's Blog, I am going to give you the Questions and Answers for the Interview.


I am sure it will help you a lot. Even if you are not preparing for any interview, but you can still read this for your knowledge and you can test yourself.



1. Which of the following functions allows you to store session data in a database?

A. session_start();

B. session_set_save_handler();

C. mysql_query();

D. You cannot store session data in a database.
Answer : B


2.
What will the following script output?


<?php

$x = 3 - 5 % 3;

echo $x;
?>

A. 2

B. 1

C. Null

D. True

E. 3
Answer : B
 



3.
Which data type will the $a variable have at the end of the following script?

<?php

$a = “1”;
?>

A. (int) 1

B. (string) “1”

C. (bool) True

D. (float) 1.0

E. (float) 1

Answer : B



4. What will the following script output?

<?php
$a = 1;
$a = $a-- + 1;
echo $a;
?>

A. 2

B. 1

C. 3

D. 0

E. Null
Answer : A


 5.  
What will the following script output?

<?php

class a {     

    private $c;     

    public function a($pass) {
         $this->c = $pass;
     }  
 

    public function print_data() { 
 
         echo $this->$c;
     }
 

 }  
 

 $a = new a(10);  
 
 $a->print_data();

?>

A. An error

B. 10

C. “10”

D. Nothing

E. A warning
Answer : A



6.   How do I find out the number of parameters passed into function?

A.    By using func_num_args()

B.    By using func_get_arg()

C.    By using func_get_args()

D.    None of the above
Answer : A


7.  What will the following script output?

<?php
error_reporting(E_ALL);
class a {
   var $c;
   function a() {
       $this->c = 10;
   }
}
class b extends a {
   function print_a() {
       echo $this->c;
   }
}
$b = new b;
$b->print_a();
?>

A. Nothing

B. An error because b does not have a constructor

C. 10

D. NULL

E. False
Answer : C


8. Is it possible to pass data from PHP to JavaScript?

A. No, because PHP is server-side, and JavaScript is client-side.

B. No, because PHP is a loosely typed language.

C. Yes, because JavaScript executes before PHP.

D. Yes, because PHP can generate valid JavaScript
Answer : D

9. Which types of form elements can be excluded from the HTTP request?


A. text, radio, and check box

B. text, submit, and hidden

C. submit and hidden

D. radio and check box


Answer : D




10.  When processing the form, what is the difference between a hidden form element and a non-hidden one, such as a text box?


A. The hidden form element does not have a name.

B. There is no difference.

C. The hidden form element does not have a value.

D. The hidden form element is excluded from the request.
Answer : B





11. Which of the following form element names can be used to create an array in PHP?

A. foo

B. [foo]

C. foo[]

D. foo[bar]
Answer : C,D



12. When an expiration date is given in a Set-Cookie header, what is the resulting behavior in subsequent requests?

A. If the expiration date has expired, the cookie is not included.

B. The behavior is the same; the expiration date is included in the Cookie
header, and you can access this information in the $_COOKIE superglobal
array.

C. The cookie persists in memory until the browser is closed.

D. The cookie is deleted and therefore not included in subsequent requests.
Answer : A





13. If you set a cookie with either setcookie() or header(), you can immediately check to see whether the client accepted it.

A. True, you can check the $_COOKIE superglobal array to see if it contains the
value you set.

B. True, but only if register_globals is enabled.

C. False, you can only use setcookie() if you need to test for acceptance.
Using header() does not work.

D. False, you must wait until you receive another HTTP request to determine
whether it includes the Cookie header.
Answer : D

14. Why must you call session_start() prior to any output?

A. Because it is easy to forget if not placed at the top of your scripts.

B. Because you can no longer access the session data store after there has been output.

C. Because session_start() sets some HTTP headers.

D. Because calling session_start() causes the HTTP headers to be sent.


Answer : C
 


15. Which of the following represents the proper way to set a session variable?

A. $_SESSION[‘foo’] = ‘bar’;

B. session_start();

C. session_set_save_handler (‘myopen’, ‘myclose’, ‘myread’,‘mywrite’, ‘mydelete’, ‘mygarbage’);

D. $foo = $_SESSION[‘foo’];


Answer : A
  

16. Which of the following types can be used as an array key? (Select three.)

A. Integer

B. Floating-point

C. Array

D. Object

E. Boolean
Answer : A,C,D

 
17. Which of the following functions can be used to sort an array by its keys in descending order?

A. sort

B. rsort

C. ksort

D. krsort

E. reverse_sort


Answer : D




18. What will the following script output?



<?php

$a = array (‘a’ => 20, 1 => 36, 40);

array_rand ($a);

echo $a[0];

?>

A. A random value from $a

B. ‘a’

C. 20

D. 36

E. Nothing


Answer : E



19.  Given

$email = ‘bob@example.com’;
which code block will output example.com?

A. print substr($email, -1 * strrpos($email, ‘@’));

B. print substr($email, strrpos($email, ‘@’));

C. print substr($email, strpos($email, ‘@’) + 1);

D. print strstr($email, ‘@’);
Answer : C

 
20.  
Which question will replace markup such as img=/smiley.png with <img src=”/smiley.png”>?

A. print preg_replace(‘/img=(\w+)/’, ‘<img src=”\1”>’, $text);

B. print preg_replace(‘/img=(\S+)/’, ‘<img src=”\1”>’, $text);

C. print preg_replace(‘/img=(\s+)/’, ‘<img src=”\1”>’, $text);

D. print preg_replace(‘/img=(\w)+/’, ‘<img src=”\1”>’, $text);


Answer : B
 


21.
Which of the following functions is most efficient for substituting fixed patterns in strings?

A. preg_replace()

B. str_replace()

C. str_ireplace()

D. substr_replace()


Answer : A
 

 
22.
What are the contents of output.txt after the following code snippet is run?

<?php
$str = ‘abcdefghijklmnop’;
$fp = fopen(“output.txt”, ‘w’);
for($i=0; $i< 4; $i++) {
fwrite($fp, $str, $i);
}

?>

A. abcd

B. aababcabcd

C. aababc

D. aaaa


Answer : C




23. Which of the following can be used to determine if a file is readable?

A. stat()

B. is_readable()

C. filetype()

D. fileowner()

E. finfo()


Answer : B




24.
If you have an open file resource, you can read data from it one line at a time with the _____ function. 
Answer : fgets()




25. Which of the following functions require an open file resource?

A. fgets()

B. fopen()

C. filemtime()

D. rewind()

E. reset()

Answer : B


26. Which of the following sentences are incorrect?
A. date() returns the current UNIX datestamp.

B. date() returns a formatted date string.

C. date() requires a time stamp to be passed to it.

D. date() returns a date array.


Answer : D

27. The ________ function will return the current UNIX time stamp.

Answer : time()



28. Which of the following functions will output the current time as 11:26 pm?
A. print date(‘H:m a’);

B. print date(‘G:M a’);

C. print date(‘G:i a’);

D. print strftime(‘%I:%M %p’);


Answer : C,D


29. Which of the following is not an aggregate function?
A. AVG

B. SUM

C. COUNT

D. GROUP BY

E. MIN


Answer : D

30. What is the default execution time set in set_time_limit()?

A.   20 secs

B.   30 secs

C.   40 secs
Answer : B

 
I hope this will help you and you will like it.

Don't forget to leave your comments.

Thank you
Ravinder

Convert Numbers into Words Using JavaScript

Hello friends,

Today in Developer's Blog, I am going to tell you the way by which we will be able to easily convert Numbers into Words using JavaScript.

First of all you will have to create a JavaScript file (.js) using below code :
var th = ['','thousand','million', 'billion','trillion'];
var dg = ['zero','one','two','three','four', 'five','six','seven','eight','nine']
var tn = ['ten','eleven','twelve','thirteen', 'fourteen','fifteen','sixteen', 'seventeen','eighteen','nineteen'];
var tw = ['twenty','thirty','forty','fifty', 'sixty','seventy','eighty','ninety']

function toWords(s)
{  
    s = s.toString(); 
    s = s.replace(/[\, ]/g,''); 
    if (s != parseFloat(s)) return 'not a number'
    var x = s.indexOf('.'); 
    if (x == -1) x = s.length; 
    if (x > 15) return 'too big'
    var n = s.split(''); 
    var str = ''
    var sk = 0; 
    for (var i=0; i < x; i++
    {
        if ((x-i)%3==2) 
        {
            if (n[i] == '1'
            {
                str += tn[Number(n[i+1])] + ' '
                i++; 
                sk=1;
            }
            else if (n[i]!=0) 
            {
                str += tw[n[i]-2] + ' ';
                sk=1;
            }
        }
        else if (n[i]!=0) 
        {
            str += dg[n[i]] +' '
            if ((x-i)%3==0) str += 'hundred ';
            sk=1;
        }

        if ((x-i)%3==1)
        {
            if (sk) str += th[(x-i-1)/3] + ' ';
            sk=0;
        }
    }
    if (x != s.length)
    {
        var y = s.length; 
        str += 'point '
        for (var i=x+1; i<y; i++) str += dg[n[i]] +' ';
    }
    return str.replace(/\s+/g,' ');
}

So by this you will be able to Convert Number into Words Using JavaScript.

I hope this will help you and you will like it.

Don't forget to leave your comments.

Thank you
Ravinder




Saturday, April 6, 2013

Create Responsive HTML Theme in Minutes

Hello friends,

Today in Developer's Blog I will tell you the way to Create Responsive HTML Theme in Minutes.

Friends, most of us just focus on designing html only for web version and not for iPad, iPhone and other Mobile Phones.

Some of us don't know how to create responsive theme and some of us don't design it until our client asks us to do that.

But now a days, as we all know that we always like to keep our self online 24X7 just because we always want to be updated with everything. So we also visit the websites from our cell phones and in U.S., most of the persons uses iPhone and iPad to browse the website instead of Desktop PC. 

So we will have to design Responsive theme now and for that we will have to learn how to do that.

We will not have to do anything fancy to design responsive theme, we will just have to define min and max width of the screen in CSS and it will be applied in that particular device automatically.

So, we will start with the Small Phones with a resolution of 240 x 320.

@media screen and (min-width: 0px) and (max-width: 319px)
{
    // write your CSS here by having maximum width of 319px and it will work in Small Phones
}

Now we will write the CSS for iPhone with a resolution of 320 x 480.

@media screen and (min-width: 320px) and (max-width: 479px)
{
    // whatever CSS you defined for Small Phones will have to be defined here for iPhone.
}

Now we will write the CSS for Small Tablet with a resolution of 480 x 640.


@media screen and (min-width: 480px) and (max-width: 767px)
{
    // whatever CSS you defined above will have to be defined here for Small Tablet.
}


Now we will write the CSS for iPad (Portrait) with a resolution of 768 x 1024.


@media screen and (min-width: 768px) and (max-width: 979px)
{
    // whatever CSS you defined above will have to be defined here for iPad (Portrait).
}

Now we will write the CSS for iPad (Landscape) with a resolution of 1024 x 768.


@media screen and (min-width: 980px)
{
    // whatever CSS you defined above will have to be defined here for iPad (Landscape).
}

So by this you will be able to Create Responsive Theme which will work in all the Mobile Devices.

I hope this will help you and you will like it.

Don't forget to leave your comments.


Thank you
Ravinder