Thursday, 16 April 2015

Why error 524 a timeout occurred

Why error 524 a timeout occurred

This error is CloudFlare error that denoting your web page is loading slow, due to any of below reason.
  • Website is Slow.
  • Temporary internet is slow.
  • Some function in website is talking long time to respond.
  • Any of mysql-query is talking more time to respond.



 In this case either you need to
increase the performance of website
OR
increase the max_execution_time on website.

What is meaning of this Error.
A 524 error states that CloudFlare was able to make a TCP connection to the origin(your website server location), but the origin server did not reply with a HTTP response before the connection timed out.
It means that CloudFlare is able to make a network connection with origin server (your website), but the origin server(webiste) took too long time to respond to the request.


If you get this error first time, then you can refresh the page OR open the website in another browser.

As we know, Most of time website execution limit is 30 seconds but if website is takes more time (more than 30 seconds) to reply, then cloudFlare did not wait and through 524 error.
This error comes when server is on cloudFlare.


If this issue persistent in your website and comes very often then you have to work hard on your website..

If this issue is coming many times then it means your website is loading very slow.
you need to optimize.

How to increase the Performance of website?
1. Optimize the application Flow.

2. Optimize the MySQLI Query.

3 - Make Fewer HTTP Requests (means Less Ajax Call)

4 - Use a Content Delivery Network (CDN)

5 - Add an Expires Header
http://stackoverflow.com/questions/4603076/add-expire-headers-in-php-cant-make-it-work

6 - Gzip Components
http://betterexplained.com/articles/how-to-optimize-your-site-with-gzip-compression/

7 - Put Stylesheets at the Top (CSS Files in head tag)

8 - Put Scripts at the Bottom (Js files in footer)

9 - Avoid CSS Expressions (e.g 100/2)

See full articles http://www.web-technology-experts-notes.in/2013/10/14-steps-to-reduce-the-loading-time-of-website.html






Wednesday, 15 April 2015

PHP problem solving interview questions and answers for Fresher and Experienced

PHP problem solving interview questions and answers for Fresher and Experienced


Question: How to save image in directory from URL?
In Below code, Just update the  $savePath and$ downloadImagePath.Script is Ready.
try {    
    $downloadImagePath='http://www.aboutcity.net/images/aboutcity.png';
    $savePath = $_SERVER['DOCUMENT_ROOT'];
    $ch = curl_init($downloadImagePath);
    $fp = fopen($savePath . '/aboutcity.png', 'wb');
    curl_setopt($ch, CURLOPT_FILE, $fp);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_exec($ch);
    curl_close($ch);
    fclose($fp);
    echo "Image Downloaded Successfully in ".$savePath.' Folder.';
} catch (Exception $e) {
    echo $e->getMessage();
}



Question: How to remove empty elements from array?
Below script will remove all the elements which have null values and empty data including space.
$array = array(
'key1'=>'value1',
'key2'=>'value2',
'key3'=>'value3',
'key4'=>' ',
'key5'=>' ',    
'key6'=>null,    
);
$filterArray = array_map('trim',$array);
$filterArray = array_filter($filterArray);

print_r($filterArray);//Array ( [key1] => value1 [key2] => value2 [key3] => value3 ) 



Question: How to convert object data to array?
It will convert an object to array.
$obj = new stdClass();
$obj->key1 ='Value1';
$obj->key2 ='Value3';
$obj->key3 ='Value3';
$obj->key4 ='Value4';
$array = (Array)$obj;
print_r($array);//Array ( [key1] => Value1 [key2] => Value3 [key3] => Value3 [key4] => Value4 ) 



Question: Is it possible a function with name?
Yes, that known as Anonymous functions.
Anonymous functions, also known as closures, allow the creation of functions which have no specified name.
$greet = function($name)
{
    printf("Hello %s", $name);
};
$greet('Hello');
$greet('Web Technology experts Notes');



Question: What is PHP_EOL in PHP?
End Of Line
PHP_EOL is used, when you want a add new line in cross-platform. It will work DOS/Mac/Unix issues.


Question: What are reserved constants in PHP?
http://php.net/manual/en/reserved.constants.php


Question: How to store array in Constants?
You can not store array in php constants.
Because php constant can store single string only.
BUT if you serialize an array to string then you can store it constants in constnats.
See Examples:
define ("MY_FRUITS", serialize (array ("apple", "cherry", "banana","mango")));



Question: Where to do PHP Practise online?
https://codeanywhere.net/
http://ideone.com/
http://writecodeonline.com/php/
http://codepad.org/
http://sandbox.onlinephpfunctions.com/
http://codepad.viper-7.com/
https://eval.in/


Question: How to start session, if already not started?
If session is started, session_id() will return the string else empty string.
if(session_id() == '') {
    session_start();
}



Question: Create a folder if it doesn't already exist?
file_exists is used to check directory exist OR NOT.
mkdir is used to create the new directory.
$path="/path/to/dir";
if (!file_exists($path)) {
    mkdir($path, 0755, true);
}



Question: How to convert date to timestamp in PHP?
strtotime function is used to convert the full date time into timestamp.
$timestamp = strtotime('22-09-2008');



Question: What is Difference between isset and array_key_exists?
isset: It is used to check, variable is defined OR NOT.
array_key_exists: it is used to check, an key is available in array or not.
bool array_key_exists ( mixed $key , array $array );



Question: How to return JSON from a PHP Script?
$array = array(
'key1'=>'value1',
'key2'=>'value2',
'key3'=>'value3',
'key4'=>' ',
'key5'=>' ',    
'key6'=>null,    
);
header('Content-Type: application/json');
echo json_encode($array);



Question: How to change the maximum upload file size to 60MB?
open php.ini file
//This is max file size
upload_max_filesize = 60M 

//this is form size including file size, that's why its greater 1 MB
post_max_size = 61M



Question: When to use self over $this?
Use $this to refer to the current object.
Use self to refer to the current class.

Example of use of $this
class MyClass1 {
    private $nonStaticMember = 'nonStaticMember  member Called.';        
    
    public function funcName() {
        return $this->nonStaticMember;
    }
}

$classObj = new MyClass1();
echo $classObj->funcName();//nonStaticMember  member Called.


Example of use of self
class MyClass2 {
    private static $staticMember = 'staticMember  member Called.';        
    
    public function funcName() {
        return $this::$staticMember;
    }
}

$classObj = new MyClass2();
echo $classObj->funcName(); //staticMember  member Called.