WHAT'S NEW?
Loading...
Showing posts with label PHP. Show all posts
Showing posts with label PHP. Show all posts
AJAX provides faster performance and dynamic user experience.

Working on many web projects at both front end and back end, implementing AJAX for communicating with the server about the user interactions in the browser at client side is always something I used to imagine in my applications when I was fresh at web development. I had been working with $.AJAX() function to do such AJAX posting of data of forms and other message passing between server and client. But I never returned an object from server to client so that i could convert the response from server directly to something like objects in javascript and prevent the parsing of response from the server.

Being specific, the response I am talking is the JSON response from server that is the result of json_encode() function in PHP. We do some task of retrieving the data as per the request from client to server, then collect them in a PHP array and finally json_encode($array). The result of this is a JSON response.

WHY JSON over traditional XML for cross-platform message passing?
XML Parsing is so much tough and takes heavy cost in terms of executing the parser. There are open tags and close tags for objects and properties of objects. Moreover, there are attributes that increase the complexity and time in parsing. But JSON is simple to parse. JSON requires less storage than XML for same message representation.

This is to my knowledge.

But I also experienced another usability of JSON response. To those platforms where data size has to be reduced for communication, JSON is the best approach. Exactly, I am talking about the mobile platform where size of data to be communicated should be as much less as possible.

Another thing of concern is, JSON Response from an API. A scenario where we need to built a web service or we need to develop some multiple software products from the use of a single database. The database should be accessible from web, mobile apps and so on. In such scenario, there can re-usability of same API functions for all software products of a Business Concern.

HENCE JSON OVER XML!

To the point!
Now, lets get more advanced to the functionality I am providing currently in my applications: "get some data from server as JSON response and dynamically generate html elements to provide full dynamic user experience to the response user wants". Get the JSON response and use javascript eval() function to generate the object representation of the JSON response. No bother of parsing the response to extract data from response.

The use is as:
Server side script in PHP:
<?php
/*here are some operations to get data from server like price, qty, color of a product the user wants to view. Instead a simple assignment is shown here.*/

$price = 50;
$qty = 100;
$color = 'white';

$arr = array('price' => $price,
                      'qty' => $qty,
                      'color' => $color
            );
echo json_encode($arr);
?>

The response from server of this script is:
{"price":50,"qty":100,"color":"white"}

Now,
After AJAX posting, I evaluate the response as:
var product = eval("("+reponse_text+")");

Here response_text is the response from the server.
So, by this time I can access the data items of object product as: product.price, product.qty and product.color in javascript.

ALL I WANT FOR MAKING USER EXPERIENCE BETTER IN SIMPLE WAY!
We get to know about new functions when we go through the documentation or its use. When I was working with CodeIgniter framework in PHP, i noticed the passing of variables from controller to views to something like:
$data['username'] = 'abc';
$data['current_page'] = 'hello.html';

These variables when passed to the views, they can be accessed as:
echo $username;
echo $current_page;

It was all the use of php extract function that has this functionality.
So, for simplicity in accessing variables, this would be more useful.
More Information is available in http://php.net/manual/en/function.extract.php

FIND MORE!!!
This time I got to test a code in PHP. So, i should write the script in some text editor, make sure if local server is running or not and navigate to browser time and again to see the results of change in code.

THIS IS SERIOUSLY TIME CONSUMING AND BORING.

So, why not use PHP interpreter from command line?
Most developers working on Linux platform are more interested to work with commands because task is faster with commands. It's same even for other platforms.

Using PHP interpreter from command line really makes the testing of some small code snippets easy and faster. To be more specific, we can test the php functions working and outputs.

In Ubuntu 14.04,
The path of PHP is already added to the environment variable. So php command can be executed from any directory. If you have installed php from a zip file, the path of PHP directory should be added for global access to php command. Otherwise, navigate to the directory that contains php command and execute the command.

The command to run interactive PHP interpreter is:
php -a

Now input can be made just like in other interpreters and termination symbol should be input at the end of each statements.

CODING IS FASTER OBVIOUSLY!
Some web scripts can cause problems when there are integration scripts within those scripts. Especially, in those scripts where third party integration is done, we should prevent user to abort the execution of the script when the user starts the execution. The execution is stopped by cancelling the page loading. For such cases, we need the ignore user abort functionality. PHP has a function defined for this: ignore_user_abort().
The prototype of this function is:
int ignore_user_abort([String $value]);
If $value is set, it ignores user abort. If $value is not set, it returns previous setting without changing it.

Example:

<?php
     ignore_user_abort(true);
?>