GZIP Compression is a simple, effective way to save bandwidth and speed up PHP application. The mechanism runs behind the GZIP compression is described below −Step1The browser/client request for a file to the server.Step2The server sends a .zip file to the browser (index.html.zip) rather than plain old index.html in response, due to which the download time and bandwidth decreases.Step3After the execution of the above step, the browser downloads the zipped file, extracts it, and then shows it to the user. This loads the webpage very quickly.In the Apache server, we have to Add the following to .htaccess file to enable ... Read More
Singleton Pattern ensures that a class has only one instance and provides a global point to access it. It ensures that only one object is available all across the application in a controlled state. Singleton pattern provides a way to access its only object which can be accessed directly without the need to instantiate the object of the class.ExampleOutputconnection createdExplanationIn the above example as we are following a singleton pattern so the object $db2 can't be created. Only a single object will be created and i.e available all across the application.
PHP 7 has added a new operator double question mark (??) operator. In PHP 7, the double question mark(??) operator known as Null Coalescing Operator.It returns its first operand if it exists and is not NULL; otherwise, it returns its second operand. It evaluates from left to right. Null Coalescing operator also can be used in a chain format.Let's take the below example to demonstrate the double question mark (??) operator.ExampleOutput9ExampleOutput34
Object.isFrozen()Object.isFrozen() method is used to find whether an object is frozen or not. An object is frozen if it followed the below criteriaIt should not be extensible.Its properties should be non-configurable.It shouldn't accept any new properties.SyntaxObject.isFrozen(obj);Parameters - Object.isFrozen() accepts an object as a parameter and scrutinizes whether it is frozen or not and returns a boolean output.ExampleIn the following example Object.isFrozen() scrutinizes whether the object 'obj' is frozen or not. Since the object is not frozen, false will be displayed as the output. Live Demo var object = { prop1 : 5 } var res = Object.isFrozen(object); ... Read More
An object property can be accessed in two ways. One is .property and the other is [property].Syntax-1Object.property;Syntax-2Object["property"];For better understanding, lets' look at the following example.In the following example an object called 'person' is defined and its properties were accessed in a dot notation.ExampleLive Demo var person = { firstname:"Ram", lastname:"kumar", age:50, designation:"content developer" }; document.write(person.firstname + " " + "is in a role of" + " " + person.designation); OutputRam is in a role of content developerIn the following example the properties of an object 'person' were accessed in bracket notation.ExampleLive Demo ... Read More
This example demonstrate about How to sort volley elements in android.Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.Step 2 − Add the following code to res/layout/activity_main.xml. In the above code, we have taken text view to show sorted volley elements.Step 3 − Add the following code to src/MainActivity.javapackage com.example.myapplication; import android.os.Build; import android.os.Bundle; import android.support.annotation.RequiresApi; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.widget.TextView; import android.widget.Toast; import com.android.volley.Request; import com.android.volley.RequestQueue; import com.android.volley.Response; import com.android.volley.VolleyError; import com.android.volley.toolbox.StringRequest; import com.android.volley.toolbox.Volley; import org.json.JSONArray; import org.json.JSONException; ... Read More
In javascript, arrays are not true arrays. They are javascript objects. So when we try to know their type using typeof() operator the displayed output will be object.Syntaxtypeof(operand);parameters - typeof() operator takes an operand and returns the data type of the operand. In the following example even though variable 'a' is an array, the typeof() operator returns the output as object because in general every array is an object.ExampleLive Demo var a = [1, 2, 5, "hello"]; document.write(typeof(a)); var b = {}; document.write(""); document.write(typeof(b)); Outputobject objectUnlike typeof() operator, Array.isArray() checks whether the passed parameter ... Read More
This example demonstrates How to sort volley JSON array based on id in android.Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.Step 2 − Add the following code to res/layout/activity_main.xml. In the above code, we have taken text view to show sorted custom object.Step 3 − Add the following code to src/MainActivity.javapackage com.example.myapplication; import android.os.Build; import android.os.Bundle; import android.support.annotation.RequiresApi; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.widget.TextView; import com.android.volley.Request; import com.android.volley.RequestQueue; import com.android.volley.Response; import com.android.volley.VolleyError; import com.android.volley.toolbox.StringRequest; import com.android.volley.toolbox.Volley; import org.json.JSONArray; import ... Read More
We can remove non-alphanumeric characters from the string with preg_replace() function in PHP. The preg_replace() function is an inbuilt function in PHP which is used to perform a regular expression for search and replace the content.syntaxpreg_replace(pattern, replacement, subject, limit, count )Let's discuss the parameters of the function underneath.patternThis parameter contains the pattern to search for.replacementIt is a mandatory parameter. This parameter may contain a string or an array with strings to replace.subjectThe string or an array with strings to search and replace.limitThe maximum possible replacements for each pattern in each subject stringcountThis is an optional parameter, if specified then this ... Read More
Output Buffering is a method to tell the PHP engine to hold the output data before sending it to the browser. As we know PHP sent the output data to the browser in pieces, but if we utilize the output buffering mechanism, the output data is stored in a variable and sent to the browser as one piece at the end of the script.ExampleLet's demonstrate with a simple example. Live DemoOutputstring(5) "Hello" string(20) "HelloTutorials Point"ExplanationIn the above example ob_get_contents() grabs all of the data gathered since we called ob_start, i.e. everything in the buffer. After that send the output data at ... Read More