Saturday, December 26, 2015

Print variable on top of the page where variable is defined bottom - php

Some time we need to print/echo a variable on the top of the page but that variable need to define after a long code bottom of the page. as we know in PHP all instructions are executed sequentially so we cannot read a variable that hasn't yet been defined. 

We can do it easily using jquery. Example

We need to print a total discount value top of the page 

<p> Total Discount : <span id="demo"></span> USD </p>

But the sum of discount value need to find after a long code 
...............................php  code bla bla bla to find a variable $sum_discount  value..................
...............................php  code bla bla bla to find a variable $sum_discount  value..................

Then asign  that value within a jquery 

 <?php    
     echo "<script type='text/javascript'> 
                        document.getElementById('demo').innerHTML = ".$sum_discount.";     
                </script>";
?>

Saturday, October 24, 2015

Skip to execute construct method when certain method called

Sometime we don't need to execute any method / code inside constructor/destructor.
example for codeigniter

class Student
{
public function __construct ()
{

        // we want to skip 2 methods(login and logout) in this 'student' class.
$exception_uris = array(
'student/login', 
'student/logout'
);

//  check called class and method from URL.If  called method are not those login/logout execute code
 
if (in_array(uri_string(), $exception_uris) == FALSE)
   {
 
       if ($this->student_m->loggedin() == FALSE)
      {
redirect('student/login');
}
}    
}
}

Friday, September 11, 2015

isset() VS empty() - which one I'll use!!!!

It depends what you are looking for, if you are just looking to see if it is empty just use empty as it checks whether it is set as well, if you want to know whether something is set or not use isset.
Empty checks if the variable is set and if it is it checks it for null, "", 0, etc
Isset just checks if is it set, it could be anything not null
With empty, the following things are considered empty:
  • "" (an empty string)
  • 0 (0 as an integer)
  • 0.0 (0 as a float)
  • "0" (0 as a string)
  • NULL
  • FALSE
  • array() (an empty array)
  • var $var; (a variable declared, but without a value in a class)

Total Pageviews