Found 1060 Articles for PHP

How To enable GZIP Compression in PHP?

Alok Prasad
Updated on 29-Jun-2020 11:41:43

5K+ Views

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

What does double question mark (??) operator mean in PHP ?

Alok Prasad
Updated on 29-Jun-2020 11:37:56

5K+ Views

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

How to convert an array to SimpleXML in PHP?

Alok Prasad
Updated on 29-Jun-2020 11:20:10

4K+ Views

We can solved the above problem using array_walk_recursive() function.array_walk_recursive()   is an inbuilt PHP function. This function converts array to XML document where keys of the array are converted into values and values of the array are converted into the element of XML.Let' demonstrate with a simple example.ExampleOutput alex account michigan NoteIf errors message display like PHP Fatal error: Uncaught Error: Class 'SimpleXMLElement' not found in then simply install php-xml, php-simplexml packages.

How to validate an email address in PHP ?

Alok Prasad
Updated on 29-Jun-2020 11:21:32

9K+ Views

In this article, we will learn to validate an email with PHP regular expression. We will learn different methods to validate email address in PHP.Method1The function preg_match() checks the input matching with patterns using regular expressions.Example Live DemoOutputValid email address.In the above example PHP preg_match() function has been used to search string for a pattern and PHP ternary operator has been used to return the true or false value based on the preg_match return.Method 2We will discuss email validation using filter_var() method.Example Live DemoOutputpattrick@tutorialspoint.com is a valid email addressRead More

What is PHP Output Buffering?

Alok Prasad
Updated on 29-Jun-2020 11:23:08

3K+ Views

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

How to remove non-alphanumeric characters in PHP stirng?

Alok Prasad
Updated on 29-Jun-2020 11:24:11

3K+ Views

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

Compare define() vs const in PHP

Alok Prasad
Updated on 31-Dec-2019 10:10:49

5K+ Views

As we know both define() and const are used to declare a constant in PHP script.SyntaxLet's discuss the difference between these two.The basic difference between these two is that const defines constants at compile time, whereas define() defines them at run time.We can't use the const keyword to declare constant in conditional blocks, while with define() we can achieve that.const accepts a static scalar(number, string or other constants like true, false, null, __FILE__), whereas define() takes any expression.consts are always case sensitive, whereas define() allows you to define case insensitive constants by passing true as the third argument.const can also ... Read More

How to convert XML file into array in PHP?

Alok Prasad
Updated on 29-Jun-2020 11:04:27

2K+ Views

To convert the XML document into PHP array, we have to utilize some PHP functions. The procedure is explained underneath with an example.Step 1We have to create an XML file that needs to convert into the array.abc.xml           AL       A                    SA          S     Step 2The above XML file will import into PHP using file_get_contents() function which read the entire file as a string and stores into a variable.Step 3After the above step, we ... Read More

How to call parent constructor in child class in PHP?

Alok Prasad
Updated on 29-Jun-2020 11:06:57

4K+ Views

We will face two cases while calling the parent constructor method in child class.Case1We can't run directly the parent class constructor in child class if the child class defines a constructor. In order to run a parent constructor, a call to parent::__construct() within the child constructor is required.Example Live DemoOutput: I am in Tutorials Point I am not in Tutorials PointExplanationIn the above example, we have used parent::__construct() to call the parent class constructor.Case2If the child does not define a constructor then it may be inherited from the parent class just like a normal class method(if it was not declared as ... Read More

How to Check if PHP session has already started?

Alok Prasad
Updated on 29-Jun-2020 09:48:01

4K+ Views

In PHP, we utilize session_start() an inbuilt function to start the session .But the problem we face in a PHP script is if we execute it more than once it throws an error. So here we will learn how to check the session started or not without calling session_start() function twice.There are two ways to follow to resolve this problem.For below PHP 5.4.0 version.ExampleExplanationIf the session not started this code above will always start the session in the PHP script.In the second method, we can utilize the function session_status(), which returns the status of the present session. This function can ... Read More

Advertisements