Wednesday, 16 March 2016

How to convert a negative number to positive Number in PHP

How to convert a negative number to positive Number in PHP

Question: How to convert a negative number to positive Number in PHP?
$converToPostive= abs(-10.2); // 10.2 
$converToPostive=abs(10.2);   //10.2

(If number is already POSITIVE, It will remain the POSITIVE)


Question: How to convert a Positive Number to Negative Number in PHP?
$converToNegative =-1 * abs(10.2); //-10.2
$converToNegative =-1 * abs(-10.2); //-10.2

(If number is already NEGATIVE, It will remain the NEGATIVE)


Question: How to convert a negative number to positive Number and Vice Versa?
echo -1 * -10.2 //10.2
echo -1 * 10.2; //-10.2

(If number is NEGATIVE then convert to POSITIVE else if number is POSITIVE then convert to NEGATIVE)


Question: How to remove the decimal part from number?
$no=10.2
$noArray = explode('.',$no);
$no=$noArray[0]; //10

$no=-10.2;
$noArray = explode('.',$no);
echo $no=$noArray[0]; //-10

(just Remove the decimal part)


Question: How to get next highest integer value by rounding up?
echo ceil(10.2);    // 11
echo ceil(10.2222);  // 11
echo ceil(-10.22);  // -10
(Remove the decimal part and get next Highest Integer number)


Question: How to get next lowest integer value by rounding down?
echo floor(10.2);    // 11
echo floor(10.2222);  // 11
echo floor(-10.22);  // -10
(Remove the decimal part and get next lowest Integer number)


Question: How to rounding off a number?
echo round(10.4);  // 10
echo round(10.5);  // -11
echo round(10.6);  // -12
(Just rounding off the number)


Monday, 14 March 2016

.htaccess RewriteRule Examples

.htaccess RewriteRule Examples

In Web Development, Many times we need to make Query string parameter as SEO Friendly, For this we need to make a changes in htacess file.
Benefits of SEO Friendly URLs.
  1. URL Readability become better
  2. Better Indexing by Search Engines
  3. Its hard to memorise the query string parameter
  4. Query string paramter in URL looks un-professional


Following are useful mod_rewrite RewriteRule EXAMPLES which you can use in your website.
Example 1
Original URL: http://domain.com/search.php?page=1
Rewritten URL:http://domain.com/search.php/page/1
.htaccess Rule:
RewriteEngine On
RewriteRule ^search.php/page/([0-9]+)?$ /search.php?page=$1 [L]



Example 2(Same as Above AND ".php")
Original URL:http://domain.com/search.php?page=1
Rewritten URL: http://domain.com/search/page/1
.htaccess RULE:
RewriteEngine On
RewriteRule ^search/page/([0-9]+)?$ /search.php?page=$1 [L]



Example 3
Original URL:http://domain.com/search.php?category=vehicles
Rewritten URL:http://domain.com/search/category/vehicles
.htaccess RULE:
RewriteEngine On
RewriteRule ^search/category/([a-zA-Z0-9]+)$ /search.php?category=$1 [L]



Example 4
Original URL: http://domain.com/search.php?category=vehical&page=1
Rewritten URL: http://domain.com/search/category/vehicles/page/1
.htaccess RULE:
RewriteEngine On
RewriteRule ^search/category/([a-zA-Z0-9]+)+/page/([0-9]+)?$ /search.php?category=$1&page=$2 [L]



Example 5
Original URL:http://domain.com/search.php?gender=men&department=clothing&products=tshirts[L]
Rewritten URL: http://domain.com/buy/men/clothing/tshirts
.htaccess RULE:
RewriteEngine On
RewriteRule ^buy/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)?$  /search.php?gender=$1&department=$2&products=$3[L]



Example 6
Original URL: http://domain.com/search.php?gender=men&department=clothing&products=tshirts&page=1
Rewritten URL:http://domain.com/buy/men/clothing/tshirts/page/2
.htaccess RULE:
RewriteEngine On
RewriteRule ^buy/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/page/([0-9]+)?$ /search.php?gender=$1&department=$2&products=$3&page=$4[L]


Note: You NEED NOT to include "RewriteEngine On" every time, Include once at top of .htacess file