Showing posts with label php. Show all posts
Showing posts with label php. Show all posts

December 20, 2014

Alternate to if else use switch statement


Alternate to if else use switch statement

If you execute the if else statements which multiple conditions
its go throw each condition and check with those condition is matching. For example you written if else condition
 
 if($i == 15)
 elseif($i == 25)
 elseif($i == 35)
 elseif($i == 45)
 elseif($i == 55)
 else
You want to match 45 value,while executing this above script variable matches with each if and else condition
and finally matches 45 value, so condition executed above with unmatched value then it took the execution time.

Alternative Switch statement

Suppose you are using the swithc statement for the above case. Example
 
 switch($i) {
   case 15:
     break;
   case 25:
     break;
   case 35:
     break;
   case 45:
     break;
   case 55:
     break;
 }
In this above switch statement, if the $i values was 45 then execute 45 matched values and go through the further process.

November 25, 2014

Find the Array key in Nested Array

In nested array find the array key for matched value

function recursive_array_search($needle, $haystack) {
    foreach($haystack as $key=>$value) {
        $current_key = $key;
        if($needle === $value
  || (is_array($value) && recursive_array_search($needle,$value) !== false)) {
            return $current_key;
        }
    }
    return false;
}
 
Find the key of the matched element in nested array.

$needle
- String which has to find.

$haystack
- Array elements $needle has to search in this array.

PHP MySQL Connection

MySQLI Connection - I for Improved

Connect the database from php throug MySQLI
 
/*hostname - localhost, user - user, password - null, database - test*/

$db = new mysqli('localhost', 'user', '', 'test');

if($db->connect_errno > 0){
    die('Error in connecting database (' . $db->connect_error . ')');
}

Now doing the simple query to get records from employee table
 
/* Do Query */

$sql = "SELECT * FROM `employee`;

if(!$result = $db->query($sql)){
    die('Query error ' . $db->error);
}

Display the output by fethcing the table records
 
/* Fetching records */

while($row = $result->fetch_assoc()){
    echo $row['username'];
}

November 10, 2014

What happen when Browser Cookie will Removed, Session retains as its ?


When webpage was loaded, unique session id will generated and that stored in cookie. If cookie will removed then session cant retain.

PHP Session and Cookie


The main difference between sessions and cookies is that cookies are stored in the user's browsers, and session are stored in server.

A cookie is small file that store in user's computer. Cookie data expiry depend on expiration date where session retain until browser run.

PHP List of array sort functions

sort().

rsort().

natsort().

natcasesort().

ksort().

krsort(). 

asort().

arsort().


Use of array_map Function in Real Time ?

It will apply for many things. For example want to do php function for set of elements will use that
Example

If have like below

$first_name = trim($_POST['firstname']);


$lastname = trim($_POST['lastname']);

but instead of above

$_POST = array_map("trim", $_POST);


$array = array_map("trim", $array);


It will remove the spaces for the set of elements.



How to Add the Element to the array First & Last, how to remove the same ?


array_push - Adding elemen to the last

array_unshift - First element to prepend

array_pop - Remove the first element of the array

array_shift - Remove the last element

How to determine PHP version in Servers ?

PHPINFO gives various detail scripting running system, Apache built-in modules, PHP working directory, PHP modules, PERL information and third party system information that are used in server.

phpinfo();

October 28, 2014

Magento Interview Questions


Getting order id for new order


Magento get new order id based on "eav_entity_store" table

PHP Interview Questions


Echo vs print.


echo() and print() are language constructs in PHP, both are used to output strings.

echo() can take multiple expressions whereas print cannot take multiple expressions.

December 15, 2013

Redirect all HTTP request to HTTPS using htaccess


open .htaccess file in root folder

RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

Fatal error: Maximum execution time of 60 seconds exceeded

Magento Discussion
Fatal error: Maximum execution time of 60 seconds exceeded in E:\wamp\www\projects\magento\magento1702\lib\Zend\Db\Statement\Pdo.php on line 228


In magento\.htaccess file

############################################
## adjust memory limit

#    php_value memory_limit 64M
    php_value memory_limit 512M
    php_value max_input_time 6000
    php_value max_execution_time 180000