Wednesday, September 28, 2016

Updating PHP 5.5.x with PHP 5.6 or PHP 7.0 in Ubuntu 13.04, 14.04, 15.04

After trying a couple of packages, settings and configuration, finally I seem to have found out a working solution:

1. Update your PPA repository with the  PHP 5.6 package sources:

 sudo add-apt-repository ppa:ondrej/php

Please note the repository is as stated above and not as:

 ppa:ondrej/php5-5.6 because this has been deprecated where the above PPA is a co-installable one, meaning you can install PHP 5.5, PHP 5.6 or PHP 7.0 as per your requirement.

Press enter to confirm. If you come across any error then you will need to install python-software-properties first as shown below:

 sudo apt-get update  
 sudo apt-get install python-software-properties  

2. Update

Once again update your packages:

 sudo apt-get update  

3. Install PHP 5.6 and its relevant mods:

 sudo apt-get install php5.6 php5.6-mbstring php5.6-mcrypt php5.6-mysql php5.6-xml   
 sudo apt-get install php7.0 # for PHP 7.0   
 sudo apt-get install php5.5 # for PHP 5.5   

4. Disable PHP 5.5:

Now this step is very important else you won't be able to use PHP 5.6. Even when you check your PHP version, it will still display as PHP 5.5. Follow the step below to disable PHP 5.5.

 sudo a2dismod php5  

5. Enable PHP 5.6:

 sudo a2enmod php5.6  

6. Restart Apache web server:

 sudo service apache2 restart  

Note: New location of php.ini file in PHP 5.6:

The location of php.ini fie is changed in PHP 5.6 unlike PHP 5.5 where it used to be located at '/etc/php5/apache2/php.ini'; the new location is at '/etc/php/5.6/apache2'

Tuesday, September 20, 2016

[[Solved]] - How to Update Eclipse from Terminal or IDE in Ubuntu 12.04, 14.04, 15.04, 16.04

Since I had installed Eclipse manually under '/opt/eclipse/' directory and not from the Ubuntu Software Centre, I couldn't update it from the terminal. Trying to Update Eclipse from the Terminal or within the IDE was giving me several errors such as these:

 Error 1:  
 An error occurred while uninstalling  
 session context was:(profile=epp.package.cpp, phase=org.eclipse.equinox.internal.p2.engine.phases.Uninstall, operand=[R]org.eclipse.platform_root 4.5.1.v20150904-0015 --> null, action=org.eclipse.equinox.internal.p2.touchpoint.natives.actions.CleanupzipAction).  
 Backup of file /opt/eclipse/.eclipseproduct failed.  
 File that was copied to backup could not be deleted: /opt/eclipse/.eclipseproduct 
 Error 2:  
 An error occurred while uninstalling  
 session context was:(profile=epp.package.cpp, phase=org.eclipse.equinox.internal.p2.engine.phases.Uninstall, operand=[R]org.eclipse.rcp_root 4.5.1.v20150904-0015 --> null, action=org.eclipse.equinox.internal.p2.touchpoint.natives.actions.CleanupzipAction).  
 Backup of file /opt/eclipse/readme/readme_eclipse.html failed.  
 File that was copied to backup could not be deleted: /opt/eclipse/readme/readme_eclipse.html 
 Error 3:  
 An error occurred while uninstalling  
 session context was:(profile=epp.package.cpp, phase=org.eclipse.equinox.internal.p2.engine.phases.Uninstall, operand=[R]org.eclipse.rcp_root 4.5.1.v20150904-0015 --> null, action=org.eclipse.equinox.internal.p2.touchpoint.natives.actions.CleanupzipAction).  
 Backup of file /opt/eclipse/readme failed.  
 Permission denied  

Update Eclipse

Looking at the above errors I figured out one of the possible cause was that Eclipse did not have SU (Super User) privilege. So, I executed Eclipse from the terminal giving it Super User privilege. And once in the IDE ran and update from within it. (Look image below)

To run Eclipse with Super User Permission from within the terminal. Launch your terminal with CTRL + ALT + T:

 cd /opt/eclipse  
 sudo ./eclipse  

Updating Eclipse from IDE

Once in Eclipse go to Help > Check for Updates. Now the Eclipse should be updated without any error.

Sunday, September 18, 2016

JavaScript Equivalent of PHP's number_format() Function

Today while going through JavaScript I came across a float value and then wanted to truncate it by two decimal places. I knew in PHP we have the number_format() function and this is what I found for Javascript:

yourFloatVarHere.toFixed(2);

Example:

Suppose you have a value say: 0.589723, and you would like to truncate this value to two decimal place.
 var divide;  
 var num_format;  
 divide = 33/86;  
 num_format = divide.toFixed(2);  

33/ 8 = 0.38372093
Since we have truncated our answer to two decimal place, the final answer will be: 0.38

Wednesday, September 14, 2016

Understanding the Difference Between isset(), empty(), is_empty() and unset() functions in PHP

isset() function in PHP: 

As the name suggests the isset() function in PHP checks whether the variable 'is set' or not. To be more clear this is what happens when this function is called:
  •  Checks whether the variable is declared or exists or set or not. It returns TRUE or 1 if the variable is not NULL, not an Empty String represented by '' or an Undeclared variable.

empty() function in PHP: 

The empty() function in PHP returns TRUE or 1 in the following cases:
  1. if the variable is undeclared
  2. if the variable is assigned zero. Example $high = 0;
  3.  if the variable is assigned an Empty string or declared NULL. Example: $high= ''; or $high = NULL;

is_null() function in PHP:

As the name suggests, the is_null() function checks whether a variable is NULL or not. This holds TRUE if:
  1. variable is not declared
  2. variable is declared as NULL. Example: $high = NULL;
Note: '' empty string is not NULL.

    unset():  

    You can wipe a variable out of existence using unset().


    Handy Usage: These functions are handy when you need to make sure that the user filled out the appropriate fields in the form.

    Example:

     <?php   
      // Experimenting with isset(), empty() & is_null()   
     // Comment/Uncomment each condition below and test  
      // $high = '';   
      // $high = NULL;   
      // $high = 0;   
      // $high = 'hello';   
      echo '$high = '.$high.'<br />';   
      echo 'isset($high) = '.isset($high).'<br />';   
      echo 'empty($high) = '.empty($high).'<br />';   
      echo 'is_null($high) = '.is_null($high).'<br />';   
      ?>   
    

    Top 5 Posts (Weekly)

    Lets Connect: