Wednesday, 16 March 2016

Difference between OR and || in PHP

Difference between OR and || in PHP

Based on behavior OR and || are same,

$a = 1;
$b = 1;

if($a==$b || $a=='5') {
   echo "here";
}  // OUTPUT: here


if($a==$b || $a=='5') {
   echo "here";
}  // OUTPUT: here



//////////////////////////////////////////////////

BUT, "OR" operator have lower precedence than ||

Because 'OR' have lower priority than '=' but '||' have higher priority. Same thing apply to "and" and && operator.

Let's check difference,

<?php
$bool = true && false;
var_dump($bool); // false, that's expected

$bool = true and false;
var_dump($bool); // true, ouch!
?>

Please refer below link for more detail,
http://php.net/manual/en/language.operators.precedence.php

So use only ("||" , "&&") operator instead of ("or","and") operator.

Enjoy your coding!!!

Difference between == (equality or Double Equals) and === (identity or Triple Equals) in PHP

Difference between == (equality or Double Equals) and === (identity or Triple Equals) in PHP

== compares the values of variables for equality, type casting as necessary. 


=== checks and returns true only if the two variables are of the same type AND have the same value.


Examples:
a === a // returns true
a == a  // returns true

1 === "1" // returns false because 1 is an integer, "1" is a string
1 == "1"  // returns true because "1" gets casted to an integer, which is 1


"abc" == "abc" // returns true
"abc" === "abc" // returns true because both operands are strings and have the same value


For the security reasons, it is better to use identity operator whenever possible because it compares variable type also.

Enjoy your coding!!!

Monday, 14 March 2016

Register Globals (register_globals) in PHP

Register Globals (register_globals)

Secure you application by disabling register_globals variable,

register_globals
was meant to help rapid application development. 


Any URL which includes a query string,the register_globals statement allows us to access the value of variable with $variable instead of $_GET['variable'] automatically.

This might be useful for us, but all variables in the code now have this property, and we can now easily get into PHP applications that do not protect against this unintended consequence.

If you have added below code into your login page,

if(!empty($_POST['user_name']) && $_POST['user_name'] == 'abc' && !empty( $_POST['password'] ) && $_POST['password'] == "abc123")
{
    $valid = true;
}
if($valid==true) { $show_user_pages = 1; }

If the application is running with register_globals ON, a user could just place valid=1 into a query string, and would then have access to whatever the script is running.


So it is better to running application with register_globals OFF.

We cannot disable register_globals from the script side, but we can use an .htaccess files or change php.ini file to do this.

Disabling with php.iniregister_globals = Off


Disabling with .htaccess
php_flag register_globals 0


Enjoy with secure php script with register_globals Off !!

Friday, 11 March 2016

Spaceship operator (<=>) in PHP7

Spaceship operator (<=>) + PHP7

PHP7 introduce the Spaceship (<=>) operator.

This <=> operator will offer combined comparison
Return 0 if values on either side are equal
Return 1 if value on the left is greater
Return -1 if the value on the right is greater



<=> operator will be similar to in older PHP versions of below syntax,
($a < $b) ? -1 : (($a > $b) ? 1 : 0);

Now in PHP 7, it will be like,
$a <=> $b;


The rules used by the combined comparison operator are same as the currently used comparison operators by PHP with <, <=, ==, >= and >.

PERL or RUBY programming background programmers are already familiar with this new operator proposed for PHP7.

It returns,
0 if $a == $b
-1 if $a < $b
1 if $a > $b

Comparing Integers,

echo 2 <=> 2; //ouputs 0
echo 2 <=> 3; //outputs -1
echo 3 <=> 2; //outputs 1


String Comparison
echo "a" <=> "a"; // 0
echo "a" <=> "b"; //-1
echo "b" <=> "a"; //1

It can be used for sorting.



Thursday, 10 March 2016

HTML5 - Difference between HTML and HTML5.



HTML5 is a markup language used for structuring and presenting content on website. HTML 5 is an updated version of HTML. It's much similar to HTML. But difference is HTML 5 contains much better support for media such as audio, video etc.

1. Simple Syntax


The simple doctype declaration is one of the many novelties in HTML5. Now you need to write only: <!doctype html>. The syntax of HTML5 is compatible with HTML4 and XHTML1, but not with SGML.


2. HTML5 is in Progress

HTML5 hasn’t been standardized like HTML4. You don’t have to worry about updating pages built using HTML4.
Elements and attributes are added and modified several times. Of course, this is dependent how much we depend on rich elements.

Build with HTML4, play with HTML5.


3. New Elements


<header> and <footer> are specifically marked for such. Because of this, it is unnecessary to identify these two elements with a <div> tag.
<section> and <article> allows you to mark specific areas of your layout as such, and should have a positive effect on on your SEO in the end.
<menu> can be used for your main menu, but it can also be used for toolbars and context menus.
<figure> element is another way to arrange text and images.
<audio> and <video> elements to use/run audio and video files on the html page
<canvas>
New <form> and <forminput> elements are added


4. No more use of some of the Elements

<b> and <font> are added in CSS itself
No more <frame>, <center>, <big> elements


Conclusion

HTML5 actually strives to be something very different.  It aims to be more of an application development platform,  which includes not only depends on text and images, but also playing video and audio, storing data in the application, and real-time networking protocols for exchanging data as it happens etc..

Enjoy your website with updated markup language HTML5!!!!

Wednesday, 9 March 2016

Redirect https to http through htaccess

Add below code in you htaccess file for redirect https to http

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

Tuesday, 8 March 2016

Set iframe height dynamically through Javascript / jQuery

Add an event listener in the parent document (the page where you called iframe)

<script type='text/javascript'>
window.addEventListener('message', function(e) {
    var data = e.data[1];
    var eventName = e.data[0];
    if(eventName=='setHeight') { document.getElementById('iframeID').height = data; }
}, false);
</script>


Write below function into your iframe,

This function will fetch document height and called parent page function to set height dynamic.
<script type='text/javascript'>
function setSize() {
  var height = document.body.scrollHeight;
  window.parent.postMessage(["setHeight", height], "*");
}


Call this function inside iframe through onload event
$( window ).load(function() {
  setSize();
});
    OR
<body onLoad="setSize();">
</script>