Monday, 12 September 2016

List Of Latest Core PHP Examples And Interviews Questions & Answers

Core PHP Examples and Interview Questions


In this blog post I am sharing a list of some common PHP interview questions & answers or some core PHP examples that is very useful and helpful the freshers and experienced too. I have combined these example from different sources listed here at one place.

Ques 1. How to reverse a string without using any builtin function?

Ans. 

<?php
$str = 'My name is Diwakar Kumar';
$len = 0;
while(isset($str[$len]) != '')
$len++;
for($i = $len ; $i >= 0 ; $i--)
{
 echo @$str[$i];
}

Ques 2. How to access global variables with a user defined function?

Ans.

<?php
$x = 5;
$y = 10;

function myTest() {
    $GLOBALS['y'] = $GLOBALS['x'] + $GLOBALS['y'];
} 

myTest();
echo $y;

Ques 3. How to use the static keyword to let a local variable not be deleted after execution of
function?

Ans.

<?php
function myTest() {
    static $x = 0;
    echo $x;
    $x++;
}

myTest();
echo "<br>";
myTest();
echo "<br>";
myTest();


Some Reference Sites: 

Saturday, 16 July 2016

How To Redirect Non-WWW to WWW, HTTPS & Vice Versa Using Htaccess

I always see that most of the web developers are stuck with URL redirection. Like redirect Non-WWW to WWW, Non-WWW to HTTPS and WWW to Non-WWW, WWW to HTTPS using .htaccess file.

So, here I listed all the code of URL redirection using htaccess. Hope it helps you.





Non-WWW and WWW to https://www.example.com/

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

RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]


Non-WWW and WWW to https://example.com/

RewriteEngine On
RewriteCond %{HTTPS} off 
RewriteCond %{ENV:HTTPS} off 
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE] 
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC] 
RewriteRule ^ https://%1%{REQUEST_URI} [L,R=301,NE]

Non-WWW and /index.php  to WWW Redirect

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


WWW to Non-WWW Redirect

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

Thursday, 28 April 2016

Which one is the most using web framework?

The most using Web Frameworks by PHP developers